Sha256: 53837c633c10a9f1753584a84cec951fc388b3acb5b4609cbf8afe0e75b463b7

Contents?: true

Size: 1.5 KB

Versions: 27

Compression:

Stored size: 1.5 KB

Contents

require 'rubygems'
require 'yaml'
require 'json'
require 'sequel'
require_relative '../txcatcher/utils/hash_string_to_sym_keys'
require_relative '../txcatcher/config'
require_relative '../txcatcher/initializer'

Sequel.extension :migration

namespace :db do

  task :environment do
    include TxCatcher::Initializer
    ConfigFile.set!
    create_config_files
    read_config_file
    connect_to_db
  end

  desc "Migrates the database"
  task :migrate, [:step] => :environment do |t, args|
    target = args[:step] && (step = args[:step].to_i) > 0 ?
               current_migration_version + step : nil

    Sequel::Migrator.run(TxCatcher.db_connection, MIGRATIONS_ROOT, target: target)
    puts "Migrated DB to version #{current_migration_version}"
    dump_schema
  end

  desc "Rollbacks database migrations"
  task :rollback, [:step] => :environment do |t, args|
    target = args[:step] && (step = args[:step].to_i) > 0 ?
      current_migration_version - step : current_migration_version - 1

    Sequel::Migrator.run(TxCatcher.db_connection, MIGRATIONS_ROOT, target: target)
    puts "Rolled back DB to version #{current_migration_version}"
    dump_schema
  end

  def current_migration_version
    db = TxCatcher.db_connection
    Sequel::Migrator.migrator_class(MIGRATIONS_ROOT).new(db, MIGRATIONS_ROOT, {}).current
  end

  def dump_schema
    TxCatcher.db_connection.extension :schema_dumper
    open('db/schema.rb', 'w') do |f|
      f.puts TxCatcher.db_connection.dump_schema_migration(same_db: false)
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
txcatcher-0.1.6 lib/tasks/db.rake
txcatcher-0.1.5 lib/tasks/db.rake
txcatcher-0.1.4 lib/tasks/db.rake
txcatcher-0.1.3 lib/tasks/db.rake
txcatcher-0.1.2 lib/tasks/db.rake
txcatcher-0.1.1 lib/tasks/db.rake
txcatcher-0.1.0 lib/tasks/db.rake