Class: Zena::Migrator
- Inherits:
-
ActiveRecord::Migrator
- Object
- ActiveRecord::Migrator
- Zena::Migrator
- Defined in:
- lib/zena/migrator.rb
Class Method Summary collapse
- .bricks_info_table_name ⇒ Object
- .current_version(brick_name) ⇒ Object
- .down(migrations_path, brick_name, target_version = nil) ⇒ Object
- .get_all_versions(brick_name) ⇒ Object
- .init_bricks_migration_table ⇒ Object
- .migrate(migrations_path, brick_name, target_version = nil) ⇒ Object
- .old_bricks_info_table_name ⇒ Object
- .up(migrations_path, brick_name, target_version = nil) ⇒ Object
Instance Method Summary collapse
-
#initialize(direction, migrations_path, brick_name, target_version = nil) ⇒ Migrator
constructor
A new instance of Migrator.
- #migrated ⇒ Object
Constructor Details
#initialize(direction, migrations_path, brick_name, target_version = nil) ⇒ Migrator
Returns a new instance of Migrator.
84 85 86 87 88 |
# File 'lib/zena/migrator.rb', line 84 def initialize(direction, migrations_path, brick_name, target_version = nil) raise StandardError.new("This database does not yet support migrations") unless ActiveRecord::Base.connection.supports_migrations? self.class.init_bricks_migration_table @direction, @migrations_path, @brick_name, @target_version = direction, migrations_path, brick_name, target_version end |
Class Method Details
.bricks_info_table_name ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/zena/migrator.rb', line 25 def bricks_info_table_name if ActiveRecord::Base.connection.tables.include?(old_bricks_info_table_name) old_bricks_info_table_name else schema_migrations_table_name end end |
.current_version(brick_name) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/zena/migrator.rb', line 37 def current_version(brick_name) sm_table = bricks_info_table_name if ActiveRecord::Base.connection.table_exists?(sm_table) get_all_versions(brick_name).max || 0 else 0 end end |
.down(migrations_path, brick_name, target_version = nil) ⇒ Object
17 18 19 |
# File 'lib/zena/migrator.rb', line 17 def down(migrations_path, brick_name, target_version = nil) self.new(:down, migrations_path, brick_name, target_version).migrate end |
.get_all_versions(brick_name) ⇒ Object
33 34 35 |
# File 'lib/zena/migrator.rb', line 33 def get_all_versions(brick_name) ActiveRecord::Base.connection.select_values("SELECT version FROM #{bricks_info_table_name} WHERE brick #{brick_name ? '=' : 'IS'} #{ActiveRecord::Base.connection.quote(brick_name)}").map(&:to_i).sort end |
.init_bricks_migration_table ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/zena/migrator.rb', line 46 def init_bricks_migration_table # Migrate from 0.11 (rails 1.2.3) to 0.13 (rails 2.3.4) connection = ActiveRecord::Base.connection sm_table = ActiveRecord::Migrator.schema_migrations_table_name si_table = ActiveRecord::Base.table_name_prefix + 'schema_info' + ActiveRecord::Base.table_name_suffix if !connection.tables.include?(sm_table) && connection.tables.include?(old_bricks_info_table_name) v_brick, v_schema = 0, 0 connection.select_all("SELECT version FROM #{old_bricks_info_table_name} WHERE brick = 'zena'", "Bricks_info fix").each do |record| v_brick = record['version'].to_i end if v_brick > 0 connection.select_all("SELECT version FROM #{si_table}", "Bricks_info fix").each do |record| v_schema = record['version'].to_i end if v_schema < v_brick connection.execute "UPDATE schema_info SET version = #{v_brick}" connection.execute "DROP TABLE #{old_bricks_info_table_name}" connection.initialize_schema_migrations_table end end end if !connection.tables.include?(sm_table) connection.initialize_schema_migrations_table end unless ActiveRecord::Base.connection.columns(schema_migrations_table_name).map{|c| c.name}.include?('brick') ActiveRecord::Migration.announce("adding 'brick' scope to schema_migrations") ActiveRecord::Migration.add_column schema_migrations_table_name, :brick, :string ActiveRecord::Migration.remove_index schema_migrations_table_name, :name => 'unique_schema_migrations' ActiveRecord::Migration.add_index schema_migrations_table_name, [:brick,:version], :name => 'unique_schema_migrations', :unique => true end end |
.migrate(migrations_path, brick_name, target_version = nil) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/zena/migrator.rb', line 5 def migrate(migrations_path, brick_name, target_version = nil) case when target_version.nil? then up(migrations_path, brick_name, target_version) when current_version(brick_name) > target_version then down(migrations_path, brick_name, target_version) else up(migrations_path, brick_name, target_version) end end |
.old_bricks_info_table_name ⇒ Object
21 22 23 |
# File 'lib/zena/migrator.rb', line 21 def old_bricks_info_table_name ActiveRecord::Base.table_name_prefix + "bricks_info" + ActiveRecord::Base.table_name_suffix end |
.up(migrations_path, brick_name, target_version = nil) ⇒ Object
13 14 15 |
# File 'lib/zena/migrator.rb', line 13 def up(migrations_path, brick_name, target_version = nil) self.new(:up, migrations_path, brick_name, target_version).migrate end |
Instance Method Details
#migrated ⇒ Object
90 91 92 |
# File 'lib/zena/migrator.rb', line 90 def migrated @migrated_versions ||= self.class.get_all_versions(@brick_name) end |