Module: Stagehand::Schema

Extended by:
Schema
Included in:
Schema
Defined in:
lib/stagehand/schema.rb,
lib/stagehand/schema/statements.rb

Defined Under Namespace

Modules: Statements

Constant Summary collapse

UNTRACKED_TABLES =
['schema_migrations', Stagehand::Staging::CommitEntry.table_name]

Instance Method Summary collapse

Instance Method Details

#add_stagehand!(options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/stagehand/schema.rb', line 33

def add_stagehand!(options = {})
  ActiveRecord::Schema.define do
    table_names = ActiveRecord::Base.connection.tables
    table_names -= UNTRACKED_TABLES
    table_names -= Array(options[:except]).collect(&:to_s)
    table_names &= Array(options[:only]).collect(&:to_s) if options[:only].present?

    table_names.each do |table_name|
      Stagehand::Schema.send :create_trigger, table_name, 'insert', 'NEW'
      Stagehand::Schema.send :create_trigger, table_name, 'update', 'NEW'
      Stagehand::Schema.send :create_trigger, table_name, 'delete', 'OLD'
    end
  end
end

#has_stagehand?(table_name = nil) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
# File 'lib/stagehand/schema.rb', line 63

def has_stagehand?(table_name = nil)
  if table_name
    trigger_exists?(table_name, 'insert')
  else
    ActiveRecord::Base.Connection.table_exists?(Stagehand::Staging::CommitEntry.table_name)
  end
end

#init_stagehand!(options = {}) ⇒ Object



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

def init_stagehand!(options = {})
  ActiveRecord::Schema.define do
    create_table :stagehand_commit_entries do |t|
      t.integer :record_id
      t.string :table_name
      t.string :operation, :null => false
      t.integer :commit_id
      t.string :session
    end

    add_index :stagehand_commit_entries, :commit_id
    add_index :stagehand_commit_entries, :operation
    add_index :stagehand_commit_entries, [:record_id, :table_name]

    # Create trigger to initialize session using a function
    ActiveRecord::Base.connection.execute("DROP TRIGGER IF EXISTS stagehand_session_trigger;")
    ActiveRecord::Base.connection.execute("
    CREATE TRIGGER stagehand_session_trigger BEFORE INSERT ON stagehand_commit_entries
    FOR EACH ROW SET NEW.session = CONNECTION_ID();")
  end

  add_stagehand!(options)
end

#remove_stagehand!(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/stagehand/schema.rb', line 48

def remove_stagehand!(options = {})
  ActiveRecord::Schema.define do
    table_names = ActiveRecord::Base.connection.tables
    table_names &= Array(options[:only]).collect(&:to_s) if options[:only].present?

    table_names.each do |table_name|
      Stagehand::Schema.send :drop_trigger, table_name, 'insert'
      Stagehand::Schema.send :drop_trigger, table_name, 'update'
      Stagehand::Schema.send :drop_trigger, table_name, 'delete'
    end

    drop_table :stagehand_commit_entries unless options[:only].present?
  end
end