data_migrate 9.0.0 → 10.0.0.rc1

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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/gempush.yml +1 -0
  3. data/.travis.yml +0 -3
  4. data/Appraisals +0 -4
  5. data/Changelog.md +8 -0
  6. data/README.md +3 -3
  7. data/lib/data_migrate/{data_migrator_five.rb → data_migrator.rb} +0 -11
  8. data/lib/data_migrate/database_tasks.rb +12 -72
  9. data/lib/data_migrate/{schema_migration_six.rb → schema_migration.rb} +4 -2
  10. data/lib/data_migrate/{status_service_five.rb → status_service.rb} +12 -6
  11. data/lib/data_migrate/tasks/data_migrate_tasks.rb +19 -39
  12. data/lib/data_migrate/version.rb +1 -1
  13. data/lib/data_migrate.rb +3 -8
  14. data/spec/data_migrate/config_spec.rb +13 -10
  15. data/spec/data_migrate/data_migrator_spec.rb +11 -32
  16. data/spec/data_migrate/data_spec.rb +0 -11
  17. data/spec/data_migrate/database_tasks_spec.rb +10 -58
  18. data/spec/data_migrate/legacy_migrator_spec.rb +6 -18
  19. data/spec/data_migrate/migration.rb +11 -13
  20. data/spec/data_migrate/migration_context_spec.rb +11 -37
  21. data/spec/data_migrate/schema_dumper_spec.rb +10 -23
  22. data/spec/data_migrate/schema_migration_spec.rb +40 -42
  23. data/spec/data_migrate/status_service_spec.rb +26 -55
  24. data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +37 -71
  25. data/spec/db/data/20091231235959_some_name.rb +1 -1
  26. data/spec/db/data/20171231235959_super_update.rb +1 -1
  27. data/spec/spec_helper.rb +2 -8
  28. data/tasks/databases.rake +28 -5
  29. metadata +9 -19
  30. data/Gemfile.rails5.2 +0 -10
  31. data/gemfiles/rails_5.2.gemfile +0 -8
  32. data/lib/data_migrate/schema_migration_five.rb +0 -31
  33. data/spec/db/6.0/20091231235959_some_name.rb +0 -9
  34. data/spec/db/6.0/20171231235959_super_update.rb +0 -9
  35. data/spec/db/data-6.0/20091231235959_some_name.rb +0 -9
  36. data/spec/db/data-6.0/20171231235959_super_update.rb +0 -9
  37. data/spec/db/data-6.0/20181128000207_excluded_file.rb.other_ext +0 -1
  38. data/spec/db/migrate/5.2/20131111111111_late_migration.rb +0 -9
  39. data/spec/db/migrate/5.2/20202020202011_db_migration.rb +0 -9
  40. /data/spec/db/migrate/{6.0/20131111111111_late_migration.rb → 20131111111111_late_migration.rb} +0 -0
  41. /data/spec/db/migrate/{6.0/20202020202011_db_migration.rb → 20202020202011_db_migration.rb} +0 -0
@@ -3,33 +3,40 @@
3
3
  require "spec_helper"
4
4
 
5
5
  describe DataMigrate::Tasks::DataMigrateTasks do
6
- describe :dump do
7
- let(:db_config) do
8
- {
9
- adapter: "sqlite3",
10
- database: "spec/db/other_test.db"
11
- }
6
+ let(:connection_db_config) do
7
+ if Gem::Dependency.new("rails", ">= 6.1").match?("rails", Gem.loaded_specs["rails"].version)
8
+ ActiveRecord::Base.connection_db_config
9
+ else
10
+ ActiveRecord::Base.configurations.configs_for.first
12
11
  end
12
+ end
13
+
14
+ before do
15
+ ActiveRecord::SchemaMigration.create_table
16
+ DataMigrate::DataSchemaMigration.create_table
17
+ end
13
18
 
19
+ after do
20
+ ActiveRecord::Migration.drop_table("data_migrations") rescue nil
21
+ ActiveRecord::Migration.drop_table("schema_migrations") rescue nil
22
+ end
23
+
24
+ describe :dump do
14
25
  before do
15
- allow(DataMigrate::DataMigrator).to receive(:db_config) { db_config }
16
26
  allow(DataMigrate::DatabaseTasks).to receive(:db_dir).and_return("spec/db")
17
- end
18
-
19
- after do
20
- ActiveRecord::Migration.drop_table("data_migrations")
27
+ DataMigrate::Tasks::DataMigrateTasks.migrate
21
28
  end
22
29
 
23
30
  context 'when not given a separate db config' do
