migration_data 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +9 -2
- data/lib/migration_data/active_record/schema_dumper.rb +16 -0
- data/lib/migration_data/squash.rb +49 -0
- data/lib/migration_data/version.rb +1 -1
- data/lib/migration_data.rb +3 -0
- data/lib/tasks/db.rake +10 -0
- data/test/rails_app/db/migrate/0_create_schema.rb +7 -0
- data/test/rake_task_test.rb +30 -0
- data/test/schema_dumper_test.rb +20 -0
- data/test/squash_test.rb +39 -0
- data/test/support/create_table_migration.rb +7 -0
- data/test/support/rails_app.rb +6 -0
- data/test/test_helper.rb +4 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d05f84f59e7360b1c3d78be9b072c48da83c629d
|
4
|
+
data.tar.gz: 64409deee872b191f27407a69e62a845c1ba5de1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 266a1f45b7a12a831fa1b1064b2aa2ae20eb6f50a3055f6995fd1d26822c8c29671bdd3eaa87f1c562677c6fadb800b21230ee48de4cf37414fc446f9521040b
|
7
|
+
data.tar.gz: 565ff180725a97a90a49519bba1f671c54b5f0a284c10db2af7078266dcf18969534f133726e9236b6468f8948ff537c762d6283071890b0ebebe562b44de9db
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -68,6 +68,10 @@ describe CreateUsers do
|
|
68
68
|
end
|
69
69
|
|
70
70
|
describe '#rollback' do
|
71
|
+
before do
|
72
|
+
described_class.new.data
|
73
|
+
end
|
74
|
+
|
71
75
|
it 'works' do
|
72
76
|
expect { described_class.new.rollback }.to_not raise_exception
|
73
77
|
end
|
@@ -75,8 +79,11 @@ describe CreateUsers do
|
|
75
79
|
end
|
76
80
|
```
|
77
81
|
|
78
|
-
The helper to load migrations `require_migration` is defined in the `migration_data/testing`. So you should to require it
|
79
|
-
|
82
|
+
The helper to load migrations `require_migration` is defined in the `migration_data/testing`. So you should to require it to have access to this convinient require extension.
|
83
|
+
|
84
|
+
## Clean old migration
|
85
|
+
|
86
|
+
Once you are tired maintaining your old migrations you can clean up the old migrations. Use `rake db:migrate:squash` for this. The task will remove all old your migrations and will generate one migration with current database schema. You don't have to run migrations after this because the generated migration will have the latest database version.
|
80
87
|
|
81
88
|
## Contributing
|
82
89
|
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module MigrationData
|
2
|
+
module ActiveRecord
|
3
|
+
class SchemaDumper < ::ActiveRecord::SchemaDumper
|
4
|
+
def self.dump(connection=::ActiveRecord::Base.connection, stream=StringIO.new, config = ::ActiveRecord::Base)
|
5
|
+
new(connection, generate_options(config)).dump(stream)
|
6
|
+
stream
|
7
|
+
end
|
8
|
+
|
9
|
+
def dump(stream)
|
10
|
+
extensions(stream)
|
11
|
+
tables(stream)
|
12
|
+
stream
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'migration_data/active_record/schema_dumper'
|
2
|
+
|
3
|
+
module MigrationData
|
4
|
+
class Squash
|
5
|
+
def call
|
6
|
+
remove_all_old_migrations
|
7
|
+
squash_schema
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def squash_schema
|
13
|
+
File.open(migration_file_name, 'w') do |f|
|
14
|
+
content =
|
15
|
+
<<-MIGRATION.gsub(/^ {10}/, '')
|
16
|
+
class CreateSchema < ActiveRecord::Migration
|
17
|
+
def change
|
18
|
+
#{MigrationData::ActiveRecord::SchemaDumper.dump.string.strip.gsub(/^/, ' ')}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
MIGRATION
|
22
|
+
|
23
|
+
f.write(content)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def remove_all_old_migrations
|
28
|
+
migration_dirs.each do |dir|
|
29
|
+
FileUtils.rm_rf(Dir.glob(File.join(dir, '**/*.rb')))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def current_version
|
34
|
+
::ActiveRecord::Migrator.current_version
|
35
|
+
end
|
36
|
+
|
37
|
+
def migration_dirs
|
38
|
+
::ActiveRecord::Tasks::DatabaseTasks.migrations_paths
|
39
|
+
end
|
40
|
+
|
41
|
+
def migration_dir
|
42
|
+
migration_dirs.first
|
43
|
+
end
|
44
|
+
|
45
|
+
def migration_file_name
|
46
|
+
File.join(migration_dir, "#{current_version}_create_schema.rb")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/migration_data.rb
CHANGED
@@ -2,4 +2,7 @@ require 'active_record'
|
|
2
2
|
require 'migration_data/version'
|
3
3
|
require 'migration_data/active_record/migration'
|
4
4
|
|
5
|
+
require 'rake'
|
6
|
+
import File.join(File.dirname(__FILE__), 'tasks', 'db.rake')
|
7
|
+
|
5
8
|
ActiveRecord::Migration.send(:include, MigrationData::ActiveRecord::Migration)
|
data/lib/tasks/db.rake
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'migration_data/squash'
|
3
|
+
|
4
|
+
describe 'db:schema:squash' do
|
5
|
+
let(:rake) { Rake::Application.new }
|
6
|
+
|
7
|
+
before do
|
8
|
+
CreateTableMigration.new.migrate(:up)
|
9
|
+
Rake.application = rake
|
10
|
+
Rake.application.rake_require('db', [File.expand_path('../../lib/tasks', __FILE__)])
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#invoke' do
|
14
|
+
it 'squashes the schema to the current migration' do
|
15
|
+
latest_migration = Rails.root.join('db', 'migrate', '0_create_schema.rb')
|
16
|
+
FileUtils.rm(latest_migration) if File.exist?(latest_migration)
|
17
|
+
|
18
|
+
Rake::Task['db:migrate:squash'].invoke
|
19
|
+
assert_equal <<-MIGRATION, File.read(latest_migration)
|
20
|
+
class CreateSchema < ActiveRecord::Migration
|
21
|
+
def change
|
22
|
+
create_table \"users\", force: :cascade do |t|
|
23
|
+
t.string \"name\"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
MIGRATION
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'migration_data/active_record/schema_dumper'
|
3
|
+
|
4
|
+
describe MigrationData::ActiveRecord::SchemaDumper do
|
5
|
+
before do
|
6
|
+
CreateTableMigration.new.migrate(:up)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '#dump' do
|
10
|
+
it 'squashes the schema' do
|
11
|
+
stream = MigrationData::ActiveRecord::SchemaDumper.dump
|
12
|
+
assert_equal <<-SCHEMA, stream.string
|
13
|
+
create_table \"users\", force: :cascade do |t|
|
14
|
+
t.string \"name\"
|
15
|
+
end
|
16
|
+
|
17
|
+
SCHEMA
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/test/squash_test.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'migration_data/squash'
|
3
|
+
|
4
|
+
describe MigrationData::Squash do
|
5
|
+
let(:squash) { MigrationData::Squash.new }
|
6
|
+
let(:db_path) { Rails.root.join('db', 'migrate') }
|
7
|
+
|
8
|
+
before do
|
9
|
+
CreateTableMigration.new.migrate(:up)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#call' do
|
13
|
+
it 'squashes the schema to the current migration' do
|
14
|
+
squash.stub(:current_version, '100500') do
|
15
|
+
squash.call
|
16
|
+
end
|
17
|
+
assert_equal <<-MIGRATION, File.read(db_path.join('100500_create_schema.rb'))
|
18
|
+
class CreateSchema < ActiveRecord::Migration
|
19
|
+
def change
|
20
|
+
create_table \"users\", force: :cascade do |t|
|
21
|
+
t.string \"name\"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
MIGRATION
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'removes all the old migrations' do
|
29
|
+
old_migration = db_path.join('100500_old_migration.rb')
|
30
|
+
File.write(old_migration, '')
|
31
|
+
|
32
|
+
assert_equal true, File.exist?(old_migration)
|
33
|
+
|
34
|
+
squash.call
|
35
|
+
|
36
|
+
assert_equal false, File.exist?(old_migration)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
require 'minitest/autorun'
|
2
2
|
require 'migration_data'
|
3
|
+
require 'rails'
|
3
4
|
|
4
|
-
|
5
|
+
Dir[File.join(File.dirname(__FILE__), 'support', '**/*.rb')].each { |f| require f }
|
6
|
+
|
7
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'tmp/test.sqlite3')
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: migration_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.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: 2015-
|
11
|
+
date: 2015-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,12 +86,21 @@ files:
|
|
86
86
|
- Rakefile
|
87
87
|
- lib/migration_data.rb
|
88
88
|
- lib/migration_data/active_record/migration.rb
|
89
|
+
- lib/migration_data/active_record/schema_dumper.rb
|
90
|
+
- lib/migration_data/squash.rb
|
89
91
|
- lib/migration_data/testing.rb
|
90
92
|
- lib/migration_data/version.rb
|
93
|
+
- lib/tasks/db.rake
|
91
94
|
- migration_data.gemspec
|
92
95
|
- test/db/20130906111511_test_migration.rb
|
93
96
|
- test/migration_test.rb
|
97
|
+
- test/rails_app/db/migrate/0_create_schema.rb
|
98
|
+
- test/rake_task_test.rb
|
94
99
|
- test/require_migration_test.rb
|
100
|
+
- test/schema_dumper_test.rb
|
101
|
+
- test/squash_test.rb
|
102
|
+
- test/support/create_table_migration.rb
|
103
|
+
- test/support/rails_app.rb
|
95
104
|
- test/test_helper.rb
|
96
105
|
homepage: ''
|
97
106
|
licenses:
|
@@ -120,5 +129,11 @@ summary: Provides possibility to write any code in migrations safely without reg
|
|
120
129
|
test_files:
|
121
130
|
- test/db/20130906111511_test_migration.rb
|
122
131
|
- test/migration_test.rb
|
132
|
+
- test/rails_app/db/migrate/0_create_schema.rb
|
133
|
+
- test/rake_task_test.rb
|
123
134
|
- test/require_migration_test.rb
|
135
|
+
- test/schema_dumper_test.rb
|
136
|
+
- test/squash_test.rb
|
137
|
+
- test/support/create_table_migration.rb
|
138
|
+
- test/support/rails_app.rb
|
124
139
|
- test/test_helper.rb
|