also_migrate 0.2.3 → 0.3.0

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.
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
7
  - 3
9
- version: 0.2.3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Winton Welsh
@@ -14,42 +14,48 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-05 00:00:00 -08:00
17
+ date: 2011-01-09 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: rspec
21
+ name: rake
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
26
- - - "="
26
+ - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  segments:
29
- - 1
30
- - 3
31
- - 1
32
- version: 1.3.1
29
+ - 0
30
+ - 8
31
+ - 7
32
+ version: 0.8.7
33
33
  type: :development
34
34
  version_requirements: *id001
35
- description: Migrate multiple tables with similar schema at once
36
- email:
37
- - mail@wintoni.us
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 1
45
+ - 0
46
+ version: "1.0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ description: Migrate multiple tables with similar schema at once.
50
+ email: mail@wintoni.us
38
51
  executables: []
39
52
 
40
53
  extensions: []
41
54
 
42
55
  extra_rdoc_files: []
43
56
 
44
- files:
45
- - lib/also_migrate/base.rb
46
- - lib/also_migrate/gems.rb
47
- - lib/also_migrate/migration.rb
48
- - lib/also_migrate/migrator.rb
49
- - lib/also_migrate/version.rb
50
- - lib/also_migrate.rb
51
- - LICENSE
52
- - README.md
57
+ files: []
58
+
53
59
  has_rdoc: true
54
60
  homepage: http://github.com/winton/also_migrate
55
61
  licenses: []
@@ -81,6 +87,6 @@ rubyforge_project:
81
87
  rubygems_version: 1.3.7
82
88
  signing_key:
83
89
  specification_version: 3
84
- summary: Migrate multiple tables with similar schema at once
90
+ summary: Migrate multiple tables with similar schema at once.
85
91
  test_files: []
86
92
 