24
- it 'does not override the default connection' do
25
- DataMigrate::Tasks::DataMigrateTasks.migrate
31
+ it 'does not override the default connection' do
26
32
  expect(ActiveRecord::Base).not_to receive(:establish_connection)
27
33
  expect(DataMigrate::SchemaDumper).to receive(:dump)
28
- DataMigrate::Tasks::DataMigrateTasks.dump
34
+
35
+ DataMigrate::Tasks::DataMigrateTasks.dump(connection_db_config)
29
36
  end
30
37
  end
31
38
 
32
- context 'when given ' do
39
+ context 'when given a separate db config' do
33
40
  let(:override_config) do
34
41
  {
35
42
  'host' => '127.0.0.1',
@@ -39,6 +46,7 @@ describe DataMigrate::Tasks::DataMigrateTasks do
39
46
  'password' => nil,
40
47
  }
41
48
  end
49
+ let(:paths) { ["spec/db/migrate"] }
42
50
 
43
51
  before do
44
52
  DataMigrate.configure do |config|
@@ -47,40 +55,20 @@ describe DataMigrate::Tasks::DataMigrateTasks do
47
55
  end
48
56
 
49
57
  it 'overrides the default connection' do
50
- DataMigrate::Tasks::DataMigrateTasks.migrate
51
58
  expect(ActiveRecord::Base).to receive(:establish_connection).with(override_config)
52
- DataMigrate::Tasks::DataMigrateTasks.dump
59
+
60
+ DataMigrate::Tasks::DataMigrateTasks.dump(connection_db_config)
53
61
  end
54
62
  end
55
63
  end
56
64
 
57
65
  describe :migrate do
58
- let(:db_config) do
59
- {
60
- adapter: "sqlite3",
61
- database: "spec/db/test.db"
62
- }
63
- end
64
-
65
- before do
66
- allow(DataMigrate::DataMigrator).to receive(:db_config) { db_config }
67
- ActiveRecord::Base.establish_connection(db_config)
68
- end
69
-
70
- after do
71
- ActiveRecord::Migration.drop_table("data_migrations")
66
+ it "first run should run the first pending migration" do
67
+ expect { DataMigrate::Tasks::DataMigrateTasks.migrate }.to output(/20091231235959 SomeName: migrating/).to_stdout
72
68
  end
73
69
 
74
- it do
75
- expect {
76
- DataMigrate::Tasks::DataMigrateTasks.migrate
77
- }.to output(/20091231235959 SomeName: migrating/).to_stdout
78
- end
79
-
80
- it do
81
- expect {
82
- DataMigrate::Tasks::DataMigrateTasks.migrate
83
- }.to output(/20171231235959 SuperUpdate: migrating/).to_stdout
70
+ it "second run should run the second pending migration" do
71
+ expect { DataMigrate::Tasks::DataMigrateTasks.migrate }.to output(/20171231235959 SuperUpdate: migrating/).to_stdout
84
72
  end
85
73
  end
86
74
 
@@ -111,52 +99,30 @@ describe DataMigrate::Tasks::DataMigrateTasks do
111
99
  it "should abort with given message and print names and versions of pending migrations" do
112
100
  expect { subject }
113
101
  .to raise_error(SystemExit, message)
