Sha256: 519a6763e410023892d3ee81ba5a6f89560ca67927e64ee6195efdbdd436e72d

Contents?: true

Size: 1.54 KB

Versions: 24

Compression:

Stored size: 1.54 KB

Contents

require 'rubygems'
require 'yaml'
require 'json'
require 'sequel'
require_relative '../txcatcher/utils/hash_string_to_sym_keys'
require_relative '../txcatcher/logger'
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

24 entries across 24 versions & 1 rubygems

Version Path
txcatcher-0.2.14 lib/tasks/db.rake
txcatcher-0.2.12 lib/tasks/db.rake
txcatcher-0.2.11 lib/tasks/db.rake
txcatcher-0.2.10 lib/tasks/db.rake
txcatcher-0.2.9 lib/tasks/db.rake
txcatcher-0.2.8 lib/tasks/db.rake
txcatcher-0.2.6 lib/tasks/db.rake
txcatcher-0.2.4 lib/tasks/db.rake
txcatcher-0.2.3 lib/tasks/db.rake
txcatcher-0.2.2 lib/tasks/db.rake
txcatcher-0.2.1 lib/tasks/db.rake
txcatcher-0.2.0 lib/tasks/db.rake
txcatcher-0.1.100 lib/tasks/db.rake
txcatcher-0.1.99 lib/tasks/db.rake
txcatcher-0.1.98 lib/tasks/db.rake
txcatcher-0.1.97 lib/tasks/db.rake
txcatcher-0.1.96 lib/tasks/db.rake
txcatcher-0.1.95 lib/tasks/db.rake
txcatcher-0.1.94 lib/tasks/db.rake
txcatcher-0.1.93 lib/tasks/db.rake