Module: UsesguidMigrations::ActiveRecordExtensions::ConnectionAdapters::SchemaStatements
- Defined in:
- lib/usesguid_migrations/active_record_extensions/connection_adapters/schema_statements.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_column_with_lfe_usesguid_migrations(table_name, column_name, type, options = {}) ⇒ Object
- #create_table_with_lfe_usesguid_migrations(table_name, options = {}) {|table_definition| ... } ⇒ Object
Class Method Details
.included(base) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/usesguid_migrations/active_record_extensions/connection_adapters/schema_statements.rb', line 6 def self.included( base ) base.module_eval do alias_method_chain :create_table, :lfe_usesguid_migrations alias_method_chain :add_column, :lfe_usesguid_migrations end end |
Instance Method Details
#add_column_with_lfe_usesguid_migrations(table_name, column_name, type, options = {}) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/usesguid_migrations/active_record_extensions/connection_adapters/schema_statements.rb', line 58 def add_column_with_lfe_usesguid_migrations( table_name, column_name, type, ={} ) return add_column_without_lfe_usesguid_migrations( table_name, column_name, type, ) unless type.to_s == 'guid' add_column_sql = "ALTER TABLE #{quote_table_name( table_name )} ADD #{quote_column_name( column_name )} VARCHAR(#{[:limit] || 22}) BINARY CHARACTER SET latin1 COLLATE latin1_bin" ( add_column_sql, ) execute( add_column_sql ) end |
#create_table_with_lfe_usesguid_migrations(table_name, options = {}) {|table_definition| ... } ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/usesguid_migrations/active_record_extensions/connection_adapters/schema_statements.rb', line 13 def create_table_with_lfe_usesguid_migrations( table_name, ={} ) table_definition = ActiveRecord::ConnectionAdapters::TableDefinition.new( self ) #table_definition = TableDefinition.new( self ) if [:guid] == false table_definition.primary_key( [:primary_key] || ActiveRecord::Base.get_primary_key( table_name ) ) unless [:id] == false else table_definition.guid_primary_key( [:primary_key] || ActiveRecord::Base.get_primary_key( table_name ) ) unless [:id] == false end yield table_definition if [:force] && table_exists?( table_name ) drop_table( table_name, ) end create_sql = "CREATE#{' TEMPORARY' if [:temporary]} TABLE " create_sql << "#{quote_table_name( table_name )} (" create_sql << table_definition.to_sql create_sql << ") #{[:options]}" execute create_sql # TODO this needs to be different for each adapter unless table_name == "schema_migrations" unless [:id] == false || [:guid] == false execute "ALTER TABLE `#{table_name}` MODIFY COLUMN `#{table_definition.primary_key_name}` VARCHAR(22) BINARY CHARACTER SET latin1 COLLATE latin1_bin NOT NULL;" execute "ALTER TABLE `#{table_name}` ADD PRIMARY KEY (#{table_definition.primary_key_name})" end return if table_definition.associative_keys.nil? table_definition.associative_keys.each do |assoc| key = assoc.name opts = assoc. sql = "ALTER TABLE `#{table_name}` MODIFY COLUMN `#{key}` VARCHAR(#{opts[:limit] || 22}) BINARY CHARACTER SET latin1 COLLATE latin1_bin" if opts[:null] == false || opts[:null].nil? sql << " NOT NULL;" else sql << ";" end execute( sql ) end end end |