mysql_framework 0.0.6 → 0.0.7

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
  SHA256:
3
- metadata.gz: b58087950c7f077b63745aea37c1b706a76a8288ac60a5f3e02bf3bc937dde99
4
- data.tar.gz: 49949a1605bc8bfafe114e0a4a5448f84be21cce1939e7ddd708828d389993ca
3
+ metadata.gz: 089e9d32caf37e266ed17f4c366511814de266b3feaeed97c24781e4f7989de0
4
+ data.tar.gz: b566cdb40dc0466cc4b2878cc0e994fc252131350e79467e83e32b608717293e
5
5
  SHA512:
6
- metadata.gz: 6a591cbb848fbcae354dd9c3a2c55c0ba1b44435701810018355ed9ca521efc2eb7a05613f92be2c9d175ea094a9b02fb1286e48ecd452d5e5985aad28ae1431
7
- data.tar.gz: 6e64a6146a7949577a377019d4d7dd8b5439b023fb5a91780f367d371377ace7a632c91343fe490698887d8277586cf2dbdd7aac385cbb0c52c00223293a27c6
6
+ metadata.gz: 39624dce18fedfc9cc8664c9428bd431fd4f7a773239b7cac6e22cba403c2d839809b55ac82dc12c7f81685f3dbd060f00eda2542db3b871499278a2188f328c
7
+ data.tar.gz: 21f989dec1fd8399d119e50f038b18679361f924919151d36258468670955712d376c7bb0d928af12a1d60eab7cecae62af9111879dd191a371c7aa1b7420c0a
@@ -13,10 +13,10 @@ module MysqlFramework
13
13
 
14
14
  initialize_script_history
15
15
 
16
- last_executed_script = retrieve_last_executed_script
16
+ executed_scripts = retrieve_executed_scripts
17
17
 
18
18
  mysql_connector.transaction do |client|
19
- pending_scripts = calculate_pending_scripts(last_executed_script)
19
+ pending_scripts = calculate_pending_scripts(executed_scripts)
20
20
  MysqlFramework.logger.info do
21
21
  "[#{self.class}] - #{pending_scripts.length} pending data store scripts found."
22
22
  end
@@ -35,7 +35,7 @@ module MysqlFramework
35
35
  initialize_script_history
36
36
 
37
37
  mysql_connector.transaction do |client|
38
- pending_scripts = calculate_pending_scripts(0)
38
+ pending_scripts = calculate_pending_scripts
39
39
  MysqlFramework.logger.info do
40
40
  "[#{self.class}] - #{pending_scripts.length} pending data store scripts found."
41
41
  end
@@ -48,23 +48,14 @@ module MysqlFramework
48
48
  end
49
49
  end
50
50
 
51
- def drop_all_tables
52
- drop_script_history
53
- all_tables.each { |table| drop_table(table) }
54
- end
55
-
56
- def retrieve_last_executed_script
51
+ def retrieve_executed_scripts
57
52
  MysqlFramework.logger.debug { "[#{self.class}] - Retrieving last executed script from history." }
58
53
 
59
- result = mysql_connector.query(<<~SQL)
54
+ results = mysql_connector.query(<<~SQL)
60
55
  SELECT `identifier` FROM `#{migration_table_name}` ORDER BY `identifier` DESC
61
56
  SQL
62
57
 
63
- if result.each.to_a.length.zero?
64
- 0
65
- else
66
- Integer(result.first[:identifier])
67
- end
58
+ results.to_a.map { |result| result[:identifier]&.to_i }
68
59
  end
69
60
 
70
61
  def initialize_script_history
@@ -80,19 +71,25 @@ module MysqlFramework
80
71
  SQL
81
72
  end
82
73
 
83
- def calculate_pending_scripts(last_executed_script)
74
+ def calculate_pending_scripts(executed_scripts = [])
84
75
  MysqlFramework.logger.debug { "[#{self.class}] - Calculating pending data store scripts." }
85
76
 
86
- migrations.map(&:new).select { |script| script.identifier > last_executed_script }.sort_by(&:identifier)
77
+ migrations.map(&:new).reject { |script| executed_scripts.include?(script.identifier) }.sort_by(&:identifier)
87
78
  end
88
79
 
89
80
  def table_exists?(table_name)
90
81
  result = mysql_connector.query(<<~SQL)
91
82
  SHOW TABLES LIKE '#{table_name}'
92
83
  SQL
84
+
93
85
  result.count == 1
94
86
  end
95
87
 
88
+ def drop_all_tables
89
+ drop_script_history
90
+ all_tables.each { |table| drop_table(table) }
91
+ end
92
+
96
93
  def drop_script_history
