From ea8f3f0a3765883c993cdd1c28ae958f097d2632 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 22 Jul 2005 19:58:03 +0000 Subject: Added migration support for SQLite (using temporary tables to simulate ALTER TABLE) #1771 [Sam Stephenson] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1893 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../connection_adapters/abstract_adapter.rb | 46 ++++++++++++++-------- 1 file changed, 30 insertions(+), 16 deletions(-) (limited to 'activerecord/lib/active_record/connection_adapters/abstract_adapter.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index e9473c1ca5..ff34c63fa1 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -380,10 +380,11 @@ module ActiveRecord table_definition.primary_key(options[:primary_key] || "id") unless options[:id] == false yield table_definition - create_sql = "CREATE TABLE #{name} (" + create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE " + create_sql << "#{name} (" create_sql << table_definition.to_sql create_sql << ") #{options[:options]}" - + execute create_sql end @@ -494,6 +495,20 @@ module ActiveRecord end end + class ColumnDefinition < Struct.new(:base, :name, :type, :limit, :default) + def to_sql + column_sql = "#{name} #{type_to_sql(type.to_sym, limit)}" + column_sql << " DEFAULT '#{default}'" if default + column_sql + end + alias to_s :to_sql + + private + def type_to_sql(name, limit) + base.type_to_sql(name, limit) rescue name + end + end + class TableDefinition attr_accessor :columns @@ -503,29 +518,28 @@ module ActiveRecord end def primary_key(name) - @columns << "#{name} #{native[:primary_key]}" + return unless column = self[name] + column.type = native[:primary_key] self end + + def [](name) + @columns.find {|column| column.name == name} + end def column(name, type, options = {}) - limit = options[:limit] || native[type.to_sym][:limit] - - column_sql = "#{name} #{type_to_sql(type.to_sym, options[:limit])}" - column_sql << " DEFAULT '#{options[:default]}'" if options[:default] - @columns << column_sql + column = self[name] || ColumnDefinition.new(@base, name, type) + column.limit = options[:limit] || native[type.to_sym][:limit] + column.default = options[:default] + @columns << column unless @columns.include? column self end - + def to_sql - @columns.join(", ") - end - - private - - def type_to_sql(name, limit) - @base.type_to_sql(name, limit) + @columns * ', ' end + private def native @base.native_database_types end -- cgit v1.2.3