mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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
This commit is contained in:
parent
28a11969ce
commit
eac7cf0b06
9 changed files with 317 additions and 15 deletions
|
@ -47,6 +47,7 @@ require 'active_record/timestamp'
|
|||
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 @@ 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
|
|
@ -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
|
||||
|
|
|
@ -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})"
|
||||
|
|
94
activerecord/lib/active_record/migration.rb
Normal file
94
activerecord/lib/active_record/migration.rb
Normal file
|
@ -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 @@ class CreateTablesTest < Test::Unit::TestCase
|
|||
|
||||
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
|
||||
|
|
9
activerecord/test/fixtures/migrations/1_people_have_last_names.rb
vendored
Normal file
9
activerecord/test/fixtures/migrations/1_people_have_last_names.rb
vendored
Normal file
|
@ -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
|
12
activerecord/test/fixtures/migrations/2_we_need_reminders.rb
vendored
Normal file
12
activerecord/test/fixtures/migrations/2_we_need_reminders.rb
vendored
Normal file
|
@ -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
|
104
activerecord/test/migration_mysql.rb
Normal file
104
activerecord/test/migration_mysql.rb
Normal file
|
@ -0,0 +1,104 @@
|
|||
require 'abstract_unit'
|
||||
require 'fixtures/person'
|
||||
require File.dirname(__FILE__) + '/fixtures/migrations/1_people_have_last_names'
|
||||
require File.dirname(__FILE__) + '/fixtures/migrations/2_we_need_reminders'
|
||||
|
||||
class Reminder < ActiveRecord::Base; end
|
||||
|
||||
class MigrationTest < Test::Unit::TestCase
|
||||
def setup
|
||||
end
|
||||
|
||||
def teardown
|
||||
ActiveRecord::Base.connection.initialize_schema_information
|
||||
ActiveRecord::Base.connection.update "UPDATE schema_info SET version = 0"
|
||||
|
||||
Reminder.connection.drop_table("reminders") rescue nil
|
||||
Reminder.reset_column_information
|
||||
|
||||
Person.connection.remove_column("people", "last_name") rescue nil
|
||||
Person.reset_column_information
|
||||
end
|
||||
|
||||
def test_add_remove_single_field
|
||||
assert !Person.column_methods_hash.include?(:last_name)
|
||||
|
||||
PeopleHaveLastNames.up
|
||||
|
||||
Person.reset_column_information
|
||||
assert Person.column_methods_hash.include?(:last_name)
|
||||
|
||||
PeopleHaveLastNames.down
|
||||
|
||||
Person.reset_column_information
|
||||
assert !Person.column_methods_hash.include?(:last_name)
|
||||
end
|
||||
|
||||
def test_add_table
|
||||
assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }
|
||||
|
||||
WeNeedReminders.up
|
||||
|
||||
assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
|
||||
assert "hello world", Reminder.find_first
|
||||
|
||||
WeNeedReminders.down
|
||||
assert_raises(ActiveRecord::StatementInvalid) { Reminder.find_first }
|
||||
end
|
||||
|
||||
def test_migrator
|
||||
assert !Person.column_methods_hash.include?(:last_name)
|
||||
assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }
|
||||
|
||||
ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/')
|
||||
|
||||
assert_equal 2, ActiveRecord::Migrator.current_version
|
||||
Person.reset_column_information
|
||||
assert Person.column_methods_hash.include?(:last_name)
|
||||
assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
|
||||
assert "hello world", Reminder.find_first
|
||||
|
||||
|
||||
ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/')
|
||||
|
||||
assert_equal 0, ActiveRecord::Migrator.current_version
|
||||
Person.reset_column_information
|
||||
assert !Person.column_methods_hash.include?(:last_name)
|
||||
assert_raises(ActiveRecord::StatementInvalid) { Reminder.find_first }
|
||||
end
|
||||
|
||||
def test_migrator_one_up
|
||||
assert !Person.column_methods_hash.include?(:last_name)
|
||||
assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }
|
||||
|
||||
ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
|
||||
|
||||
Person.reset_column_information
|
||||
assert Person.column_methods_hash.include?(:last_name)
|
||||
assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }
|
||||
|
||||
|
||||
ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 2)
|
||||
|
||||
assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
|
||||
assert "hello world", Reminder.find_first
|
||||
end
|
||||
|
||||
def test_migrator_one_down
|
||||
ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/')
|
||||
|
||||
ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
|
||||
|
||||
Person.reset_column_information
|
||||
assert Person.column_methods_hash.include?(:last_name)
|
||||
assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }
|
||||
end
|
||||
|
||||
def test_migrator_one_up_one_down
|
||||
ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
|
||||
ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 0)
|
||||
|
||||
assert !Person.column_methods_hash.include?(:last_name)
|
||||
assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue