1 #require 'active_record/connection_adapters/abstract/schema_statements'
3 module ::ActiveRecord::ConnectionAdapters::SchemaStatements
4 alias :old_initialize_schema_information :initialize_schema_information
5 def initialize_schema_information
6 # create the normal schema stuff
7 old_initialize_schema_information
9 # create the engines schema stuff.
11 execute "CREATE TABLE #{engine_schema_info_table_name} (engine_name #{type_to_sql(:string)}, version #{type_to_sql(:integer)})"
12 rescue ActiveRecord::StatementInvalid
13 # Schema has been initialized
17 def engine_schema_info_table_name
18 ActiveRecord::Base.wrapped_table_name "engine_schema_info"
25 class EngineMigrator < ActiveRecord::Migrator
27 # We need to be able to set the 'current' engine being migrated.
28 cattr_accessor :current_engine
32 def schema_info_table_name
33 ActiveRecord::Base.wrapped_table_name "engine_schema_info"
37 result = ActiveRecord::Base.connection.select_one("SELECT version FROM #{schema_info_table_name} WHERE engine_name = '#{current_engine.name}'")
39 result["version"].to_i
41 # There probably isn't an entry for this engine in the migration info table.
42 # We need to create that entry, and set the version to 0
43 ActiveRecord::Base.connection.execute("INSERT INTO #{schema_info_table_name} (version, engine_name) VALUES (0,'#{current_engine.name}')")
49 def set_schema_version(version)
50 ActiveRecord::Base.connection.update("UPDATE #{self.class.schema_info_table_name} SET version = #{down? ? version.to_i - 1 : version.to_i} WHERE engine_name = '#{self.current_engine.name}'")