Permalink
Browse files

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
  • Loading branch information...
dhh committed Mar 1, 2005
1 parent 28a1196 commit eac7cf0b0608132673220d9045b8ff51dc0804e1
@@ -47,6 +47,7 @@
require 'active_record/acts/list'
require 'active_record/acts/tree'
require 'active_record/locking'
+require 'active_record/migration'
ActiveRecord::Base.class_eval do
include ActiveRecord::Validations
@@ -356,6 +356,38 @@ def add_limit!(sql, limit)
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 @@ def format_log_entry(message, dump = nil)
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
@@ -63,12 +63,33 @@ class MysqlAdapter < AbstractAdapter
"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 @@ def update(sql, name = nil)
alias_method :delete, :update
+
def begin_db_transaction
begin
execute "BEGIN"
@@ -134,15 +156,17 @@ def rollback_db_transaction
# 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 @@ def drop_database(name)
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
@@ -87,6 +87,22 @@ def binary_to_string(value)
#
# * <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 @@ def adapter_name()
'SQLite'
end
+
protected
def table_structure(table_name)
execute "PRAGMA table_info(#{table_name})"
@@ -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
@@ -45,13 +45,13 @@ def run_sql_file(connection, path)
def test_table_creation
adapter_name = ActiveRecord::Base.connection.adapter_name.downcase
- run_sql_file ActiveRecord::Base.connection, "test/fixtures/db_definitions/" + adapter_name + ".drop.sql"
- run_sql_file ActiveRecord::Base.connection, "test/fixtures/db_definitions/" + adapter_name + ".sql"
+ run_sql_file ActiveRecord::Base.connection, "#{File.dirname(__FILE__)}/fixtures/db_definitions/" + adapter_name + ".drop.sql"
+ run_sql_file ActiveRecord::Base.connection, "#{File.dirname(__FILE__)}/fixtures/db_definitions/" + adapter_name + ".sql"
# Now do the same thing with the connection used by multiple_db_test.rb
adapter_name = Course.retrieve_connection.adapter_name.downcase
- run_sql_file Course.retrieve_connection, "test/fixtures/db_definitions/" + adapter_name + "2.drop.sql"
- run_sql_file Course.retrieve_connection, "test/fixtures/db_definitions/" + adapter_name + "2.sql"
+ run_sql_file Course.retrieve_connection, "#{File.dirname(__FILE__)}/fixtures/db_definitions/" + adapter_name + "2.drop.sql"
+ run_sql_file Course.retrieve_connection, "#{File.dirname(__FILE__)}/fixtures/db_definitions/" + adapter_name + "2.sql"
assert_equal 1,1
end
@@ -0,0 +1,9 @@
+class PeopleHaveLastNames < ActiveRecord::Migration
+ def self.up
+ add_column "people", "last_name", :string
+ end
+
+ def self.down
+ remove_column "people", "last_name"
+ end
+end
@@ -0,0 +1,12 @@
+class WeNeedReminders < ActiveRecord::Migration
+ def self.up
+ create_table("reminders") do |t|
+ t.column :content, :text
+ t.column :remind_at, :datetime
+ end
+ end
+
+ def self.down
+ drop_table "reminders"
+ end
+end
Oops, something went wrong.

0 comments on commit eac7cf0

Please sign in to comment.