usesguid_migrations 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,10 @@
1
+ = 1.0.2 2010 01-26
2
+
3
+ * Added SQLite support
4
+ * Changed add_column now recognizes :guid and :guid_fk, the later automatically adding an _id to the
5
+ column name, if not already done
6
+
7
+
1
8
  = 1.0.1 2009-12-20
2
9
 
3
10
  * Added add_column override that recognizes the :guid type
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.0.2
@@ -1,46 +1,93 @@
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
1
+ module UsesguidMigrations::ActiveRecordExtensions::ConnectionAdapters
2
+ module MysqlAdapter
3
+
4
+ def primary_key_name( table_name, name=nil )
5
+ results = execute( "SHOW CREATE TABLE `#{table_name}`", name )
6
+ keys = []
7
+ primary_key = nil
8
+
9
+ results.each do |row|
10
+ row[1].each do |line|
11
+ keys << $1 if line =~ /^ [`"](.+?)[`"] varchar\(22\) character set latin1 collate latin1_bin NOT NULL?,?$/
12
+ primary_key = $1 if line =~ /^ PRIMARY KEY \([`"](.+?)[`"]\)$/
13
+ primary_key = $1 if line =~ /^ PRIMARY KEY \([`"](.+?)[`"]\),$/
14
+ primary_key = $1 if line =~ /^ PRIMARY KEY \([`"](.+?)[`"]\), $/
21
15
  end
16
+ end
22
17
 
23
- def foreign_keys( table_name, name=nil )
24
- results = execute( "SHOW CREATE TABLE `#{table_name}`", name )
18
+ return keys.include?( primary_key ) ? primary_key : nil
19
+ end
25
20
 
26
- null_foreign_keys = []
27
- not_null_foreign_keys = []
28
- primary_keys = []
21
+ def foreign_keys( table_name, name=nil )
22
+ results = execute( "SHOW CREATE TABLE `#{table_name}`", name )
29
23
 
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
24
+ null_foreign_keys = []
25
+ not_null_foreign_keys = []
26
+ primary_keys = []
38
27
 
28
+ results.each do |row|
29
+ row[1].each do |line|
30
+ null_foreign_keys << $1 if line =~ /^ [`"](.+?)[`"] varchar\(22\) character set latin1 collate latin1_bin default NULL?,?$/
31
+ not_null_foreign_keys << $1 if line =~ /^ [`"](.+?)[`"] varchar\(22\) character set latin1 collate latin1_bin NOT NULL?,?$/
32
+ primary_keys << $1 if line =~ /^ PRIMARY KEY \([`"](.+?)[`"]\)$/
33
+ primary_keys << $1 if line =~ /^ PRIMARY KEY \([`"](.+?)[`"]\),$/
34
+ end
35
+ end
39
36
 
40
- return null_foreign_keys - primary_keys, not_null_foreign_keys - primary_keys
37
+ return null_foreign_keys - primary_keys, not_null_foreign_keys - primary_keys
38
+ end
39
+
40
+ def usesguid_create_table( table_name, table_definition, options )
41
+ execute usesuid_create_table_statement( table_name, table_definition, options )
42
+
43
+ unless table_name == "schema_migrations"
44
+ unless options[:id] == false || options[:guid] == false
45
+ execute usesguid_alter_column_statement( table_name, table_definition.primary_key_name, :null => false )
46
+ execute usesguid_make_column_primary_key( table_name, table_definition.primary_key_name )
47
+ end
48
+
49
+ return if table_definition.associative_keys.nil?
50
+
51
+ table_definition.associative_keys.each do |assoc|
52
+ execute usesguid_alter_column_statement( table_name, assoc.name, assoc.options )
41
53
  end
42
-
43
54
  end
44
55
  end
56
+
57
+ def uses_guid_add_column( table_name, column_name, type, options={} )
58
+ column = ActiveRecord::ConnectionAdapters::ColumnDefinition.new( self, column_name, :string )
59
+ if options[:limit]
60
+ column.limit = options[:limit]
61
+ #elsif native[type.to_sym].is_a?( Hash )
62
+ # column.limit = native[type.to_sym][:limit]
63
+ end
64
+ column.precision = options[:precision]
65
+ column.scale = options[:scale]
66
+ column.default = options[:default]
67
+ column.null = options[:null]
68
+
69
+ execute usesguid_add_column_statement( table_name, column )
70
+ end
71
+
72
+ def usesuid_create_table_statement( table_name, table_definition, options )
73
+ create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "
74
+ create_sql << "#{quote_table_name( table_name )} ("
75
+ create_sql << table_definition.to_sql
76
+ create_sql << ") #{options[:options]}"
77
+ create_sql
78
+ end
79
+
80
+ def usesguid_alter_column_statement( table_name, pk_name, options={} )
81
+ "ALTER TABLE `#{table_name}` MODIFY COLUMN `#{pk_name}` VARCHAR(#{options[:limit] || 22}) BINARY CHARACTER SET latin1 COLLATE latin1_bin#{options[:null] == true ? '' : ' NOT NULL'};"
82
+ end
83
+
84
+ def usesguid_make_column_primary_key( table_name, pk_name )
85
+ "ALTER TABLE `#{table_name}` ADD PRIMARY KEY (#{pk_name})"
86
+ end
87
+
88
+ def usesguid_add_column_statement( table_name, column_def )
89
+ "ALTER TABLE #{quote_table_name( table_name )} ADD #{quote_column_name( column_def.name )} VARCHAR(#{column_def.limit || 22}) BINARY CHARACTER SET latin1 COLLATE latin1_bin"
90
+ end
91
+
45
92
  end
46
93
  end
@@ -1,68 +1,33 @@
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
- alias_method_chain :add_column, :lfe_usesguid_migrations
10
- end
11
- end
12
-
13
- def create_table_with_lfe_usesguid_migrations( table_name, options={} )
14
- table_definition = ActiveRecord::ConnectionAdapters::TableDefinition.new( self )
15
- #table_definition = TableDefinition.new( self )
16
-
17
- if options[:guid] == false
18
- table_definition.primary_key( options[:primary_key] || ActiveRecord::Base.get_primary_key( table_name ) ) unless options[:id] == false
19
- else
20
- table_definition.guid_primary_key( options[:primary_key] || ActiveRecord::Base.get_primary_key( table_name ) ) unless options[:id] == false
21
- end
1
+ module UsesguidMigrations::ActiveRecordExtensions::ConnectionAdapters
2
+ module SchemaStatements
22
3
 
23
- yield table_definition
24
-
25
- if options[:force] && table_exists?( table_name )
26
- drop_table( table_name, options )
27
- end
28
-
29
- create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "
30
- create_sql << "#{quote_table_name( table_name )} ("
31
- create_sql << table_definition.to_sql
32
- create_sql << ") #{options[:options]}"
33
- execute create_sql
4
+ def self.included( base )
5
+ base.module_eval do
6
+ alias_method_chain :create_table, :lfe_usesguid_migrations
7
+ alias_method_chain :add_column, :lfe_usesguid_migrations
8
+ end
9
+ end
34
10
 
35
- # TODO this needs to be different for each adapter
36
- unless table_name == "schema_migrations"
37
- unless options[:id] == false || options[:guid] == false
38
- execute "ALTER TABLE `#{table_name}` MODIFY COLUMN `#{table_definition.primary_key_name}` VARCHAR(22) BINARY CHARACTER SET latin1 COLLATE latin1_bin NOT NULL;"
39
- execute "ALTER TABLE `#{table_name}` ADD PRIMARY KEY (#{table_definition.primary_key_name})"
40
- end
11
+ def create_table_with_lfe_usesguid_migrations( table_name, options={} )
12
+ table_definition = ActiveRecord::ConnectionAdapters::TableDefinition.new( self )
41
13
 
42
- return if table_definition.associative_keys.nil?
14
+ if options[:guid] == false
15
+ table_definition.primary_key( options[:primary_key] || ActiveRecord::Base.get_primary_key( table_name ) ) unless options[:id] == false
16
+ else
17
+ table_definition.guid_primary_key( options[:primary_key] || ActiveRecord::Base.get_primary_key( table_name ) ) unless options[:id] == false
18
+ end
43
19
 
44
- table_definition.associative_keys.each do |assoc|
45
- key = assoc.name
46
- opts = assoc.options
47
- sql = "ALTER TABLE `#{table_name}` MODIFY COLUMN `#{key}` VARCHAR(#{opts[:limit] || 22}) BINARY CHARACTER SET latin1 COLLATE latin1_bin"
48
- if opts[:null] == false || opts[:null].nil?
49
- sql << " NOT NULL;"
50
- else
51
- sql << ";"
52
- end
53
- execute( sql )
54
- end
55
- end
56
- end
20
+ yield table_definition
57
21
 
58
- def add_column_with_lfe_usesguid_migrations( table_name, column_name, type, options={} )
59
- return add_column_without_lfe_usesguid_migrations( table_name, column_name, type, options ) unless type.to_s == 'guid'
22
+ drop_table( table_name, options ) if options[:force] && table_exists?( table_name )
23
+ usesguid_create_table( table_name, table_definition, options )
24
+ end
60
25
 
61
- add_column_sql = "ALTER TABLE #{quote_table_name( table_name )} ADD #{quote_column_name( column_name )} VARCHAR(#{options[:limit] || 22}) BINARY CHARACTER SET latin1 COLLATE latin1_bin"
62
- add_column_options!( add_column_sql, options )
63
- execute( add_column_sql )
64
- end
65
- end
26
+ def add_column_with_lfe_usesguid_migrations( table_name, column_name, type, options={} )
27
+ return add_column_without_lfe_usesguid_migrations( table_name, column_name, type, options ) unless %w(guid guid_fk).include?( type.to_s )
28
+
29
+ column_name = (type.to_s == 'guid_fk' && column_name) ? "#{column_name}_id" : column_name
30
+ uses_guid_add_column( table_name, column_name, type, options )
66
31
  end
67
32
  end
68
33
  end
@@ -0,0 +1,70 @@
1
+ module UsesguidMigrations::ActiveRecordExtensions::ConnectionAdapters
2
+ module SqliteAdapter
3
+
4
+ def usesguid_create_table( table_name, table_definition, options )
5
+ begin
6
+ sql = usesuid_create_table_statement( table_name, table_definition, options )
7
+ sql.sub!( '"id" blob(22) NOT NULL', '"id" blob(22) PRIMARY KEY NOT NULL' )
8
+ execute( sql )
9
+ rescue SQLite3::MisuseException; end
10
+
11
+ unless table_name == "schema_migrations"
12
+ unless options[:id] == false || options[:guid] == false
13
+ sql = usesguid_alter_column_statement( table_name, table_definition.primary_key_name, :null => false )
14
+ execute( sql ) unless sql.nil? || sql.empty?
15
+ sql = usesguid_make_column_primary_key( table_name, table_definition.primary_key_name )
16
+ execute( sql ) unless sql.nil? || sql.empty?
17
+ end
18
+
19
+ return if table_definition.associative_keys.nil?
20
+
21
+ table_definition.associative_keys.each do |assoc|
22
+ begin
23
+ sql = usesguid_alter_column_statement( table_name, assoc.name, assoc.options )
24
+ execute( sql ) unless sql.nil? || sql.empty?
25
+ rescue SQLite3::MisuseException; end
26
+ end
27
+ end
28
+ end
29
+
30
+ def uses_guid_add_column( table_name, column_name, type, options={} )
31
+ column = ActiveRecord::ConnectionAdapters::ColumnDefinition.new( self, column_name, :text )
32
+ if options[:limit]
33
+ column.limit = options[:limit]
34
+ #elsif native[type.to_sym].is_a?( Hash )
35
+ # column.limit = native[type.to_sym][:limit]
36
+ end
37
+ column.precision = options[:precision]
38
+ column.scale = options[:scale]
39
+ column.default = options[:default]
40
+ column.null = options[:null]
41
+
42
+ add_column_sql = usesguid_add_column_statement( table_name, column )
43
+
44
+ begin
45
+ execute( add_column_sql )
46
+ rescue SQLite3::MisuseException; end
47
+ end
48
+
49
+ def usesuid_create_table_statement( table_name, table_definition, options )
50
+ create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "
51
+ create_sql << "#{quote_table_name( table_name )} ("
52
+ create_sql << table_definition.to_sql
53
+ create_sql << ") #{options[:options]}"
54
+ create_sql
55
+ end
56
+
57
+ def usesguid_alter_column_statement( table_name, pk_name, options={} )
58
+ ''
59
+ end
60
+
61
+ def usesguid_make_column_primary_key( table_name, pk_name )
62
+ ''
63
+ end
64
+
65
+ def usesguid_add_column_statement( table_name, column_def )
66
+ "ALTER TABLE #{quote_table_name( table_name )} ADD COLUMN #{column_def.to_sql} DEFAULT('')"
67
+ end
68
+
69
+ end
70
+ end
@@ -1,38 +1,35 @@
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
1
+ module UsesguidMigrations::ActiveRecordExtensions::ConnectionAdapters
2
+ module TableDefinition
13
3
 
14
- module InstanceMethods
15
- def guid_primary_key( name )
16
- @primary_key_name = name
17
- column( name, :binary, :limit => 22, :null => false )
18
- end
4
+ def self.included( base )
5
+ base.class_eval do
6
+ include InstanceMethods
7
+ attr_accessor :primary_key_name
8
+ attr_accessor :associative_keys
9
+ end
10
+ end
11
+
12
+ module InstanceMethods
13
+ def guid_primary_key( name )
14
+ @primary_key_name = name
15
+ column( name, :binary, :limit => 22, :null => false )
16
+ end
19
17
 
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
18
+ def references_with_guid( name, options={} )
19
+ name = name.to_s
20
+ name = "#{name}_id" unless name.end_with?( "_id" )
21
+ guid( name, options )
22
+ end
25
23
 
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
-
24
+ def guid( name, options={} )
25
+ @associative_keys = [] if @associative_keys.nil?
26
+ options.merge!( :limit => 22 )
27
+ # make not nullable the default for a guid column as it is likely a foreign key
28
+ options.merge!( :null => false ) unless options[:null] == true
29
+ @associative_keys << OpenStruct.new( :name => name, :options => options )
30
+ column( name, :binary, options )
35
31
  end
36
32
  end
33
+
37
34
  end
38
35
  end
@@ -6,12 +6,16 @@ require 'usesguid_migrations/active_record_extensions/schema'
6
6
  require 'usesguid_migrations/active_record_extensions/schema_dumper'
7
7
  require 'usesguid_migrations/active_record_extensions/connection_adapters/mysql_adapter'
8
8
  require 'usesguid_migrations/active_record_extensions/connection_adapters/schema_statements'
9
+ require 'usesguid_migrations/active_record_extensions/connection_adapters/sqlite_adapter'
9
10
  require 'usesguid_migrations/active_record_extensions/connection_adapters/table_definition'
10
11
 
11
12
  module UsesguidMigrations
12
- VERSION = '1.0.1'
13
+ VERSION = '1.0.2'
13
14
  end
14
15
 
16
+ processor, platform, *rest = RUBY_PLATFORM.split("-")
17
+ is_windows = platform == 'mswin32'
18
+
15
19
  ActiveRecord::Base.send( :include, UsesguidMigrations::ActiveRecordExtensions::Base ) if defined?( ActiveRecord::Base )
16
20
  ActiveRecord::Schema.send( :include, UsesguidMigrations::ActiveRecordExtensions::Schema ) if defined?( ActiveRecord::Schema )
17
21
  ActiveRecord::SchemaDumper.send( :include, UsesguidMigrations::ActiveRecordExtensions::SchemaDumper ) if defined?( ActiveRecord::SchemaDumper )
@@ -23,4 +27,11 @@ if defined?( ActiveRecord::ConnectionAdapters::TableDefinition )
23
27
  end
24
28
  if defined?( ActiveRecord::ConnectionAdapters::MysqlAdapter )
25
29
  ActiveRecord::ConnectionAdapters::MysqlAdapter.send( :include, UsesguidMigrations::ActiveRecordExtensions::ConnectionAdapters::MysqlAdapter )
30
+ end
31
+ if defined?( ActiveRecord::ConnectionAdapters::SQLiteAdapter )
32
+ ActiveRecord::ConnectionAdapters::SQLiteAdapter.send( :include, UsesguidMigrations::ActiveRecordExtensions::ConnectionAdapters::SqliteAdapter )
33
+ end
34
+
35
+ if is_windows
36
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.send( :include, UsesguidMigrations::ActiveRecordExtensions::ConnectionAdapters::SqliteAdapter )
26
37
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{usesguid_migrations}
8
- s.version = "1.0.1"
8
+ s.version = "1.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jason Harrelson"]
12
- s.date = %q{2009-12-20}
12
+ s.date = %q{2010-01-26}
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
14
  s.email = %q{jason@lookforwardenterprises.com}
15
15
  s.extra_rdoc_files = [
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
27
27
  "lib/usesguid_migrations/active_record_extensions/base.rb",
28
28
  "lib/usesguid_migrations/active_record_extensions/connection_adapters/mysql_adapter.rb",
29
29
  "lib/usesguid_migrations/active_record_extensions/connection_adapters/schema_statements.rb",
30
+ "lib/usesguid_migrations/active_record_extensions/connection_adapters/sqlite_adapter.rb",
30
31
  "lib/usesguid_migrations/active_record_extensions/connection_adapters/table_definition.rb",
31
32
  "lib/usesguid_migrations/active_record_extensions/schema.rb",
32
33
  "lib/usesguid_migrations/active_record_extensions/schema_dumper.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usesguid_migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Harrelson
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-20 00:00:00 -06:00
12
+ date: 2010-01-26 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -62,6 +62,7 @@ files:
62
62
  - lib/usesguid_migrations/active_record_extensions/base.rb
63
63
  - lib/usesguid_migrations/active_record_extensions/connection_adapters/mysql_adapter.rb
64
64
  - lib/usesguid_migrations/active_record_extensions/connection_adapters/schema_statements.rb
65
+ - lib/usesguid_migrations/active_record_extensions/connection_adapters/sqlite_adapter.rb
65
66
  - lib/usesguid_migrations/active_record_extensions/connection_adapters/table_definition.rb
66
67
  - lib/usesguid_migrations/active_record_extensions/schema.rb
67
68
  - lib/usesguid_migrations/active_record_extensions/schema_dumper.rb