changed properties and the name of hyperchad to selectricity
[selectricity-live] / vendor / plugins / engines / lib / engines / migration_extensions.rb
1 #require 'active_record/connection_adapters/abstract/schema_statements'
2
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
8     
9     # create the engines schema stuff.    
10     begin
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
14     end
15   end
16
17   def engine_schema_info_table_name
18     ActiveRecord::Base.wrapped_table_name "engine_schema_info"
19   end
20 end
21
22
23 require 'breakpoint'
24 module ::Engines
25   class EngineMigrator < ActiveRecord::Migrator
26
27     # We need to be able to set the 'current' engine being migrated.
28     cattr_accessor :current_engine
29
30     class << self
31
32       def schema_info_table_name
33         ActiveRecord::Base.wrapped_table_name "engine_schema_info"
34       end
35
36       def current_version
37         result = ActiveRecord::Base.connection.select_one("SELECT version FROM #{schema_info_table_name} WHERE engine_name = '#{current_engine.name}'")
38         if result
39           result["version"].to_i
40         else
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}')")      
44           0
45         end
46       end    
47     end
48
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}'")
51     end
52   end
53 end

Benjamin Mako Hill || Want to submit a patch?