nondestructive_migrations 1.2 → 1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/generators/templates/create_data_migrations.rb +1 -1
- data/lib/nondestructive_migrator.rb +119 -0
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f389c93c38f25401d05e11b08921ace9e4025d9f
|
4
|
+
data.tar.gz: d4246a47917c08d7cfec109f713ecf905a8bb9fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9ebd1d861b048f79473318ad24ebce61574441006736bb5e6614b77a40cf4f64c5c66e6e59ac578be9dc30ff0e04d526010288464183658b4b0827ea1cb7f2c
|
7
|
+
data.tar.gz: 60768f7a36f9f711e4190fd965af90918f4d0b1d578ec5207df1cb289baa529b171fe40086320e10fa8e83024600c0fd46939fd97bb05839b9a2f23a6f4cde96
|
@@ -5,6 +5,125 @@ class NondestructiveMigrator < ActiveRecord::Migrator
|
|
5
5
|
# This class related to data migration.
|
6
6
|
# Used in rake tasks (rake data:[migrate|rollback|up|down])
|
7
7
|
|
8
|
+
if defined?(ActiveRecord::MigrationContext)
|
9
|
+
class SchemaMigration < ActiveRecord::SchemaMigration
|
10
|
+
def self.table_name
|
11
|
+
NondestructiveMigrator.schema_migrations_table_name
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class MigrationContext < ActiveRecord::MigrationContext
|
16
|
+
def initialize(migrations_paths)
|
17
|
+
super(migrations_paths)
|
18
|
+
@schema_migration = NondestructiveMigrator::SchemaMigration
|
19
|
+
end
|
20
|
+
|
21
|
+
def new_migrator(*args)
|
22
|
+
result = NondestructiveMigrator.new(*args)
|
23
|
+
result.migration_context = self
|
24
|
+
result
|
25
|
+
end
|
26
|
+
|
27
|
+
# these methods are copied from ActiveRecord::Migrator
|
28
|
+
# replaced:
|
29
|
+
# 1.) ActiveRecord::SchemaMigration with @schema_migration
|
30
|
+
# 2.) ActiveRecord::Migrator.new with new_migrator
|
31
|
+
|
32
|
+
def get_all_versions
|
33
|
+
if @schema_migration.table_exists?
|
34
|
+
@schema_migration.all_versions.map(&:to_i)
|
35
|
+
else
|
36
|
+
[]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def migrations_status
|
41
|
+
db_list = @schema_migration.normalized_versions
|
42
|
+
|
43
|
+
file_list = migration_files.map do |file|
|
44
|
+
version, name, scope = parse_migration_filename(file)
|
45
|
+
raise IllegalMigrationNameError.new(file) unless version
|
46
|
+
version = @schema_migration.normalize_migration_number(version)
|
47
|
+
status = db_list.delete(version) ? "up" : "down"
|
48
|
+
[status, version, (name + scope).humanize]
|
49
|
+
end.compact
|
50
|
+
|
51
|
+
db_list.map! do |version|
|
52
|
+
["up", version, "********** NO FILE **********"]
|
53
|
+
end
|
54
|
+
|
55
|
+
(db_list + file_list).sort_by { |_, version, _| version }
|
56
|
+
end
|
57
|
+
|
58
|
+
def move(direction, steps)
|
59
|
+
migrator = new_migrator(direction, migrations)
|
60
|
+
|
61
|
+
if current_version != 0 && !migrator.current_migration
|
62
|
+
raise UnknownMigrationVersionError.new(current_version)
|
63
|
+
end
|
64
|
+
|
65
|
+
start_index =
|
66
|
+
if current_version == 0
|
67
|
+
0
|
68
|
+
else
|
69
|
+
migrator.migrations.index(migrator.current_migration)
|
70
|
+
end
|
71
|
+
|
72
|
+
finish = migrator.migrations[start_index + steps]
|
73
|
+
version = finish ? finish.version : 0
|
74
|
+
send(direction, version)
|
75
|
+
end
|
76
|
+
|
77
|
+
def up(target_version = nil)
|
78
|
+
selected_migrations = if block_given?
|
79
|
+
migrations.select { |m| yield m }
|
80
|
+
else
|
81
|
+
migrations
|
82
|
+
end
|
83
|
+
|
84
|
+
new_migrator(:up, selected_migrations, target_version).migrate
|
85
|
+
end
|
86
|
+
|
87
|
+
def down(target_version = nil)
|
88
|
+
selected_migrations = if block_given?
|
89
|
+
migrations.select { |m| yield m }
|
90
|
+
else
|
91
|
+
migrations
|
92
|
+
end
|
93
|
+
|
94
|
+
new_migrator(:down, selected_migrations, target_version).migrate
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
class << self
|
99
|
+
def context(path)
|
100
|
+
NondestructiveMigrator::MigrationContext.new(path)
|
101
|
+
end
|
102
|
+
|
103
|
+
def new_migrator(path, *args)
|
104
|
+
result = self.new(*args)
|
105
|
+
result.migration_context=context(path)
|
106
|
+
result
|
107
|
+
end
|
108
|
+
|
109
|
+
def migrate(path)
|
110
|
+
context(path).migrate()
|
111
|
+
end
|
112
|
+
|
113
|
+
def run(direction, path, target_version)
|
114
|
+
new_migrator(path, direction, context(path).migrations, target_version).run
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def migration_context=(context)
|
119
|
+
@migration_context = context
|
120
|
+
end
|
121
|
+
|
122
|
+
def load_migrated
|
123
|
+
@migrated_versions = Set.new(@migration_context.get_all_versions)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
8
127
|
def record_version_state_after_migrating(version)
|
9
128
|
if down?
|
10
129
|
migrated.delete(version)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nondestructive_migrations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '1.
|
4
|
+
version: '1.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Fleetwood-Boldt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '5.
|
19
|
+
version: '5.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '5.
|
26
|
+
version: '5.1'
|
27
27
|
description: Separate schema-only migrations from nondestrucitve (data) migrations
|
28
28
|
in your Rails app
|
29
29
|
email: jason.fb@datatravels.com
|
@@ -59,8 +59,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
59
|
version: '0'
|
60
60
|
requirements: []
|
61
61
|
rubyforge_project:
|
62
|
-
rubygems_version: 2.
|
62
|
+
rubygems_version: 2.4.8
|
63
63
|
signing_key:
|
64
64
|
specification_version: 4
|
65
65
|
summary: Nondestructive (data-only) migrations for your Rails app
|
66
66
|
test_files: []
|
67
|
+
has_rdoc:
|