aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
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
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')
-rwxr-xr-xactiverecord/lib/active_record/connection_adapters/abstract_adapter.rb48
-rwxr-xr-xactiverecord/lib/active_record/connection_adapters/mysql_adapter.rb39
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb17
-rw-r--r--activerecord/lib/active_record/migration.rb94
4 files changed, 187 insertions, 11 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
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index cf7c700b66..0c78b921b6 100755
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -63,12 +63,33 @@ module ActiveRecord
"Lost connection to MySQL server during query",
"MySQL server has gone away"
]
-
+
+ def native_database_types
+ {
+ :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY",
+ :string => "varchar(255)",
+ :text => "text",
+ :integer => "int(11)",
+ :float => "float",
+ :datetime => "datetime",
+ :timestamp => "datetime",
+ :time => "datetime",
+ :date => "date",
+ :binary => "blob",
+ :boolean => "tinyint(1)"
+ }
+ end
+
def initialize(connection, logger, connection_options=nil)
super(connection, logger)
@connection_options = connection_options
end
+ def adapter_name
+ 'MySQL'
+ end
+
+
def select_all(sql, name = nil)
select(sql, name)
end
@@ -111,6 +132,7 @@ module ActiveRecord
alias_method :delete, :update
+
def begin_db_transaction
begin
execute "BEGIN"
@@ -134,15 +156,17 @@ module ActiveRecord
# Transactions aren't supported
end
end
+
def quote_column_name(name)
return "`#{name}`"
end
- def adapter_name()
- 'MySQL'
+ def quote_string(s)
+ Mysql::quote(s)
end
-
+
+
def structure_dump
select_all("SHOW TABLES").inject("") do |structure, table|
structure += select_one("SHOW CREATE TABLE #{table.to_a.first.last}")["Create Table"] + ";\n\n"
@@ -161,11 +185,8 @@ module ActiveRecord
def create_database(name)
execute "CREATE DATABASE #{name}"
end
-
- def quote_string(s)
- Mysql::quote(s)
- end
-
+
+
private
def select(sql, name = nil)
result = nil
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
index 4958094ecb..ba786667cf 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -87,6 +87,22 @@ module ActiveRecord
#
# * <tt>:dbfile</tt> -- Path to the database file.
class SQLiteAdapter < AbstractAdapter
+ def native_database_types
+ {
+ :primary_key => "INTEGER PRIMARY KEY NOT NULL",
+ :string => "VARCHAR(255)",
+ :text => "TEXT",
+ :integer => "INTEGER",
+ :float => "float",
+ :datetime => "DATETIME",
+ :timestamp => "DATETIME",
+ :time => "DATETIME",
+ :date => "DATE",
+ :binary => "BLOB",
+ :boolean => "INTEGER"
+ }
+ end
+
def execute(sql, name = nil)
log(sql, name) { @connection.execute(sql) }
end
@@ -150,6 +166,7 @@ module ActiveRecord
'SQLite'
end
+
protected
def table_structure(table_name)
execute "PRAGMA table_info(#{table_name})"
diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb
new file mode 100644
index 0000000000..8734a0831e
--- /dev/null
+++ b/activerecord/lib/active_record/migration.rb
@@ -0,0 +1,94 @@
+module ActiveRecord
+ class IrreversibleMigration < ActiveRecordError
+ end
+
+ class Migration
+ class << self
+ def up() end
+ def down() end
+
+ private
+ def method_missing(method, *arguments, &block)
+ ActiveRecord::Base.connection.send(method, *arguments, &block)
+ end
+ end
+ end
+
+ class Migrator
+ class << self
+ def up(migrations_path, target_version = nil)
+ new(:up, migrations_path, target_version).migrate
+ end
+
+ def down(migrations_path, target_version = nil)
+ new(:down, migrations_path, target_version).migrate
+ end
+
+ def current_version
+ Base.connection.select_one("SELECT version FROM schema_info")["version"].to_i
+ end
+ end
+
+ def initialize(direction, migrations_path, target_version = nil)
+ @direction, @migrations_path, @target_version = direction, migrations_path, target_version
+ end
+
+ def current_version
+ self.class.current_version
+ end
+
+ def migrate
+ migration_classes do |version, migration_class|
+ Base.logger.info("Reached target version: #{@target_version}") and break if reached_target_version?(version)
+ next if irrelevant_migration?(version)
+
+ Base.logger.info "Migrating to #{migration_class} (#{version})"
+ migration_class.send(@direction)
+
+ set_schema_version(version)
+ end
+ end
+
+ private
+ def migration_classes
+ for migration_file in migration_files
+ load(migration_file)
+ version, name = migration_version_and_name(migration_file)
+ yield version, migration_class(name)
+ end
+ end
+
+ def migration_files
+ files = Dir["#{@migrations_path}/[0-9]*_*.rb"]
+ down? ? files.reverse : files
+ end
+
+ def migration_class(migration_name)
+ migration_name.camelize.constantize
+ end
+
+ def migration_version_and_name(migration_file)
+ return *migration_file.scan(/([0-9]+)_([_a-z0-9]*).rb/).first
+ end
+
+ def set_schema_version(version)
+ Base.connection.update("UPDATE schema_info SET version = #{down? ? version.to_i - 1 : version.to_i}")
+ end
+
+ def up?
+ @direction == :up
+ end
+
+ def down?
+ @direction == :down
+ end
+
+ def reached_target_version?(version)
+ (up? && version.to_i - 1 == @target_version) || (down? && version.to_i == @target_version)
+ end
+
+ def irrelevant_migration?(version)
+ (up? && version.to_i <= current_version) || (down? && version.to_i > current_version)
+ end
+ end
+end \ No newline at end of file