Skip to content

Commit

Permalink
Make migrations honor table name prefixes and suffixes.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2352 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
Marcel Molina committed Sep 26, 2005
1 parent 0f276de commit 1465f9c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN* *SVN*


* Make migrations honor table name prefixes and suffixes. #2298 [Jakob S, Marcel Molina]

* Correct and optimize PostgreSQL bytea escaping. #1745, #1837 [dave@cherryville.org, ken@miriamtech.com, bellis@deepthought.org] * Correct and optimize PostgreSQL bytea escaping. #1745, #1837 [dave@cherryville.org, ken@miriamtech.com, bellis@deepthought.org]


* Fixtures should only reset a PostgreSQL sequence if it corresponds to an integer primary key named id. #1749 [chris@chrisbrinker.com] * Fixtures should only reset a PostgreSQL sequence if it corresponds to an integer primary key named id. #1749 [chris@chrisbrinker.com]
Expand Down
Expand Up @@ -102,8 +102,8 @@ def structure_dump #:nodoc:


def initialize_schema_information #:nodoc: def initialize_schema_information #:nodoc:
begin begin
execute "CREATE TABLE schema_info (version #{type_to_sql(:integer)})" execute "CREATE TABLE #{ActiveRecord::Migrator.schema_info_table_name} (version #{type_to_sql(:integer)})"
execute "INSERT INTO schema_info (version) VALUES(0)" execute "INSERT INTO #{ActiveRecord::Migrator.schema_info_table_name} (version) VALUES(0)"
rescue ActiveRecord::StatementInvalid rescue ActiveRecord::StatementInvalid
# Schema has been intialized # Schema has been intialized
end end
Expand All @@ -112,7 +112,7 @@ def initialize_schema_information #:nodoc:
def dump_schema_information #:nodoc: def dump_schema_information #:nodoc:
begin begin
if (current_schema = ActiveRecord::Migrator.current_version) > 0 if (current_schema = ActiveRecord::Migrator.current_version) > 0
return "INSERT INTO schema_info (version) VALUES (#{current_schema});" return "INSERT INTO #{ActiveRecord::Migrator.schema_info_table_name} (version) VALUES (#{current_schema});"
end end
rescue ActiveRecord::StatementInvalid rescue ActiveRecord::StatementInvalid
# No Schema Info # No Schema Info
Expand Down
15 changes: 13 additions & 2 deletions activerecord/lib/active_record/migration.rb
Expand Up @@ -142,6 +142,7 @@ def down() end


private private
def method_missing(method, *arguments, &block) def method_missing(method, *arguments, &block)
arguments[0] = Migrator.proper_table_name(arguments.first) unless arguments.empty?
ActiveRecord::Base.connection.send(method, *arguments, &block) ActiveRecord::Base.connection.send(method, *arguments, &block)
end end
end end
Expand Down Expand Up @@ -169,9 +170,19 @@ def down(migrations_path, target_version = nil)
self.new(:down, migrations_path, target_version).migrate self.new(:down, migrations_path, target_version).migrate
end end


def schema_info_table_name
Base.table_name_prefix + "schema_info" + Base.table_name_suffix
end

def current_version def current_version
(Base.connection.select_one("SELECT version FROM schema_info") || {"version" => 0})["version"].to_i (Base.connection.select_one("SELECT version FROM #{schema_info_table_name}") || {"version" => 0})["version"].to_i
end end

def proper_table_name(name)
# Use the ActiveRecord objects own table_name, or pre/suffix from ActiveRecord::Base if name is a symbol/string
name.table_name rescue "#{ActiveRecord::Base.table_name_prefix}#{name}#{ActiveRecord::Base.table_name_suffix}"
end

end end


def initialize(direction, migrations_path, target_version = nil) def initialize(direction, migrations_path, target_version = nil)
Expand Down Expand Up @@ -222,7 +233,7 @@ def migration_version_and_name(migration_file)
end end


def set_schema_version(version) def set_schema_version(version)
Base.connection.update("UPDATE schema_info SET version = #{down? ? version.to_i - 1 : version.to_i}") Base.connection.update("UPDATE #{self.class.schema_info_table_name} SET version = #{down? ? version.to_i - 1 : version.to_i}")
end end


def up? def up?
Expand Down
55 changes: 53 additions & 2 deletions activerecord/test/migration_test.rb
Expand Up @@ -15,7 +15,7 @@ def setup


def teardown def teardown
ActiveRecord::Base.connection.initialize_schema_information ActiveRecord::Base.connection.initialize_schema_information
ActiveRecord::Base.connection.update "UPDATE schema_info SET version = 0" ActiveRecord::Base.connection.update "UPDATE #{ActiveRecord::Migrator.schema_info_table_name} SET version = 0"


Reminder.connection.drop_table("reminders") rescue nil Reminder.connection.drop_table("reminders") rescue nil
Reminder.connection.drop_table("people_reminders") rescue nil Reminder.connection.drop_table("people_reminders") rescue nil
Expand Down Expand Up @@ -262,5 +262,56 @@ def test_migrator_going_down_due_to_version_target
assert Reminder.create("content" => "hello world", "remind_at" => Time.now) assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
assert_equal "hello world", Reminder.find(:first).content assert_equal "hello world", Reminder.find(:first).content
end end
end
def test_schema_info_table_name
ActiveRecord::Base.table_name_prefix = "prefix_"
ActiveRecord::Base.table_name_suffix = "_suffix"
assert_equal "prefix_schema_info_suffix", ActiveRecord::Migrator.schema_info_table_name
ActiveRecord::Base.table_name_prefix = ""
ActiveRecord::Base.table_name_suffix = ""
assert_equal "schema_info", ActiveRecord::Migrator.schema_info_table_name
end

def test_proper_table_name
assert_equal "table", ActiveRecord::Migrator.proper_table_name('table')
assert_equal "table", ActiveRecord::Migrator.proper_table_name(:table)
assert_equal "reminders", ActiveRecord::Migrator.proper_table_name(Reminder)
assert_equal Reminder.table_name, ActiveRecord::Migrator.proper_table_name(Reminder)

# Use the model's own prefix/suffix if a model is given
ActiveRecord::Base.table_name_prefix = "ARprefix_"
ActiveRecord::Base.table_name_suffix = "_ARsuffix"
Reminder.table_name_prefix = 'prefix_'
Reminder.table_name_suffix = '_suffix'
assert_equal "prefix_reminders_suffix", ActiveRecord::Migrator.proper_table_name(Reminder)
Reminder.table_name_prefix = ''
Reminder.table_name_suffix = ''

# Use AR::Base's prefix/suffix if string or symbol is given
ActiveRecord::Base.table_name_prefix = "prefix_"
ActiveRecord::Base.table_name_suffix = "_suffix"
assert_equal "prefix_table_suffix", ActiveRecord::Migrator.proper_table_name('table')
assert_equal "prefix_table_suffix", ActiveRecord::Migrator.proper_table_name(:table)
ActiveRecord::Base.table_name_prefix = ""
ActiveRecord::Base.table_name_suffix = ""
end

def test_add_drop_table_with_prefix_and_suffix
assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }

ActiveRecord::Base.table_name_prefix = 'prefix_'
ActiveRecord::Base.table_name_suffix = '_suffix'
WeNeedReminders.up

assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
assert_equal "hello world", Reminder.find(:first).content

WeNeedReminders.down
assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
ActiveRecord::Base.table_name_prefix = ''
ActiveRecord::Base.table_name_suffix = ''
end

end

end end

0 comments on commit 1465f9c

Please sign in to comment.