Sha256: a09c36553e14fa424c421e48112e0a6a0ac7c430a1cf95825eb34f3a09adf279

Contents?: true

Size: 1.49 KB

Versions: 14

Compression:

Stored size: 1.49 KB

Contents

require "hanami/utils/kernel"

module Hanami
  class CLI
    module Commands
      module Db
        # @since 1.1.0
        # @api private
        class Rollback < Command
          requires "model.sql"

          desc "Rollback migrations"

          argument :steps, desc: "Number of steps to rollback the database", default: 1

          example [
            "  # Rollbacks latest migration",
            "2 # Rollbacks last two migrations"
          ]

          # @since 1.1.0
          # @api private
          def call(steps:, **)
            context = Context.new(steps: steps)
            context = assert_valid_steps!(context)

            rollback_database(context)
          end

          private

          # @since 1.1.0
          # @api private
          def assert_valid_steps!(context)
            context = context.with(steps: Utils::Kernel.Integer(context.steps.to_s))
            handle_error(context) unless context.steps.positive?
            context
          rescue TypeError
            handle_error(context)
          end

          # @since 1.1.0
          # @api private
          def rollback_database(context)
            require "hanami/model/migrator"
            Hanami::Model::Migrator.rollback(steps: context.steps)
          end

          # @since 1.1.0
          # @api private
          def handle_error(context)
            warn "the number of steps must be a positive integer (you entered `#{context.steps}')."
            exit(1)
          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
hanami-1.3.5 lib/hanami/cli/commands/db/rollback.rb
hanami-1.3.4 lib/hanami/cli/commands/db/rollback.rb
hanami-1.3.3 lib/hanami/cli/commands/db/rollback.rb
hanami-1.3.2 lib/hanami/cli/commands/db/rollback.rb
hanami-1.3.1 lib/hanami/cli/commands/db/rollback.rb
hanami-1.3.0 lib/hanami/cli/commands/db/rollback.rb
hanami-1.3.0.beta1 lib/hanami/cli/commands/db/rollback.rb
hanami-1.2.0 lib/hanami/cli/commands/db/rollback.rb
hanami-1.2.0.rc2 lib/hanami/cli/commands/db/rollback.rb
hanami-1.2.0.rc1 lib/hanami/cli/commands/db/rollback.rb
hanami-1.2.0.beta2 lib/hanami/cli/commands/db/rollback.rb
hanami-1.2.0.beta1 lib/hanami/cli/commands/db/rollback.rb
hanami-1.1.1 lib/hanami/cli/commands/db/rollback.rb
hanami-1.1.0 lib/hanami/cli/commands/db/rollback.rb