lib/pliny/db_support.rb in pliny-0.30.1 vs lib/pliny/db_support.rb in pliny-0.31.0

- old
+ new

@@ -44,27 +44,45 @@ def create(name) db.run(%{CREATE DATABASE "#{name}"}) end - def migrate(target=nil) - Sequel::Migrator.apply(db, "./db/migrate", target) + def migrate(target = nil) + Sequel::Migrator.apply(db, MIGRATION_DIR, target) end + def version + return 0 unless db.table_exists?(:schema_migrations) + + current = db[:schema_migrations].order(Sequel.desc(:filename)).first + + return 0 unless current + + version = current[:filename].match(Sequel::Migrator::MIGRATION_FILE_PATTERN).captures.first + version ||= 0 + Integer(version) + end + def rollback - return unless db.tables.include?(:schema_migrations) - return unless current = db[:schema_migrations].order(Sequel.desc(:filename)).first + current_version = version + return if current_version.zero? - migrations = Dir["./db/migrate/*.rb"].map { |f| File.basename(f).to_i }.sort - target = 0 # by default, rollback everything - index = migrations.index(current[:filename].to_i) + migrations = Dir["#{MIGRATION_DIR}/*.rb"].map { |f| File.basename(f).to_i }.sort + + target = 0 # by default, rollback everything + index = migrations.index(current_version) if index > 0 target = migrations[index - 1] end - Sequel::Migrator.apply(db, "./db/migrate", target) + Sequel::Migrator.apply(db, MIGRATION_DIR, target) end def disconnect @db.disconnect end + + private + + MIGRATION_DIR = "./db/migrate".freeze + private_constant :MIGRATION_DIR end end