Module: PgPartitions

Defined in:
lib/pg_partitions.rb,
lib/pg_partitions/sql.rb,
lib/pg_partitions/version.rb

Defined Under Namespace

Modules: SQL

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#add_partition(table, name, check:) ⇒ Object



7
8
9
10
# File 'lib/pg_partitions.rb', line 7

def add_partition(table, name, check:)
  statement = SQL::Partition.new(table, check)
  create_table(name, id: false, options: statement.to_sql)
end

#add_partition_trigger(table, name, conditions) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pg_partitions.rb', line 12

def add_partition_trigger(table, name, conditions)
  insert_trigger    = SQL::Trigger.new(table, "#{name}_insert", 'BEFORE INSERT')
  delete_function   = SQL::DeleteFunction.new(table, "#{name}_delete")
  delete_trigger    = SQL::Trigger.new(table, "#{name}_delete", 'AFTER INSERT')

  reversible do |dir|
    dir.up do
      update_partition_trigger(table, name, conditions)
      execute insert_trigger.to_sql

      execute delete_function.to_sql
      execute delete_trigger.to_sql
    end

    dir.down do
      drop_partition_trigger(table, name)
    end
  end
end

#drop_partition_trigger(table, name) ⇒ Object



43
44
45
46
47
48
# File 'lib/pg_partitions.rb', line 43

def drop_partition_trigger(table, name)
  execute "DROP TRIGGER #{name}_insert ON #{table}"
  execute "DROP FUNCTION #{name}_insert()"
  execute "DROP TRIGGER #{name}_delete ON #{table}"
  execute "DROP FUNCTION #{name}_delete()"
end

#update_partition_trigger(table, name, conditions) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/pg_partitions.rb', line 32

def update_partition_trigger(table, name, conditions)
  insert_conditions = SQL::If.new(conditions)
  insert_function   = SQL::InsertFunction.new(
    table,
    "#{name}_insert",
    insert_conditions.to_sql
  )

  execute insert_function.to_sql
end