usesguid_migrations 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/History.txt +3 -0
- data/LICENSE +20 -0
- data/README.rdoc +130 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/lib/usesguid_migrations/active_record_extensions/base.rb +31 -0
- data/lib/usesguid_migrations/active_record_extensions/connection_adapters/mysql_adapter.rb +46 -0
- data/lib/usesguid_migrations/active_record_extensions/connection_adapters/schema_statements.rb +60 -0
- data/lib/usesguid_migrations/active_record_extensions/connection_adapters/table_definition.rb +38 -0
- data/lib/usesguid_migrations/active_record_extensions/schema.rb +29 -0
- data/lib/usesguid_migrations/active_record_extensions/schema_dumper.rb +138 -0
- data/lib/usesguid_migrations.rb +26 -0
- data/script/console +10 -0
- data/testdb.rake.txt +44 -0
- data/usesguid_migrations.gemspec +62 -0
- metadata +100 -0
data/.gitignore
ADDED
data/History.txt
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 C. Jason Harrelson (midas)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
= usesguid_migrations
|
2
|
+
|
3
|
+
http://github.com/midas/usesguid_migrations/tree/master
|
4
|
+
|
5
|
+
|
6
|
+
== DESCRIPTION
|
7
|
+
|
8
|
+
To be used with http://github.com/BMorearty/usesguid/tree/master. It can be a pain to explicitly declare the correct
|
9
|
+
column types, etc. for your usesguid migrations. For instance:
|
10
|
+
|
11
|
+
create_table :users, :id => false, :force => true do |t|
|
12
|
+
t.binary :id, :limit => 22, :null => false
|
13
|
+
...
|
14
|
+
t.binary :account_id, :limit => 22, :null => false
|
15
|
+
end
|
16
|
+
|
17
|
+
execute "ALTER TABLE `users` MODIFY COLUMN `id` VARCHAR(22) BINARY CHARACTER SET latin1 COLLATE latin1_bin NOT NULL;"
|
18
|
+
execute "ALTER TABLE `users` ADD PRIMARY KEY (id)"
|
19
|
+
execute "ALTER TABLE `users` MODIFY COLUMN `account_id` VARCHAR(22) BINARY CHARACTER SET latin1 COLLATE latin1_bin NOT NULL;"
|
20
|
+
|
21
|
+
In addition, this technique will fail to keep you schema.rb file correctly up to date. The binary varchar(22) fields will actually be
|
22
|
+
output as:
|
23
|
+
|
24
|
+
t.string "id", :limit => 22, :null => false
|
25
|
+
t.string "account_id", :limit => 22
|
26
|
+
|
27
|
+
This will include no:
|
28
|
+
|
29
|
+
execute "ALTER TABLE `users` MODIFY COLUMN `id` VARCHAR(22) BINARY CHARACTER SET latin1 COLLATE latin1_bin NOT NULL;"
|
30
|
+
execute "ALTER TABLE `users` ADD PRIMARY KEY (id)"
|
31
|
+
execute "ALTER TABLE `users` MODIFY COLUMN `account_id` VARCHAR(22) BINARY CHARACTER SET latin1 COLLATE latin1_bin NOT NULL;"
|
32
|
+
|
33
|
+
So you will end up getting unique id collisions due to the non-case sensitivity of the string field in MySQL without a latin1_bin
|
34
|
+
collation.
|
35
|
+
|
36
|
+
Enter usesguid_migrations. This plugin will automatically create an id field (just as migrations currently do), but it will be of the
|
37
|
+
type necessary for usesguid to work correctly.
|
38
|
+
|
39
|
+
|
40
|
+
== FEATURES
|
41
|
+
|
42
|
+
* Automatically generate an id field with correct typing for usesguid
|
43
|
+
* Migration associated method for generating foreign keys with correct typing for usesguid
|
44
|
+
* Schema dumper will create schema.rb files with the correct typing for usesguid
|
45
|
+
|
46
|
+
|
47
|
+
== TO DO
|
48
|
+
|
49
|
+
* Add ability for the add_column and change_column methods to accept an option turning them into guids
|
50
|
+
* Tests, tests and more tests (if anyone has any ideas of how to accomplish this please let me know, I am currently at a loss of how to test something like this)
|
51
|
+
|
52
|
+
|
53
|
+
== REQUIREMENTS
|
54
|
+
|
55
|
+
* Rails 2.0 or above
|
56
|
+
* usesguid Gem (http://github.com/midas/usesguid)
|
57
|
+
|
58
|
+
|
59
|
+
== USAGE
|
60
|
+
|
61
|
+
Simply create a normal create table migration and an id field with correct typing for usesguid will be generated.
|
62
|
+
|
63
|
+
In addition, if you would like to declare a foreign key field that needs to be of the
|
64
|
+
correct type to use usesguid, just use the associated method:
|
65
|
+
|
66
|
+
create_table :whatevers, :force => true do |t|
|
67
|
+
t.references_with_guid :account
|
68
|
+
end
|
69
|
+
|
70
|
+
This will create a field named account_id that is of the correct type for usesguid. It will also assume that the column
|
71
|
+
is not nullable as it is more than likely a foreign key. to Make it nullable you must use th e:null => true option:
|
72
|
+
|
73
|
+
t.references_with_guid :account
|
74
|
+
|
75
|
+
or
|
76
|
+
|
77
|
+
t.references_with_guid :account, :null => true
|
78
|
+
|
79
|
+
If you need a table with a standard Rails id field just use the :guid option of the crate_table method.
|
80
|
+
|
81
|
+
create_table :users, :guid => false do |t|
|
82
|
+
...
|
83
|
+
t.references_with_guid :account # will still use a guid regardless of the :guid => false option in the create_table params above
|
84
|
+
end
|
85
|
+
|
86
|
+
If you need a table with no id at all, just use the normal :primary_key option of the create_table method and
|
87
|
+
you will have no id field automatically generated.
|
88
|
+
|
89
|
+
create_table :users, :primary_key => false do |t|
|
90
|
+
...
|
91
|
+
end
|
92
|
+
|
93
|
+
If you run into any issues with running tests or specs, it could be something strange happening in your schema.rb file. This
|
94
|
+
file is used to prepare the database for testing. To override this and make migrations be used to prepare the database, copy
|
95
|
+
and rename the testdb.rake.txt file to {project root}/lib/tasks/testdb.rake.
|
96
|
+
|
97
|
+
|
98
|
+
== INSTALL
|
99
|
+
|
100
|
+
From the command line:
|
101
|
+
|
102
|
+
sudo gem install usesguid_migrations
|
103
|
+
|
104
|
+
In Rails environment.rb:
|
105
|
+
|
106
|
+
config.gem "usesguid_migrations"
|
107
|
+
|
108
|
+
|
109
|
+
== LICENSE
|
110
|
+
|
111
|
+
Copyright (c) 2009 C. Jason Harrelson (midas)
|
112
|
+
|
113
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
114
|
+
a copy of this software and associated documentation files (the
|
115
|
+
"Software"), to deal in the Software without restriction, including
|
116
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
117
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
118
|
+
permit persons to whom the Software is furnished to do so, subject to
|
119
|
+
the following conditions:
|
120
|
+
|
121
|
+
The above copyright notice and this permission notice shall be
|
122
|
+
included in all copies or substantial portions of the Software.
|
123
|
+
|
124
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
125
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
126
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
127
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
128
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
129
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
130
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "usesguid_migrations"
|
8
|
+
gem.summary = %Q{Makes your migrations work with usesguid plugin without explicitly defining the keys migrations.}
|
9
|
+
gem.description = %Q{Makes your migrations work with usesguid plugin without explicitly defining the primary key id or foreign keys in migrations.}
|
10
|
+
gem.email = "jason@lookforwardenterprises.com"
|
11
|
+
gem.homepage = "http://github.com/midas/usesguid_migrations"
|
12
|
+
gem.authors = ["Jason Harrelson"]
|
13
|
+
gem.add_development_dependency "rspec"
|
14
|
+
gem.add_dependency "activerecord", ">= 2.3"
|
15
|
+
gem.add_dependency "usesguid"
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
if File.exist?('VERSION')
|
41
|
+
version = File.read('VERSION')
|
42
|
+
else
|
43
|
+
version = ""
|
44
|
+
end
|
45
|
+
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = "usesguid_migrations #{version}"
|
48
|
+
rdoc.rdoc_files.include('README*')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module UsesguidMigrations
|
2
|
+
module ActiveRecordExtensions
|
3
|
+
module Base
|
4
|
+
|
5
|
+
def self.included( base )
|
6
|
+
base.extend( ClassMethods )
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def self.extended( base )
|
11
|
+
class << base
|
12
|
+
alias_method_chain :abstract_class?, :lfe_usesguid_migrations
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def abstract_class_with_lfe_usesguid_migrations?
|
17
|
+
abstract_class_without_lfe_usesguid_migrations? || !(name =~ /^Abstract/).nil?
|
18
|
+
end
|
19
|
+
|
20
|
+
def base_class?
|
21
|
+
self == base_class
|
22
|
+
end
|
23
|
+
|
24
|
+
def pluralized_table_name( table_name )
|
25
|
+
ActiveRecord::Base.pluralize_table_names ? table_name.to_s.pluralize : table_name
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module UsesguidMigrations
|
2
|
+
module ActiveRecordExtensions
|
3
|
+
module ConnectionAdapters
|
4
|
+
module MysqlAdapter
|
5
|
+
|
6
|
+
def primary_key_name( table_name, name=nil )
|
7
|
+
results = execute( "SHOW CREATE TABLE `#{table_name}`", name )
|
8
|
+
keys = []
|
9
|
+
primary_key = nil
|
10
|
+
|
11
|
+
results.each do |row|
|
12
|
+
row[1].each do |line|
|
13
|
+
keys << $1 if line =~ /^ [`"](.+?)[`"] varchar\(22\) character set latin1 collate latin1_bin NOT NULL?,?$/
|
14
|
+
primary_key = $1 if line =~ /^ PRIMARY KEY \([`"](.+?)[`"]\)$/
|
15
|
+
primary_key = $1 if line =~ /^ PRIMARY KEY \([`"](.+?)[`"]\),$/
|
16
|
+
primary_key = $1 if line =~ /^ PRIMARY KEY \([`"](.+?)[`"]\), $/
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
return keys.include?( primary_key ) ? primary_key : nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def foreign_keys( table_name, name=nil )
|
24
|
+
results = execute( "SHOW CREATE TABLE `#{table_name}`", name )
|
25
|
+
|
26
|
+
null_foreign_keys = []
|
27
|
+
not_null_foreign_keys = []
|
28
|
+
primary_keys = []
|
29
|
+
|
30
|
+
results.each do |row|
|
31
|
+
row[1].each do |line|
|
32
|
+
null_foreign_keys << $1 if line =~ /^ [`"](.+?)[`"] varchar\(22\) character set latin1 collate latin1_bin default NULL?,?$/
|
33
|
+
not_null_foreign_keys << $1 if line =~ /^ [`"](.+?)[`"] varchar\(22\) character set latin1 collate latin1_bin NOT NULL?,?$/
|
34
|
+
primary_keys << $1 if line =~ /^ PRIMARY KEY \([`"](.+?)[`"]\)$/
|
35
|
+
primary_keys << $1 if line =~ /^ PRIMARY KEY \([`"](.+?)[`"]\),$/
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
return null_foreign_keys - primary_keys, not_null_foreign_keys - primary_keys
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/usesguid_migrations/active_record_extensions/connection_adapters/schema_statements.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module UsesguidMigrations
|
2
|
+
module ActiveRecordExtensions
|
3
|
+
module ConnectionAdapters
|
4
|
+
module SchemaStatements
|
5
|
+
|
6
|
+
def self.included( base )
|
7
|
+
base.module_eval do
|
8
|
+
alias_method_chain :create_table, :lfe_usesguid_migrations
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_table_with_lfe_usesguid_migrations( table_name, options={} )
|
13
|
+
table_definition = ActiveRecord::ConnectionAdapters::TableDefinition.new( self )
|
14
|
+
#table_definition = TableDefinition.new( self )
|
15
|
+
|
16
|
+
if options[:guid] == false
|
17
|
+
table_definition.primary_key( options[:primary_key] || ActiveRecord::Base.get_primary_key( table_name ) ) unless options[:id] == false
|
18
|
+
else
|
19
|
+
table_definition.guid_primary_key( options[:primary_key] || ActiveRecord::Base.get_primary_key( table_name ) ) unless options[:id] == false
|
20
|
+
end
|
21
|
+
|
22
|
+
yield table_definition
|
23
|
+
|
24
|
+
if options[:force] && table_exists?( table_name )
|
25
|
+
drop_table( table_name, options )
|
26
|
+
end
|
27
|
+
|
28
|
+
create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "
|
29
|
+
create_sql << "#{quote_table_name( table_name )} ("
|
30
|
+
create_sql << table_definition.to_sql
|
31
|
+
create_sql << ") #{options[:options]}"
|
32
|
+
execute create_sql
|
33
|
+
|
34
|
+
# TODO this needs to be different for each adapter
|
35
|
+
unless table_name == "schema_migrations"
|
36
|
+
unless options[:id] == false || options[:guid] == false
|
37
|
+
execute "ALTER TABLE `#{table_name}` MODIFY COLUMN `#{table_definition.primary_key_name}` VARCHAR(22) BINARY CHARACTER SET latin1 COLLATE latin1_bin NOT NULL;"
|
38
|
+
execute "ALTER TABLE `#{table_name}` ADD PRIMARY KEY (#{table_definition.primary_key_name})"
|
39
|
+
end
|
40
|
+
|
41
|
+
return if table_definition.associative_keys.nil?
|
42
|
+
|
43
|
+
table_definition.associative_keys.each do |assoc|
|
44
|
+
key = assoc.name
|
45
|
+
opts = assoc.options
|
46
|
+
sql = "ALTER TABLE `#{table_name}` MODIFY COLUMN `#{key}` VARCHAR(#{opts[:limit] || 22}) BINARY CHARACTER SET latin1 COLLATE latin1_bin"
|
47
|
+
if opts[:null] == false || opts[:null].nil?
|
48
|
+
sql << " NOT NULL;"
|
49
|
+
else
|
50
|
+
sql << ";"
|
51
|
+
end
|
52
|
+
execute( sql )
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module UsesguidMigrations
|
2
|
+
module ActiveRecordExtensions
|
3
|
+
module ConnectionAdapters
|
4
|
+
module TableDefinition
|
5
|
+
|
6
|
+
def self.included( base )
|
7
|
+
base.class_eval do
|
8
|
+
include InstanceMethods
|
9
|
+
attr_accessor :primary_key_name
|
10
|
+
attr_accessor :associative_keys
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module InstanceMethods
|
15
|
+
def guid_primary_key( name )
|
16
|
+
@primary_key_name = name
|
17
|
+
column( name, :binary, :limit => 22, :null => false )
|
18
|
+
end
|
19
|
+
|
20
|
+
def references_with_guid( name, options={} )
|
21
|
+
name = name.to_s
|
22
|
+
name = "#{name}_id" unless name.end_with?( "_id" )
|
23
|
+
guid( name, options )
|
24
|
+
end
|
25
|
+
|
26
|
+
def guid( name, options={} )
|
27
|
+
@associative_keys = [] if @associative_keys.nil?
|
28
|
+
options.merge!( :limit => 22 )
|
29
|
+
options.merge!( :null => false ) unless options[:null] == true # make not nullable the default for a guid column as it is likely a foreign key
|
30
|
+
@associative_keys << OpenStruct.new( :name => name, :options => options )
|
31
|
+
column( name, :binary, options )
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module UsesguidMigrations
|
2
|
+
module ActiveRecordExtensions
|
3
|
+
module Schema
|
4
|
+
|
5
|
+
def self.included( base )
|
6
|
+
base.extend( ClassMethods )
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def self.extended( base )
|
11
|
+
class << base
|
12
|
+
attr_accessor :defining
|
13
|
+
alias :defining? :defining
|
14
|
+
|
15
|
+
alias_method_chain :define, :lfe_usesguid_migrations
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def define_with_lfe_usesguid_migrations( info={}, &block )
|
20
|
+
self.defining = true
|
21
|
+
define_without_lfe_usesguid_migrations( info, &block )
|
22
|
+
ensure
|
23
|
+
self.defining = false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
module UsesguidMigrations
|
2
|
+
module ActiveRecordExtensions
|
3
|
+
module SchemaDumper
|
4
|
+
|
5
|
+
def self.included( base )
|
6
|
+
base.class_eval do
|
7
|
+
private
|
8
|
+
alias_method_chain :table, :lfe_usesguid_migrations
|
9
|
+
alias_method_chain :indexes, :lfe_usesguid_migrations
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def table_with_lfe_usesguid_migrations( table, stream )
|
16
|
+
#table_without_lfe_usesguid_migrations( table, stream )
|
17
|
+
columns = @connection.columns( table )
|
18
|
+
begin
|
19
|
+
tbl = StringIO.new
|
20
|
+
|
21
|
+
guid_pk = @connection.primary_key_name( table )
|
22
|
+
guid_fks = @connection.foreign_keys( table )
|
23
|
+
|
24
|
+
if @connection.respond_to?( :pk_and_sequence_for )
|
25
|
+
pk, pk_seq = @connection.pk_and_sequence_for( table )
|
26
|
+
end
|
27
|
+
pk ||= 'id'
|
28
|
+
|
29
|
+
tbl.print " create_table #{table.inspect}"
|
30
|
+
if col = columns.detect { |c| c.name == pk }
|
31
|
+
tbl.print %Q(, :primary_key => "#{pk}") if pk != 'id'
|
32
|
+
if col.type == :string
|
33
|
+
tbl.print ", :id => false"
|
34
|
+
elsif col.type == :integer
|
35
|
+
tbl.print ", :guid => false"
|
36
|
+
end
|
37
|
+
else
|
38
|
+
tbl.print ", :id => false"
|
39
|
+
end
|
40
|
+
tbl.print ", :force => true"
|
41
|
+
tbl.puts " do |t|"
|
42
|
+
|
43
|
+
column_specs = columns.map do |column|
|
44
|
+
raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" if @types[column.type].nil?
|
45
|
+
next if column.name == pk
|
46
|
+
spec = {}
|
47
|
+
spec[:name] = column.name.inspect
|
48
|
+
spec[:type] = column.type.to_s
|
49
|
+
spec[:limit] = column.limit.inspect if column.limit != @types[column.type][:limit] && column.type != :decimal
|
50
|
+
spec[:precision] = column.precision.inspect if !column.precision.nil?
|
51
|
+
spec[:scale] = column.scale.inspect if !column.scale.nil?
|
52
|
+
spec[:null] = 'false' if !column.null
|
53
|
+
spec[:default] = default_string(column.default) if column.has_default?
|
54
|
+
(spec.keys - [:name, :type]).each{ |k| spec[k].insert(0, "#{k.inspect} => ") }
|
55
|
+
spec
|
56
|
+
end.compact
|
57
|
+
|
58
|
+
unless guid_pk.nil? || guid_pk.empty?
|
59
|
+
column_specs.insert( 0, { :name => "\"#{guid_pk}\"", :type => 'binary', :limit => ':limit => 22', :null => ':null => false' } )
|
60
|
+
end
|
61
|
+
|
62
|
+
names = column_specs.map { |h| h[:name] }
|
63
|
+
|
64
|
+
unless guid_fks.nil?
|
65
|
+
guid_fks.each do |fk|
|
66
|
+
pos = names.index( "\"#{fk}\"" )
|
67
|
+
if pos
|
68
|
+
spec = column_specs[pos]
|
69
|
+
spec[:type] = "binary"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# find all migration keys used in this table
|
75
|
+
keys = [:name, :limit, :precision, :scale, :default, :null] & column_specs.map(&:keys).flatten
|
76
|
+
|
77
|
+
# figure out the lengths for each column based on above keys
|
78
|
+
lengths = keys.map{ |key| column_specs.map{ |spec| spec[key] ? spec[key].length + 2 : 0 }.max }
|
79
|
+
|
80
|
+
# the string we're going to sprintf our values against, with standardized column widths
|
81
|
+
format_string = lengths.map{ |len| "%-#{len}s" }
|
82
|
+
|
83
|
+
# find the max length for the 'type' column, which is special
|
84
|
+
type_length = column_specs.map{ |column| column[:type].length }.max
|
85
|
+
|
86
|
+
# add column type definition to our format string
|
87
|
+
format_string.unshift " t.%-#{type_length}s "
|
88
|
+
|
89
|
+
format_string *= ''
|
90
|
+
|
91
|
+
column_specs.each do |colspec|
|
92
|
+
values = keys.zip(lengths).map{ |key, len| colspec.key?(key) ? colspec[key] + ", " : " " * len }
|
93
|
+
values.unshift colspec[:type]
|
94
|
+
tbl.print((format_string % values).gsub(/,\s*$/, ''))
|
95
|
+
tbl.puts
|
96
|
+
end
|
97
|
+
|
98
|
+
tbl.puts " end"
|
99
|
+
tbl.puts
|
100
|
+
|
101
|
+
indexes(table, tbl)
|
102
|
+
|
103
|
+
tbl.rewind
|
104
|
+
stream.print tbl.read
|
105
|
+
rescue => e
|
106
|
+
stream.puts "# Could not dump table #{table.inspect} because of following #{e.class}"
|
107
|
+
stream.puts "# #{e.message}"
|
108
|
+
stream.puts
|
109
|
+
end
|
110
|
+
|
111
|
+
stream
|
112
|
+
end
|
113
|
+
|
114
|
+
def indexes_with_lfe_usesguid_migrations( table, stream )
|
115
|
+
pk = @connection.primary_key_name( table )
|
116
|
+
null_foreign_keys, not_null_foreign_keys = @connection.foreign_keys( table )
|
117
|
+
|
118
|
+
unless pk.nil? || pk.empty?
|
119
|
+
stream.puts " execute \"ALTER TABLE `#{table}` MODIFY COLUMN `#{pk}` VARCHAR(22) BINARY CHARACTER SET latin1 COLLATE latin1_bin NOT NULL;\""
|
120
|
+
stream.puts " execute \"ALTER TABLE `#{table}` ADD PRIMARY KEY (#{pk})\""
|
121
|
+
end
|
122
|
+
stream.puts if (null_foreign_keys.nil? || null_foreign_keys.empty?) && (not_null_foreign_keys.nil? || not_null_foreign_keys.empty?)
|
123
|
+
|
124
|
+
null_foreign_keys.each do |key|
|
125
|
+
stream.puts " execute \"ALTER TABLE `#{table}` MODIFY COLUMN `#{key}` VARCHAR(22) BINARY CHARACTER SET latin1 COLLATE latin1_bin NULL;\""
|
126
|
+
end
|
127
|
+
|
128
|
+
not_null_foreign_keys.each do |key|
|
129
|
+
stream.puts " execute \"ALTER TABLE `#{table}` MODIFY COLUMN `#{key}` VARCHAR(22) BINARY CHARACTER SET latin1 COLLATE latin1_bin NOT NULL;\""
|
130
|
+
end
|
131
|
+
|
132
|
+
stream.puts unless (null_foreign_keys.nil? || null_foreign_keys.empty?) && (not_null_foreign_keys.nil? || not_null_foreign_keys.empty?)
|
133
|
+
|
134
|
+
indexes_without_lfe_usesguid_migrations( table, stream )
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require 'usesguid_migrations/active_record_extensions/base'
|
5
|
+
require 'usesguid_migrations/active_record_extensions/schema'
|
6
|
+
require 'usesguid_migrations/active_record_extensions/schema_dumper'
|
7
|
+
require 'usesguid_migrations/active_record_extensions/connection_adapters/mysql_adapter'
|
8
|
+
require 'usesguid_migrations/active_record_extensions/connection_adapters/schema_statements'
|
9
|
+
require 'usesguid_migrations/active_record_extensions/connection_adapters/table_definition'
|
10
|
+
|
11
|
+
module UsesguidMigrations
|
12
|
+
VERSION = '1.0.0'
|
13
|
+
end
|
14
|
+
|
15
|
+
ActiveRecord::Base.send( :include, UsesguidMigrations::ActiveRecordExtensions::Base ) if defined?( ActiveRecord::Base )
|
16
|
+
ActiveRecord::Schema.send( :include, UsesguidMigrations::ActiveRecordExtensions::Schema ) if defined?( ActiveRecord::Schema )
|
17
|
+
ActiveRecord::SchemaDumper.send( :include, UsesguidMigrations::ActiveRecordExtensions::SchemaDumper ) if defined?( ActiveRecord::SchemaDumper )
|
18
|
+
if defined?( ActiveRecord::ConnectionAdapters::SchemaStatements )
|
19
|
+
ActiveRecord::ConnectionAdapters::SchemaStatements.send( :include, UsesguidMigrations::ActiveRecordExtensions::ConnectionAdapters::SchemaStatements )
|
20
|
+
end
|
21
|
+
if defined?( ActiveRecord::ConnectionAdapters::TableDefinition )
|
22
|
+
ActiveRecord::ConnectionAdapters::TableDefinition.send( :include, UsesguidMigrations::ActiveRecordExtensions::ConnectionAdapters::TableDefinition )
|
23
|
+
end
|
24
|
+
if defined?( ActiveRecord::ConnectionAdapters::MysqlAdapter )
|
25
|
+
ActiveRecord::ConnectionAdapters::MysqlAdapter.send( :include, UsesguidMigrations::ActiveRecordExtensions::ConnectionAdapters::MysqlAdapter )
|
26
|
+
end
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/usesguid_migrations.rb'}"
|
9
|
+
puts "Loading usesguid_migrations gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/testdb.rake.txt
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
module Rake
|
2
|
+
module TaskManager
|
3
|
+
def redefine_task(task_class, *args, &block)
|
4
|
+
task_name, deps = resolve_args(args)
|
5
|
+
task_name = task_class.scope_name(@scope, task_name)
|
6
|
+
deps = [deps] unless deps.respond_to?(:to_ary)
|
7
|
+
deps = deps.collect {|d| d.to_s }
|
8
|
+
task = @tasks[task_name.to_s] = task_class.new(task_name, self)
|
9
|
+
task.application = self
|
10
|
+
#task.add_comment(@last_comment)
|
11
|
+
@last_comment = nil
|
12
|
+
task.enhance(deps, &block)
|
13
|
+
task
|
14
|
+
end
|
15
|
+
end
|
16
|
+
class Task
|
17
|
+
class << self
|
18
|
+
def redefine_task(args, &block)
|
19
|
+
Rake.application.redefine_task(self, args, &block)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def redefine_task(args, &block)
|
26
|
+
Rake::Task.redefine_task(args, &block)
|
27
|
+
end
|
28
|
+
|
29
|
+
namespace :db do
|
30
|
+
namespace :test do
|
31
|
+
|
32
|
+
desc 'Prepare the test database and migrate schema'
|
33
|
+
redefine_task :prepare => :environment do
|
34
|
+
Rake::Task['db:test:migrate_schema'].invoke
|
35
|
+
end
|
36
|
+
|
37
|
+
desc 'Use the migrations to create the test database'
|
38
|
+
task :migrate_schema => 'db:test:purge' do
|
39
|
+
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
|
40
|
+
ActiveRecord::Migrator.migrate("db/migrate/")
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{usesguid_migrations}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Jason Harrelson"]
|
12
|
+
s.date = %q{2009-11-13}
|
13
|
+
s.description = %q{Makes your migrations work with usesguid plugin without explicitly defining the primary key id or foreign keys in migrations.}
|
14
|
+
s.email = %q{jason@lookforwardenterprises.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"History.txt",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/usesguid_migrations.rb",
|
27
|
+
"lib/usesguid_migrations/active_record_extensions/base.rb",
|
28
|
+
"lib/usesguid_migrations/active_record_extensions/connection_adapters/mysql_adapter.rb",
|
29
|
+
"lib/usesguid_migrations/active_record_extensions/connection_adapters/schema_statements.rb",
|
30
|
+
"lib/usesguid_migrations/active_record_extensions/connection_adapters/table_definition.rb",
|
31
|
+
"lib/usesguid_migrations/active_record_extensions/schema.rb",
|
32
|
+
"lib/usesguid_migrations/active_record_extensions/schema_dumper.rb",
|
33
|
+
"script/console",
|
34
|
+
"testdb.rake.txt",
|
35
|
+
"usesguid_migrations.gemspec"
|
36
|
+
]
|
37
|
+
s.homepage = %q{http://github.com/midas/usesguid_migrations}
|
38
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
39
|
+
s.require_paths = ["lib"]
|
40
|
+
s.rubygems_version = %q{1.3.5}
|
41
|
+
s.summary = %q{Makes your migrations work with usesguid plugin without explicitly defining the keys migrations.}
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
49
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 2.3"])
|
50
|
+
s.add_runtime_dependency(%q<usesguid>, [">= 0"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
53
|
+
s.add_dependency(%q<activerecord>, [">= 2.3"])
|
54
|
+
s.add_dependency(%q<usesguid>, [">= 0"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
58
|
+
s.add_dependency(%q<activerecord>, [">= 2.3"])
|
59
|
+
s.add_dependency(%q<usesguid>, [">= 0"])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: usesguid_migrations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jason Harrelson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-13 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activerecord
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "2.3"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: usesguid
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
description: Makes your migrations work with usesguid plugin without explicitly defining the primary key id or foreign keys in migrations.
|
46
|
+
email: jason@lookforwardenterprises.com
|
47
|
+
executables: []
|
48
|
+
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files:
|
52
|
+
- LICENSE
|
53
|
+
- README.rdoc
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- History.txt
|
57
|
+
- LICENSE
|
58
|
+
- README.rdoc
|
59
|
+
- Rakefile
|
60
|
+
- VERSION
|
61
|
+
- lib/usesguid_migrations.rb
|
62
|
+
- lib/usesguid_migrations/active_record_extensions/base.rb
|
63
|
+
- lib/usesguid_migrations/active_record_extensions/connection_adapters/mysql_adapter.rb
|
64
|
+
- lib/usesguid_migrations/active_record_extensions/connection_adapters/schema_statements.rb
|
65
|
+
- lib/usesguid_migrations/active_record_extensions/connection_adapters/table_definition.rb
|
66
|
+
- lib/usesguid_migrations/active_record_extensions/schema.rb
|
67
|
+
- lib/usesguid_migrations/active_record_extensions/schema_dumper.rb
|
68
|
+
- script/console
|
69
|
+
- testdb.rake.txt
|
70
|
+
- usesguid_migrations.gemspec
|
71
|
+
has_rdoc: true
|
72
|
+
homepage: http://github.com/midas/usesguid_migrations
|
73
|
+
licenses: []
|
74
|
+
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options:
|
77
|
+
- --charset=UTF-8
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: "0"
|
85
|
+
version:
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: "0"
|
91
|
+
version:
|
92
|
+
requirements: []
|
93
|
+
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 1.3.5
|
96
|
+
signing_key:
|
97
|
+
specification_version: 3
|
98
|
+
summary: Makes your migrations work with usesguid plugin without explicitly defining the keys migrations.
|
99
|
+
test_files: []
|
100
|
+
|