class SimpleMigrator::Migrator
Attributes
connection[R]
Public Class Methods
default_table_name()
click to toggle source
# File lib/simple_migrator/migrator.rb, line 26 def self.default_table_name :simple_migrator_schema_migrations end
new(connection, migration_table_name = nil)
click to toggle source
# File lib/simple_migrator/migrator.rb, line 3 def initialize(connection, migration_table_name = nil) @table_name = migration_table_name @connection = connection ensure_migration_table! end
Public Instance Methods
migrate(name, &block)
click to toggle source
# File lib/simple_migrator/migrator.rb, line 9 def migrate(name, &block) connection.transaction do unless migrated?(name) execute(block) was_migrated(name) end end end
migrated?(migration_name)
click to toggle source
# File lib/simple_migrator/migrator.rb, line 18 def migrated? migration_name migrations_table.where(migration_name: migration_name).any? end
migrations_table()
click to toggle source
# File lib/simple_migrator/migrator.rb, line 22 def migrations_table connection[table_name] end
table_name()
click to toggle source
# File lib/simple_migrator/migrator.rb, line 30 def table_name @table_name || self.class.default_table_name end
Private Instance Methods
ensure_migration_table!()
click to toggle source
# File lib/simple_migrator/migrator.rb, line 42 def ensure_migration_table! connection.create_table?(table_name) do primary_key :id String :migration_name, null: false index [:migration_name], unique: true DateTime :timestamp, null: false end end
execute(migration)
click to toggle source
# File lib/simple_migrator/migrator.rb, line 36 def execute(migration) migration.call(connection) end
was_migrated(migration_name)
click to toggle source
# File lib/simple_migrator/migrator.rb, line 51 def was_migrated(migration_name) migrations_table.insert(timestamp: Time.now.utc, migration_name: migration_name) end