also_migrate 0.3.4 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +14 -7
- data/config/gemsets.yml +1 -1
- data/config/gemspec.yml +1 -1
- data/lib/also_migrate.rb +1 -3
- data/lib/also_migrate/migration.rb +20 -28
- data/lib/also_migrate/migrator.rb +47 -48
- data/rails/init.rb +1 -5
- data/spec/Rakefile +2 -2
- data/spec/also_migrate_spec.rb +41 -17
- data/spec/spec_helper.rb +1 -9
- metadata +3 -8
- data/lib/also_migrate/base.rb +0 -35
- data/spec/fixtures/article.rb +0 -2
- data/spec/fixtures/comment.rb +0 -2
data/README.md
CHANGED
@@ -10,13 +10,14 @@ Requirements
|
|
10
10
|
gem install also_migrate
|
11
11
|
</pre>
|
12
12
|
|
13
|
-
|
14
|
-
|
13
|
+
Configure
|
14
|
+
---------
|
15
15
|
|
16
16
|
<pre>
|
17
|
-
|
18
|
-
|
19
|
-
:
|
17
|
+
AlsoMigrate.configuration = [
|
18
|
+
{
|
19
|
+
:source => 'articles',
|
20
|
+
:destination => 'article_archives',
|
20
21
|
:add => [
|
21
22
|
# Parameters to ActiveRecord::ConnectionAdapters::SchemaStatements#add_column
|
22
23
|
[ 'deleted_at', :datetime, {} ]
|
@@ -24,12 +25,18 @@ class Article < ActiveRecord::Base
|
|
24
25
|
:subtract => 'restored_at',
|
25
26
|
:ignore => 'deleted_at',
|
26
27
|
:indexes => 'id'
|
27
|
-
|
28
|
-
|
28
|
+
},
|
29
|
+
{
|
30
|
+
:source => 'users',
|
31
|
+
:destination => [ 'banned_users', 'deleted_users' ]
|
32
|
+
}
|
33
|
+
]
|
29
34
|
</pre>
|
30
35
|
|
31
36
|
Options:
|
32
37
|
|
38
|
+
* <code>source</code> Database schema source table
|
39
|
+
* <code>destination</code> Database schema destination table (can also be an array of tables)
|
33
40
|
* <code>add</code> Create columns that the original table doesn't have (defaults to none)
|
34
41
|
* <code>subtract</code> Exclude columns from the original table (defaults to none)
|
35
42
|
* <code>ignore</code> Ignore migrations that apply to certain columns (defaults to none)
|
data/config/gemsets.yml
CHANGED
data/config/gemspec.yml
CHANGED
data/lib/also_migrate.rb
CHANGED
@@ -2,16 +2,14 @@ require File.dirname(__FILE__) + '/also_migrate/gems'
|
|
2
2
|
|
3
3
|
$:.unshift File.dirname(__FILE__)
|
4
4
|
|
5
|
-
require 'also_migrate/base'
|
6
5
|
require 'also_migrate/migration'
|
7
6
|
require 'also_migrate/migrator'
|
8
7
|
|
9
8
|
module AlsoMigrate
|
10
9
|
class <<self
|
11
|
-
attr_accessor :
|
10
|
+
attr_accessor :configuration
|
12
11
|
end
|
13
12
|
end
|
14
13
|
|
15
|
-
ActiveRecord::Base.send(:include, AlsoMigrate::Base)
|
16
14
|
ActiveRecord::Migrator.send(:include, AlsoMigrate::Migrator)
|
17
15
|
ActiveRecord::Migration.send(:include, AlsoMigrate::Migration)
|
@@ -31,35 +31,27 @@ module AlsoMigrate
|
|
31
31
|
table_name = ActiveRecord::Migrator.proper_table_name(args[0])
|
32
32
|
|
33
33
|
# Find models
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
next unless config[:table_name] == table_name
|
42
|
-
|
43
|
-
# Don't change ignored columns
|
44
|
-
options[:ignore].each do |column|
|
45
|
-
next if args.include?(column) || args.include?(column.intern)
|
46
|
-
end
|
34
|
+
(::AlsoMigrate.configuration || []).each do |config|
|
35
|
+
next unless config[:source].to_s == table_name
|
36
|
+
|
37
|
+
# Don't change ignored columns
|
38
|
+
[ config[:ignore] ].flatten.compact.each do |column|
|
39
|
+
next if args.include?(column) || args.include?(column.intern)
|
40
|
+
end
|
47
41
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
end
|
62
|
-
end
|
42
|
+
# Run migration
|
43
|
+
if method == :create_table
|
44
|
+
ActiveRecord::Migrator::AlsoMigrate.create_tables(config)
|
45
|
+
elsif method == :add_index && !config[:indexes].nil?
|
46
|
+
next
|
47
|
+
else
|
48
|
+
[ config[:destination] ].flatten.compact.each do |table|
|
49
|
+
if connection.table_exists?(table)
|
50
|
+
args[0] = table
|
51
|
+
begin
|
52
|
+
connection.send(method, *args, &block)
|
53
|
+
rescue Exception => e
|
54
|
+
puts "(also_migrate warning) #{e.message}"
|
63
55
|
end
|
64
56
|
end
|
65
57
|
end
|
@@ -14,12 +14,8 @@ module AlsoMigrate
|
|
14
14
|
module InstanceMethods
|
15
15
|
|
16
16
|
def migrate_with_also_migrate
|
17
|
-
|
18
|
-
|
19
|
-
if klass.respond_to?(:also_migrate_config)
|
20
|
-
AlsoMigrate.create_tables(klass)
|
21
|
-
end
|
22
|
-
end
|
17
|
+
(::AlsoMigrate.configuration || []).each do |config|
|
18
|
+
AlsoMigrate.create_tables(config)
|
23
19
|
end
|
24
20
|
rescue Exception => e
|
25
21
|
puts "AlsoMigrate error: #{e.message}"
|
@@ -35,58 +31,61 @@ module AlsoMigrate
|
|
35
31
|
ActiveRecord::Base.connection
|
36
32
|
end
|
37
33
|
|
38
|
-
def create_tables(
|
39
|
-
config
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
34
|
+
def create_tables(config)
|
35
|
+
[ config[:destination] ].flatten.compact.each do |new_table|
|
36
|
+
if !connection.table_exists?(new_table) && connection.table_exists?(config[:source])
|
37
|
+
columns = connection.columns(config[:source]).collect(&:name)
|
38
|
+
columns -= [ config[:subtract] ].flatten.compact.collect(&:to_s)
|
39
|
+
columns.collect! { |col| connection.quote_column_name(col) }
|
40
|
+
if config[:indexes]
|
41
|
+
engine =
|
42
|
+
if connection.class.to_s.include?('Mysql')
|
43
|
+
'ENGINE=' + connection.select_one(<<-SQL)['Engine']
|
44
|
+
SHOW TABLE STATUS
|
45
|
+
WHERE Name = '#{config[:source]}'
|
46
|
+
SQL
|
47
|
+
end
|
48
|
+
connection.execute(<<-SQL)
|
49
|
+
CREATE TABLE #{new_table} #{engine}
|
50
|
+
AS SELECT #{columns.join(',')}
|
51
|
+
FROM #{config[:source]}
|
52
|
+
WHERE false;
|
53
|
+
SQL
|
54
|
+
[ config[:indexes] ].flatten.compact.each do |column|
|
55
|
+
connection.add_index(new_table, column)
|
56
|
+
end
|
57
|
+
else
|
58
|
+
if connection.class.to_s.include?('SQLite')
|
59
|
+
col_string = connection.columns(old_table).collect {|c|
|
60
|
+
"#{c.name} #{c.sql_type}"
|
61
|
+
}.join(', ')
|
58
62
|
connection.execute(<<-SQL)
|
59
|
-
CREATE TABLE #{new_table}
|
60
|
-
|
61
|
-
FROM #{old_table}
|
62
|
-
WHERE false;
|
63
|
+
CREATE TABLE #{new_table}
|
64
|
+
(#{col_string})
|
63
65
|
SQL
|
64
|
-
indexes.each do |column|
|
65
|
-
connection.add_index(new_table, column)
|
66
|
-
end
|
67
66
|
else
|
68
67
|
connection.execute(<<-SQL)
|
69
68
|
CREATE TABLE #{new_table}
|
70
|
-
LIKE #{
|
69
|
+
LIKE #{config[:source]};
|
71
70
|
SQL
|
72
71
|
end
|
73
72
|
end
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
73
|
+
end
|
74
|
+
if connection.table_exists?(new_table)
|
75
|
+
if config[:add] || config[:subtract]
|
76
|
+
columns = connection.columns(new_table).collect(&:name)
|
77
|
+
end
|
78
|
+
if config[:add]
|
79
|
+
config[:add].each do |column|
|
80
|
+
unless columns.include?(column[0])
|
81
|
+
connection.add_column(*([ new_table ] + column))
|
83
82
|
end
|
84
83
|
end
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
84
|
+
end
|
85
|
+
if config[:subtract]
|
86
|
+
[ config[:subtract] ].flatten.compact.each do |column|
|
87
|
+
if columns.include?(column)
|
88
|
+
connection.remove_column(new_table, column)
|
90
89
|
end
|
91
90
|
end
|
92
91
|
end
|
data/rails/init.rb
CHANGED
data/spec/Rakefile
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/../lib/also_migrate/gems')
|
2
2
|
|
3
|
-
AlsoMigrate::Gems.
|
3
|
+
AlsoMigrate::Gems.activate(:active_wrapper)
|
4
4
|
|
5
5
|
require 'active_wrapper/tasks'
|
6
6
|
|
7
7
|
#begin
|
8
8
|
ActiveWrapper::Tasks.new(
|
9
9
|
:base => File.dirname(__FILE__),
|
10
|
-
:env => 'test'
|
10
|
+
:env => ENV['ENV'] || 'test'
|
11
11
|
)
|
12
12
|
# rescue Exception
|
13
13
|
# end
|
data/spec/also_migrate_spec.rb
CHANGED
@@ -10,15 +10,16 @@ describe AlsoMigrate do
|
|
10
10
|
reset_fixture
|
11
11
|
|
12
12
|
if description == "table doesn't exist yet"
|
13
|
-
|
14
|
-
:
|
13
|
+
AlsoMigrate.configuration << {
|
14
|
+
:source => :articles,
|
15
|
+
:destination => :article_archives,
|
15
16
|
:add => [
|
16
17
|
[ 'deleted_at', :datetime ]
|
17
18
|
],
|
18
19
|
:subtract => 'restored_at',
|
19
20
|
:ignore => 'body',
|
20
21
|
:indexes => 'id'
|
21
|
-
|
22
|
+
}
|
22
23
|
end
|
23
24
|
|
24
25
|
$db.migrate(1)
|
@@ -26,15 +27,16 @@ describe AlsoMigrate do
|
|
26
27
|
$db.migrate(1)
|
27
28
|
|
28
29
|
if description == "table already exists"
|
29
|
-
|
30
|
-
:
|
30
|
+
AlsoMigrate.configuration << {
|
31
|
+
:source => :articles,
|
32
|
+
:destination => :article_archives,
|
31
33
|
:add => [
|
32
34
|
[ 'deleted_at', :datetime ]
|
33
35
|
],
|
34
36
|
:subtract => %w(restored_at),
|
35
37
|
:ignore => %w(body),
|
36
38
|
:indexes => %w(id)
|
37
|
-
|
39
|
+
}
|
38
40
|
$db.migrate(1)
|
39
41
|
end
|
40
42
|
end
|
@@ -78,14 +80,20 @@ describe AlsoMigrate do
|
|
78
80
|
reset_fixture
|
79
81
|
|
80
82
|
if description == "table doesn't exist yet"
|
81
|
-
|
83
|
+
AlsoMigrate.configuration << {
|
84
|
+
:source => :articles,
|
85
|
+
:destination => :article_archives
|
86
|
+
}
|
82
87
|
end
|
83
88
|
|
84
89
|
$db.migrate(0)
|
85
90
|
$db.migrate(1)
|
86
91
|
|
87
92
|
if description == "table already exists"
|
88
|
-
|
93
|
+
AlsoMigrate.configuration << {
|
94
|
+
:source => :articles,
|
95
|
+
:destination => :article_archives
|
96
|
+
}
|
89
97
|
$db.migrate(1)
|
90
98
|
end
|
91
99
|
end
|
@@ -102,8 +110,14 @@ describe AlsoMigrate do
|
|
102
110
|
reset_fixture
|
103
111
|
|
104
112
|
if description == "table doesn't exist yet"
|
105
|
-
|
106
|
-
|
113
|
+
AlsoMigrate.configuration << {
|
114
|
+
:source => :articles,
|
115
|
+
:destination => :article_archives
|
116
|
+
}
|
117
|
+
AlsoMigrate.configuration << {
|
118
|
+
:source => :comments,
|
119
|
+
:destination => :comment_archives
|
120
|
+
}
|
107
121
|
end
|
108
122
|
|
109
123
|
$db.migrate(0)
|
@@ -112,8 +126,14 @@ describe AlsoMigrate do
|
|
112
126
|
$db.migrate(3)
|
113
127
|
|
114
128
|
if description == "table already exists"
|
115
|
-
|
116
|
-
|
129
|
+
AlsoMigrate.configuration << {
|
130
|
+
:source => :articles,
|
131
|
+
:destination => :article_archives
|
132
|
+
}
|
133
|
+
AlsoMigrate.configuration << {
|
134
|
+
:source => :comments,
|
135
|
+
:destination => :comment_archives
|
136
|
+
}
|
117
137
|
$db.migrate(3)
|
118
138
|
end
|
119
139
|
end
|
@@ -132,19 +152,23 @@ describe AlsoMigrate do
|
|
132
152
|
before(:each) do
|
133
153
|
reset_fixture
|
134
154
|
|
135
|
-
|
155
|
+
AlsoMigrate.configuration << {
|
156
|
+
:source => :articles,
|
157
|
+
:destination => :article_archives
|
158
|
+
}
|
136
159
|
|
137
160
|
$db.migrate(0)
|
138
161
|
$db.migrate(1)
|
139
162
|
|
140
|
-
|
141
|
-
|
142
|
-
:
|
163
|
+
AlsoMigrate.configuration = []
|
164
|
+
AlsoMigrate.configuration << {
|
165
|
+
:source => :articles,
|
166
|
+
:destination => :article_archives,
|
143
167
|
:add => [
|
144
168
|
[ 'deleted_at', :datetime ]
|
145
169
|
],
|
146
170
|
:subtract => 'restored_at'
|
147
|
-
|
171
|
+
}
|
148
172
|
end
|
149
173
|
|
150
174
|
it "should add and remove fields" do
|
data/spec/spec_helper.rb
CHANGED
@@ -8,8 +8,6 @@ AlsoMigrate::Gems.activate :active_wrapper, :rspec
|
|
8
8
|
require 'active_wrapper'
|
9
9
|
|
10
10
|
require "#{$root}/lib/also_migrate"
|
11
|
-
require "#{$root}/spec/fixtures/article"
|
12
|
-
require "#{$root}/spec/fixtures/comment"
|
13
11
|
require 'pp'
|
14
12
|
|
15
13
|
Spec::Runner.configure do |config|
|
@@ -84,13 +82,7 @@ def migrate_with_state(version)
|
|
84
82
|
end
|
85
83
|
|
86
84
|
def reset_fixture
|
87
|
-
|
88
|
-
Article.also_migrate_config = nil
|
89
|
-
end
|
90
|
-
|
91
|
-
if Comment.respond_to?(:also_migrate_config)
|
92
|
-
Comment.also_migrate_config = nil
|
93
|
-
end
|
85
|
+
AlsoMigrate.configuration = []
|
94
86
|
|
95
87
|
if connection.table_exists?('article_archives')
|
96
88
|
connection.execute('DROP TABLE article_archives')
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 5
|
9
|
+
version: 0.3.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Winton Welsh
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-01-
|
17
|
+
date: 2011-01-25 00:00:00 -08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -64,7 +64,6 @@ files:
|
|
64
64
|
- config/gemspec.yml
|
65
65
|
- init.rb
|
66
66
|
- lib/also_migrate.rb
|
67
|
-
- lib/also_migrate/base.rb
|
68
67
|
- lib/also_migrate/gems.rb
|
69
68
|
- lib/also_migrate/migration.rb
|
70
69
|
- lib/also_migrate/migrator.rb
|
@@ -76,8 +75,6 @@ files:
|
|
76
75
|
- spec/db/migrate/001_create_articles.rb
|
77
76
|
- spec/db/migrate/002_add_permalink.rb
|
78
77
|
- spec/db/migrate/003_create_comments.rb
|
79
|
-
- spec/fixtures/article.rb
|
80
|
-
- spec/fixtures/comment.rb
|
81
78
|
- spec/fixtures/gemsets.yml
|
82
79
|
- spec/fixtures/gemspec.yml
|
83
80
|
- spec/spec_helper.rb
|
@@ -121,8 +118,6 @@ test_files:
|
|
121
118
|
- spec/db/migrate/001_create_articles.rb
|
122
119
|
- spec/db/migrate/002_add_permalink.rb
|
123
120
|
- spec/db/migrate/003_create_comments.rb
|
124
|
-
- spec/fixtures/article.rb
|
125
|
-
- spec/fixtures/comment.rb
|
126
121
|
- spec/fixtures/gemsets.yml
|
127
122
|
- spec/fixtures/gemspec.yml
|
128
123
|
- spec/spec_helper.rb
|
data/lib/also_migrate/base.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
module AlsoMigrate
|
2
|
-
module Base
|
3
|
-
|
4
|
-
def self.included(base)
|
5
|
-
unless base.respond_to?(:also_migrate)
|
6
|
-
base.extend ClassMethods
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
module ClassMethods
|
11
|
-
|
12
|
-
def also_migrate(*args)
|
13
|
-
options = args.extract_options!
|
14
|
-
@also_migrate_config ||= []
|
15
|
-
@also_migrate_config << {
|
16
|
-
:table_name => self.table_name,
|
17
|
-
:tables => args.collect(&:to_s),
|
18
|
-
:options => {
|
19
|
-
:add => options[:add] ? options[:add] : [],
|
20
|
-
:subtract => [ options[:subtract] ].flatten.compact,
|
21
|
-
:ignore => [ options[:ignore] ].flatten.compact,
|
22
|
-
:indexes => options[:indexes] ? [ options[:indexes] ].flatten : nil
|
23
|
-
}
|
24
|
-
}
|
25
|
-
self.class_eval do
|
26
|
-
class <<self
|
27
|
-
attr_accessor :also_migrate_config
|
28
|
-
end
|
29
|
-
end
|
30
|
-
::AlsoMigrate.classes ||= []
|
31
|
-
::AlsoMigrate.classes << self
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
data/spec/fixtures/article.rb
DELETED
data/spec/fixtures/comment.rb
DELETED