aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-03-01 14:27:32 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-03-01 14:27:32 +0000
commiteac7cf0b0608132673220d9045b8ff51dc0804e1 (patch)
tree770902bf0f248f387520de7312f6db9a3cf8f504 /activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
parent28a11969ced52abf3d7d51f486779cf31d1efe1f (diff)
downloadrails-eac7cf0b0608132673220d9045b8ff51dc0804e1.tar.gz
rails-eac7cf0b0608132673220d9045b8ff51dc0804e1.tar.bz2
rails-eac7cf0b0608132673220d9045b8ff51dc0804e1.zip
Added preliminary support for an agile database migration technique (currently only for MySQL)
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@818 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract_adapter.rb')
-rwxr-xr-xactiverecord/lib/active_record/connection_adapters/abstract_adapter.rb48
1 files changed, 46 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
index 605a82bf74..d686d71246 100755
--- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -356,6 +356,38 @@ module ActiveRecord
sql << " LIMIT #{limit}"
end
+
+ def initialize_schema_information
+ begin
+ execute "CREATE TABLE schema_info (version #{native_database_types[:integer]})"
+ insert "INSERT INTO schema_info (version) VALUES(0)"
+ rescue ActiveRecord::StatementInvalid
+ # Schema has been intialized
+ end
+ end
+
+ def create_table(name)
+ execute "CREATE TABLE #{name} (id #{native_database_types[:primary_key]})"
+ table_definition = yield TableDefinition.new
+ table_definition.columns.each { |column_name, type, options| add_column(name, column_name, type, options) }
+ end
+
+ def drop_table(name)
+ execute "DROP TABLE #{name}"
+ end
+
+ def add_column(table_name, column_name, type, options = {})
+ add_column_sql = "ALTER TABLE #{table_name} ADD #{column_name} #{native_database_types[type]}"
+ add_column_sql << "(#{limit})" if options[:limit]
+ add_column_sql << " DEFAULT '#{options[:default]}'" if options[:default]
+ execute(add_column_sql)
+ end
+
+ def remove_column(table_name, column_name)
+ execute "ALTER TABLE #{table_name} DROP #{column_name}"
+ end
+
+
protected
def log(sql, name, connection = nil)
connection ||= @connection
@@ -402,6 +434,18 @@ module ActiveRecord
log_entry
end
end
-
+
+ class TableDefinition
+ attr_accessor :columns
+
+ def initialize
+ @columns = []
+ end
+
+ def column(name, type, options = {})
+ @columns << [ name, type, options ]
+ self
+ end
+ end
end
-end
+end \ No newline at end of file