culturecode_stagehand 0.8.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d1ca6b2ede30dbeed3da3f4800d413bcf60034f
4
- data.tar.gz: ef9a06e54ec5318e811818b3243d52da2af98b09
3
+ metadata.gz: 764024a2751221f034e59dd01d47ba537c18231c
4
+ data.tar.gz: 306cb8e2ab525a4b85c13bcc7cb818572158127d
5
5
  SHA512:
6
- metadata.gz: 2e42ce38a3a24048d9574aed6c3318668fe7809518669cfa0f34e23d4c91e3ca5199be9b11772cfb58283dd4098f3e0d6c0e2023d0fe6f400b76d7ebab6f12b0
7
- data.tar.gz: c147dd44ef36edd4f8b28e8711e27a97233614586bbec766a2508ede2c3a7c3618e8e6ee256e8b6612bca8f88320da1691d613d8a6321a7c8bbf595a4b1c8d82
6
+ metadata.gz: 310328eb8b60bbadd3a58bfdfae0565b7bb51170d3cc3bb06fa6050962d23efc03a6365f49bf8713b173df2fcf707e9de2fa07b3865357b00d02bbfc00c1d314
7
+ data.tar.gz: 85b5fb29cae9d1e3d1c81fd847b6caf35b864bd9dca1fcc87f7003446bccc98b605070a31b205333190b2af5e13d10ec867d0948119a3779a16fc28d9ba3f9c5
@@ -20,11 +20,7 @@ module Stagehand
20
20
  add_index :stagehand_commit_entries, [:record_id, :table_name] # Used for 'matching' scope
21
21
  add_index :stagehand_commit_entries, [:operation, :commit_id] # Used for looking up start entries, and 'not_in_progress' scope
22
22
 
