class Communard::Commands

Constants

Status

Attributes

configuration[R]

Public Class Methods

new(configuration) click to toggle source
# File lib/communard/commands.rb, line 17
def initialize(configuration)
  @configuration = configuration
  Sequel.extension :migration, :core_extensions
end

Public Instance Methods

check_current() click to toggle source
# File lib/communard/commands.rb, line 52
def check_current
  Sequel::Migrator.check_current(connection, migrations_dir)
end
create_database() click to toggle source
# File lib/communard/commands.rb, line 22
def create_database
  return if adapter == "sqlite"
  run_without_database("CREATE DATABASE %{database_name}")
rescue Sequel::DatabaseError => error
  if error.message.to_s =~ /database (.*) already exists/
    configuration.default_logger.warn "Database #{$1} already exists."
  else
    raise
  end
end
drop_database() click to toggle source
# File lib/communard/commands.rb, line 33
def drop_database
  if adapter == "sqlite"
    file = database_name
    File.rm(file) if File.exist?(file)
  else
    run_without_database("DROP DATABASE IF EXISTS %{database_name}")
  end
end
dump_schema() click to toggle source
# File lib/communard/commands.rb, line 76
def dump_schema
  conn = configuration.silent_connection
  conn.extension :schema_dumper
  schema = conn.dump_schema_migration(same_db: configuration.same_db)
  schema_file.open("w") { |f| f.puts schema.gsub(/^\s+$/m, "").gsub(/:(\w+)=>/, '\1: ') }
end
load_schema() click to toggle source
# File lib/communard/commands.rb, line 70
def load_schema
  migration = instance_eval(schema_file.read, schema_file.expand_path.to_s, 1)
  conn = configuration.silent_connection
  migration.apply(conn, :up)
end
migrate(target: nil) click to toggle source
# File lib/communard/commands.rb, line 42
def migrate(target: nil)
  target = target.to_i if target.to_s.match?(/\A\d+\z/)
  migrator(target: target, current: nil).run
  dump_schema if target.nil? && configuration.dump_after_migrating
end
migrations_dir() click to toggle source
# File lib/communard/commands.rb, line 119
def migrations_dir
  db_path.join("migrate")
end
rollback(step: 1) click to toggle source
# File lib/communard/commands.rb, line 56
def rollback(step: 1)
  available = applied_migrations
  if available.size == 1
    migrate(target: 0)
  else
    target = available[-step - 1]
    if target
      migrate(target: target.split(/_/, 2).first)
    else
      fail ArgumentError, "Cannot roll back to #{step}"
    end
  end
end
run_without_database(query) click to toggle source
# File lib/communard/commands.rb, line 100
def run_without_database(query)
  opts = options.dup
  database_name = opts.delete("database")
  if adapter == "postgres"
    opts["database"]           = "postgres"
    opts["schema_search_path"] = "public"
  end
  conn = Sequel.connect(opts)
  conn.run(query % { database_name: database_name })
end
schema_file() click to toggle source
# File lib/communard/commands.rb, line 111
def schema_file
  db_path.join("schema.rb")
end
seed() click to toggle source
# File lib/communard/commands.rb, line 48
def seed
  load seeds_file if seeds_file.exist?
end
seeds_file() click to toggle source
# File lib/communard/commands.rb, line 115
def seeds_file
  db_path.join("seeds.rb")
end
status() click to toggle source
# File lib/communard/commands.rb, line 83
def status
  results = Hash.new { |h, k| h[k] = Status.new(k, false, false) }
  available = Pathname.glob(migrations_dir.join("*.rb")).map(&:basename).map(&:to_s)
  available.each { |migration| results[migration].available = true }
  applied_migrations.each { |migration| results[migration].applied = true }

  $stdout.puts
  $stdout.puts "database: #{connection.opts.fetch(:database)}"
  $stdout.puts
  $stdout.puts " Status   Migration ID    Migration Name"
  $stdout.puts "--------------------------------------------------"
  results.values.sort.each do |result|
    $stdout.puts "  %-7s %-15s %s" % [ result.status, result.id, result.name ]
  end
  $stdout.puts
end

Private Instance Methods

applied_migrations() click to toggle source
# File lib/communard/commands.rb, line 125
def applied_migrations
  available = Pathname.glob(migrations_dir.join("*.rb")).map(&:basename).map(&:to_s)
  m = migrator(allow_missing_migration_files: true)
  if m.is_a?(Sequel::IntegerMigrator)
    available.select { |f| f.split("_", 2).first.to_i <= m.current }
  else
    m.applied_migrations
  end
end
migrator(opts = {}) click to toggle source
# File lib/communard/commands.rb, line 135
def migrator(opts = {})
  migrator = Sequel::Migrator.migrator_class(migrations_dir)
  migrator.new(connection, migrations_dir, opts)
end