Class: Messaging::Adapters::Postgres::Categories

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/messaging/adapters/postgres/categories.rb,
lib/messaging/adapters/postgres/categories/row.rb

Defined Under Namespace

Classes: Row

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ nil, Category

Get a category by name

Parameters:

  • name (String)

    the name of the category

Returns:

  • (nil)

    if no category exists with the given name

  • (Category)


16
17
18
# File 'lib/messaging/adapters/postgres/categories.rb', line 16

def [](name)
  all_categories.find { |c| c.name == name }
end

#create(name) ⇒ Category

Creates a table partition for the given category

Parameters:

  • name (String)

    the name of the category

Returns:



24
25
26
27
28
29
30
31
32
# File 'lib/messaging/adapters/postgres/categories.rb', line 24

def create(name)
  table_name = Category.table_name_for(name)
  sql = <<~SQL
    CREATE TABLE messaging.#{table_name}
    PARTITION OF messaging.messages FOR VALUES IN ('#{name}');
  SQL
  connection.execute sql
  Category.new(name, table_name)
end

#create_and_partition_by_day(name) ⇒ CategoryWithPartitions

Creates a table partition for the given category that in turn is partitioned based on created_at

Parameters:

  • name (String)

    the name of the category

Returns:



39
40
41
42
43
44
45
46
47
48
# File 'lib/messaging/adapters/postgres/categories.rb', line 39

def create_and_partition_by_day(name)
  table_name = Category.table_name_for(name)
  sql = <<~SQL
    CREATE TABLE messaging.#{table_name}
    PARTITION OF messaging.messages FOR VALUES IN ('#{name}')
    PARTITION BY RANGE (created_at);
  SQL
  connection.execute sql
  CategoryWithPartitions.new(name, table_name)
end

#drop(name) ⇒ Object

Drops the table partition (including all messages) for the given category

Parameters:

  • name (String)

    the name of the category



53
54
55
56
57
58
59
# File 'lib/messaging/adapters/postgres/categories.rb', line 53

def drop(name)
  table_name = Category.table_name_for(name)
  sql = <<~SQL
    drop TABLE messaging.#{table_name}
  SQL
  connection.execute sql
end

#eachObject



7
8
9
# File 'lib/messaging/adapters/postgres/categories.rb', line 7

def each
  all_categories.each { |c| yield c }
end