Sha256: eddf63f895bfe917750a45ae07a45b272c83b9f661d06db773675092805ca779

Contents?: true

Size: 1.7 KB

Versions: 40

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

require "logger"

# rubocop:disable Layout/ClassStructure
module Sequel
  class TimestampMigrator
    # Rollback a migration
    def undo(version)
      path = files.find { |file| migration_version_from_file(get_filename(file)) == version }
      error!("Migration #{version} does not exist in the filesystem") unless path

      filename = get_filename(path)
      error!("Migration #{version} is not applied") unless applied_migrations.include?(filename)

      migration = get_migration(path)

      time = Time.now
      db.log_info("Undoing migration #{filename}")

      checked_transaction(migration) do
        migration.apply(db, :down)
        ds.filter(column => filename).delete
      end

      elapsed = format("%<time>0.6f", time: Time.now - time)
      db.log_info("Finished undoing migration #{filename}, took #{elapsed} seconds")
    end

    module TimestampMigratorLogger
      # Setup the logger
      def run
        db.loggers << Logger.new($stdout, level: :info)
        level = db.sql_log_level
        db.sql_log_level = :debug
        db.log_info("Begin applying migrations")
        super
      ensure
        db.sql_log_level = level
        db.loggers.pop
      end
    end

    Sequel::TimestampMigrator.prepend(TimestampMigratorLogger)

    private

    def get_migration(path)
      migration = load_migration_file(path)

      return migration if Gem::Version.new(Sequel.version) >= Gem::Version.new("5.6")
      # :nocov:
      Migration.descendants.last
      # :nocov:
    end

    def get_filename(path)
      File.basename(path).downcase
    end

    def error!(message)
      raise Sequel::Migrator::Error, message
    end
  end
end
# rubocop:enable Layout/ClassStructure

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
umbrellio-sequel-plugins-0.17.0 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.16.1 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.16.0.239 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.16.0.238 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.16.0.235 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.16.0.234 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.16.0.233 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.16.0 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.16.0.211 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.15.0.198 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.14.0.192 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.14.0.189 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.14.0.188 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.14.0.187 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.14.0 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.13.0.185 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.13.0.172 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.13.0 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.12.0.169 lib/sequel/timestamp_migrator_undo_extension.rb
umbrellio-sequel-plugins-0.12.0 lib/sequel/timestamp_migrator_undo_extension.rb