Module: Stagehand::Schema

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

Defined Under Namespace

Modules: DumperExtensions, Statements

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#add_stagehand!(options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/stagehand/schema.rb', line 30

def add_stagehand!(options = {})
  return if Database.connected_to_production? && !Stagehand::Configuration.single_connection?

  ActiveRecord::Schema.define do
    Stagehand::Schema.send :each_table, options do |table_name|
      Stagehand::Schema.send :create_operation_trigger, table_name, 'insert', 'NEW'
      Stagehand::Schema.send :create_operation_trigger, table_name, 'update', 'NEW'
      Stagehand::Schema.send :create_operation_trigger, table_name, 'delete', 'OLD'
    end
  end
end

#has_stagehand?(table_name = nil) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
# File 'lib/stagehand/schema.rb', line 55

def has_stagehand?(table_name = nil)
  if UNTRACKED_TABLES.include?(table_name.to_s)
    return false
  elsif table_name
    has_stagehand_triggers?(table_name)
  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
# 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
      t.datetime :created_at
    end

    add_index :stagehand_commit_entries, :commit_id # Used for looking up all entries within a commit
    add_index :stagehand_commit_entries, [:record_id, :table_name] # Used for 'matching' scope
    add_index :stagehand_commit_entries, [:operation, :commit_id] # Used for looking up start entries, and 'not_in_progress' scope

    Stagehand::Schema.send :create_session_trigger
  end

  add_stagehand!(options)
end

#remove_stagehand!(options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/stagehand/schema.rb', line 42

def remove_stagehand!(options = {})
  ActiveRecord::Schema.define do
    Stagehand::Schema.send :each_table, options do |table_name|
      next unless Stagehand::Schema.send :has_stagehand_triggers?, 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