data_migrate 6.1.0 → 6.6.0

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: 18145281c69f3a789a8fc2de0873b3ddbed3c1ccec3faed5de71a9ed2dad6e58
4
- data.tar.gz: 93e91ecc167ad2fb8cf2ef664adf405e9d47868ead4ce0f24057269c9139dea0
3
+ metadata.gz: b5b08f7465fcf3fba0054816fa8b07b07216ba3831114b0a8d477e24caa7ba98
4
+ data.tar.gz: 556f3785c1b198cc36daac457809b95fe79754276b2ad4f1056ab1f7dfbd748d
5
5
  SHA512:
6
- metadata.gz: 3ff5ee90e37051fb6668ae63a2640c41cc8be3e301a6c5bb7b58a5581f81de71b54ecab7f66b0136c88733d9f4314b2e85e5c739da4ba9939607d7e0cde8c8b6
7
- data.tar.gz: a328b2ddc325d69d98197ae2ae5db29932377e1c0bf1947ab08297bc5973ed167fbbc42ed70d35993fb02250efab721cdb0a4fb6789339da6f02af10a1664093
6
+ metadata.gz: 3df442c3c71eb087476c1c66a0ad1b0d74672b98a6ada2fb9349874f28a3bb3321ba3bc856278f63756d778e9ea7568771788d6e94a255f6f92a0d673f9782a8
7
+ data.tar.gz: f56faf7e4dd10ef8a1c9e0698190ee5a6cdf2217e1bbdc1a153bc64b3b43e32d3d0065b81fe2e15f9811fe6a5ba3ee40f722d0009da1e00586f7be97349210cc
@@ -1,9 +1,6 @@
1
1
  name: Ruby Gem
2
2
 
3
3
  on:
4
- pull_request:
5
- branches:
6
- - master
7
4
  push:
8
5
  branches:
9
6
  - master
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/
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## 6.6.0
4
+
5
+ Allow data dump connection to be configured [lewhit](https://github.com/lewhit)
6
+
7
+ ## 6.4.0
8
+
9
+ Add primary key to data_migrations table [aandis](https://github.com/aandis)
10
+
11
+ ## 6.3.0
12
+
13
+ Add `abort_if_pending_migrations` rake tasks [tomgia](https://github.com/tomgia)
14
+
15
+ ## 6.2.0
16
+
17
+ Add `rake data:schema:load` [timkrins](https://github.com/timkrins)
18
+
3
19
  ## 6.1.0
4
20
 
5
21
  Fixing `rake db:schema:load:with_data` for Rails 6
data/README.md CHANGED
@@ -82,25 +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:version # Retrieves the current schema version number for data migrations
94
- rake db:forward:with_data # Pushes the schema to the next version (specify steps w/ STEP=n)
95
- rake db:migrate:down:with_data # Runs the "down" for a given migration VERSION
96
- rake db:migrate:redo:with_data # Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x)
97
- rake db:migrate:status:with_data # Display status of data and schema migrations
98
- rake db:migrate:up:with_data # Runs the "up" for a given migration VERSION
99
- rake db:migrate:with_data # Migrate the database data and schema (options: VERSION=x, VERBOSE=false)
100
- rake db:rollback:with_data # Rolls the schema back to the previous version (specify steps w/ STEP=n)
101
- rake db:schema:load:with_data # Load both schema.rb and data_schema.rb files into the database
102
- rake db:structure:load:with_data # Load both structure.sql and data_schema.rb files into the database
103
- 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
+
104
108
 
105
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.
106
110
 
@@ -118,6 +122,13 @@ You can override this setting in `config/initializers/data_migrate.rb`
118
122
  ```ruby
119
123
  DataMigrate.configure do |config|
120
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
+ }
121
132
  end
122
133
 
123
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)
@@ -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.1.0".freeze
2
+ VERSION = "6.6.0".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
@@ -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,20 +335,31 @@ 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
353
+
354
+ namespace :schema do
355
+ desc "Load data_schema.rb file into the database"
356
+ task load: :environment do
357
+ DataMigrate::DatabaseTasks.load_schema_current(
358
+ :ruby,
359
+ ENV["DATA_SCHEMA"]
360
+ )
361
+ end
362
+ end
344
363
  end
345
364
 
346
365
  def pending_migrations
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.1.0
4
+ version: 6.6.0
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-11-21 00:00:00.000000000 Z
13
+ date: 2020-12-15 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.1.4
292
292
  signing_key:
293
293
  specification_version: 4
294
294
  summary: Rake tasks to migrate data alongside schema changes.