data/LICENSE DELETED
@@ -1,18 +0,0 @@
1
- Copyright (c) 2010 Winton Welsh
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy of
4
- this software and associated documentation files (the "Software"), to deal in
5
- the Software without restriction, including without limitation the rights to
6
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
- the Software, and to permit persons to whom the Software is furnished to do so,
8
- subject to the following conditions:
9
-
10
- The above copyright notice and this permission notice shall be included in all
11
- copies or substantial portions of the Software.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
- IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md DELETED
@@ -1,43 +0,0 @@
1
- AlsoMigrate
2
- ===========
3
-
4
- Migrate multiple tables with similar schema at once.
5
-
6
- Requirements
7
- ------------
8
-
9
- <pre>
10
- sudo gem install also_migrate
11
- </pre>
12
-
13
- Define the model
14
- ----------------
15
-
16
- <pre>
17
- class Article &lt; ActiveRecord::Base
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
- )
28
- end
29
- </pre>
30
-
31
- Options:
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)
35
- * <code>ignore</code> Ignore migrations that apply to certain columns (defaults to none)
36
- * <code>indexes</code> Only index certain columns (duplicates all indexes by default)
37
-
38
- That's it!
39
- ----------
40
-
41
- Next time you migrate, <code>article_archives</code> is created if it doesn't exist.
42
-
43
- Any new migration applied to <code>articles</code> is automatically applied to <code>article_archives</code>.
data/lib/also_migrate.rb DELETED
@@ -1,21 +0,0 @@
1
- require File.dirname(__FILE__) + '/also_migrate/gems'
2
-
3
- AlsoMigrate::Gems.require(:lib)
4
-
5
- $:.unshift File.dirname(__FILE__)
6
-
7
- require 'also_migrate/version'
8
-
9
- require 'also_migrate/base'
10
- require 'also_migrate/migration'
11
- require 'also_migrate/migrator'
12
-
13
- module AlsoMigrate
14
- class <<self
15
- attr_accessor :classes
16
- end
17
- end
18
-
19
- ActiveRecord::Base.send(:include, AlsoMigrate::Base)
20
- ActiveRecord::Migrator.send(:include, AlsoMigrate::Migrator)
21
- ActiveRecord::Migration.send(:include, AlsoMigrate::Migration)
@@ -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,44 +0,0 @@
1
- unless defined?(AlsoMigrate::Gems)
2
-
3
- require 'rubygems'
4
-
5
- module AlsoMigrate
6
- class Gems
7
-
8
- VERSIONS = {
9
- :active_wrapper => '=0.4.0',
10
- :rake => '=0.8.7',
11
- :rspec => '=1.3.1'
12
- }
13
-
14
- TYPES = {
15
- :gemspec => [],
16
- :gemspec_dev => [ :rspec ],
17
- :lib => [],
18
- :rake => [ :rake, :rspec ],
19
- :spec => [ :active_wrapper, :rspec ],
20
- :spec_rake => [ :active_wrapper ]
21
- }
22
-
23
- class <<self
24
-
25
- def lockfile
26
- file = File.expand_path('../../../gems', __FILE__)
27
- unless File.exists?(file)
28
- File.open(file, 'w') do |f|
29
- Gem.loaded_specs.each do |key, value|
30
- f.puts "#{key} #{value.version.version}"
31
- end
32
- end
33
- end
34
- end
35
-
36
- def require(type=nil)
37
- (TYPES[type] || TYPES.values.flatten.compact).each do |name|
38
- gem name.to_s, VERSIONS[name]
39
- end
40
- end
41
- end
42
- end
43
- end
44
- end
@@ -1,72 +0,0 @@
1
- module AlsoMigrate
2
- module Migration
3
-
4
- def self.included(base)
5
- unless base.respond_to?(:method_missing_with_also_migrate)
6
- base.extend ClassMethods
7
- base.class_eval do
8
- class <<self
9
- alias_method :method_missing_without_also_migrate, :method_missing
10
- alias_method :method_missing, :method_missing_with_also_migrate
11
- end
12
- end
13
- end
14
- end
15
-
16
- module ClassMethods
17
-
18
- def method_missing_with_also_migrate(method, *arguments, &block)
19
- args = Marshal.load(Marshal.dump(arguments))
20
- method_missing_without_also_migrate(method, *arguments, &block)
21
-
22
- supported = [
23
- :add_column, :add_index, :add_timestamps, :change_column,
24
- :change_column_default, :change_table, :create_table,
25
- :drop_table, :remove_column, :remove_columns,
26
- :remove_timestamps, :rename_column, :rename_table
27
- ]
28
-
29
- if !args.empty? && supported.include?(method)
30
- connection = ActiveRecord::Base.connection
31
- table_name = ActiveRecord::Migrator.proper_table_name(args[0])
32
-
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
47
-
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
63
- end
64
- end
65
- end
66
- end
67
- end
68
- end
69
- end
70
- end
71
- end
72
- end
@@ -1,100 +0,0 @@
1
- module AlsoMigrate
2
- module Migrator
3
-
4
- def self.included(base)
5
- unless base.respond_to?(:migrate_with_also_migrate)
6
- base.send :include, InstanceMethods
7
- base.class_eval do
8
- alias_method :migrate_without_also_migrate, :migrate
9
- alias_method :migrate, :migrate_with_also_migrate
10
- end
11
- end
12
- end
13
-
14
- module InstanceMethods
15
-
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
23
- end
24
- rescue Exception => e
25
- puts "AlsoMigrate error: #{e.message}"
26
- puts e.backtrace.join("\n")
27
- ensure
28
- migrate_without_also_migrate
29
- end
30
-
31
- module AlsoMigrate
32
- class <<self
33
-
34
- def connection
35
- ActiveRecord::Base.connection
36
- end
37
-
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
58
- connection.execute(<<-SQL)
59
- CREATE TABLE #{new_table} #{engine}
60
- AS SELECT #{columns.join(',')}
61
- FROM #{old_table}
62
- WHERE false;
63
- SQL
64
- indexes.each do |column|
65
- connection.add_index(new_table, column)
66
- end
67
- else
68
- connection.execute(<<-SQL)
69
- CREATE TABLE #{new_table}
70
- LIKE #{old_table};
71
- SQL
72
- end
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
93
- end
94
- end
95
- end
96
- end
97
- end
98
- end
99
- end
100
- end
@@ -1,3 +0,0 @@
1
- module AlsoMigrate
2
- VERSION = "0.2.3" unless defined?(::AlsoMigrate::VERSION)
3
- end