lib/hanami/model/migrator/adapter.rb in hanami-model-1.1.0.beta1 vs lib/hanami/model/migrator/adapter.rb in hanami-model-1.1.0.beta2
- old
+ new
@@ -82,10 +82,21 @@
Sequel::Migrator.run(connection.raw, migrations, target: version, allow_missing_migration_files: true)
rescue Sequel::Migrator::Error => e
raise MigrationError.new(e.message)
end
+ # @since 1.1.0
+ # @api private
+ def rollback(migrations, steps)
+ table = migrations_table_dataset
+ version = version_to_rollback(table, steps)
+
+ Sequel::Migrator.run(connection.raw, migrations, target: version, allow_missing_migration_files: true)
+ rescue Sequel::Migrator::Error => e
+ raise MigrationError.new(e.message)
+ end
+
# Load database schema.
# It must be implemented by subclasses.
#
# @since 0.4.0
# @api private
@@ -98,20 +109,40 @@
# Database version.
#
# @since 0.4.0
# @api private
def version
- table = connection.table(MIGRATIONS_TABLE)
+ table = migrations_table_dataset
return if table.nil?
- if record = table.order(MIGRATIONS_TABLE_VERSION_COLUMN).last
- record.fetch(MIGRATIONS_TABLE_VERSION_COLUMN).scan(/\A[\d]{14}/).first.to_s
- end
+ record = table.order(MIGRATIONS_TABLE_VERSION_COLUMN).last
+ return if record.nil?
+
+ record.fetch(MIGRATIONS_TABLE_VERSION_COLUMN).scan(MIGRATIONS_FILE_NAME_PATTERN).first.to_s
end
private
+ # @since 1.1.0
+ # @api private
+ MIGRATIONS_FILE_NAME_PATTERN = /\A[\d]{14}/
+
+ # @since 1.1.0
+ # @api private
+ def version_to_rollback(table, steps)
+ record = table.order(Sequel.desc(MIGRATIONS_TABLE_VERSION_COLUMN)).all[steps]
+ return 0 unless record
+
+ record.fetch(MIGRATIONS_TABLE_VERSION_COLUMN).scan(MIGRATIONS_FILE_NAME_PATTERN).first.to_i
+ end
+
+ # @since 1.1.0
+ # @api private
+ def migrations_table_dataset
+ connection.table(MIGRATIONS_TABLE)
+ end
+
# @since 0.5.0
# @api private
attr_reader :connection
# @since 0.4.0
@@ -121,13 +152,13 @@
end
# Returns a database connection
#
# Given a DB connection URI we can connect to a specific database or not, we need this when creating
- # or droping a database. Important to notice that we can't always open a _global_ DB connection,
+ # or dropping a database. Important to notice that we can't always open a _global_ DB connection,
# because most of the times application's DB user has no rights to do so.
#
- # @param global [Boolean] determine whether or not a connection should specify an database.
+ # @param global [Boolean] determine whether or not a connection should specify a database.
#
# @since 0.5.0
# @api private
def new_connection(global: false)
uri = global ? connection.global_uri : connection.uri