also_migrate 0.2.0 → 0.2.1
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.
- data/README.md +13 -2
- data/lib/also_migrate/base.rb +2 -0
- data/lib/also_migrate/migrator.rb +27 -8
- data/lib/also_migrate/version.rb +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -15,14 +15,25 @@ Define the model
|
|
15
15
|
|
16
16
|
<pre>
|
17
17
|
class Article < ActiveRecord::Base
|
18
|
-
also_migrate
|
18
|
+
also_migrate(
|
19
|
+
:article_archives,
|
20
|
+
:add => [
|
21
|
+
# Parameters to ActiveRecord::ConnectionAdapters::SchemaStatements#add_column
|
22
|
+
[ 'deleted_at', :datetime, {} ]
|
23
|
+
],
|
24
|
+
:subtract => 'restored_at',
|
25
|
+
:ignore => 'deleted_at',
|
26
|
+
:indexes => 'id'
|
27
|
+
)
|
19
28
|
end
|
20
29
|
</pre>
|
21
30
|
|
22
31
|
Options:
|
23
32
|
|
33
|
+
* <code>add</code> Create columns that the original table doesn't have (defaults to none)
|
34
|
+
* <code>subtract</code> Exclude columns from the original table (defaults to none)
|
24
35
|
* <code>ignore</code> Ignore migrations that apply to certain columns (defaults to none)
|
25
|
-
* <code>indexes</code> Only index certain columns (
|
36
|
+
* <code>indexes</code> Only index certain columns (duplicates all indexes by default)
|
26
37
|
|
27
38
|
That's it!
|
28
39
|
----------
|
data/lib/also_migrate/base.rb
CHANGED
@@ -15,6 +15,8 @@ module AlsoMigrate
|
|
15
15
|
@also_migrate_config << {
|
16
16
|
:tables => args.collect(&:to_s),
|
17
17
|
:options => {
|
18
|
+
:add => options[:add] ? options[:add] : [],
|
19
|
+
:subtract => [ options[:subtract] ].flatten.compact,
|
18
20
|
:ignore => [ options[:ignore] ].flatten.compact,
|
19
21
|
:indexes => options[:indexes] ? [ options[:indexes] ].flatten : nil
|
20
22
|
}
|
@@ -44,17 +44,17 @@ module AlsoMigrate
|
|
44
44
|
config[:tables].each do |new_table|
|
45
45
|
if !connection.table_exists?(new_table) && connection.table_exists?(old_table)
|
46
46
|
columns = connection.columns(old_table).collect(&:name)
|
47
|
-
columns -= options[:
|
47
|
+
columns -= options[:subtract].collect(&:to_s)
|
48
48
|
columns.collect! { |col| connection.quote_column_name(col) }
|
49
|
-
engine =
|
50
|
-
if connection.class.to_s.include?('Mysql')
|
51
|
-
'ENGINE=' + connection.select_one(<<-SQL)['Engine']
|
52
|
-
SHOW TABLE STATUS
|
53
|
-
WHERE Name = '#{old_table}'
|
54
|
-
SQL
|
55
|
-
end
|
56
49
|
indexes = options[:indexes]
|
57
50
|
if indexes
|
51
|
+
engine =
|
52
|
+
if connection.class.to_s.include?('Mysql')
|
53
|
+
'ENGINE=' + connection.select_one(<<-SQL)['Engine']
|
54
|
+
SHOW TABLE STATUS
|
55
|
+
WHERE Name = '#{old_table}'
|
56
|
+
SQL
|
57
|
+
end
|
58
58
|
connection.execute(<<-SQL)
|
59
59
|
CREATE TABLE #{new_table} #{engine}
|
60
60
|
AS SELECT #{columns.join(',')}
|
@@ -71,6 +71,25 @@ module AlsoMigrate
|
|
71
71
|
SQL
|
72
72
|
end
|
73
73
|
end
|
74
|
+
if connection.table_exists?(new_table)
|
75
|
+
if options[:add] || options[:subtract]
|
76
|
+
columns = connection.columns(new_table).collect(&:name)
|
77
|
+
end
|
78
|
+
if options[:add]
|
79
|
+
options[:add].each do |column|
|
80
|
+
unless columns.include?(column[0])
|
81
|
+
connection.add_column(*([ new_table ] + column))
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
if options[:subtract]
|
86
|
+
options[:subtract].each do |column|
|
87
|
+
if columns.include?(column)
|
88
|
+
connection.remove_column(new_table, column)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
74
93
|
end
|
75
94
|
end
|
76
95
|
end
|
data/lib/also_migrate/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
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: 2010-12-
|
17
|
+
date: 2010-12-12 00:00:00 -08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|