migration_data 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -3
- data/Gemfile +1 -1
- data/README.md +4 -4
- data/lib/migration_data/active_record/migration.rb +10 -0
- data/lib/migration_data/squash.rb +2 -2
- data/lib/migration_data/testing.rb +1 -1
- data/lib/migration_data/version.rb +1 -1
- data/lib/tasks/db.rake +2 -1
- data/migration_data.gemspec +2 -2
- data/test/rails_app/config/database.yml +8 -0
- data/test/rails_app/db/test.sqlite3 +0 -0
- data/test/rake_task_test.rb +21 -2
- data/test/require_migration_test.rb +3 -3
- data/test/schema_dumper_test.rb +1 -1
- data/test/squash_test.rb +1 -1
- data/test/support/rails_app.rb +5 -0
- data/test/test_helper.rb +5 -5
- metadata +17 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c8dbb5b25cfbd8b8e4c6b83e81fb23481644a48
|
4
|
+
data.tar.gz: 92f5bc8d4c74dbdc460430553a76ff2cc7015507
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f4d04e8978a30b390c11371b3657ac806df432fc24831b830450975b49e09098f321051cf2d6b0913f931ec10599fe0f6089f5fa41f8ef502a379056ae868d7
|
7
|
+
data.tar.gz: d4d7a9e33cc75794e073da3b2a3c9ea7980bd6496cd090b726f284d37ac891acd87f8bf8b8a92e5670ee2a9f85f37e91b728f220824150671bc3be534f821647
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# MigrationData
|
2
2
|
|
3
|
-
[![Build Status](https://travis-ci.org/ka8725/migration_data.
|
3
|
+
[![Build Status](https://travis-ci.org/ka8725/migration_data.svg?branch=master)](https://travis-ci.org/ka8725/migration_data)
|
4
4
|
|
5
5
|
This gem provides functionality to write any code in migrations safely without regression.
|
6
6
|
|
@@ -30,7 +30,7 @@ Or install it yourself as:
|
|
30
30
|
|
31
31
|
## Usage
|
32
32
|
|
33
|
-
|
33
|
+
In your migration define a `data` method:
|
34
34
|
|
35
35
|
```ruby
|
36
36
|
class CreateUsers < ActiveRecord::Migration
|
@@ -71,7 +71,7 @@ describe CreateUsers do
|
|
71
71
|
before do
|
72
72
|
described_class.new.data
|
73
73
|
end
|
74
|
-
|
74
|
+
|
75
75
|
it 'works' do
|
76
76
|
expect { described_class.new.rollback }.to_not raise_exception
|
77
77
|
end
|
@@ -83,7 +83,7 @@ The helper to load migrations `require_migration` is defined in the `migration_d
|
|
83
83
|
|
84
84
|
## Clean old migration
|
85
85
|
|
86
|
-
|
86
|
+
Use `rake db:migrate:squash` to remove all your old migrations and generate one migration with the current database schema. You don't have to run migrations after this because the generated migration will have the latest database version.
|
87
87
|
|
88
88
|
## Contributing
|
89
89
|
|
@@ -1,6 +1,16 @@
|
|
1
1
|
module MigrationData
|
2
2
|
module ActiveRecord
|
3
3
|
module Migration
|
4
|
+
class << self
|
5
|
+
def migration_dirs
|
6
|
+
::ActiveRecord::Tasks::DatabaseTasks.migrations_paths
|
7
|
+
end
|
8
|
+
|
9
|
+
def migration_dir
|
10
|
+
migration_dirs.first
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
4
14
|
def self.included(base)
|
5
15
|
base.class_eval do
|
6
16
|
def exec_migration_with_data(conn, direction)
|
@@ -35,11 +35,11 @@ module MigrationData
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def migration_dirs
|
38
|
-
::ActiveRecord::
|
38
|
+
MigrationData::ActiveRecord::Migration.migration_dirs
|
39
39
|
end
|
40
40
|
|
41
41
|
def migration_dir
|
42
|
-
|
42
|
+
MigrationData::ActiveRecord::Migration.migration_dir
|
43
43
|
end
|
44
44
|
|
45
45
|
def migration_file_name
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'rails'
|
2
2
|
|
3
3
|
def require_migration(migration_name)
|
4
|
-
path = ActiveRecord::
|
4
|
+
path = MigrationData::ActiveRecord::Migration.migration_dir
|
5
5
|
all_migrations = ActiveRecord::Migrator.migrations(path)
|
6
6
|
|
7
7
|
migration_name += '.rb' unless migration_name.end_with?('.rb')
|
data/lib/tasks/db.rake
CHANGED
@@ -3,7 +3,8 @@ require 'migration_data/squash'
|
|
3
3
|
namespace :db do
|
4
4
|
namespace :migrate do
|
5
5
|
desc 'Squashes all current migrations and dumps current schema to the latest one'
|
6
|
-
task :squash do
|
6
|
+
task :squash => 'db:load_config' do
|
7
|
+
::ActiveRecord::Base.establish_connection
|
7
8
|
MigrationData::Squash.new.call
|
8
9
|
end
|
9
10
|
end
|
data/migration_data.gemspec
CHANGED
@@ -23,6 +23,6 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.require_paths = ["lib"]
|
24
24
|
|
25
25
|
spec.add_development_dependency 'bundler', '~> 1.5'
|
26
|
-
spec.add_development_dependency 'rake'
|
27
|
-
spec.add_development_dependency 'sqlite3'
|
26
|
+
spec.add_development_dependency 'rake'
|
27
|
+
spec.add_development_dependency 'sqlite3'
|
28
28
|
end
|
Binary file
|
data/test/rake_task_test.rb
CHANGED
@@ -4,12 +4,31 @@ require 'migration_data/squash'
|
|
4
4
|
describe 'db:schema:squash' do
|
5
5
|
let(:rake) { Rake::Application.new }
|
6
6
|
|
7
|
-
|
7
|
+
def migrate_existing_migrations
|
8
8
|
CreateTableMigration.new.migrate(:up)
|
9
|
+
end
|
10
|
+
|
11
|
+
def load_rails_db_env
|
12
|
+
load 'active_record/railties/databases.rake'
|
13
|
+
Rake::Task['db:load_config'].invoke
|
14
|
+
end
|
15
|
+
|
16
|
+
def stub_dependent_tasks
|
17
|
+
rake.intern(Rake::Task, 'db:load_config')
|
18
|
+
end
|
19
|
+
|
20
|
+
def prepare_rake_env_for_testing
|
9
21
|
Rake.application = rake
|
10
22
|
Rake.application.rake_require('db', [File.expand_path('../../lib/tasks', __FILE__)])
|
11
23
|
end
|
12
24
|
|
25
|
+
before do
|
26
|
+
migrate_existing_migrations
|
27
|
+
load_rails_db_env
|
28
|
+
stub_dependent_tasks
|
29
|
+
prepare_rake_env_for_testing
|
30
|
+
end
|
31
|
+
|
13
32
|
describe '#invoke' do
|
14
33
|
it 'squashes the schema to the current migration' do
|
15
34
|
latest_migration = Rails.root.join('db', 'migrate', '0_create_schema.rb')
|
@@ -19,7 +38,7 @@ describe 'db:schema:squash' do
|
|
19
38
|
assert_equal <<-MIGRATION, File.read(latest_migration)
|
20
39
|
class CreateSchema < ActiveRecord::Migration
|
21
40
|
def change
|
22
|
-
create_table \"users\", force: #{
|
41
|
+
create_table \"users\", force: #{FORCE_MIGRATION} do |t|
|
23
42
|
t.string \"name\"
|
24
43
|
end
|
25
44
|
end
|
@@ -9,17 +9,17 @@ describe '#require_migration' do
|
|
9
9
|
|
10
10
|
it 'loads existed migation' do
|
11
11
|
Rails.stub(:root, db_path) do
|
12
|
-
ActiveRecord::
|
12
|
+
MigrationData::ActiveRecord::Migration.stub(:migration_dir, db_path) do
|
13
13
|
require_migration 'test_migration'
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
18
|
it 'raises exception on load unexisted migration' do
|
19
|
-
ActiveRecord::
|
19
|
+
MigrationData::ActiveRecord::Migration.stub(:migration_dir, db_path) do
|
20
20
|
assert_raises LoadError, 'cannot load such file -- test_migration2.rb' do
|
21
21
|
require_migration('test_migration2')
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
25
|
-
end
|
25
|
+
end
|
data/test/schema_dumper_test.rb
CHANGED
@@ -10,7 +10,7 @@ describe MigrationData::ActiveRecord::SchemaDumper do
|
|
10
10
|
it 'squashes the schema' do
|
11
11
|
stream = MigrationData::ActiveRecord::SchemaDumper.dump
|
12
12
|
assert_equal <<-SCHEMA, stream.string
|
13
|
-
create_table \"users\", force: #{
|
13
|
+
create_table \"users\", force: #{FORCE_MIGRATION} do |t|
|
14
14
|
t.string \"name\"
|
15
15
|
end
|
16
16
|
|
data/test/squash_test.rb
CHANGED
@@ -17,7 +17,7 @@ describe MigrationData::Squash do
|
|
17
17
|
assert_equal <<-MIGRATION, File.read(db_path.join('100500_create_schema.rb'))
|
18
18
|
class CreateSchema < ActiveRecord::Migration
|
19
19
|
def change
|
20
|
-
create_table \"users\", force: #{
|
20
|
+
create_table \"users\", force: #{FORCE_MIGRATION} do |t|
|
21
21
|
t.string \"name\"
|
22
22
|
end
|
23
23
|
end
|
data/test/support/rails_app.rb
CHANGED
@@ -1,6 +1,11 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
1
3
|
class RailsApp < Rails::Application
|
2
4
|
end
|
3
5
|
|
4
6
|
Rails.application.config.root = 'test/rails_app'
|
5
7
|
|
8
|
+
db_config = YAML.load_file(Rails.application.config.root.join('config', 'database.yml'))
|
9
|
+
::ActiveRecord::Tasks::DatabaseTasks.database_configuration = db_config
|
10
|
+
|
6
11
|
FileUtils.mkdir_p(Rails.application.config.root.join('db', 'migrate'))
|
data/test/test_helper.rb
CHANGED
@@ -6,8 +6,8 @@ Dir[File.join(File.dirname(__FILE__), 'support', '**/*.rb')].each { |f| require
|
|
6
6
|
|
7
7
|
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'tmp/test.sqlite3')
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
FORCE_MIGRATION = if ActiveRecord.version < Gem::Version.new('4.2.0.rc1')
|
10
|
+
'true'
|
11
|
+
else
|
12
|
+
':cascade'
|
13
|
+
end
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: migration_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Koleshko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.5'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.5'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: sqlite3
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
description: |-
|
@@ -64,8 +64,8 @@ executables: []
|
|
64
64
|
extensions: []
|
65
65
|
extra_rdoc_files: []
|
66
66
|
files:
|
67
|
-
-
|
68
|
-
-
|
67
|
+
- .gitignore
|
68
|
+
- .travis.yml
|
69
69
|
- Gemfile
|
70
70
|
- LICENSE.txt
|
71
71
|
- README.md
|
@@ -80,7 +80,9 @@ files:
|
|
80
80
|
- migration_data.gemspec
|
81
81
|
- test/db/20130906111511_test_migration.rb
|
82
82
|
- test/migration_test.rb
|
83
|
+
- test/rails_app/config/database.yml
|
83
84
|
- test/rails_app/db/migrate/0_create_schema.rb
|
85
|
+
- test/rails_app/db/test.sqlite3
|
84
86
|
- test/rake_task_test.rb
|
85
87
|
- test/require_migration_test.rb
|
86
88
|
- test/schema_dumper_test.rb
|
@@ -98,24 +100,26 @@ require_paths:
|
|
98
100
|
- lib
|
99
101
|
required_ruby_version: !ruby/object:Gem::Requirement
|
100
102
|
requirements:
|
101
|
-
- -
|
103
|
+
- - '>='
|
102
104
|
- !ruby/object:Gem::Version
|
103
105
|
version: '0'
|
104
106
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
107
|
requirements:
|
106
|
-
- -
|
108
|
+
- - '>='
|
107
109
|
- !ruby/object:Gem::Version
|
108
110
|
version: '0'
|
109
111
|
requirements: []
|
110
112
|
rubyforge_project:
|
111
|
-
rubygems_version: 2.
|
113
|
+
rubygems_version: 2.0.14.1
|
112
114
|
signing_key:
|
113
115
|
specification_version: 4
|
114
116
|
summary: Provides possibility to write any code in migrations safely without regression.
|
115
117
|
test_files:
|
116
118
|
- test/db/20130906111511_test_migration.rb
|
117
119
|
- test/migration_test.rb
|
120
|
+
- test/rails_app/config/database.yml
|
118
121
|
- test/rails_app/db/migrate/0_create_schema.rb
|
122
|
+
- test/rails_app/db/test.sqlite3
|
119
123
|
- test/rake_task_test.rb
|
120
124
|
- test/require_migration_test.rb
|
121
125
|
- test/schema_dumper_test.rb
|
@@ -123,4 +127,3 @@ test_files:
|
|
123
127
|
- test/support/create_table_migration.rb
|
124
128
|
- test/support/rails_app.rb
|
125
129
|
- test/test_helper.rb
|
126
|
-
has_rdoc:
|