Class: CreatePgSqlTriggersTables

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/pg_sql_triggers/templates/create_pg_sql_triggers_tables.rb

Instance Method Summary collapse

Instance Method Details

#changeObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/generators/pg_sql_triggers/templates/create_pg_sql_triggers_tables.rb', line 4

def change
  # Registry table - source of truth for all triggers
  create_table :pg_sql_triggers_registry do |t|
    t.string :trigger_name, null: false
    t.string :table_name, null: false
    t.integer :version, null: false, default: 1
    t.boolean :enabled, null: false, default: false
    t.string :checksum, null: false
    t.string :source, null: false # dsl, generated, manual_sql
    t.string :environment
    t.text :definition # Stored DSL or SQL definition
    t.text :function_body # The actual function body
    t.text :condition # Optional WHEN clause condition
    t.string :timing, default: "before", null: false # Trigger timing: before or after
    t.datetime :installed_at
    t.datetime :last_verified_at

    t.timestamps
  end

  add_index :pg_sql_triggers_registry, :trigger_name, unique: true
  add_index :pg_sql_triggers_registry, :table_name
  add_index :pg_sql_triggers_registry, :enabled
  add_index :pg_sql_triggers_registry, :source
  add_index :pg_sql_triggers_registry, :environment
  add_index :pg_sql_triggers_registry, :timing

  # Trigger migrations table - tracks which trigger migrations have been run
  create_table :trigger_migrations do |t|
    t.string :version, null: false
  end

  add_index :trigger_migrations, :version, unique: true
end