also_migrate 0.1.0 → 0.1.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/{MIT-LICENSE → LICENSE} +2 -2
- data/{README.markdown → README.md} +3 -3
- data/lib/also_migrate.rb +6 -2
- data/lib/also_migrate/migration.rb +6 -1
- data/lib/also_migrate/migrator.rb +19 -48
- data/lib/also_migrate/version.rb +3 -0
- metadata +56 -32
- data/Rakefile +0 -2
- data/init.rb +0 -1
- data/log/development.log +0 -6
- data/rails/init.rb +0 -2
- data/require.rb +0 -52
- data/spec/Rakefile +0 -10
- data/spec/also_migrate_spec.rb +0 -53
- data/spec/config/database.yml.example +0 -6
- data/spec/db/migrate/001_create_articles.rb +0 -14
- data/spec/db/migrate/002_add_permalink.rb +0 -9
- data/spec/db/migrate/003_remove_ignored.rb +0 -9
- data/spec/fixtures/article.rb +0 -3
- data/spec/log/test.log +0 -6455
- data/spec/spec_helper.rb +0 -27
data/{MIT-LICENSE → LICENSE}
RENAMED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c)
|
1
|
+
Copyright (c) 2010 Winton Welsh
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
4
|
this software and associated documentation files (the "Software"), to deal in
|
@@ -15,4 +15,4 @@ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
15
15
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
16
16
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
17
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.
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -1,7 +1,7 @@
|
|
1
1
|
AlsoMigrate
|
2
2
|
===========
|
3
3
|
|
4
|
-
Migrate multiple tables with similar schema.
|
4
|
+
Migrate multiple tables with similar schema at once.
|
5
5
|
|
6
6
|
Requirements
|
7
7
|
------------
|
@@ -21,8 +21,8 @@ end
|
|
21
21
|
|
22
22
|
Options:
|
23
23
|
|
24
|
-
* <code
|
25
|
-
* <code
|
24
|
+
* <code>ignore</code> Ignore migrations that apply to certain columns (defaults to none)
|
25
|
+
* <code>indexes</code> Only index certain columns (defaults to all)
|
26
26
|
|
27
27
|
That's it!
|
28
28
|
----------
|
data/lib/also_migrate.rb
CHANGED
@@ -1,5 +1,9 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
$:.unshift File.dirname(__FILE__) + '/also_migrate'
|
2
|
+
|
3
|
+
require 'version'
|
4
|
+
require 'base'
|
5
|
+
require 'migration'
|
6
|
+
require 'migrator'
|
3
7
|
|
4
8
|
ActiveRecord::Base.send(:include, AlsoMigrate::Base)
|
5
9
|
ActiveRecord::Migrator.send(:include, AlsoMigrate::Migrator)
|
@@ -34,6 +34,7 @@ module AlsoMigrate
|
|
34
34
|
Object.subclasses_of(ActiveRecord::Base).each do |klass|
|
35
35
|
if klass.respond_to?(:also_migrate_config)
|
36
36
|
next unless klass.table_name == table_name
|
37
|
+
next if klass.also_migrate_config.nil?
|
37
38
|
klass.also_migrate_config.each do |config|
|
38
39
|
options = config[:options]
|
39
40
|
tables = config[:tables]
|
@@ -52,7 +53,11 @@ module AlsoMigrate
|
|
52
53
|
elsif connection.table_exists?(table)
|
53
54
|
args[0] = table
|
54
55
|
args[1] = table if method == :rename_table
|
55
|
-
|
56
|
+
begin
|
57
|
+
connection.send(method, *args, &block)
|
58
|
+
rescue Exception => e
|
59
|
+
puts "(also_migrate warning) #{e.message}"
|
60
|
+
end
|
56
61
|
end
|
57
62
|
end
|
58
63
|
end
|
@@ -19,6 +19,9 @@ module AlsoMigrate
|
|
19
19
|
AlsoMigrate.create_tables(klass)
|
20
20
|
end
|
21
21
|
end
|
22
|
+
rescue Exception => e
|
23
|
+
puts "AlsoMigrate error: #{e.message}"
|
24
|
+
puts e.backtrace.join("\n")
|
22
25
|
ensure
|
23
26
|
migrate_without_also_migrate
|
24
27
|
end
|
@@ -32,6 +35,7 @@ module AlsoMigrate
|
|
32
35
|
|
33
36
|
def create_tables(klass)
|
34
37
|
config = klass.also_migrate_config
|
38
|
+
return unless config
|
35
39
|
old_table = klass.table_name
|
36
40
|
config.each do |config|
|
37
41
|
options = config[:options]
|
@@ -47,60 +51,27 @@ module AlsoMigrate
|
|
47
51
|
WHERE Name = '#{old_table}'
|
48
52
|
SQL
|
49
53
|
end
|
50
|
-
connection.execute(<<-SQL)
|
51
|
-
CREATE TABLE #{new_table} #{engine}
|
52
|
-
AS SELECT #{columns.join(',')}
|
53
|
-
FROM #{old_table}
|
54
|
-
WHERE false;
|
55
|
-
SQL
|
56
54
|
indexes = options[:indexes]
|
57
|
-
indexes
|
58
|
-
|
59
|
-
|
55
|
+
if indexes
|
56
|
+
connection.execute(<<-SQL)
|
57
|
+
CREATE TABLE #{new_table} #{engine}
|
58
|
+
AS SELECT #{columns.join(',')}
|
59
|
+
FROM #{old_table}
|
60
|
+
WHERE false;
|
61
|
+
SQL
|
62
|
+
indexes.each do |column|
|
63
|
+
connection.add_index(new_table, column)
|
64
|
+
end
|
65
|
+
else
|
66
|
+
connection.execute(<<-SQL)
|
67
|
+
CREATE TABLE #{new_table}
|
68
|
+
LIKE #{old_table};
|
69
|
+
SQL
|
60
70
|
end
|
61
71
|
end
|
62
72
|
end
|
63
73
|
end
|
64
74
|
end
|
65
|
-
|
66
|
-
def indexed_columns(table_name)
|
67
|
-
# MySQL
|
68
|
-
if connection.class.to_s.include?('Mysql')
|
69
|
-
index_query = "SHOW INDEX FROM #{table_name}"
|
70
|
-
connection.select_all(index_query).collect do |r|
|
71
|
-
r["Column_name"]
|
72
|
-
end
|
73
|
-
# PostgreSQL
|
74
|
-
# http://stackoverflow.com/questions/2204058/show-which-columns-an-index-is-on-in-postgresql/2213199
|
75
|
-
elsif connection.class.to_s.include?('PostgreSQL')
|
76
|
-
index_query = <<-SQL
|
77
|
-
select
|
78
|
-
t.relname as table_name,
|
79
|
-
i.relname as index_name,
|
80
|
-
a.attname as column_name
|
81
|
-
from
|
82
|
-
pg_class t,
|
83
|
-
pg_class i,
|
84
|
-
pg_index ix,
|
85
|
-
pg_attribute a
|
86
|
-
where
|
87
|
-
t.oid = ix.indrelid
|
88
|
-
and i.oid = ix.indexrelid
|
89
|
-
and a.attrelid = t.oid
|
90
|
-
and a.attnum = ANY(ix.indkey)
|
91
|
-
and t.relkind = 'r'
|
92
|
-
and t.relname = '#{table_name}'
|
93
|
-
order by
|
94
|
-
t.relname,
|
95
|
-
i.relname
|
96
|
-
SQL
|
97
|
-
connection.select_all(index_query).collect do |r|
|
98
|
-
r["column_name"]
|
99
|
-
end
|
100
|
-
else
|
101
|
-
raise 'AlsoMigrate does not support this database adapter'
|
102
|
-
end
|
103
|
-
end
|
104
75
|
end
|
105
76
|
end
|
106
77
|
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: also_migrate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 25
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Winton Welsh
|
@@ -9,48 +15,60 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date: 2010-
|
18
|
+
date: 2010-07-19 00:00:00 -07:00
|
13
19
|
default_executable:
|
14
20
|
dependencies:
|
15
21
|
- !ruby/object:Gem::Dependency
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
name: bundler
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
20
27
|
requirements:
|
21
28
|
- - "="
|
22
29
|
- !ruby/object:Gem::Version
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
30
|
+
hash: 62196361
|
31
|
+
segments:
|
32
|
+
- 1
|
33
|
+
- 0
|
34
|
+
- 0
|
35
|
+
- beta
|
36
|
+
- 5
|
37
|
+
version: 1.0.0.beta.5
|
38
|
+
requirement: *id001
|
39
|
+
- !ruby/object:Gem::Dependency
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
name: rspec
|
43
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - "="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
hash: 27
|
49
|
+
segments:
|
50
|
+
- 1
|
51
|
+
- 3
|
52
|
+
- 0
|
53
|
+
version: 1.3.0
|
54
|
+
requirement: *id002
|
55
|
+
description: Migrate multiple tables with similar schema at once
|
56
|
+
email:
|
57
|
+
- mail@wintoni.us
|
27
58
|
executables: []
|
28
59
|
|
29
60
|
extensions: []
|
30
61
|
|
31
|
-
extra_rdoc_files:
|
32
|
-
|
62
|
+
extra_rdoc_files: []
|
63
|
+
|
33
64
|
files:
|
34
|
-
- init.rb
|
35
65
|
- lib/also_migrate/base.rb
|
36
66
|
- lib/also_migrate/migration.rb
|
37
67
|
- lib/also_migrate/migrator.rb
|
68
|
+
- lib/also_migrate/version.rb
|
38
69
|
- lib/also_migrate.rb
|
39
|
-
-
|
40
|
-
-
|
41
|
-
- rails/init.rb
|
42
|
-
- Rakefile
|
43
|
-
- README.markdown
|
44
|
-
- require.rb
|
45
|
-
- spec/also_migrate_spec.rb
|
46
|
-
- spec/config/database.yml.example
|
47
|
-
- spec/db/migrate/001_create_articles.rb
|
48
|
-
- spec/db/migrate/002_add_permalink.rb
|
49
|
-
- spec/db/migrate/003_remove_ignored.rb
|
50
|
-
- spec/fixtures/article.rb
|
51
|
-
- spec/log/test.log
|
52
|
-
- spec/Rakefile
|
53
|
-
- spec/spec_helper.rb
|
70
|
+
- LICENSE
|
71
|
+
- README.md
|
54
72
|
has_rdoc: true
|
55
73
|
homepage: http://github.com/winton/also_migrate
|
56
74
|
licenses: []
|
@@ -61,23 +79,29 @@ rdoc_options: []
|
|
61
79
|
require_paths:
|
62
80
|
- lib
|
63
81
|
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
64
83
|
requirements:
|
65
84
|
- - ">="
|
66
85
|
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
67
89
|
version: "0"
|
68
|
-
version:
|
69
90
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
70
92
|
requirements:
|
71
93
|
- - ">="
|
72
94
|
- !ruby/object:Gem::Version
|
95
|
+
hash: 3
|
96
|
+
segments:
|
97
|
+
- 0
|
73
98
|
version: "0"
|
74
|
-
version:
|
75
99
|
requirements: []
|
76
100
|
|
77
101
|
rubyforge_project:
|
78
|
-
rubygems_version: 1.3.
|
102
|
+
rubygems_version: 1.3.7
|
79
103
|
signing_key:
|
80
104
|
specification_version: 3
|
81
|
-
summary: Migrate multiple tables with similar schema
|
105
|
+
summary: Migrate multiple tables with similar schema at once
|
82
106
|
test_files: []
|
83
107
|
|
data/Rakefile
DELETED
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + "/rails/init"
|
data/log/development.log
DELETED
@@ -1,6 +0,0 @@
|
|
1
|
-
[4;36;1mSQL (0.2ms)[0m [0;1mSET SQL_AUTO_IS_NULL=0[0m
|
2
|
-
[4;35;1mUser Load (59.6ms)[0m [0mSELECT * FROM `users` ORDER BY users.id DESC LIMIT 1[0m
|
3
|
-
[4;36;1mUser Columns (21.6ms)[0m [0;1mSHOW FIELDS FROM `users`[0m
|
4
|
-
[4;36;1mSQL (0.3ms)[0m [0;1mSET SQL_AUTO_IS_NULL=0[0m
|
5
|
-
[4;35;1mSQL (0.3ms)[0m [0mSHOW TABLES[0m
|
6
|
-
[4;36;1mUser Columns (13.0ms)[0m [0;1mSHOW FIELDS FROM `users`[0m
|
data/rails/init.rb
DELETED
data/require.rb
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
gem 'require'
|
3
|
-
require 'require'
|
4
|
-
|
5
|
-
Require do
|
6
|
-
gem(:active_wrapper, '=0.2.3') { require 'active_wrapper' }
|
7
|
-
gem :require, '=0.2.6'
|
8
|
-
gem(:rake, '=0.8.7') { require 'rake' }
|
9
|
-
gem :rspec, '=1.3.0'
|
10
|
-
|
11
|
-
gemspec do
|
12
|
-
author 'Winton Welsh'
|
13
|
-
dependencies do
|
14
|
-
gem :require
|
15
|
-
end
|
16
|
-
email 'mail@wintoni.us'
|
17
|
-
name 'also_migrate'
|
18
|
-
homepage "http://github.com/winton/#{name}"
|
19
|
-
summary "Migrate multiple tables with similar schema"
|
20
|
-
version '0.1.0'
|
21
|
-
end
|
22
|
-
|
23
|
-
bin { require 'lib/also_migrate' }
|
24
|
-
|
25
|
-
lib do
|
26
|
-
require 'lib/also_migrate/base'
|
27
|
-
require 'lib/also_migrate/migration'
|
28
|
-
require 'lib/also_migrate/migrator'
|
29
|
-
end
|
30
|
-
|
31
|
-
rails_init { require 'lib/also_migrate' }
|
32
|
-
|
33
|
-
rakefile do
|
34
|
-
gem(:active_wrapper)
|
35
|
-
gem(:rake) { require 'rake/gempackagetask' }
|
36
|
-
gem(:rspec) { require 'spec/rake/spectask' }
|
37
|
-
require 'require/tasks'
|
38
|
-
end
|
39
|
-
|
40
|
-
spec_helper do
|
41
|
-
gem(:active_wrapper)
|
42
|
-
require 'require/spec_helper'
|
43
|
-
require 'rails/init'
|
44
|
-
require 'pp'
|
45
|
-
require 'spec/fixtures/article'
|
46
|
-
end
|
47
|
-
|
48
|
-
spec_rakefile do
|
49
|
-
gem(:rake)
|
50
|
-
gem(:active_wrapper) { require 'active_wrapper/tasks' }
|
51
|
-
end
|
52
|
-
end
|
data/spec/Rakefile
DELETED
data/spec/also_migrate_spec.rb
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe AlsoMigrate do
|
4
|
-
|
5
|
-
describe 'fixture config' do
|
6
|
-
|
7
|
-
before(:each) do
|
8
|
-
$db.migrate(1)
|
9
|
-
$db.migrate(0)
|
10
|
-
$db.migrate(1)
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'should migrate both tables up' do
|
14
|
-
migrate_with_state(2)
|
15
|
-
(@new_article_columns - @old_article_columns).should == [ 'permalink' ]
|
16
|
-
(@new_archive_columns - @old_archive_columns).should == [ 'permalink' ]
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'should migrate both tables down' do
|
20
|
-
$db.migrate(2)
|
21
|
-
migrate_with_state(1)
|
22
|
-
(@old_article_columns - @new_article_columns).should == [ 'permalink' ]
|
23
|
-
(@old_archive_columns - @new_archive_columns).should == [ 'permalink' ]
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should ignore the body column column" do
|
27
|
-
(columns('articles') - columns('article_archives')).should == [ 'body' ]
|
28
|
-
connection.remove_column(:articles, :body)
|
29
|
-
(columns('articles') - columns('article_archives')).should == []
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should only add an index for id" do
|
33
|
-
ActiveRecord::Migrator::AlsoMigrate.indexed_columns('articles').should == [ 'id', 'read' ]
|
34
|
-
ActiveRecord::Migrator::AlsoMigrate.indexed_columns('article_archives').should == [ 'id' ]
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
describe 'no index config' do
|
39
|
-
|
40
|
-
before(:each) do
|
41
|
-
Article.also_migrate_config = nil
|
42
|
-
Article.also_migrate :article_archives
|
43
|
-
$db.migrate(1)
|
44
|
-
$db.migrate(0)
|
45
|
-
$db.migrate(1)
|
46
|
-
end
|
47
|
-
|
48
|
-
it "should add all indexes" do
|
49
|
-
ActiveRecord::Migrator::AlsoMigrate.indexed_columns('articles').should == [ 'id', 'read' ]
|
50
|
-
ActiveRecord::Migrator::AlsoMigrate.indexed_columns('article_archives').should == [ 'id', 'read' ]
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|