97
94
  drop_table(migration_table_name)
98
95
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MysqlFramework
4
- VERSION = '0.0.6'
4
+ VERSION = '0.0.7'
5
5
  end
@@ -45,22 +45,13 @@ describe MysqlFramework::Scripts::Manager do
45
45
  end
46
46
  end
47
47
 
48
- describe '#drop_all_tables' do
49
- it 'drops the script history table and any registered tables' do
50
- expect(subject).to receive(:drop_script_history)
51
- expect(subject).to receive(:drop_table).with('test')
52
- expect(subject).to receive(:drop_table).with('demo')
53
-
54
- subject.drop_all_tables
55
- end
56
- end
57
-
58
- describe '#retrieve_last_executed_script' do
48
+ describe '#retrieve_executed_scripts' do
59
49
  before(:each) { subject.initialize_script_history }
60
50
 
61
51
  context 'when no scripts have been executed' do
62
- it 'returns 0' do
63
- expect(subject.retrieve_last_executed_script).to eq(0)
52
+ it 'returns an empty array' do
53
+ expect(subject.retrieve_executed_scripts).to be_a(Array)
54
+ expect(subject.retrieve_executed_scripts).to be_empty
64
55
  end
65
56
  end
66
57
 
@@ -69,7 +60,7 @@ describe MysqlFramework::Scripts::Manager do
69
60
  after(:each) { subject.drop_script_history }
70
61
 
71
62
  it 'returns the last executed script' do
72
- expect(subject.retrieve_last_executed_script).to eq(201807031200)
63
+ expect(subject.retrieve_executed_scripts).to eq([201807031200, 201801011030])
73
64
  end
74
65
  end
75
66
  end
@@ -83,22 +74,27 @@ describe MysqlFramework::Scripts::Manager do
83
74
  end
84
75
 
85
76
  describe '#calculate_pending_scripts' do
86
- it 'returns any scripts that are newer than the given date in ascending order' do
87
- timestamp = 201701010000 # 00:00 01/01/2017
88
- results = subject.calculate_pending_scripts(timestamp)
77
+ before(:each) { subject.drop_script_history }
78
+
79
+ context 'no migrations have been run' do
80
+ it 'returns all scripts that are missing in ascending order' do
81
+ results = subject.calculate_pending_scripts([])
89
82
 
90
- expect(results.length).to eq(3)
91
- expect(results[0]).to be_a(MysqlFramework::Support::Scripts::CreateTestTable)
92
- expect(results[1]).to be_a(MysqlFramework::Support::Scripts::CreateDemoTable)
83
+ expect(results.length).to eq(3)
84
+ expect(results[0]).to be_a(MysqlFramework::Support::Scripts::CreateTestTable)
85
+ expect(results[1]).to be_a(MysqlFramework::Support::Scripts::CreateDemoTable)
86
+ expect(results[2]).to be_a(MysqlFramework::Support::Scripts::CreateTestProc)
87
+ end
93
88
  end
94
89
 
95
- context 'when there are scripts older than the given date' do
96
- it 'returns only scripts that are newer than the given date in ascending order' do
97
- timestamp = 201802021010 # 10:10 02/02/2018
98
- results = subject.calculate_pending_scripts(timestamp)
90
+ context 'some migrations have been run' do
91
+ it 'returns any scripts that are missing in ascending order' do
92
+ timestamp = 201806021520
93
+ results = subject.calculate_pending_scripts([timestamp])
99
94
 
100
95
  expect(results.length).to eq(2)
101
- expect(results[0]).to be_a(MysqlFramework::Support::Scripts::CreateDemoTable)
96
+ expect(results[0]).to be_a(MysqlFramework::Support::Scripts::CreateTestTable)
97
+ expect(results[1]).to be_a(MysqlFramework::Support::Scripts::CreateTestProc)
102
98
  end
103
99
  end
104
100
  end
@@ -117,6 +113,16 @@ describe MysqlFramework::Scripts::Manager do
117
113
  end
118
114
  end
119
115
 
116
+ describe '#drop_all_tables' do
117
+ it 'drops the script history table and any registered tables' do
118
+ expect(subject).to receive(:drop_script_history)
119
+ expect(subject).to receive(:drop_table).with('test')
120
+ expect(subject).to receive(:drop_table).with('demo')
121
+
122
+ subject.drop_all_tables
123
+ end
124
+ end
125
+
120
126
  describe '#drop_script_history' do
121
127
  it 'drops the migration script history table' do
122
128
  query = <<~SQL
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysql_framework
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sage
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-26 00:00:00.000000000 Z
11
+ date: 2018-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler