data_migrate 6.2.0 → 6.6.2

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: 406033afc1679b99c3126cbbe9d033db56db817bf98a14a2f7926517e5f54587
4
- data.tar.gz: d33213b5b15ab486fce6e0d1e276d77be5a5dc69768bebb8d26400b7fe3523a8
3
+ metadata.gz: d7ade2dd2b613ca6bff60e0afbba1d88cbe9fa4aba6a133b158f2dea3c3dcc3a
4
+ data.tar.gz: ba747d96bdbd3f31598cb56bd125995d850e1fc1d5c3ca6e90554198fa816014
5
5
  SHA512:
6
- metadata.gz: eddd59586a06045a5120af7c1bb44b7f9da3099fbbdcda532f7975fb4356ba2c82a4b08bc460104df880c0030c8037cdbac0300a29ca8c789f457a3d8a7127d1
7
- data.tar.gz: 062cf7e95ae5dd4ebc7ce321fe040726badba05aa049b83cc5a2b46ac0e52d654cda011582052d928af3bcd90c95b4c1883da32522e49793b3196ffff0960e2c
6
+ metadata.gz: 60a953d37627c879c8355765f98448c7fd90bbeaa270c3449c9eb1e6d2f7c08df79eae14bf487a3950d58f20722b27190ec1e562a83b89e78d76dcf9823d6e86
7
+ data.tar.gz: 564372cfea442ca66c7a2a918bcc671662cc40f9beace2493fefcf5b7901df22abdd19f5c28aba57586a1a208c8446123a7490f473b3a1c6ebeb4aa74fd68c4d
data/.gitignore CHANGED
@@ -3,6 +3,8 @@
3
3
  *.lock
4
4
  gemfiles/.bundle
5
5
  spec/db/test.db
6
+ spec/db/other_test.db
7
+ spec/db/data_schema.rb
6
8
  .vscode/
7
9
  .DS_Store
8
10
  .idea/
data/Changelog.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## 6.6.2
4
+ ## 6.6.1
5
+
6
+ configs_for deprecation notice [borama](https://github.com/borama)
7
+ ## 6.6.0
8
+
9
+ Allow data dump connection to be configured [lewhit](https://github.com/lewhit)
10
+
11
+ ## 6.4.0
12
+
13
+ Add primary key to data_migrations table [aandis](https://github.com/aandis)
14
+
15
+ ## 6.3.0
16
+
17
+ Add `abort_if_pending_migrations` rake tasks [tomgia](https://github.com/tomgia)
18
+
3
19
  ## 6.2.0
4
20
 
5
21
  Add `rake data:schema:load` [timkrins](https://github.com/timkrins)
data/README.md CHANGED
@@ -82,26 +82,29 @@ You can generate a data migration as you would a schema migration:
82
82
  ### Rake Tasks
83
83
 
84
84
  $> rake -T data
85
- rake data:dump # Create a db/data_schema.rb file that stores the current data version
86
- rake data:forward # Pushes the schema to the next version (specify steps w/ STEP=n)
87
- rake data:migrate # Migrate data migrations (options: VERSION=x, VERBOSE=false)
88
- rake data:migrate:down # Runs the "down" for a given migration VERSION
89
- rake data:migrate:redo # Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x)
90
- rake data:migrate:status # Display status of data migrations
91
- rake data:migrate:up # Runs the "up" for a given migration VERSION
92
- rake data:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n)
93
- rake data:schema:load # Load data_schema.rb file into the database without running the data migrations
94
- rake data:version # Retrieves the current schema version number for data migrations
95
- rake db:forward:with_data # Pushes the schema to the next version (specify steps w/ STEP=n)
96
- rake db:migrate:down:with_data # Runs the "down" for a given migration VERSION
97
- rake db:migrate:redo:with_data # Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x)
98
- rake db:migrate:status:with_data # Display status of data and schema migrations
99
- rake db:migrate:up:with_data # Runs the "up" for a given migration VERSION
100
- rake db:migrate:with_data # Migrate the database data and schema (options: VERSION=x, VERBOSE=false)
101
- rake db:rollback:with_data # Rolls the schema back to the previous version (specify steps w/ STEP=n)
102
- rake db:schema:load:with_data # Load both schema.rb and data_schema.rb files into the database
103
- rake db:structure:load:with_data # Load both structure.sql and data_schema.rb files into the database
104
- rake db:version:with_data # Retrieves the current schema version numbers for data and schema migrations
85
+ rake data:abort_if_pending_migrations # Raises an error if there are pending data migrations
86
+ rake data:dump # Create a db/data_schema.rb file that stores the current data version
87
+ rake data:forward # Pushes the schema to the next version (specify steps w/ STEP=n)
88
+ rake data:migrate # Migrate data migrations (options: VERSION=x, VERBOSE=false)
89
+ rake data:migrate:down # Runs the "down" for a given migration VERSION
90
+ rake data:migrate:redo # Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x)
91
+ rake data:migrate:status # Display status of data migrations
92
+ rake data:migrate:up # Runs the "up" for a given migration VERSION
93
+ rake data:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n)
94
+ rake data:schema:load # Load data_schema.rb file into the database without running the data migrations
95
+ rake data:version # Retrieves the current schema version number for data migrations
96
+ rake db:abort_if_pending_migrations:with_data # Raises an error if there are pending migrations or data migrations
97
+ rake db:forward:with_data # Pushes the schema to the next version (specify steps w/ STEP=n)
98
+ rake db:migrate:down:with_data # Runs the "down" for a given migration VERSION
99
+ rake db:migrate:redo:with_data # Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x)
100
+ rake db:migrate:status:with_data # Display status of data and schema migrations
101
+ rake db:migrate:up:with_data # Runs the "up" for a given migration VERSION
102
+ rake db:migrate:with_data # Migrate the database data and schema (options: VERSION=x, VERBOSE=false)
103
+ rake db:rollback:with_data # Rolls the schema back to the previous version (specify steps w/ STEP=n)
104
+ rake db:schema:load:with_data # Load both schema.rb and data_schema.rb file into the database
105
+ rake db:structure:load:with_data # Load both structure.sql and data_schema.rb file into the database
106
+ rake db:version:with_data # Retrieves the current schema version numbers for data and schema migrations
107
+
105
108
 
106
109
  Tasks work as they would with the 'vanilla' db version. The 'with_data' addition to the 'db' tasks will run the task in the context of both the data and schema migrations. That is, rake db:rollback:with_data will check to see if it was a schema or data migration invoked last, and do that. Tasks invoked in that space also have an additional line of output, indicating if the action is performed on data or schema.
107
110
 
@@ -119,6 +122,13 @@ You can override this setting in `config/initializers/data_migrate.rb`
119
122
  ```ruby
120
123
  DataMigrate.configure do |config|
121
124
  config.data_migrations_path = "db/awesomepath/"
125
+ config.db_configuration = {
126
+ 'host' => '127.0.0.1',
127
+ 'database' => 'awesome_database',
128
+ 'adapter' => 'mysql2',
129
+ 'username' => 'root',
130
+ 'password' => nil,
131
+ }
122
132
  end
123
133
 
124
134
  ```
@@ -12,10 +12,11 @@ module DataMigrate
12
12
  end
13
13
 
14
14
  class Config
15
- attr_accessor :data_migrations_path
15
+ attr_accessor :data_migrations_path, :db_configuration
16
16
 
17
17
  def initialize
18
18
  @data_migrations_path = "db/data/"
19
+ @db_configuration = nil
19
20
  end
20
21
  end
21
22
  end
@@ -76,17 +76,8 @@ module DataMigrate
76
76
 
77
77
  def create_table(sm_table)
78
78
  ActiveRecord::Base.connection.create_table(sm_table, id: false) do |schema_migrations_table|
79
- schema_migrations_table.column :version, :string, null: false
79
+ schema_migrations_table.string :version, primary_key: true
80
80
  end
81
-
82
- suffix = ActiveRecord::Base.table_name_suffix
83
- prefix = ActiveRecord::Base.table_name_prefix
84
- index_name = "#{prefix}unique_data_migrations#{suffix}"
85
-
86
- options = {unique: true, name: index_name}
87
- options[:length] = 191 if ActiveRecord::Base.connection_config[:adapter] == "mysql2"
88
-
89
- ActiveRecord::Base.connection.add_index sm_table, :version, options
90
81
  end
91
82
 
92
83
  def table_exists?(connection, table_name)
@@ -75,8 +75,13 @@ module DataMigrate
75
75
  end
76
76
 
77
77
  def db_config
78
- ActiveRecord::Base.configurations[Rails.env || "development"] ||
79
- ENV["DATABASE_URL"]
78
+ env = Rails.env || "development"
79
+ ar_config = if (Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR >= 1) || Rails::VERSION::MAJOR > 6
80
+ ActiveRecord::Base.configurations.configs_for(env_name: env).first
81
+ else
82
+ ActiveRecord::Base.configurations[env]
83
+ end
84
+ ar_config || ENV["DATABASE_URL"]
80
85
  end
81
86
  end
82
87
 
@@ -6,8 +6,8 @@ module DataMigrate
6
6
  ActiveRecord::Base.table_name_prefix + 'data_migrations' + ActiveRecord::Base.table_name_suffix
7
7
  end
8
8
 
9
- def index_name
10
- "#{table_name_prefix}unique_data_migrations#{table_name_suffix}"
9
+ def primary_key
10
+ "version"
11
11
  end
12
12
  end
13
13
  end
@@ -69,7 +69,7 @@ module DataMigrate
69
69
  def self.pending_data_migrations
70
70
  data_migrations = DataMigrate::DataMigrator.migrations(data_migrations_path)
71
71
  sort_migrations(DataMigrate::DataMigrator.new(:up, data_migrations ).
72
- pending_migrations.map {|m| { version: m.version, kind: :data }})
72
+ pending_migrations.map {|m| { version: m.version, name: m.name, kind: :data }})
73
73
  end
74
74
 
75
75
  def self.pending_schema_migrations
@@ -14,8 +14,8 @@ module DataMigrate
14
14
  ActiveRecord::Base.table_name_prefix + "data_migrations" + ActiveRecord::Base.table_name_suffix
15
15
  end
16
16
 
17
- def index_name
18
- "#{table_name_prefix}unique_data_migrations#{table_name_suffix}"
17
+ def primary_key
18
+ "version"
19
19
  end
20
20
  end
21
21
 
@@ -14,8 +14,8 @@ module DataMigrate
14
14
  ActiveRecord::Base.table_name_prefix + 'data_migrations' + ActiveRecord::Base.table_name_suffix
15
15
  end
16
16
 
17
- def index_name
18
- "#{table_name_prefix}unique_data_migrations#{table_name_suffix}"
17
+ def primary_key
18
+ "version"
19
19
  end
20
20
  end
21
21
 
@@ -7,7 +7,7 @@ module DataMigrate
7
7
  sort_migrations(
8
8
  ActiveRecord::Migrator.new(:up, all_migrations).
9
9
  pending_migrations.
10
- map {|m| { version: m.version, kind: :schema }}
10
+ map {|m| { version: m.version, name: m.name, kind: :schema }}
11
11
  )
12
12
  end
13
13
 
@@ -7,7 +7,7 @@ module DataMigrate
7
7
  sort_migrations(
8
8
  ActiveRecord::Migrator.new(:up, all_migrations).
9
9
  pending_migrations.
10
- map {|m| { version: m.version, kind: :schema }}
10
+ map {|m| { version: m.version, name: m.name, kind: :schema }}
11
11
  )
12
12
  end
13
13
 
@@ -6,6 +6,16 @@ module DataMigrate
6
6
  @migrations_paths ||= DataMigrate.config.data_migrations_path
7
7
  end
8
8
 
9
+ def dump
10
+ if ActiveRecord::Base.dump_schema_after_migration
11
+ filename = DataMigrate::DatabaseTasks.schema_file
12
+ ActiveRecord::Base.establish_connection(DataMigrate.config.db_configuration) if DataMigrate.config.db_configuration
13
+ File.open(filename, "w:utf-8") do |file|
14
+ DataMigrate::SchemaDumper.dump(ActiveRecord::Base.connection, file)
15
+ end
16
+ end
17
+ end
18
+
9
19
  def migrate
10
20
  DataMigrate::DataMigrator.assure_data_schema_table
11
21
  target_version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
@@ -16,6 +26,16 @@ module DataMigrate
16
26
  DataMigrate::DataMigrator.migrate(migrations_paths, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
17
27
  end
18
28
  end
29
+
30
+ def abort_if_pending_migrations(migrations, message)
31
+ if migrations.any?
32
+ puts "You have #{migrations.size} pending #{migrations.size > 1 ? 'migrations:' : 'migration:'}"
33
+ migrations.each do |pending_migration|
34
+ puts " %4d %s" % [pending_migration[:version], pending_migration[:name]]
35
+ end
36
+ abort message
37
+ end
38
+ end
19
39
  end
20
40
  end
21
41
  end
@@ -1,3 +1,3 @@
1
1
  module DataMigrate
2
- VERSION = "6.2.0".freeze
2
+ VERSION = "6.6.2".freeze
3
3
  end
@@ -9,8 +9,8 @@ describe DataMigrate::DataSchemaMigration do
9
9
  end
10
10
 
11
11
  describe :index_name do
12
- it "returns correct index name" do
13
- expect(subject.index_name).to eq("unique_data_migrations")
12
+ it "returns correct primary key name" do
13
+ expect(subject.primary_key).to eq("version")
14
14
  end
15
15
  end
16
16
  end
@@ -14,6 +14,6 @@ describe subject do
14
14
  it "uses correct index name" do
15
15
  expect(subject).to receive(:table_name_prefix) { "" }
16
16
  expect(subject).to receive(:table_name_suffix) { "" }
17
- expect(subject.index_name).to eq("unique_data_migrations")
17
+ expect(subject.primary_key).to eq("version")
18
18
  end
19
19
  end
@@ -3,23 +3,74 @@
3
3
  require "spec_helper"
4
4
 
5
5
  describe DataMigrate::Tasks::DataMigrateTasks do
6
- let(:db_config) do
7
- {
8
- adapter: "sqlite3",
9
- database: "spec/db/test.db"
10
- }
11
- end
6
+ describe :dump do
7
+ let(:db_config) do
8
+ {
9
+ adapter: "sqlite3",
10
+ database: "spec/db/other_test.db"
11
+ }
12
+ end
12
13
 
13
- before do
14
- allow(DataMigrate::DataMigrator).to receive(:db_config) { db_config }
15
- ActiveRecord::Base.establish_connection(db_config)
16
- end
14
+ before do
15
+ allow(DataMigrate::DataMigrator).to receive(:db_config) { db_config }
16
+ allow(DataMigrate::DatabaseTasks).to receive(:db_dir).and_return("spec/db")
17
+ end
18
+
19
+ after do
20
+ ActiveRecord::Migration.drop_table("data_migrations")
21
+ end
22
+
23
+ context 'when not given a separate db config' do
24
+ it 'does not override the default connection' do
25
+ DataMigrate::Tasks::DataMigrateTasks.migrate
26
+ expect(ActiveRecord::Base).not_to receive(:establish_connection)
27
+ expect(DataMigrate::SchemaDumper).to receive(:dump)
28
+ DataMigrate::Tasks::DataMigrateTasks.dump
29
+ end
30
+ end
31
+
32
+ context 'when given ' do
33
+ let(:override_config) do
34
+ {
35
+ 'host' => '127.0.0.1',
36
+ 'database' => 'other_test',
37
+ 'adapter' => 'sqlite3',
38
+ 'username' => 'root',
39
+ 'password' => nil,
40
+ }
41
+ end
42
+
43
+ before do
44
+ DataMigrate.configure do |config|
45
+ config.db_configuration = override_config
46
+ end
47
+ end
17
48
 
18
- after do
19
- ActiveRecord::Migration.drop_table("data_migrations")
49
+ it 'overrides the default connection' do
50
+ DataMigrate::Tasks::DataMigrateTasks.migrate
51
+ expect(ActiveRecord::Base).to receive(:establish_connection).with(override_config)
52
+ DataMigrate::Tasks::DataMigrateTasks.dump
53
+ end
54
+ end
20
55
  end
21
56
 
22
57
  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")
72
+ end
73
+
23
74
  it do
24
75
  expect {
25
76
  DataMigrate::Tasks::DataMigrateTasks.migrate
@@ -32,4 +83,36 @@ describe DataMigrate::Tasks::DataMigrateTasks do
32
83
  }.to output(/20171231235959 SuperUpdate: migrating/).to_stdout
33
84
  end
34
85
  end
86
+
87
+ describe :abort_if_pending_migrations do
88
+ subject { DataMigrate::Tasks::DataMigrateTasks.abort_if_pending_migrations(migrations, message) }
89
+
90
+ let(:message) { "ABORT_MESSAGE" }
91
+
92
+ context "when there are no pending migrations" do
93
+ let(:migrations) { [] }
94
+
95
+ it "shouldn't do anything" do
96
+ expect { subject }.to_not raise_error
97
+ end
98
+ end
99
+
100
+ context "when there are pending migrations" do
101
+ let(:migrations) do
102
+ [{
103
+ name: "A",
104
+ version: 1
105
+ }, {
106
+ name: 'B',
107
+ version: 2
108
+ }]
109
+ end
110
+
111
+ it "should abort with given message and print names and versions of pending migrations" do
112
+ expect { subject }
113
+ .to raise_error(SystemExit, message)
114
+ .and output("You have 2 pending migrations:\n 1 A\n 2 B\n").to_stdout
115
+ end
116
+ end
117
+ end
35
118
  end
data/tasks/databases.rake CHANGED
@@ -223,6 +223,14 @@ namespace :db do
223
223
  end
224
224
  end
225
225
 
226
+ namespace :abort_if_pending_migrations do
227
+ desc "Raises an error if there are pending migrations or data migrations"
228
+ task with_data: :environment do
229
+ message = %{Run `rake db:migrate:with_data` to update your database then try again.}
230
+ DataMigrate::Tasks::DataMigrateTasks.abort_if_pending_migrations(pending_migrations, message)
231
+ end
232
+ end
233
+
226
234
  namespace :schema do
227
235
  namespace :load do
228
236
  desc "Load both schema.rb and data_schema.rb file into the database"
@@ -327,21 +335,22 @@ namespace :data do
327
335
  puts "Current data version: #{DataMigrate::DataMigrator.current_version}"
328
336
  end
329
337
 
338
+ desc "Raises an error if there are pending data migrations"
339
+ task abort_if_pending_migrations: :environment do
340
+ message = %{Run `rake data:migrate` to update your database then try again.}
341
+ DataMigrate::Tasks::DataMigrateTasks.abort_if_pending_migrations(pending_data_migrations, message)
342
+ end
343
+
330
344
  desc "Create a db/data_schema.rb file that stores the current data version"
331
345
  task dump: :environment do
332
- if ActiveRecord::Base.dump_schema_after_migration
333
- filename = DataMigrate::DatabaseTasks.schema_file
334
- File.open(filename, "w:utf-8") do |file|
335
- DataMigrate::SchemaDumper.dump(ActiveRecord::Base.connection, file)
336
- end
337
- end
346
+ DataMigrate::Tasks::DataMigrateTasks.dump
338
347
 
339
348
  # Allow this task to be called as many times as required. An example
340
349
  # is the migrate:redo task, which calls other two internally
341
350
  # that depend on this one.
342
351
  Rake::Task["data:dump"].reenable
343
352
  end
344
-
353
+
345
354
  namespace :schema do
346
355
  desc "Load data_schema.rb file into the database"
347
356
  task load: :environment do
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: 6.2.0
4
+ version: 6.6.2
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: 2019-12-09 00:00:00.000000000 Z
13
+ date: 2021-02-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -288,7 +288,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
288
288
  - !ruby/object:Gem::Version
289
289
  version: '0'
290
290
  requirements: []
291
- rubygems_version: 3.0.3
291
+ rubygems_version: 3.2.3
292
292
  signing_key:
293
293
  specification_version: 4
294
294
  summary: Rake tasks to migrate data alongside schema changes.