data_migrate 7.0.0 → 8.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.ruby-version +1 -1
  3. data/.travis.yml +6 -9
  4. data/Appraisals +4 -8
  5. data/Changelog.md +16 -1
  6. data/Gemfile +0 -7
  7. data/README.md +1 -1
  8. data/gemfiles/{rails_5.1.gemfile → rails_7.0.gemfile} +1 -1
  9. data/lib/data_migrate/database_tasks.rb +14 -0
  10. data/lib/data_migrate/schema_dumper.rb +1 -5
  11. data/lib/data_migrate/status_service_five.rb +6 -2
  12. data/lib/data_migrate/tasks/data_migrate_tasks.rb +93 -8
  13. data/lib/data_migrate/version.rb +1 -1
  14. data/lib/data_migrate.rb +10 -32
  15. data/lib/generators/data_migration/data_migration_generator.rb +1 -7
  16. data/lib/generators/data_migration/templates/data_migration.rb +2 -0
  17. data/spec/data_migrate/database_tasks_spec.rb +4 -9
  18. data/spec/data_migrate/migration_context_spec.rb +6 -14
  19. data/spec/data_migrate/schema_migration_spec.rb +21 -22
  20. data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +46 -0
  21. data/spec/db/data/partial_schema/data_schema.rb +0 -1
  22. data/spec/db/data/partial_schema/test_data_schema.rb +0 -1
  23. data/spec/db/data/schema/data_schema.rb +0 -1
  24. data/spec/db/data/schema/test_data_schema.rb +0 -1
  25. data/spec/generators/data_migration/data_migration_generator_spec.rb +7 -7
  26. data/spec/spec_helper.rb +3 -13
  27. data/tasks/databases.rake +20 -95
  28. metadata +6 -22
  29. data/Gemfile.rails5 +0 -10
  30. data/Gemfile.rails5.1 +0 -10
  31. data/gemfiles/rails_4.1.gemfile +0 -7
  32. data/gemfiles/rails_4.2.gemfile +0 -9
  33. data/gemfiles/rails_5.0.gemfile +0 -8
  34. data/lib/data_migrate/data_migrator.rb +0 -101
  35. data/lib/data_migrate/migration.rb +0 -26
  36. data/lib/data_migrate/migration_five.rb +0 -26
  37. data/lib/data_migrate/schema_migration.rb +0 -31
  38. data/lib/data_migrate/status_service.rb +0 -65
  39. data/spec/db/5.0/20091231235959_some_name.rb +0 -9
  40. data/spec/db/5.0/20171231235959_super_update.rb +0 -9
  41. data/spec/db/migrate/4.2/20131111111111_late_migration.rb +0 -9
  42. data/spec/db/migrate/4.2/20202020202011_db_migration.rb +0 -9
  43. data/spec/db/migrate/5.0/20131111111111_late_migration.rb +0 -9
  44. data/spec/db/migrate/5.0/20202020202011_db_migration.rb +0 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7dd1c0cc04a76e4332a3af19199beab36e329ecb2af1ab8d0fd706d0457e6f93
4
- data.tar.gz: 1431e365d30537f284039aa1dd6dd7db12b24f9ec845fc3d0d3448adbec70c1c
3
+ metadata.gz: '09b6187a1a958297e53be31177698552df42454a2e075fcaf392add7fb9f9085'
4
+ data.tar.gz: 71af0e938b31d223f04399e771ff1dbbba8cac52b5560ce0305da7935e1c03a0
5
5
  SHA512:
6
- metadata.gz: c46327a2feee44ac29106baefa254204ce4adfa914e849ae0f63c6b617369ad3c4c00b0ece39f94137b5df2547ceecf73f8d3716539b38476467cac11b9ce664
7
- data.tar.gz: d635f7561555a22a3695f60bfcb51eeb93ed89cce158abaeba78f373dbb37746be42ff94fb9277f65d48964887cde7587e215bbda3ee565f3c3aee3e40af6d46
6
+ metadata.gz: dd9810087eaef6a8fb537401908d945007436b946759284fdfdd5836d9d7632518452039ec1432aab23743b54bd58401fa2e44572af34a87c47e924f7612c71f
7
+ data.tar.gz: 5b0aa4e76fc0641c3d609f4f434d91905c48a2a281952c9ebc0817aa71cea757bc5c68823351f83b1a2776e11c87085320bbb7a884cb75f29304f0235eab7453
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.6.1
1
+ 2.7.5
data/.travis.yml CHANGED
@@ -1,17 +1,14 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.4.4
4
- - 2.5.5
5
- - 2.6.2
3
+ - 2.7
4
+ - 3.0
6
5
  script: bundle exec rspec