114
- .and output("You have 2 pending migrations:\n 1 A\n 2 B\n").to_stdout
102
+ .and output(match(/You have #{migrations.count} pending migrations:/)
103
+ .and match(Regexp.new(migrations.map { |m| m.slice(:version, :name)
104
+ .values.join("\\W+") }.join("\\W+")))).to_stdout
115
105
  end
116
106
  end
117
107
  end
118
108
 
119
- describe :status do
120
- let(:db_config) do
121
- {
122
- adapter: "sqlite3",
123
- database: "spec/db/test.db"
124
- }
125
- end
126
-
109
+ describe ".status" do
127
110
  before do
128
- if Rails::VERSION::MAJOR == 5
129
- ActiveRecord::Base.configurations['test'] = db_config
130
- else
131
- hash_config = ActiveRecord::DatabaseConfigurations::HashConfig.new('test', 'test', db_config)
132
- config_obj = ActiveRecord::DatabaseConfigurations.new([hash_config])
133
- allow(ActiveRecord::Base).to receive(:configurations).and_return(config_obj)
134
- end
135
-
136
- allow(Rails).to receive(:root) { '.' }
137
-
138
- if Rails::VERSION::MAJOR == 5
139
- allow(DataMigrate::Tasks::DataMigrateTasks).to receive(:schema_migrations_path) { 'spec/db/migrate/5.2' }
140
- else
141
- allow(DataMigrate::Tasks::DataMigrateTasks).to receive(:schema_migrations_path) { 'spec/db/migrate/6.0' }
142
- end
111
+ allow(Rails).to receive(:root) { "." }
112
+ allow(Rails).to receive(:application) { OpenStruct.new(config: OpenStruct.new(paths: { "db/migrate" => ["spec/db/migrate"] })) }
143
113
 
144
114
  DataMigrate::Tasks::DataMigrateTasks.migrate
145
115
  end
146
116
 
147
- after do
148
- ActiveRecord::Migration.drop_table("data_migrations")
149
- end
150
-
151
117
  it "should display data migration status" do
152
118
  expect {
153
- DataMigrate::Tasks::DataMigrateTasks.status
119
+ DataMigrate::Tasks::DataMigrateTasks.status(connection_db_config)
154
120
  }.to output(/up 20091231235959 Some name/).to_stdout
155
121
  end
156
122
 
157
123
  it "should display schema and data migration status" do
158
124
  expect {
159
- DataMigrate::Tasks::DataMigrateTasks.status_with_schema
125
+ DataMigrate::Tasks::DataMigrateTasks.status_with_schema(connection_db_config)
160
126
  }.to output(match(/up data 20091231235959 Some name/)
161
127
  .and match(/down schema 20131111111111 Late migration/)).to_stdout
162
128
  end
@@ -1,4 +1,4 @@
1
- class SomeName < ActiveRecord::Migration[5.2]
1
+ class SomeName < ActiveRecord::Migration[6.0]
2
2
  def up
3
3
  puts "Doing data migration"
4
4
  end
@@ -1,4 +1,4 @@
1
- class SuperUpdate < ActiveRecord::Migration[5.2]
1
+ class SuperUpdate < ActiveRecord::Migration[6.0]
2
2
  def up
3
3
  puts "Doing SuperUpdate"
4
4
  end
data/spec/spec_helper.rb CHANGED
@@ -20,14 +20,8 @@ RSpec.configure do |config|
20
20
  if example.metadata[:no_override]
21
21
  else
22
22
  @prev_data_migrations_path = DataMigrate.config.data_migrations_path
23
- if Rails::VERSION::MAJOR == 5
24
- DataMigrate.configure do |config|
25
- config.data_migrations_path = "spec/db/data"
26
- end
27
- else
28
- DataMigrate.configure do |config|
29
- config.data_migrations_path = "spec/db/6.0"
30
- end
23
+ DataMigrate.configure do |config|
24
+ config.data_migrations_path = "spec/db/data"
31
25
  end
32
26
  end
33
27
  end
data/tasks/databases.rake CHANGED
@@ -1,10 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'data_migrate/tasks/data_migrate_tasks'
2
4
 
3
5
  namespace :db do
4
6
  namespace :migrate do
5
7
  desc "Migrate the database data and schema (options: VERSION=x, VERBOSE=false)."
6
8
  task :with_data => :environment do
7
- DataMigrate::DataMigrator.assure_data_schema_table
9
+ original_db_config = ActiveRecord::Base.connection_db_config
10
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
11
+ ActiveRecord::Base.establish_connection(db_config)
12
+ DataMigrate::DataMigrator.assure_data_schema_table
8
13
 
9
14
  ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
10
15
  target_version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
@@ -48,9 +53,12 @@ namespace :db do
48
53
  migrations.each do |migration|
49
54
  DataMigrate::DatabaseTasks.run_migration(migration, migration[:direction])
50
55
  end
56
+ end
51
57
 
52
58
  Rake::Task["db:_dump"].invoke
53
59
  Rake::Task["data:dump"].invoke
60
+ ensure
61
+ ActiveRecord::Base.establish_connection(original_db_config)
54
62
  end
55
63
 
56
64
  namespace :redo do
@@ -114,7 +122,10 @@ namespace :db do
114
122
  namespace :status do
115
123
  desc "Display status of data and schema migrations"
116
124
  task :with_data => :environment do
117
- DataMigrate::Tasks::DataMigrateTasks.status_with_schema
125
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
126
+ ActiveRecord::Base.establish_connection(db_config)
127
+ DataMigrate::Tasks::DataMigrateTasks.status_with_schema(db_config)
128
+ end
118
129
  end
119
130
  end
120
131
  end # END OF MIGRATE NAME SPACE
@@ -193,8 +204,14 @@ end
193
204
  namespace :data do
194
205
  desc 'Migrate data migrations (options: VERSION=x, VERBOSE=false)'
195
206
  task :migrate => :environment do
196
- DataMigrate::Tasks::DataMigrateTasks.migrate
207
+ original_db_config = ActiveRecord::Base.connection_db_config
208
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
209
+ ActiveRecord::Base.establish_connection(db_config)
210
+ DataMigrate::Tasks::DataMigrateTasks.migrate
211
+ end
197
212
  Rake::Task["data:dump"].invoke
213
+ ensure
214
+ ActiveRecord::Base.establish_connection(original_db_config)
198
215
  end
199
216
 
200
217
  namespace :migrate do
@@ -230,7 +247,10 @@ namespace :data do
230
247
 
231
248
  desc "Display status of data migrations"
232
249
  task :status => :environment do
233
- DataMigrate::Tasks::DataMigrateTasks.status
250
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
251
+ ActiveRecord::Base.establish_connection(db_config)
252
+ DataMigrate::Tasks::DataMigrateTasks.status(db_config)
253
+ end
234
254
  end
235
255
  end
236
256
 
@@ -269,7 +289,10 @@ namespace :data do
269
289
 
270
290
  desc "Create a db/data_schema.rb file that stores the current data version"
271
291
  task dump: :environment do
272
- DataMigrate::Tasks::DataMigrateTasks.dump
292
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
293
+ ActiveRecord::Base.establish_connection(db_config)
294
+ DataMigrate::Tasks::DataMigrateTasks.dump(db_config)
295
+ end
273
296
 
274
297
  # Allow this task to be called as many times as required. An example
275
298
  # is the migrate:redo task, which calls other two internally
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: 9.0.0
4
+ version: 10.0.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew J Vargo
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2023-02-21 00:00:00.000000000 Z
13
+ date: 2023-04-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -203,13 +203,11 @@ files:
203
203
  - Appraisals
204
204
  - Changelog.md
205
205
  - Gemfile
206
- - Gemfile.rails5.2
207
206
  - Gemfile.rails6.1
208
207
  - LICENSE
209
208
  - README.md
210
209
  - Rakefile
211
210
  - data_migrate.gemspec
212
- - gemfiles/rails_5.2.gemfile
213
211
  - gemfiles/rails_6.0.gemfile
214
212
  - gemfiles/rails_6.1.gemfile
215
213
  - gemfiles/rails_7.0.gemfile
@@ -217,7 +215,7 @@ files:
217
215
  - lib/capistrano/data_migrate/migrate.rb
218
216
  - lib/data_migrate.rb
219
217
  - lib/data_migrate/config.rb
220
- - lib/data_migrate/data_migrator_five.rb
218
+ - lib/data_migrate/data_migrator.rb
221
219
  - lib/data_migrate/data_schema.rb
222
220
  - lib/data_migrate/data_schema_migration.rb
223
221
  - lib/data_migrate/database_tasks.rb
@@ -225,9 +223,8 @@ files:
225
223
  - lib/data_migrate/migration_context.rb
226
224
  - lib/data_migrate/railtie.rb
227
225
  - lib/data_migrate/schema_dumper.rb
228
- - lib/data_migrate/schema_migration_five.rb
229
- - lib/data_migrate/schema_migration_six.rb
230
- - lib/data_migrate/status_service_five.rb
226
+ - lib/data_migrate/schema_migration.rb
227
+ - lib/data_migrate/status_service.rb
231
228
  - lib/data_migrate/tasks/data_migrate_tasks.rb
232
229
  - lib/data_migrate/version.rb
233
230
  - lib/generators/data_migrate.rb
@@ -247,11 +244,6 @@ files:
247
244
  - spec/data_migrate/schema_migration_spec.rb
248
245
  - spec/data_migrate/status_service_spec.rb
249
246
  - spec/data_migrate/tasks/data_migrate_tasks_spec.rb
250
- - spec/db/6.0/20091231235959_some_name.rb
251
- - spec/db/6.0/20171231235959_super_update.rb
252
- - spec/db/data-6.0/20091231235959_some_name.rb
253
- - spec/db/data-6.0/20171231235959_super_update.rb
254
- - spec/db/data-6.0/20181128000207_excluded_file.rb.other_ext
255
247
  - spec/db/data/20091231235959_some_name.rb
256
248
  - spec/db/data/20171231235959_super_update.rb
257
249
  - spec/db/data/20181128000207_excluded_file.rb.other_ext
@@ -259,10 +251,8 @@ files:
259
251
  - spec/db/data/partial_schema/test_data_schema.rb
260
252
  - spec/db/data/schema/data_schema.rb
261
253
  - spec/db/data/schema/test_data_schema.rb
262
- - spec/db/migrate/5.2/20131111111111_late_migration.rb
263
- - spec/db/migrate/5.2/20202020202011_db_migration.rb
264
- - spec/db/migrate/6.0/20131111111111_late_migration.rb
265
- - spec/db/migrate/6.0/20202020202011_db_migration.rb
254
+ - spec/db/migrate/20131111111111_late_migration.rb
255
+ - spec/db/migrate/20202020202011_db_migration.rb
266
256
  - spec/generators/data_migration/data_migration_generator_spec.rb
267
257
  - spec/spec_helper.rb
268
258
  - tasks/.gitkeep
@@ -282,9 +272,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
282
272
  version: '0'
283
273
  required_rubygems_version: !ruby/object:Gem::Requirement
284
274
  requirements:
285
- - - ">="
275
+ - - ">"
286
276
  - !ruby/object:Gem::Version
287
- version: '0'
277
+ version: 1.3.1
288
278
  requirements: []
289
279
  rubygems_version: 3.3.26
290
280
  signing_key:
data/Gemfile.rails5.2 DELETED
@@ -1,10 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- # Specify your gem's dependencies in data_migrate.gemspec
4
- gemspec
5
- %w[
6
- activerecord
7
- railties
8
- ].each do |rails_gem|
9
- gem rails_gem, '~> 5.2'
10
- end
@@ -1,8 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "http://rubygems.org"
4
-
5
- gem "rails", "~> 5.2.3"
6
- gem "sqlite3", "~> 1.4"
7
-
8
- gemspec path: "../"
@@ -1,31 +0,0 @@
1
- module DataMigrate
2
- # Helper class to getting access to db schema
3
- # to allow data/schema combiation tasks
4
- class SchemaMigration
5
- def self.pending_schema_migrations
6
- all_migrations = DataMigrate::MigrationContext.new(migrations_paths).migrations
7
- sort_migrations(
8
- ActiveRecord::Migrator.new(:up, all_migrations).
9
- pending_migrations.
10
- map {|m| { version: m.version, name: m.name, kind: :schema }}
11
- )
12
- end
13
-
14
- def self.run(direction, migration_paths, version)
15
- ActiveRecord::MigrationContext.new(migration_paths).run(direction, version)
16
- end
17
-
18
- def self.sort_migrations(set1, set2 = nil)
19
- migrations = set1 + (set2 || [])
20
- migrations.sort {|a, b| sort_string(a) <=> sort_string(b)}
21
- end
22
-
23
- def self.migrations_paths
24
- Rails.application.config.paths["db/migrate"].to_a
25
- end
26
-
27
- def self.sort_string(migration)
28
- "#{migration[:version]}_#{migration[:kind] == :data ? 1 : 0}"
29
- end
30
- end
31
- end
@@ -1,9 +0,0 @@
1
- class SomeName < ActiveRecord::Migration[6.0]
2
- def up
3
- puts "Doing data migration"
4
- end
5
-
6
- def down
7
- raise ActiveRecord::IrreversibleMigration
8
- end
9
- end
@@ -1,9 +0,0 @@
1
- class SuperUpdate < ActiveRecord::Migration[6.0]
2
- def up
3
- puts "Doing data migration"
4
- end
5
-
6
- def down
7
- raise ActiveRecord::IrreversibleMigration
8
- end
9
- end
@@ -1,9 +0,0 @@
1
- class SomeName < ActiveRecord::Migration[6.0]
2
- def up
3
- puts "Doing data migration"
4
- end
5
-
6
- def down
7
- puts "Undoing SomeName"
8
- end
9
- end
@@ -1,9 +0,0 @@
1
- class SuperUpdate < ActiveRecord::Migration[6.0]
2
- def up
3
- puts "Doing SuperUpdate"
4
- end
5
-
6
- def down
7
- puts "Undoing SuperUpdate"
8
- end
9
- end
@@ -1 +0,0 @@
1
- # This file should be excluded
@@ -1,9 +0,0 @@
1
- class LateMigration < ActiveRecord::Migration[5.2]
2
- def up
3
- puts "Doing schema LateMigration"
4
- end
5
-
6
- def down
7
- puts "Undoing LateMigration"
8
- end
9
- end
@@ -1,9 +0,0 @@
1
- class DbMigration < ActiveRecord::Migration[5.2]
2
- def up
3
- puts "Doing schema migration"
4
- end
5
-
6
- def down
7
- puts "Undoing DbMigration"
8
- end
9
- end