class BazaMigrations::Migration
Public Class Methods
new(args = {})
click to toggle source
# File lib/baza_migrations/migration.rb, line 2 def initialize(args = {}) @db = args.fetch(:db) raise "No database was given" unless @db @changes = [] end
Public Instance Methods
change()
click to toggle source
# File lib/baza_migrations/migration.rb, line 8 def change raise BazaMigrations::Errors::NotImplementedError end
down()
click to toggle source
# File lib/baza_migrations/migration.rb, line 16 def down raise BazaMigrations::Errors::NotImplementedError end
migrate(direction)
click to toggle source
# File lib/baza_migrations/migration.rb, line 20 def migrate(direction) if direction == :up begin up execute_changes rescue BazaMigrations::Errors::NotImplementedError change execute_changes end elsif direction == :down begin down execute_changes rescue BazaMigrations::Errors::NotImplementedError change rollback_changed_changes end else raise "Invalid direction: #{direction}" end @changes = [] end
up()
click to toggle source
# File lib/baza_migrations/migration.rb, line 12 def up raise BazaMigrations::Errors::NotImplementedError end
Protected Instance Methods
execute_changes()
click to toggle source
# File lib/baza_migrations/migration.rb, line 101 def execute_changes @changes.each do |change| change.sql.each do |sql| if sql.is_a?(String) @db.q(sql) elsif sql.respond_to?(:call) sql.call else raise "Didn't know what to do with: #{sql.class.name}" end end end end
new_command(type, *args)
click to toggle source
# File lib/baza_migrations/migration.rb, line 129 def new_command(type, *args) command = BazaMigrations::Commands.const_get(type).new(*args) command.db = @db command.table = @schema_migrations_table command end
rollback_changed_changes()
click to toggle source
# File lib/baza_migrations/migration.rb, line 115 def rollback_changed_changes @changes.reverse_each do |change| change.changed_rollback_sql.each do |sql| if sql.is_a?(String) @db.q(sql) elsif sql.respond_to?(:call) sql.call else raise "Didn't know what to do with: #{sql.class.name}" end end end end
Private Instance Methods
add_column(table_name, column_name, type)
click to toggle source
# File lib/baza_migrations/migration.rb, line 65 def add_column(table_name, column_name, type) @changes << new_command(:AddColumn, table_name, column_name, type) end
add_index(name, columns, args = {})
click to toggle source
# File lib/baza_migrations/migration.rb, line 57 def add_index(name, columns, args = {}) @changes << new_command(:AddIndex, name, columns, args) end
column_exists?(table_name, column_name)
click to toggle source
# File lib/baza_migrations/migration.rb, line 80 def column_exists?(table_name, column_name) @db.tables[table_name].column(column_name) return true rescue Errno::ENOENT return false end
create_table(name) { |command| ... }
click to toggle source
# File lib/baza_migrations/migration.rb, line 46 def create_table(name) command = new_command(:CreateTable, name) @changes << command yield command end
drop_table(name)
click to toggle source
# File lib/baza_migrations/migration.rb, line 53 def drop_table(name) @changes << new_command(:DropTable, name) end
index_exists?(table_name, column_names)
click to toggle source
# File lib/baza_migrations/migration.rb, line 87 def index_exists?(table_name, column_names) column_names = [column_names] unless column_names.is_a?(Array) index_name = "index_#{table_name}_on_#{column_names.join("_and_")}" begin @db.tables[table_name].index(index_name) return true rescue Errno::ENOENT return false end end
remove_column(table_name, column_name)
click to toggle source
# File lib/baza_migrations/migration.rb, line 69 def remove_column(table_name, column_name) @changes << new_command(:RemoveColumn, table_name, column_name) end
remove_index(table_name, index_name)
click to toggle source
# File lib/baza_migrations/migration.rb, line 61 def remove_index(table_name, index_name) @changes << new_command(:RemoveIndex, table_name, index_name) end
table_exists?(table_name)
click to toggle source
# File lib/baza_migrations/migration.rb, line 73 def table_exists?(table_name) @db.tables[table_name] return true rescue Errno::ENOENT return false end