also_migrate 0.3.4 → 0.3.5

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 CHANGED
@@ -10,13 +10,14 @@ Requirements
10
10
  gem install also_migrate
11
11
  </pre>
12
12
 
13
- Define the model
14
- ----------------
13
+ Configure
14
+ ---------
15
15
 
16
16
  <pre>
17
- class Article &lt; ActiveRecord::Base
18
- also_migrate(
19
- :article_archives,
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 &lt; ActiveRecord::Base
24
25
  :subtract => 'restored_at',
25
26
  :ignore => 'deleted_at',
26
27
  :indexes => 'id'
27
- )
28
- end
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)
@@ -1,4 +1,4 @@
1
1
  also_migrate:
2
- active_wrapper: =0.4.2
2
+ active_wrapper: =0.4.4
3
3
  rake: >=0.8.7
4
4
  rspec: ~>1.0
@@ -1,5 +1,5 @@
1
1
  name: also_migrate
2
- version: 0.3.4
2
+ version: 0.3.5
3
3
  authors:
4
4
  - Winton Welsh
5
5
  email: mail@wintoni.us
@@ -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 :classes
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
- if ::AlsoMigrate.classes
35
- ::AlsoMigrate.classes.uniq.each do |klass|
36
- if klass.also_migrate_config
37
- klass.also_migrate_config.each do |config|
38
- options = config[:options]
39
- tables = config[:tables]
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
- # Run migration
49
- config[:tables].each do |table|
50
- if method == :create_table
51
- ActiveRecord::Migrator::AlsoMigrate.create_tables(klass)
52
- elsif method == :add_index && !options[:indexes].nil?
53
- next
54
- elsif connection.table_exists?(table)
55
- args[0] = table
56
- args[1] = table if method == :rename_table
57
- begin
58
- connection.send(method, *args, &block)
59
- rescue Exception => e
60
- puts "(also_migrate warning) #{e.message}"
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
- if ::AlsoMigrate.classes
18
- ::AlsoMigrate.classes.uniq.each do |klass|
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(klass)
39
- config = klass.also_migrate_config
40
- return unless config
41
- old_table = klass.table_name
42
- config.each do |config|
43
- options = config[:options]
44
- config[:tables].each do |new_table|
45
- if !connection.table_exists?(new_table) && connection.table_exists?(old_table)
46
- columns = connection.columns(old_table).collect(&:name)
47
- columns -= options[:subtract].collect(&:to_s)
48
- columns.collect! { |col| connection.quote_column_name(col) }
49
- indexes = options[:indexes]
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
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} #{engine}
60
- AS SELECT #{columns.join(',')}
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 #{old_table};
69
+ LIKE #{config[:source]};
71
70
  SQL
72
71
  end
73
72
  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
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
- if options[:subtract]
86
- options[:subtract].each do |column|
87
- if columns.include?(column)
88
- connection.remove_column(new_table, column)
89
- end
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
@@ -1,5 +1 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../lib/also_migrate')
2
-
3
- unless $override_rails_rake_task == false
4
- $rails_rake_task = false
5
- end
1
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/also_migrate')
@@ -1,13 +1,13 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../lib/also_migrate/gems')
2
2
 
3
- AlsoMigrate::Gems.require(:spec_rake)
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
@@ -10,15 +10,16 @@ describe AlsoMigrate do
10
10
  reset_fixture
11
11
 
12
12
  if description == "table doesn't exist yet"
13
- Article.also_migrate(
14
- :article_archives,
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
- Article.also_migrate(
30
- :article_archives,
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
- Article.also_migrate :article_archives
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
- Article.also_migrate :article_archives
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
- Article.also_migrate :article_archives
106
- Comment.also_migrate :comment_archives
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
- Article.also_migrate :article_archives
116
- Comment.also_migrate :comment_archives
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
- Article.also_migrate :article_archives
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
- Article.also_migrate_config = nil
141
- Article.also_migrate(
142
- :article_archives,
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
@@ -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
- if Article.respond_to?(:also_migrate_config)
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
- - 4
9
- version: 0.3.4
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-20 00:00:00 -08:00
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
@@ -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
@@ -1,2 +0,0 @@
1
- class Article < ActiveRecord::Base
2
- end
@@ -1,2 +0,0 @@
1
- class Comment < ActiveRecord::Base
2
- end