data_migrate 3.3.1 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/.hound.yml +4 -0
- data/.overcommit.yml +21 -0
- data/.rubocop.yml +2 -0
- data/.ruby-style.yml +1059 -0
- data/Changelog.md +3 -1
- data/README.md +3 -0
- data/data_migrate.gemspec +2 -0
- data/lib/data_migrate.rb +1 -0
- data/lib/data_migrate/status_service.rb +65 -0
- data/lib/data_migrate/version.rb +1 -1
- data/spec/data_migrate/status_service_spec.rb +96 -0
- data/spec/db/data/20091231235959_some_name.rb +0 -0
- data/spec/db/data/20171231235959_super_update.rb +0 -0
- data/tasks/databases.rake +2 -23
- metadata +42 -3
data/Changelog.md
CHANGED
data/README.md
CHANGED
|
@@ -141,6 +141,9 @@ Tasks work as they would with the 'vanilla' db version. The 'with_data' addition
|
|
|
141
141
|
With 'up' and 'down', you can specify the option 'BOTH', which defaults to false. Using true, will migrate both the data and schema (in the desired direction) if they both match the version provided. Again, going up, schema is given precedence. Down its data.
|
|
142
142
|
|
|
143
143
|
`rake db:migrate:status:with_data` provides and additional column to indicate which type of migration.
|
|
144
|
+
### Configuration
|
|
145
|
+
|
|
146
|
+
`data_migrate` respects `ActiveRecord::Base.dump_schema_after_migration`. If it is set to `false`, data schema file will not be generated
|
|
144
147
|
|
|
145
148
|
## Capistrano Support
|
|
146
149
|
|
data/data_migrate.gemspec
CHANGED
|
@@ -24,6 +24,8 @@ Gem::Specification.new do |s|
|
|
|
24
24
|
s.add_development_dependency "rb-readline"
|
|
25
25
|
s.add_development_dependency "sqlite3"
|
|
26
26
|
s.add_development_dependency "timecop"
|
|
27
|
+
s.add_development_dependency "rubocop"
|
|
28
|
+
s.add_development_dependency "overcommit"
|
|
27
29
|
|
|
28
30
|
|
|
29
31
|
s.files = `git ls-files`.split("\n")
|
data/lib/data_migrate.rb
CHANGED
|
@@ -6,6 +6,7 @@ require File.join(File.dirname(__FILE__), "data_migrate",
|
|
|
6
6
|
require File.join(File.dirname(__FILE__), "data_migrate", "data_schema")
|
|
7
7
|
require File.join(File.dirname(__FILE__), "data_migrate", "database_tasks")
|
|
8
8
|
require File.join(File.dirname(__FILE__), "data_migrate", "schema_dumper")
|
|
9
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "status_service")
|
|
9
10
|
if Rails::VERSION::MAJOR >= 5
|
|
10
11
|
require File.join(File.dirname(__FILE__), "data_migrate", "migration_five")
|
|
11
12
|
else
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
module DataMigrate
|
|
2
|
+
class StatusService
|
|
3
|
+
class << self
|
|
4
|
+
def dump(connection = ActiveRecord::Base.connection, stream = STDOUT)
|
|
5
|
+
new(connection).dump(stream)
|
|
6
|
+
stream
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def initialize(connection)
|
|
11
|
+
@connection = connection
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def root_folder
|
|
15
|
+
Rails.root
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def dump(stream)
|
|
19
|
+
unless @connection.table_exists?(table_name)
|
|
20
|
+
stream.puts "Data migrations table does not exist yet."
|
|
21
|
+
return
|
|
22
|
+
end
|
|
23
|
+
sql = "SELECT version FROM #{DataMigrate::DataMigrator.schema_migrations_table_name}"
|
|
24
|
+
db_list = ActiveRecord::Base.connection.select_values(sql)
|
|
25
|
+
output(stream, db_list)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def table_name
|
|
31
|
+
DataMigrate::DataMigrator.schema_migrations_table_name
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def output(stream, db_list)
|
|
35
|
+
stream.puts "#{"Status".center(8)} #{"Migration ID".ljust(14)} Migration Name"
|
|
36
|
+
stream.puts "-" * 50
|
|
37
|
+
list = migration_files(db_list) + migration_list(db_list)
|
|
38
|
+
list.sort! {|line1, line2| line1[1] <=> line2[1]}
|
|
39
|
+
list.each do |file|
|
|
40
|
+
stream.puts "#{file[0].center(8)} #{file[1].ljust(14)} #{file[2]}"
|
|
41
|
+
end
|
|
42
|
+
stream.puts
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def migration_list(db_list)
|
|
46
|
+
list = []
|
|
47
|
+
db_list.each do |version|
|
|
48
|
+
list << ["up", version, "*** NO FILE ***"]
|
|
49
|
+
end
|
|
50
|
+
list
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def migration_files(db_list)
|
|
54
|
+
file_list = []
|
|
55
|
+
Dir.foreach(File.join(root_folder, "db", "data")) do |file|
|
|
56
|
+
# only files matching "20091231235959_some_name.rb" pattern
|
|
57
|
+
if match_data = DataMigrate::DataMigrator.match(file)
|
|
58
|
+
status = db_list.delete(match_data[1]) ? "up" : "down"
|
|
59
|
+
file_list << [status, match_data[1], match_data[2].humanize]
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
file_list
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
data/lib/data_migrate/version.rb
CHANGED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
describe DataMigrate::StatusService do
|
|
6
|
+
let(:subject) { DataMigrate::SchemaDumper }
|
|
7
|
+
let(:db_config) do
|
|
8
|
+
{
|
|
9
|
+
adapter: "sqlite3",
|
|
10
|
+
database: "spec/db/test.db"
|
|
11
|
+
}
|
|
12
|
+
end
|
|
13
|
+
let(:service) { DataMigrate::StatusService }
|
|
14
|
+
|
|
15
|
+
describe :dump do
|
|
16
|
+
before do
|
|
17
|
+
ActiveRecord::Base.establish_connection(db_config)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
describe "table does not exist" do
|
|
21
|
+
it "table doesn't exist" do
|
|
22
|
+
expect_any_instance_of(service).to receive(:table_name) { "bogus"}
|
|
23
|
+
stream = StringIO.new
|
|
24
|
+
service.dump(ActiveRecord::Base.connection, stream)
|
|
25
|
+
stream.rewind
|
|
26
|
+
|
|
27
|
+
expected = "Data migrations table does not exist"
|
|
28
|
+
expect(stream.read).to include expected
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
describe :dump do
|
|
34
|
+
context "table exists"
|
|
35
|
+
let(:fixture_file_timestamps) do
|
|
36
|
+
%w[20091231235959 20101231235959 20111231235959]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
before do
|
|
40
|
+
expect(DataMigrate::DataMigrator).
|
|
41
|
+
to receive(:db_config) { db_config }.at_least(:once)
|
|
42
|
+
ActiveRecord::Base.establish_connection(db_config)
|
|
43
|
+
|
|
44
|
+
ActiveRecord::Base.connection.initialize_schema_migrations_table
|
|
45
|
+
DataMigrate::DataMigrator.assure_data_schema_table
|
|
46
|
+
|
|
47
|
+
ActiveRecord::Base.connection.execute <<-SQL
|
|
48
|
+
INSERT INTO #{DataMigrate::DataMigrator.schema_migrations_table_name}
|
|
49
|
+
VALUES #{fixture_file_timestamps.map { |t| "(#{t})" }.join(', ')}
|
|
50
|
+
SQL
|
|
51
|
+
|
|
52
|
+
expect_any_instance_of(service).to receive(:root_folder) { "spec" }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
after do
|
|
56
|
+
ActiveRecord::Migration.drop_table("data_migrations")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it "shows successfully executed migration" do
|
|
60
|
+
stream = StringIO.new
|
|
61
|
+
service.dump(ActiveRecord::Base.connection, stream)
|
|
62
|
+
stream.rewind
|
|
63
|
+
|
|
64
|
+
expected = " up 20091231235959 Some name"
|
|
65
|
+
expect(stream.read).to include expected
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it "shows missing file migration" do
|
|
69
|
+
stream = StringIO.new
|
|
70
|
+
service.dump(ActiveRecord::Base.connection, stream)
|
|
71
|
+
stream.rewind
|
|
72
|
+
|
|
73
|
+
expected = " up 20101231235959 *** NO FILE ***"
|
|
74
|
+
s = stream.read
|
|
75
|
+
expect(s).to include expected
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
it "shows migration that has not run yet" do
|
|
79
|
+
stream = StringIO.new
|
|
80
|
+
service.dump(ActiveRecord::Base.connection, stream)
|
|
81
|
+
stream.rewind
|
|
82
|
+
|
|
83
|
+
expected = " down 20171231235959 Super update"
|
|
84
|
+
s = stream.read
|
|
85
|
+
expect(s).to include expected
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
it "outputs migrations in chronological order" do
|
|
89
|
+
stream = StringIO.new
|
|
90
|
+
service.dump(ActiveRecord::Base.connection, stream)
|
|
91
|
+
stream.rewind
|
|
92
|
+
s = stream.read
|
|
93
|
+
expect(s.index("20091231235959")).to be < s.index("20111231235959")
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
File without changes
|
|
File without changes
|
data/tasks/databases.rake
CHANGED
|
@@ -286,30 +286,9 @@ namespace :data do
|
|
|
286
286
|
task :status => :environment do
|
|
287
287
|
config = ActiveRecord::Base.configurations[Rails.env || 'development']
|
|
288
288
|
ActiveRecord::Base.establish_connection(config)
|
|
289
|
-
|
|
290
|
-
puts 'Data migrations table does not exist yet.'
|
|
291
|
-
next # means "return" for rake task
|
|
292
|
-
end
|
|
293
|
-
db_list = ActiveRecord::Base.connection.select_values("SELECT version FROM #{DataMigrate::DataMigrator.schema_migrations_table_name}")
|
|
294
|
-
file_list = []
|
|
295
|
-
Dir.foreach(File.join(Rails.root, 'db', 'data')) do |file|
|
|
296
|
-
# only files matching "20091231235959_some_name.rb" pattern
|
|
297
|
-
if match_data = DataMigrate::DataMigrator.match(file)
|
|
298
|
-
status = db_list.delete(match_data[1]) ? 'up' : 'down'
|
|
299
|
-
file_list << [status, match_data[1], match_data[2]]
|
|
300
|
-
end
|
|
301
|
-
end
|
|
302
|
-
# output
|
|
289
|
+
connection = ActiveRecord::Base.connection
|
|
303
290
|
puts "\ndatabase: #{config['database']}\n\n"
|
|
304
|
-
|
|
305
|
-
puts "-" * 50
|
|
306
|
-
file_list.each do |file|
|
|
307
|
-
puts "#{file[0].center(8)} #{file[1].ljust(14)} #{file[2].humanize}"
|
|
308
|
-
end
|
|
309
|
-
db_list.each do |version|
|
|
310
|
-
puts "#{'up'.center(8)} #{version.ljust(14)} *** NO FILE ***"
|
|
311
|
-
end
|
|
312
|
-
puts
|
|
291
|
+
DataMigrate::StatusService.dump(connection)
|
|
313
292
|
end
|
|
314
293
|
end
|
|
315
294
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: data_migrate
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andrew J Vargo
|
|
@@ -9,7 +9,7 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2018-
|
|
12
|
+
date: 2018-02-08 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: rails
|
|
@@ -137,6 +137,34 @@ dependencies:
|
|
|
137
137
|
- - ">="
|
|
138
138
|
- !ruby/object:Gem::Version
|
|
139
139
|
version: '0'
|
|
140
|
+
- !ruby/object:Gem::Dependency
|
|
141
|
+
name: rubocop
|
|
142
|
+
requirement: !ruby/object:Gem::Requirement
|
|
143
|
+
requirements:
|
|
144
|
+
- - ">="
|
|
145
|
+
- !ruby/object:Gem::Version
|
|
146
|
+
version: '0'
|
|
147
|
+
type: :development
|
|
148
|
+
prerelease: false
|
|
149
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
150
|
+
requirements:
|
|
151
|
+
- - ">="
|
|
152
|
+
- !ruby/object:Gem::Version
|
|
153
|
+
version: '0'
|
|
154
|
+
- !ruby/object:Gem::Dependency
|
|
155
|
+
name: overcommit
|
|
156
|
+
requirement: !ruby/object:Gem::Requirement
|
|
157
|
+
requirements:
|
|
158
|
+
- - ">="
|
|
159
|
+
- !ruby/object:Gem::Version
|
|
160
|
+
version: '0'
|
|
161
|
+
type: :development
|
|
162
|
+
prerelease: false
|
|
163
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
164
|
+
requirements:
|
|
165
|
+
- - ">="
|
|
166
|
+
- !ruby/object:Gem::Version
|
|
167
|
+
version: '0'
|
|
140
168
|
description: Rake tasks to migrate data alongside schema changes.
|
|
141
169
|
email:
|
|
142
170
|
- ajvargo@computer.org
|
|
@@ -146,7 +174,11 @@ extensions: []
|
|
|
146
174
|
extra_rdoc_files: []
|
|
147
175
|
files:
|
|
148
176
|
- ".gitignore"
|
|
177
|
+
- ".hound.yml"
|
|
178
|
+
- ".overcommit.yml"
|
|
149
179
|
- ".rspec"
|
|
180
|
+
- ".rubocop.yml"
|
|
181
|
+
- ".ruby-style.yml"
|
|
150
182
|
- ".travis.yml"
|
|
151
183
|
- Appraisals
|
|
152
184
|
- Changelog.md
|
|
@@ -173,6 +205,7 @@ files:
|
|
|
173
205
|
- lib/data_migrate/migration_five.rb
|
|
174
206
|
- lib/data_migrate/railtie.rb
|
|
175
207
|
- lib/data_migrate/schema_dumper.rb
|
|
208
|
+
- lib/data_migrate/status_service.rb
|
|
176
209
|
- lib/data_migrate/version.rb
|
|
177
210
|
- lib/generators/data_migrate.rb
|
|
178
211
|
- lib/generators/data_migration/data_migration_generator.rb
|
|
@@ -185,6 +218,9 @@ files:
|
|
|
185
218
|
- spec/data_migrate/database_tasks_spec.rb
|
|
186
219
|
- spec/data_migrate/migration.rb
|
|
187
220
|
- spec/data_migrate/schema_dumper_spec.rb
|
|
221
|
+
- spec/data_migrate/status_service_spec.rb
|
|
222
|
+
- spec/db/data/20091231235959_some_name.rb
|
|
223
|
+
- spec/db/data/20171231235959_super_update.rb
|
|
188
224
|
- spec/generators/data_migration/data_migration_generator_spec.rb
|
|
189
225
|
- spec/spec_helper.rb
|
|
190
226
|
- tasks/.gitkeep
|
|
@@ -212,7 +248,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
212
248
|
version: '0'
|
|
213
249
|
requirements: []
|
|
214
250
|
rubyforge_project: data_migrate
|
|
215
|
-
rubygems_version: 2.5.
|
|
251
|
+
rubygems_version: 2.5.1
|
|
216
252
|
signing_key:
|
|
217
253
|
specification_version: 4
|
|
218
254
|
summary: Rake tasks to migrate data alongside schema changes.
|
|
@@ -223,5 +259,8 @@ test_files:
|
|
|
223
259
|
- spec/data_migrate/database_tasks_spec.rb
|
|
224
260
|
- spec/data_migrate/migration.rb
|
|
225
261
|
- spec/data_migrate/schema_dumper_spec.rb
|
|
262
|
+
- spec/data_migrate/status_service_spec.rb
|
|
263
|
+
- spec/db/data/20091231235959_some_name.rb
|
|
264
|
+
- spec/db/data/20171231235959_super_update.rb
|
|
226
265
|
- spec/generators/data_migration/data_migration_generator_spec.rb
|
|
227
266
|
- spec/spec_helper.rb
|