23
- # Create trigger to initialize session using a function
24
- ActiveRecord::Base.connection.execute("DROP TRIGGER IF EXISTS stagehand_session_trigger;")
25
- ActiveRecord::Base.connection.execute("
26
- CREATE TRIGGER stagehand_session_trigger BEFORE INSERT ON stagehand_commit_entries
27
- FOR EACH ROW SET NEW.session = CONNECTION_ID();")
23
+ Stagehand::Schema.send :create_session_trigger
28
24
  end
29
25
 
30
26
  add_stagehand!(options)
@@ -38,9 +34,9 @@ module Stagehand
38
34
  table_names &= Array(options[:only]).collect(&:to_s) if options[:only].present?
39
35
 
40
36
  table_names.each do |table_name|
41
- Stagehand::Schema.send :create_trigger, table_name, 'insert', 'NEW'
42
- Stagehand::Schema.send :create_trigger, table_name, 'update', 'NEW'
43
- Stagehand::Schema.send :create_trigger, table_name, 'delete', 'OLD'
37
+ Stagehand::Schema.send :create_operation_trigger, table_name, 'insert', 'NEW'
38
+ Stagehand::Schema.send :create_operation_trigger, table_name, 'update', 'NEW'
39
+ Stagehand::Schema.send :create_operation_trigger, table_name, 'delete', 'OLD'
44
40
  end
45
41
  end
46
42
  end
@@ -61,7 +57,9 @@ module Stagehand
61
57
  end
62
58
 
63
59
  def has_stagehand?(table_name = nil)
64
- if table_name
60
+ if UNTRACKED_TABLES.include?(table_name.to_s)
61
+ return false
62
+ elsif table_name
65
63
  trigger_exists?(table_name, 'insert')
66
64
  else
67
65
  ActiveRecord::Base.Connection.table_exists?(Stagehand::Staging::CommitEntry.table_name)
@@ -70,29 +68,42 @@ module Stagehand
70
68
 
71
69
  private
72
70
 
73
- def create_trigger(table_name, trigger_action, record)
74
- return if trigger_exists?(table_name, trigger_action)
71
+ # Create trigger to initialize session using a function
72
+ def create_session_trigger
73
+ drop_trigger(:stagehand_commit_entries, :insert)
74
+ create_trigger(:stagehand_commit_entries, :insert, :before, <<-SQL)
75
+ SET NEW.session = CONNECTION_ID();
76
+ SQL
77
+ end
78
+
79
+ def create_operation_trigger(table_name, trigger_event, record)
80
+ return if trigger_exists?(table_name, trigger_event)
75
81
 
76
- ActiveRecord::Base.connection.execute("
77
- CREATE TRIGGER #{trigger_name(table_name, trigger_action)} AFTER #{trigger_action.upcase} ON #{table_name}
78
- FOR EACH ROW
82
+ create_trigger(table_name, trigger_event, :after, <<-SQL)
79
83
  BEGIN
80
84
  INSERT INTO stagehand_commit_entries (record_id, table_name, operation)
81
- VALUES (#{record}.id, '#{table_name}', '#{trigger_action}');
85
+ VALUES (#{record}.id, '#{table_name}', '#{trigger_event}');
82
86
  END;
83
- ")
87
+ SQL
88
+ end
89
+
90
+ def create_trigger(table_name, trigger_event, trigger_time, row_action)
91
+ ActiveRecord::Base.connection.execute <<-SQL
92
+ CREATE TRIGGER #{trigger_name(table_name, trigger_event)} #{trigger_time} #{trigger_event}
93
+ ON #{table_name} FOR EACH ROW #{row_action}
94
+ SQL
84
95
  end
85
96
 
86
- def drop_trigger(table_name, trigger_action)
87
- ActiveRecord::Base.connection.execute("DROP TRIGGER IF EXISTS #{trigger_name(table_name, trigger_action)};")
97
+ def drop_trigger(table_name, trigger_event)
98
+ ActiveRecord::Base.connection.execute("DROP TRIGGER IF EXISTS #{trigger_name(table_name, trigger_event)};")
88
99
  end
89
100
 
90
- def trigger_exists?(table_name, trigger_action)
91
- ActiveRecord::Base.connection.select_one("SHOW TRIGGERS where `trigger` = '#{trigger_name(table_name, trigger_action)}'").present?
101
+ def trigger_exists?(table_name, trigger_event)
102
+ ActiveRecord::Base.connection.select_one("SHOW TRIGGERS where `trigger` = '#{trigger_name(table_name, trigger_event)}'").present?
92
103
  end
93
104
 
94
- def trigger_name(table_name, trigger_action)
95
- "stagehand_#{trigger_action}_trigger_#{table_name}".downcase
105
+ def trigger_name(table_name, trigger_event)
106
+ "stagehand_#{trigger_event}_trigger_#{table_name}".downcase
96
107
  end
97
108
  end
98
109
  end
@@ -5,9 +5,10 @@ module Stagehand
5
5
  def create_table(table_name, options = {})
6
6
  super
7
7
 
8
+ return if Database.connected_to_production? && !Stagehand::Configuration.single_connection?
9
+ Schema.send(:create_session_trigger) if table_name == Staging::CommitEntry.table_name
8
10
  return if options.symbolize_keys[:stagehand] == false
9
11
  return if UNTRACKED_TABLES.include?(table_name)
10
- return if Database.connected_to_production? && !Stagehand::Configuration.single_connection?
11
12
 
12
13
  Schema.add_stagehand! :only => table_name
13
14
  end
@@ -7,6 +7,7 @@ module Stagehand
7
7
 
8
8
  def self.capture(subject_record = nil, &block)
9
9
  start_operation = start_commit(subject_record)
10
+ init_session!(start_operation)
10
11
  block.call(start_operation)
11
12
  return end_commit(start_operation)
12
13
  rescue => e
@@ -39,7 +40,7 @@ module Stagehand
39
40
 
40
41
  start_operation.subject = subject_record
41
42
 
42
- return start_operation.reload # Reload to ensure session is set
43
+ return start_operation
43
44
  end
44
45
 
45
46
  # Sets the commit_id on all the entries between the start and end op.
@@ -55,6 +56,12 @@ module Stagehand
55
56
  return new(start_operation.id)
56
57
  end
57
58
 
59
+ # Reload to ensure session set by the database is read by ActiveRecord
60
+ def self.init_session!(entry)
61
+ entry.reload
62
+ raise BlankCommitEntrySession unless entry.session.present?
63
+ end
64
+
58
65
  public
59
66
 
60
67
  def initialize(start_id)
@@ -100,4 +107,5 @@ module Stagehand
100
107
 
101
108
  # EXCEPTIONS
102
109
  class CommitNotFound < StandardError; end
110
+ class BlankCommitEntrySession < StandardError; end
103
111
  end
@@ -5,12 +5,12 @@ module Stagehand
5
5
 
6
6
  class_methods do
7
7
  def connection
8
- return super if Configuration.ghost_mode?
9
-
10
- if Stagehand::Database.connected_to_production?
11
- Stagehand::Database::StagingProbe.connection
12
- else
8
+ if Configuration.ghost_mode?
9
+ super
10
+ elsif Stagehand::Database.connected_to_staging?
13
11
  ActiveRecord::Base.connection
12
+ else
13
+ Stagehand::Database::StagingProbe.connection
14
14
  end
15
15
  end
16
16
  end
@@ -9,11 +9,11 @@ module Stagehand
9
9
 
10
10
  # Immediately attempt to sync the changes from the block if possible
11
11
  # The block is wrapped in a transaction to prevent changes to records while being synced
12
- def sync_now(&block)
12
+ def sync_now(subject_record = nil, &block)
13
13
  raise SyncBlockRequired unless block_given?
14
14
 
15
15
  Database.transaction do
16
- checklist = Checklist.new(Commit.capture(&block).entries, :preconfirm_subject => false)
16
+ checklist = Checklist.new(Commit.capture(subject_record, &block).entries, :preconfirm_subject => false)
17
17
  sync_checklist(checklist) unless checklist.requires_confirmation?
18
18
  end
19
19
  end
@@ -1,3 +1,3 @@
1
1
  module Stagehand
2
- VERSION = "0.8.1"
2
+ VERSION = "0.9.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: culturecode_stagehand
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicholas Jakobsen
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-08-20 00:00:00.000000000 Z
12
+ date: 2016-09-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails