data_migrate 5.1.0 → 6.0.4.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +5 -5
  2. data/.rbenv-gemsets +2 -0
  3. data/.ruby-version +1 -0
  4. data/.travis.yml +7 -4
  5. data/Appraisals +7 -9
  6. data/Changelog.md +39 -3
  7. data/Gemfile +2 -0
  8. data/Gemfile.rails5.1 +1 -1
  9. data/README.md +30 -18
  10. data/data_migrate.gemspec +2 -2
  11. data/gemfiles/rails_4.2.gemfile +2 -1
  12. data/gemfiles/rails_5.0.gemfile +1 -1
  13. data/gemfiles/rails_5.1.gemfile +1 -1
  14. data/gemfiles/rails_5.2.gemfile +1 -1
  15. data/gemfiles/rails_6.0.gemfile +8 -0
  16. data/lib/capistrano/data_migrate/migrate.rb +3 -4
  17. data/lib/data_migrate.rb +14 -10
  18. data/lib/data_migrate/config.rb +21 -0
  19. data/lib/data_migrate/data_migrator.rb +20 -14
  20. data/lib/data_migrate/data_migrator_five.rb +7 -3
  21. data/lib/data_migrate/database_tasks.rb +3 -1
  22. data/lib/data_migrate/schema_migration_five.rb +1 -1
  23. data/lib/data_migrate/schema_migration_six.rb +31 -0
  24. data/lib/data_migrate/status_service.rb +1 -1
  25. data/lib/data_migrate/tasks/data_migrate_tasks.rb +4 -8
  26. data/lib/data_migrate/version.rb +1 -1
  27. data/lib/generators/data_migration/data_migration_generator.rb +10 -1
  28. data/spec/data_migrate/config_spec.rb +27 -0
  29. data/spec/data_migrate/data_migrator_spec.rb +21 -0
  30. data/spec/data_migrate/database_tasks_spec.rb +4 -10
  31. data/spec/data_migrate/migration_context_spec.rb +13 -4
  32. data/spec/data_migrate/schema_migration_spec.rb +3 -1
  33. data/spec/data_migrate/status_service_spec.rb +10 -1
  34. data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +2 -17
  35. data/spec/db/{4.2 → 6.0}/20091231235959_some_name.rb +1 -1
  36. data/spec/db/{4.2 → 6.0}/20171231235959_super_update.rb +1 -1
  37. data/spec/db/data-6.0/20091231235959_some_name.rb +9 -0
  38. data/spec/db/data-6.0/20171231235959_super_update.rb +9 -0
  39. data/spec/db/data-6.0/20181128000207_excluded_file.rb.other_ext +1 -0
  40. data/spec/db/data/20181128000207_excluded_file.rb.other_ext +1 -0
  41. data/spec/db/migrate/6.0/20131111111111_late_migration.rb +9 -0
  42. data/spec/db/migrate/6.0/20202020202011_db_migration.rb +9 -0
  43. data/spec/generators/data_migration/data_migration_generator_spec.rb +33 -0
  44. data/spec/spec_helper.rb +26 -4
  45. data/tasks/databases.rake +14 -10
  46. metadata +34 -16
@@ -1,10 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "active_record"
4
+ require "data_migrate/config"
4
5
 
5
6
  module DataMigrate
6
7
  class DataMigrator < ActiveRecord::Migrator
7
- self.migrations_paths = ["db/data"]
8
+ def self.migrations_paths
9
+ [DataMigrate.config.data_migrations_path]
10
+ end
8
11
 
9
12
  def self.assure_data_schema_table
10
13
  ActiveRecord::Base.establish_connection(db_config)
@@ -53,9 +56,10 @@ module DataMigrate
53
56
  DataMigrate::MigrationContext.new(migrations_paths).migrations_status
54
57
  end
55
58
 
56
- #TODO: this was added to be backward compatible, need to re-evaluate
59
+ # TODO: this was added to be backward compatible, need to re-evaluate
57
60
  def migrations(_migrations_paths)
58
- DataMigrate::MigrationContext.new(migrations_paths).migrations
61
+ #DataMigrate::MigrationContext.new(migrations_paths).migrations
62
+ DataMigrate::MigrationContext.new(_migrations_paths).migrations
59
63
  end
60
64
 
61
65
  #TODO: this was added to be backward compatible, need to re-evaluate
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "data_migrate/config"
4
+
3
5
  module DataMigrate
4
6
  ##
5
7
  # This class extends DatabaseTasks to add a schema_file method.
@@ -26,7 +28,7 @@ module DataMigrate
26
28
  end
27
29
 
28
30
  def self.data_migrations_path
29
- "db/data/"
31
+ DataMigrate.config.data_migrations_path
30
32
  end
31
33
 
32
34
  def self.schema_migrations_path
@@ -3,7 +3,7 @@ module DataMigrate
3
3
  # to allow data/schema combiation tasks
4
4
  class SchemaMigration
5
5
  def self.pending_schema_migrations
6
- all_migrations = ActiveRecord::MigrationContext.new(migrations_paths).migrations
6
+ all_migrations = DataMigrate::MigrationContext.new(migrations_paths).migrations
7
7
  sort_migrations(
8
8
  ActiveRecord::Migrator.new(:up, all_migrations).
9
9
  pending_migrations.
@@ -0,0 +1,31 @@
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, ActiveRecord::Base.connection.schema_migration).
9
+ pending_migrations.
10
+ map {|m| { version: m.version, kind: :schema }}
11
+ )
12
+ end
13
+
14
+ def self.run(direction, migration_paths, version)
15
+ ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::Base.connection.schema_migration).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
@@ -52,7 +52,7 @@ module DataMigrate
52
52
 
53
53
  def migration_files(db_list)
54
54
  file_list = []
55
- Dir.foreach(File.join(root_folder, "db", "data")) do |file|
55
+ Dir.foreach(File.join(root_folder, DataMigrate.config.data_migrations_path)) do |file|
56
56
  # only files matching "20091231235959_some_name.rb" pattern
57
57
  if match_data = DataMigrate::DataMigrator.match(file)
58
58
  status = db_list.delete(match_data[1]) ? "up" : "down"
@@ -3,21 +3,17 @@ module DataMigrate
3
3
  module DataMigrateTasks
4
4
  extend self
5
5
  def migrations_paths
6
- @migrations_paths ||= begin
7
- if Rails.application && Rails.application.paths["data/migrate"]
8
- Rails.application.paths["data/migrate"].to_a
9
- end
10
- end
6
+ @migrations_paths ||= DataMigrate.config.data_migrations_path
11
7
  end
12
8
 
13
9
  def migrate
14
10
  DataMigrate::DataMigrator.assure_data_schema_table
15
11
  target_version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
16
- if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR == 2
12
+ if (Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR == 2) ||
13
+ Rails::VERSION::MAJOR == 6
17
14
  DataMigrate::MigrationContext.new(migrations_paths).migrate(target_version)
18
15
  else
19
- paths = migrations_paths || "db/data/"
20
- DataMigrate::DataMigrator.migrate(paths, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
16
+ DataMigrate::DataMigrator.migrate(migrations_paths, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
21
17
  end
22
18
  end
23
19
  end
@@ -1,3 +1,3 @@
1
1
  module DataMigrate
2
- VERSION = "5.1.0".freeze
2
+ VERSION = "6.0.4.beta".freeze
3
3
  end
@@ -2,6 +2,7 @@ require "generators/data_migrate"
2
2
  require "rails/generators"
3
3
  require "rails/generators/active_record/migration"
4
4
  require "rails/generators/migration"
5
+ require "data_migrate/config"
5
6
 
6
7
  module DataMigrate
7
8
  module Generators
@@ -13,7 +14,7 @@ module DataMigrate
13
14
 
14
15
  def create_data_migration
15
16
  set_local_assigns!
16
- migration_template "data_migration.rb", "db/data/#{file_name}.rb"
17
+ migration_template "data_migration.rb", data_migrations_file_path
17
18
  end
18
19
 
19
20
  protected
@@ -34,6 +35,14 @@ module DataMigrate
34
35
  "ActiveRecord::Migration"
35
36
  end
36
37
  end
38
+
39
+ def data_migrations_file_path
40
+ File.join(data_migrations_path, "#{file_name}.rb")
41
+ end
42
+
43
+ def data_migrations_path
44
+ DataMigrate.config.data_migrations_path
45
+ end
37
46
  end
38
47
  end
39
48
  end
@@ -0,0 +1,27 @@
1
+ require "spec_helper"
2
+
3
+ describe DataMigrate::Config do
4
+
5
+ it "sets default data_migrations_path path", :no_override do
6
+ expect(DataMigrate.config.data_migrations_path).to eq "db/data/"
7
+ end
8
+
9
+ describe "data migration path configured" do
10
+ before do
11
+ @before = DataMigrate.config.data_migrations_path
12
+ DataMigrate.configure do |config|
13
+ config.data_migrations_path = "db/awesome/"
14
+ end
15
+ end
16
+
17
+ after do
18
+ DataMigrate.configure do |config|
19
+ config.data_migrations_path = @before
20
+ end
21
+ end
22
+
23
+ it do
24
+ expect(DataMigrate.config.data_migrations_path).to eq "db/awesome/"
25
+ end
26
+ end
27
+ end
@@ -9,6 +9,11 @@ describe DataMigrate::DataMigrator do
9
9
  }
10
10
  end
11
11
 
12
+ before do
13
+ allow(DataMigrate::DataMigrator).to receive(:db_config) { db_config }
14
+ ActiveRecord::Base.establish_connection(db_config)
15
+ end
16
+
12
17
  describe :load_migrated do
13
18
  before do
14
19
  allow(subject).to receive(:db_config) { db_config }.at_least(:once)
@@ -56,6 +61,22 @@ describe DataMigrate::DataMigrator do
56
61
  end
57
62
  end
58
63
 
64
+ describe "#migrations_status" do
65
+ before do
66
+ allow(subject).to receive(:db_config) { db_config }.at_least(:once)
67
+ ActiveRecord::Base.establish_connection(db_config)
68
+ ::ActiveRecord::SchemaMigration.create_table
69
+ DataMigrate::DataSchemaMigration.create_table
70
+ end
71
+
72
+ it "returns all migrations statuses" do
73
+ status = subject.migrations_status
74
+ expect(status.length).to eq 2
75
+ expect(status.first).to eq ["down", "20091231235959", "Some name"]
76
+ expect(status.second).to eq ["down", "20171231235959", "Super update"]
77
+ end
78
+ end
79
+
59
80
  describe :match do
60
81
  context "when the file does not match" do
61
82
  it "returns nil" do
@@ -5,7 +5,9 @@ require "spec_helper"
5
5
  describe DataMigrate::DatabaseTasks do
6
6
  let(:subject) { DataMigrate::DatabaseTasks }
7
7
  let(:migration_path) {
8
- if Rails::VERSION::MAJOR == 5
8
+ if Rails::VERSION::MAJOR == 6
9
+ "spec/db/migrate/6.0"
10
+ elsif Rails::VERSION::MAJOR == 5
9
11
  if Rails::VERSION::MINOR == 2
10
12
  "spec/db/migrate/5.2"
11
13
  else
@@ -16,15 +18,7 @@ describe DataMigrate::DatabaseTasks do
16
18
  end
17
19
  }
18
20
  let(:data_migrations_path) {
19
- if Rails::VERSION::MAJOR == 5
20
- if Rails::VERSION::MINOR == 2
21
- "spec/db/data"
22
- else
23
- "spec/db/5.0"
24
- end
25
- else
26
- "spec/db/4.2"
27
- end
21
+ DataMigrate.config.data_migrations_path
28
22
  }
29
23
  let(:db_config) do
30
24
  {
@@ -2,15 +2,24 @@ require "spec_helper"
2
2
 
3
3
  describe DataMigrate::DataMigrator do
4
4
  let(:context) {
5
- DataMigrate::MigrationContext.new("spec/db/data")
5
+ if (Rails::VERSION::MAJOR == 6)
6
+ DataMigrate::MigrationContext.new("spec/db/data-6.0")
7
+ else
8
+ DataMigrate::MigrationContext.new("spec/db/data")
9
+ end
6
10
  }
7
11
  let(:schema_context) {
8
- ActiveRecord::MigrationContext.new("spec/db/migrate/5.2")
12
+ if (Rails::VERSION::MAJOR == 6)
13
+ ActiveRecord::MigrationContext.new("spec/db/migrate/6.0", ActiveRecord::Base.connection.schema_migration)
14
+ else
15
+ ActiveRecord::MigrationContext.new("spec/db/migrate/5.2")
16
+ end
9
17
  }
10
18
 
11
19
  before do
12
- unless Rails::VERSION::MAJOR == 5 and
13
- Rails::VERSION::MINOR == 2
20
+ unless (Rails::VERSION::MAJOR == 5 and
21
+ Rails::VERSION::MINOR == 2) ||
22
+ Rails::VERSION::MAJOR == 6
14
23
  skip("Tests are only applicable for Rails 5.2")
15
24
  end
16
25
  end
@@ -4,7 +4,9 @@ require "spec_helper"
4
4
 
5
5
  describe DataMigrate::SchemaMigration do
6
6
  let(:migration_path) {
7
- if Rails::VERSION::MAJOR == 5
7
+ if Rails::VERSION::MAJOR == 6
8
+ "spec/db/migrate/6.0"
9
+ elsif Rails::VERSION::MAJOR == 5
8
10
  if Rails::VERSION::MINOR == 2
9
11
  "spec/db/migrate/5.2"
10
12
  else
@@ -46,7 +46,7 @@ describe DataMigrate::StatusService do
46
46
  VALUES #{fixture_file_timestamps.map { |t| "(#{t})" }.join(', ')}
47
47
  SQL
48
48
 
49
- allow_any_instance_of(service).to receive(:root_folder) { "spec" }
49
+ allow_any_instance_of(service).to receive(:root_folder) { "./" }
50
50
  end
51
51
 
52
52
  after do
@@ -62,6 +62,15 @@ describe DataMigrate::StatusService do
62
62
  expect(stream.read).to include expected
63
63
  end
64
64
 
65
+ it "excludes files without .rb extension" do
66
+ stream = StringIO.new
67
+ service.dump(ActiveRecord::Base.connection, stream)
68
+ stream.rewind
69
+
70
+ expected = "20181128000207 Excluded file"
71
+ expect(stream.read).to_not include expected
72
+ end
73
+
65
74
  it "shows missing file migration" do
66
75
  stream = StringIO.new
67
76
  service.dump(ActiveRecord::Base.connection, stream)
@@ -11,21 +11,6 @@ describe DataMigrate::Tasks::DataMigrateTasks do
11
11
  end
12
12
 
13
13
  before do
14
- if Rails::VERSION::MAJOR == 5
15
- if Rails::VERSION::MINOR == 2
16
- allow(DataMigrate::Tasks::DataMigrateTasks).to receive(:migrations_paths) {
17
- "spec/db/data"
18
- }
19
- else
20
- allow(DataMigrate::Tasks::DataMigrateTasks).to receive(:migrations_paths) {
21
- "spec/db/5.0"
22
- }
23
- end
24
- else
25
- allow(DataMigrate::Tasks::DataMigrateTasks).to receive(:migrations_paths) {
26
- "spec/db/4.2"
27
- }
28
- end
29
14
  allow(DataMigrate::DataMigrator).to receive(:db_config) { db_config }
30
15
  ActiveRecord::Base.establish_connection(db_config)
31
16
  end
@@ -36,9 +21,9 @@ describe DataMigrate::Tasks::DataMigrateTasks do
36
21
 
37
22
  describe :migrate do
38
23
  it do
39
- expect {
24
+ expect {
40
25
  DataMigrate::Tasks::DataMigrateTasks.migrate
41
- }.to output(/20091231235959 SomeName: migrating/).to_stdout
26
+ }.to output(/20091231235959 SomeName: migrating/).to_stdout
42
27
  end
43
28
 
44
29
  it do
@@ -1,4 +1,4 @@
1
- class SomeName < ActiveRecord::Migration
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
1
+ class SuperUpdate < ActiveRecord::Migration[6.0]
2
2
  def up
3
3
  puts "Doing data migration"
4
4
  end
@@ -0,0 +1,9 @@
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
@@ -0,0 +1,9 @@
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
@@ -0,0 +1 @@
1
+ # This file should be excluded
@@ -0,0 +1 @@
1
+ # This file should be excluded
@@ -0,0 +1,9 @@
1
+ class LateMigration < ActiveRecord::Migration[6.0]
2
+ def up
3
+ puts "Doing schema LateMigration"
4
+ end
5
+
6
+ def down
7
+ puts "Undoing LateMigration"
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class DbMigration < ActiveRecord::Migration[6.0]
2
+ def up
3
+ puts "Doing schema migration"
4
+ end
5
+
6
+ def down
7
+ puts "Undoing DbMigration"
8
+ end
9
+ end
@@ -24,4 +24,37 @@ describe DataMigrate::Generators::DataMigrationGenerator do
24
24
  end
25
25
  end
26
26
  end
27
+
28
+ describe :create_data_migration do
29
+ let(:subject) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }
30
+ let(:data_migrations_file_path) { 'abc/my_migration.rb' }
31
+
32
+ context 'when custom data migrations path has a trailing slash' do
33
+ before do
34
+ DataMigrate.config.data_migrations_path = 'abc/'
35
+ end
36
+
37
+ it 'returns correct file path' do
38
+ expect(subject).to receive(:migration_template).with(
39
+ 'data_migration.rb', data_migrations_file_path
40
+ )
41
+
42
+ subject.create_data_migration
43
+ end
44
+ end
45
+
46
+ context 'when custom data migrations path does not have a trailing slash' do
47
+ before do
48
+ DataMigrate.config.data_migrations_path = 'abc'
49
+ end
50
+
51
+ it 'returns correct file path' do
52
+ expect(subject).to receive(:migration_template).with(
53
+ 'data_migration.rb', data_migrations_file_path
54
+ )
55
+
56
+ subject.create_data_migration
57
+ end
58
+ end
59
+ end
27
60
  end