lib/hanami/model/migrator.rb in hanami-model-1.1.0.beta1 vs lib/hanami/model/migrator.rb in hanami-model-1.1.0.beta2
- old
+ new
@@ -87,10 +87,11 @@
#
# @since 0.4.0
#
# @see Hanami::Model::Configuration#adapter
# @see Hanami::Model::Configuration#migrations
+ # @see Hanami::Model::Configuration#rollback
#
# @example Migrate Up
# require 'hanami/model'
# require 'hanami/model/migrator'
#
@@ -114,18 +115,54 @@
# end
#
# # Reads all files from "db/migrations" and apply them
# Hanami::Model::Migrator.migrate
#
- # # Migrate to a specifiy version
+ # # Migrate to a specific version
# Hanami::Model::Migrator.migrate(version: "20150610133853")
#
# NOTE: Class level interface SHOULD be removed in Hanami 2.0
def self.migrate(version: nil)
new.migrate(version: version)
end
+ # Rollback database schema
+ #
+ # @param steps [Number,NilClass] number of versions to rollback
+ #
+ # @raise [Hanami::Model::MigrationError] if an error occurs
+ #
+ # @since 1.1.0
+ #
+ # @see Hanami::Model::Configuration#adapter
+ # @see Hanami::Model::Configuration#migrations
+ # @see Hanami::Model::Configuration#migrate
+ #
+ # @example Rollback
+ # require 'hanami/model'
+ # require 'hanami/model/migrator'
+ #
+ # Hanami::Model.configure do
+ # # ...
+ # adapter :sql, 'postgres://localhost/foo'
+ # migrations 'db/migrations'
+ # end
+ #
+ # # Reads all files from "db/migrations" and apply them
+ # Hanami::Model::Migrator.migrate
+ #
+ # # By default only rollback one version
+ # Hanami::Model::Migrator.rollback
+ #
+ # # Use a hash passing a number of versions to rollback, it will rollbacks those versions
+ # Hanami::Model::Migrator.rollback(versions: 2)
+ #
+ # NOTE: Class level interface SHOULD be removed in Hanami 2.0
+ def self.rollback(steps: 1)
+ new.rollback(steps: steps)
+ end
+
# Migrate, dump schema, delete migrations.
#
# This is an experimental feature.
# It may change or be removed in the future.
#
@@ -187,11 +224,11 @@
# # ...
# adapter :sql, 'postgres://localhost/foo'
# migrations 'db/migrations'
# end
#
- # Hanami::Model::Migrator.prepare # => creates `foo' and run migrations
+ # Hanami::Model::Migrator.prepare # => creates `foo' and runs migrations
#
# @example Prepare Database (with schema dump)
# require 'hanami/model'
# require 'hanami/model/migrator'
#
@@ -264,10 +301,18 @@
# @see Hanami::Model::Migrator.migrate
def migrate(version: nil)
adapter.migrate(migrations, version) if migrations?
end
+ # @since 1.1.0
+ # @api private
+ #
+ # @see Hanami::Model::Migrator.rollback
+ def rollback(steps: 1)
+ adapter.rollback(migrations, steps.abs) if migrations?
+ end
+
# @since 0.7.0
# @api private
#
# @see Hanami::Model::Migrator.apply
def apply
@@ -280,11 +325,11 @@
# @api private
#
# @see Hanami::Model::Migrator.prepare
def prepare
drop
- rescue
+ rescue # rubocop:disable Lint/HandleExceptions
ensure
create
adapter.load
migrate
end
@@ -295,18 +340,18 @@
# @see Hanami::Model::Migrator.version
def version
adapter.version
end
- private
-
# Hanami::Model configuration
#
# @since 0.4.0
# @api private
def self.configuration
Model.configuration
end
+
+ private
# @since 0.7.0
# @api private
attr_reader :configuration