Sha256: b8e7b4352286407432303cba6689da15502905e120d75d0b6d7fb788fb68a35e

Contents?: true

Size: 1.47 KB

Versions: 6

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

require "snowpack/cli/application/command"
require_relative "structure/dump"
require_relative "utils/database"

module Snowpack
  module CLI
    module Application
      module Commands
        module DB
          class Rollback < Command
            desc "Rollback database to a previous migration"

            option :target, desc: "Target migration number", aliases: ["-t"]

            def call(target: nil, **)
              migration_code, migration_name = find_migration(target)

              measure "database #{database.name} rolled back to #{migration_name}" do
                database.gateway.run_migrations(target: Integer(migration_code))
              end

              run_command Structure::Dump
            end

            private

            def database
              @database ||= Utils::Database.for_application(application)
            end

            def find_migration(code)
              migration = database.applied_migrations.yield_self { |migrations|
                if code
                  migrations.detect { |m| m.split("_").first == code }
                else
                  migrations.last
                end
              }

              migration_code = code || migration.split("_").first
              migration_name = File.basename(migration, ".*")

              [migration_code, migration_name]
            end
          end
        end

        register "db rollback", Commands::DB::Rollback
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
snowpack-1.0.0.alpha7 lib/snowpack/cli/application/commands/db/rollback.rb
snowpack-1.0.0.alpha6 lib/snowpack/cli/application/commands/db/rollback.rb
snowpack-1.0.0.alpha5 lib/snowpack/cli/application/commands/db/rollback.rb
snowpack-1.0.0.alpha4 lib/snowpack/cli/application/commands/db/rollback.rb
snowpack-1.0.0.alpha3 lib/snowpack/cli/application/commands/db/rollback.rb
snowpack-1.0.0.alpha2 lib/snowpack/cli/application/commands/db/rollback.rb