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

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, options={} )
  return add_column_without_lfe_usesguid_migrations( table_name, column_name, type, options ) unless type.to_s == 'guid'

  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"
  add_column_options!( add_column_sql, options )
  execute( add_column_sql )
end

#create_table_with_lfe_usesguid_migrations(table_name, options = {}) {|table_definition| ... } ⇒ Object

Yields:

  • (table_definition)


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, options={} )
  table_definition = ActiveRecord::ConnectionAdapters::TableDefinition.new( self )
  #table_definition = TableDefinition.new( self )

  if options[:guid] == false
    table_definition.primary_key( options[:primary_key] || ActiveRecord::Base.get_primary_key( table_name ) ) unless options[:id] == false
  else
    table_definition.guid_primary_key( options[:primary_key] || ActiveRecord::Base.get_primary_key( table_name ) ) unless options[:id] == false
  end

  yield table_definition

  if options[:force] && table_exists?( table_name )
    drop_table( table_name, options )
  end

  create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "
  create_sql << "#{quote_table_name( table_name )} ("
  create_sql << table_definition.to_sql
  create_sql << ") #{options[:options]}"
  execute create_sql

  # TODO this needs to be different for each adapter
  unless table_name == "schema_migrations"
    unless options[:id] == false || options[: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.options
      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