7
6
  gemfile:
8
- - gemfiles/rails_5.1.gemfile
9
7
  - gemfiles/rails_5.2.gemfile
10
8
  - gemfiles/rails_6.0.gemfile
11
9
  - gemfiles/rails_6.1.gemfile
12
- matrix:
10
+ - gemfiles/rails_7.0.gemfile
11
+ jobs:
13
12
  exclude:
14
- - rvm: 2.4.4
15
- gemfile: gemfiles/rails_6.0.gemfile
16
- - rvm: 2.4.4
17
- gemfile: gemfiles/rails_6.1.gemfile
13
+ - rvm: 3.0
14
+ gemfile: gemfiles/rails_5.2.gemfile
data/Appraisals CHANGED
@@ -1,11 +1,3 @@
1
- appraise 'rails-5.0' do
2
- gem 'rails', '5.0.7.2'
3
- end
4
-
5
- appraise 'rails-5.1' do
6
- gem 'rails', '5.1.7'
7
- end
8
-
9
1
  appraise 'rails-5.2' do
10
2
  gem 'rails', '~> 5.2.3'
11
3
  end
@@ -17,3 +9,7 @@ end
17
9
  appraise 'rails-6.1' do
18
10
  gem 'rails', '~> 6.1.0'
19
11
  end
12
+
13
+ appraise 'rails-7.0' do
14
+ gem 'rails', '~> 7.0'
15
+ end
data/Changelog.md CHANGED
@@ -1,9 +1,24 @@
1
1
  # Changelog
2
2
 
3
+ ## 8.0.0.rc2
4
+
5
+ Bug fixes [gdott9](https://github.com/gdott9)
6
+
7
+ ## 8.0.0.rc1
8
+ Add support for Rails 7
9
+ Removed support for Rails versions below 5.2. Now are supported only versions Rails 5.2 and up
10
+
11
+ ## 7.0.2
12
+
13
+ Remove magic comment in migration files [y-yagi](https://github.com/y-yagi)
14
+ User frozen string [jonnay](https://github.com/jonnay)
15
+ ## 7.0.1
16
+ Use SchemaMigration.migrations_paths in main rake task [lewhit](https://github.com/lewhit)
17
+
3
18
  ## 6.8.0
4
19
 
5
20
  Specify database name for migrations_paths [lewhit](https://github.com/lewhit)
6
- # 6.7.0
21
+ ## 6.7.0
7
22
 
8
23
  Add configuration for which database name is to be used for database migrations [lewhit](https://github.com/lewhit)
9
24
  Add tests for Rails 6.1 [lewhit](https://github.com/lewhit)
data/Gemfile CHANGED
@@ -1,11 +1,4 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in data_migrate.gemspec
4
- %w[
5
- activerecord
6
- railties
7
- ].each do |rails_gem|
8
- gem rails_gem, '~> 6.0.0'
9
- end
10
3
  gem 'sqlite3', "~> 1.4"
11
4
  gemspec
data/README.md CHANGED
@@ -36,7 +36,7 @@ table to track all migrations.
36
36
 
37
37
  ## Rails Support
38
38
 
39
- Support Rails 5.0 through 6.0
39
+ Support Rails 5.2 through 7.0
40
40
 
41
41
  ### Important notes for older versions
42
42
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "rails", "5.1.7"
6
5
  gem "sqlite3", "~> 1.4"
6
+ gem "rails", "~> 7.0"
7
7
 
8
8
  gemspec path: "../"
@@ -23,6 +23,20 @@ module DataMigrate
23
23
  ENV["DATA_SCHEMA"] || File.join(schema_location, filename)
24
24
  end
25
25
 
26
+ def schema_dump_path(db_config, format = ActiveRecord.schema_format)
27
+ return ENV["DATA_SCHEMA"] if ENV["DATA_SCHEMA"]
28
+
29
+ filename = if db_config.primary?
30
+ schema_file_type(format)
31
+ else
32
+ [db_config.name, schema_file_type(format)].join("_")
33
+ end
34
+
35
+ return unless filename
36
+
37
+ File.dirname(filename) == schema_location ? filename : File.join(schema_location, filename)
38
+ end
39
+
26
40
  def schema_location
27
41
  db_dir
28
42
  end
@@ -8,7 +8,7 @@ module DataMigrate
8
8
  private_class_method :new
9
9
 
10
10
  class << self
11
- def dump(connection = ActiveRecord::Base.connection, stream = STDOUT)
11
+ def dump(connection = ActiveRecord::Base.connection, stream = $stdout)
12
12
  new(connection).dump(stream)
13
13
  stream
14
14
  end
@@ -17,10 +17,6 @@ module DataMigrate
17
17
  def dump(stream)
18
18
  define_params = @version ? "version: #{@version}" : ""
19
19
 
20
- if stream.respond_to?(:external_encoding) && stream.external_encoding
21
- stream.puts "# encoding: #{stream.external_encoding.name}"
22
- end
23
-
24
20
  stream.puts "DataMigrate::Data.define(#{define_params})"
25
21
 
26
22
  stream
@@ -1,7 +1,7 @@
1
1
  module DataMigrate
2
2
  class StatusService
3
3
  class << self
4
- def dump(connection = ActiveRecord::Base.connection, stream = STDOUT)
4
+ def dump(connection = ActiveRecord::Base.connection, stream = $stdout)
5
5
  new(connection).dump(stream)
6
6
  stream
7
7
  end
@@ -32,7 +32,11 @@ module DataMigrate
32
32
  end
33
33
 
34
34
  # output
35
- stream.puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
35
+ if ActiveRecord.version >= Gem::Version.new('7.0')
36
+ stream.puts "\ndatabase: #{ActiveRecord::Base.connection_db_config.configuration_hash[:database]}\n\n"
37
+ else
38
+ stream.puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
39
+ end
36
40
  stream.puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
37
41
  stream.puts "-" * 50
38
42
  db_list.each do |status, version, name|
@@ -2,12 +2,17 @@ module DataMigrate
2
2
  module Tasks
3
3
  module DataMigrateTasks
4
4
  extend self
5
+
6
+ def schema_migrations_path
7
+ File.join('db', 'migrate')
8
+ end
9
+
5
10
  def migrations_paths
6
11
  @migrations_paths ||= DataMigrate.config.data_migrations_path
7
12
  end
8
13
 
9
14
  def dump
10
- if ActiveRecord::Base.dump_schema_after_migration
15
+ if dump_schema_after_migration?
11
16
  filename = DataMigrate::DatabaseTasks.schema_file
12
17
  ActiveRecord::Base.establish_connection(DataMigrate.config.db_configuration) if DataMigrate.config.db_configuration
13
18
  File.open(filename, "w:utf-8") do |file|
@@ -17,14 +22,10 @@ module DataMigrate
17
22
  end
18
23
 
19
24
  def migrate
20
- DataMigrate::DataMigrator.assure_data_schema_table
21
25
  target_version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
22
- if (Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR == 2) ||
23
- Rails::VERSION::MAJOR == 6
24
- DataMigrate::MigrationContext.new(migrations_paths).migrate(target_version)
25
- else
26
- DataMigrate::DataMigrator.migrate(migrations_paths, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
27
- end
26
+
27
+ DataMigrate::DataMigrator.assure_data_schema_table
28
+ DataMigrate::MigrationContext.new(migrations_paths).migrate(target_version)
28
29
  end
29
30
 
30
31
  def abort_if_pending_migrations(migrations, message)
@@ -36,6 +37,90 @@ module DataMigrate
36
37
  abort message
37
38
  end
38
39
  end
40
+
41
+ def dump_schema_after_migration?
42
+ if ActiveRecord.respond_to?(:dump_schema_after_migration)
43
+ ActiveRecord.dump_schema_after_migration
44
+ else
45
+ ActiveRecord::Base.dump_schema_after_migration
46
+ end
47
+ end
48
+
49
+ def status
50
+ config = connect_to_database
51
+ return unless config
52
+
53
+ connection = ActiveRecord::Base.connection
54
+ puts "\ndatabase: #{config['database']}\n\n"
55
+ DataMigrate::StatusService.dump(connection)
56
+ end
57
+
58
+ def status_with_schema
59
+ config = connect_to_database
60
+ return unless config
61
+
62
+ db_list_data = ActiveRecord::Base.connection.select_values(
63
+ "SELECT version FROM #{DataMigrate::DataSchemaMigration.table_name}"
64
+ )
65
+ db_list_schema = ActiveRecord::Base.connection.select_values(
66
+ "SELECT version FROM #{ActiveRecord::SchemaMigration.schema_migrations_table_name}"
67
+ )
68
+ file_list = []
69
+
70
+ Dir.foreach(File.join(Rails.root, migrations_paths)) do |file|
71
+ # only files matching "20091231235959_some_name.rb" pattern
72
+ if match_data = /(\d{14})_(.+)\.rb/.match(file)
73
+ status = db_list_data.delete(match_data[1]) ? 'up' : 'down'
74
+ file_list << [status, match_data[1], match_data[2], 'data']
75
+ end
76
+ end
77
+
78
+ Dir.foreach(File.join(Rails.root, schema_migrations_path)) do |file|
79
+ # only files matching "20091231235959_some_name.rb" pattern
80
+ if match_data = /(\d{14})_(.+)\.rb/.match(file)
81
+ status = db_list_schema.delete(match_data[1]) ? 'up' : 'down'
82
+ file_list << [status, match_data[1], match_data[2], 'schema']
83
+ end
84
+ end
85
+
86
+ file_list.sort!{|a,b| "#{a[1]}_#{a[3] == 'data' ? 1 : 0}" <=> "#{b[1]}_#{b[3] == 'data' ? 1 : 0}" }
87
+
88
+ # output
89
+ puts "\ndatabase: #{config['database']}\n\n"
90
+ puts "#{"Status".center(8)} #{"Type".center(8)} #{"Migration ID".ljust(14)} Migration Name"
91
+ puts "-" * 60
92
+ file_list.each do |file|
93
+ puts "#{file[0].center(8)} #{file[3].center(8)} #{file[1].ljust(14)} #{file[2].humanize}"
94
+ end
95
+ db_list_schema.each do |version|
96
+ puts "#{'up'.center(8)} #{version.ljust(14)} *** NO SCHEMA FILE ***"
97
+ end
98
+ db_list_data.each do |version|
99
+ puts "#{'up'.center(8)} #{version.ljust(14)} *** NO DATA FILE ***"
100
+ end
101
+ puts
102
+ end
103
+
104
+ private
105
+
106
+ def connect_to_database
107
+ config = if ActiveRecord.version < Gem::Version.new('6.1')
108
+ ActiveRecord::Base.configurations[Rails.env || 'development']
109
+ else
110
+ ActiveRecord::Base.configurations.find_db_config(Rails.env || 'development').configuration_hash
111
+ end
112
+ ActiveRecord::Base.establish_connection(config)
113
+
114
+ unless DataMigrate::DataSchemaMigration.table_exists?
115
+ puts 'Data migrations table does not exist yet.'
116
+ config = nil
117
+ end
118
+ unless ActiveRecord::SchemaMigration.table_exists?
119
+ puts 'Schema migrations table does not exist yet.'
120
+ config = nil
121
+ end
122
+ config
123
+ end
39
124
  end
40
125
  end
41
126
  end
@@ -1,3 +1,3 @@
1
1
  module DataMigrate
2
- VERSION = "7.0.0".freeze
2
+ VERSION = "8.0.0.rc2".freeze
3
3
  end
data/lib/data_migrate.rb CHANGED
@@ -1,44 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- if Rails::VERSION::MAJOR == 6
4
- require File.join(File.dirname(__FILE__), "data_migrate", "data_migrator_five")
5
- elsif Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR == 2
6
- require File.join(File.dirname(__FILE__), "data_migrate", "data_migrator_five")
7
- else
8
- require File.join(File.dirname(__FILE__), "data_migrate", "data_migrator")
9
- end
10
- require File.join(File.dirname(__FILE__), "data_migrate",
11
- "data_schema_migration")
3
+ require File.join(File.dirname(__FILE__), "data_migrate", "data_migrator_five")
4
+ require File.join(File.dirname(__FILE__), "data_migrate", "data_schema_migration")
12
5
  require File.join(File.dirname(__FILE__), "data_migrate", "data_schema")
13
6
  require File.join(File.dirname(__FILE__), "data_migrate", "database_tasks")
14
7
  require File.join(File.dirname(__FILE__), "data_migrate", "schema_dumper")
15
- if Rails::VERSION::MAJOR == 6
16
- require File.join(File.dirname(__FILE__), "data_migrate", "status_service_five")
17
- require File.join(File.dirname(__FILE__), "data_migrate", "schema_migration_six")
18
- elsif Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR == 2
19
- require File.join(File.dirname(__FILE__), "data_migrate", "status_service_five")
20
- require File.join(File.dirname(__FILE__), "data_migrate", "schema_migration_five")
21
- else
22
- require File.join(File.dirname(__FILE__), "data_migrate", "status_service")
23
- require File.join(File.dirname(__FILE__), "data_migrate", "schema_migration")
24
- end
25
-
26
- if Rails::VERSION::MAJOR == 6
27
- require File.join(File.dirname(__FILE__), "data_migrate", "migration_context")
28
- # require File.join(File.dirname(__FILE__), "data_migrate", "migration_five")
29
- elsif Rails::VERSION::MAJOR == 5
30
- if Rails::VERSION::MINOR == 2
31
- require File.join(File.dirname(__FILE__), "data_migrate", "migration_context")
32
- else
33
- require File.join(File.dirname(__FILE__), "data_migrate", "migration_five")
34
- end
35
- else
36
- require File.join(File.dirname(__FILE__), "data_migrate", "migration")
37
- end
8
+ require File.join(File.dirname(__FILE__), "data_migrate", "status_service_five")
9
+ require File.join(File.dirname(__FILE__), "data_migrate", "migration_context")
38
10
  require File.join(File.dirname(__FILE__), "data_migrate", "railtie")
39
11
  require File.join(File.dirname(__FILE__), "data_migrate", "tasks/data_migrate_tasks")
40
12
  require File.join(File.dirname(__FILE__), "data_migrate", "legacy_migrator")
41
13
  require File.join(File.dirname(__FILE__), "data_migrate", "config")
42
14
 
15
+ if Rails::VERSION::MAJOR == 5
16
+ require File.join(File.dirname(__FILE__), "data_migrate", "schema_migration_five")
17
+ else
18
+ require File.join(File.dirname(__FILE__), "data_migrate", "schema_migration_six")
19
+ end
20
+
43
21
  module DataMigrate
44
22
  end
@@ -27,13 +27,7 @@ module DataMigrate
27
27
  end
28
28
 
29
29
  def migration_base_class_name
30
- if ActiveRecord.version >= Gem::Version.new("5.0")
31
- "ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]"
32
- elsif ActiveRecord.version >= Gem::Version.new("5.2")
33
- "DataMigrate::MigrationContext"
34
- else
35
- "ActiveRecord::Migration"
36
- end
30
+ "ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]"
37
31
  end
38
32
 
39
33
  def data_migrations_file_path
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class <%= migration_class_name %> < <%= migration_base_class_name %>
2
4
  def up
3
5
  end
@@ -5,16 +5,10 @@ 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 == 6
9
- "spec/db/migrate/6.0"
10
- elsif Rails::VERSION::MAJOR == 5
11
- if Rails::VERSION::MINOR == 2
12
- "spec/db/migrate/5.2"
13
- else
14
- "spec/db/migrate/5.0"
15
- end
8
+ if Rails::VERSION::MAJOR == 5
9
+ "spec/db/migrate/5.2"
16
10
  else
17
- "spec/db/migrate/4.2"
11
+ "spec/db/migrate/6.0"
18
12
  end
19
13
  }
20
14
  let(:data_migrations_path) {
@@ -32,6 +26,7 @@ describe DataMigrate::DatabaseTasks do
32
26
  # Rails.application.config.paths["db"].first
33
27
  # @see https://github.com/rails/rails/blob/a7d49ef78c36df2d1ca876451f30915ada1079a5/activerecord/lib/active_record/tasks/database_tasks.rb#L54
34
28
  allow(subject).to receive(:db_dir).and_return("db")
29
+ allow(ActiveRecord::Tasks::DatabaseTasks).to receive(:db_dir).and_return("db")
35
30
  end
36
31
 
37
32
  before do
@@ -2,28 +2,20 @@ require "spec_helper"
2
2
 
3
3
  describe DataMigrate::DataMigrator do
4
4
  let(:context) {
5
- if (Rails::VERSION::MAJOR == 6)
6
- DataMigrate::MigrationContext.new("spec/db/data-6.0")
7
- else
5
+ if (Rails::VERSION::MAJOR == 5)
8
6
  DataMigrate::MigrationContext.new("spec/db/data")
7
+ else
8
+ DataMigrate::MigrationContext.new("spec/db/data-6.0")
9
9
  end
10
10
  }
11
11
  let(:schema_context) {
12
- if (Rails::VERSION::MAJOR == 6)
13
- ActiveRecord::MigrationContext.new("spec/db/migrate/6.0", ActiveRecord::Base.connection.schema_migration)
14
- else
12
+ if (Rails::VERSION::MAJOR == 5)
15
13
  ActiveRecord::MigrationContext.new("spec/db/migrate/5.2")
14
+ else
15
+ ActiveRecord::MigrationContext.new("spec/db/migrate/6.0", ActiveRecord::Base.connection.schema_migration)
16
16
  end
17
17
  }
18
18
 
19
- before do
20
- unless (Rails::VERSION::MAJOR == 5 and
21
- Rails::VERSION::MINOR == 2) ||
22
- Rails::VERSION::MAJOR == 6
23
- skip("Tests are only applicable for Rails 5.2")
24
- end
25
- end
26
-
27
19
  after do
28
20
  begin
29
21
  ActiveRecord::Migration.drop_table("data_migrations")
@@ -4,16 +4,10 @@ require "spec_helper"
4
4
 
5
5
  describe DataMigrate::SchemaMigration do
6
6
  let(:migration_path) {
7
- if Rails::VERSION::MAJOR == 6
8
- "spec/db/migrate/6.0"
9
- elsif Rails::VERSION::MAJOR == 5
10
- if Rails::VERSION::MINOR == 2
11
- "spec/db/migrate/5.2"
12
- else
13
- "spec/db/migrate/5.0"
14
- end
7
+ if Rails::VERSION::MAJOR == 5
8
+ "spec/db/migrate/5.2"
15
9
  else
16
- "spec/db/migrate/4.2"
10
+ "spec/db/migrate/6.0"
17
11
  end
18
12
  }
19
13
 
@@ -72,22 +66,27 @@ describe DataMigrate::SchemaMigration do
72
66
  if Rails.version > '6'
73
67
  describe :migrations_paths do
74
68
  context 'when a db_name is configured' do
69
+ let(:config) { double(:config) }
75
70
  let(:paths) { ['spec/db/migrate/6.0', 'spec/db/components/migrate/6.0'] }
76
-
77
- if Rails.version > '6.1'
78
- before do
79
- allow(ActiveRecord::Base.configurations.configs_for(env_name: 'test', name: 'primary')).to receive(:migrations_paths).and_return(paths)
80
- DataMigrate.configure do |config|
81
- config.spec_name = 'primary'
82
- end
71
+ let(:config_options) do
72
+ if Rails.version > '6.1'
73
+ { env_name: 'test', name: 'primary' }
74
+ else
75
+ { env_name: 'test', spec_name: 'primary' }
83
76
  end
84
- else
85
- before do
86
- allow(ActiveRecord::Base.configurations.configs_for(env_name: 'test', spec_name: 'primary')).to receive(:migrations_paths).and_return(paths)
87
- DataMigrate.configure do |config|
88
- config.spec_name = 'primary'
89
- end
77
+ end
78
+
79
+ before do
80
+ DataMigrate.configure do |config|
81
+ config.spec_name = 'primary'
90
82
  end
83
+
84
+ allow(ActiveRecord::Base.configurations)
85
+ .to receive(:configs_for)
86
+ .with(config_options)
87
+ .and_return(config)
88
+
89
+ allow(config).to receive(:migrations_paths).and_return(paths)
91
90
  end
92
91
 
93
92
  it 'lists schema migration paths' do
@@ -115,4 +115,50 @@ describe DataMigrate::Tasks::DataMigrateTasks do
115
115
  end
116
116
  end
117
117
  end
118
+
119
+ describe :status do
120
+ let(:db_config) do
121
+ {
122
+ adapter: "sqlite3",
123
+ database: "spec/db/test.db"
124
+ }
125
+ end
126
+
127
+ 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
143
+
144
+ DataMigrate::Tasks::DataMigrateTasks.migrate
145
+ end
146
+
147
+ after do
148
+ ActiveRecord::Migration.drop_table("data_migrations")
149
+ end
150
+
151
+ it "should display data migration status" do
152
+ expect {
153
+ DataMigrate::Tasks::DataMigrateTasks.status
154
+ }.to output(/up 20091231235959 Some name/).to_stdout
155
+ end
156
+
157
+ it "should display schema and data migration status" do
158
+ expect {
159
+ DataMigrate::Tasks::DataMigrateTasks.status_with_schema
160
+ }.to output(match(/up data 20091231235959 Some name/)
161
+ .and match(/down schema 20131111111111 Late migration/)).to_stdout
162
+ end
163
+ end
118
164
  end
@@ -1,2 +1 @@
1
- # encoding: UTF-8
2
1
  DataMigrate::Data.define(version: 20091231235959)
@@ -1,2 +1 @@
1
- # encoding: UTF-8
2
1
  DataMigrate::Data.define(version: 20091231235959)
@@ -1,2 +1 @@
1
- # encoding: UTF-8
2
1
  DataMigrate::Data.define(version: 20171231235959)
@@ -1,2 +1 @@
1
- # encoding: UTF-8
2
1
  DataMigrate::Data.define(version: 20171231235959)
@@ -8,8 +8,12 @@ describe DataMigrate::Generators::DataMigrationGenerator do
8
8
  describe :next_migration_number do
9
9
  it "next migration" do
10
10
  Timecop.freeze("2016-12-03 22:15:26 -0800") do
11
- expect(ActiveRecord::Base).to receive(:timestamped_migrations) { true }
12
- expect(subject.next_migration_number(1)).to eq("20161204061526")
11
+ if ActiveRecord.version >= Gem::Version.new('7.0')
12
+ expect(ActiveRecord).to receive(:timestamped_migrations) { true }
13
+ else
14
+ expect(ActiveRecord::Base).to receive(:timestamped_migrations) { true }
15
+ end
16
+ expect(subject.next_migration_number(1).to_s).to eq("20161204061526")
13
17
  end
14
18
  end
15
19
  end
@@ -17,11 +21,7 @@ describe DataMigrate::Generators::DataMigrationGenerator do
17
21
  describe :migration_base_class_name do
18
22
  let(:subject) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }
19
23
  it "returns the correct base class name" do
20
- if ActiveRecord.version >= Gem::Version.new('5.0')
21
- expect(subject.send(:migration_base_class_name)).to eq("ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]")
22
- else
23
- expect(subject.send(:migration_base_class_name)).to eq('ActiveRecord::Migration')
24
- end
24
+ expect(subject.send(:migration_base_class_name)).to eq("ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]")
25
25
  end
26
26
  end
27
27
 
data/spec/spec_helper.rb CHANGED
@@ -20,23 +20,13 @@ 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 == 6
23
+ if Rails::VERSION::MAJOR == 5
24
24
  DataMigrate.configure do |config|
25
- config.data_migrations_path = "spec/db/6.0"
26
- end
27
- elsif Rails::VERSION::MAJOR == 5
28
- if Rails::VERSION::MINOR == 2
29
- DataMigrate.configure do |config|
30
- config.data_migrations_path = "spec/db/data"
31
- end
32
- else
33
- DataMigrate.configure do |config|
34
- config.data_migrations_path = "spec/db/5.0"
35
- end
25
+ config.data_migrations_path = "spec/db/data"
36
26
  end
37
27
  else
38
28
  DataMigrate.configure do |config|
39
- config.data_migrations_path = "spec/db/5.0"
29
+ config.data_migrations_path = "spec/db/6.0"
40
30
  end
41
31
  end
42
32
  end