class Pliny::DbSupport

Constants

MIGRATION_DIR

Attributes

db[RW]

Public Class Methods

admin_url(database_url) click to toggle source
# File lib/pliny/db_support.rb, line 8
def self.admin_url(database_url)
  uri = URI.parse(database_url)
  uri.path = "/postgres"
  uri.to_s
end
new(url, sequel_logger) click to toggle source
# File lib/pliny/db_support.rb, line 33
def initialize(url, sequel_logger)
  @db = Sequel.connect(url)
  if sequel_logger
    @db.loggers << sequel_logger
  end
end
run(url, sequel_log_io=StringIO.new) { |instance| ... } click to toggle source
# File lib/pliny/db_support.rb, line 23
def self.run(url, sequel_log_io=StringIO.new)
  logger = Logger.new(sequel_log_io)
  instance = new(url, logger)
  yield instance
  instance.disconnect
  Sequel::DATABASES.delete(instance)
end
setup?(database_url) click to toggle source
# File lib/pliny/db_support.rb, line 14
def self.setup?(database_url)
  @db = Sequel.connect(database_url)
  @db.test_connection
  @db.disconnect
  return true
rescue Sequel::DatabaseConnectionError
  return false
end

Public Instance Methods

create(name) click to toggle source
# File lib/pliny/db_support.rb, line 45
def create(name)
  db.run(%{CREATE DATABASE "#{name}"})
end
disconnect() click to toggle source
# File lib/pliny/db_support.rb, line 207
def disconnect
  @db.disconnect
end
exists?(name) click to toggle source
# File lib/pliny/db_support.rb, line 40
def exists?(name)
  res = db.fetch("SELECT 1 FROM pg_database WHERE datname = ?", name)
  return res.count > 0
end
get_migrations_from_database() click to toggle source
# File lib/pliny/db_support.rb, line 187
def get_migrations_from_database
  return [] unless db.table_exists?(:schema_migrations)

  db[:schema_migrations].order(Sequel.asc(:filename)).select_map(:filename)
end
migrate(target = nil) click to toggle source
# File lib/pliny/db_support.rb, line 49
def migrate(target = nil)
  Sequel::Migrator.apply(db, MIGRATION_DIR, target)
end
rollback() click to toggle source
# File lib/pliny/db_support.rb, line 193
def rollback
  current_version = version
  return if current_version.zero?

  migrations = Dir["#{MIGRATION_DIR}/*.rb"].map { |f| File.basename(f).to_i }.sort

  target = 0 # by default, rollback everything
  index = migrations.index(current_version)
  if index > 0
    target = migrations[index - 1]
  end
  Sequel::Migrator.apply(db, MIGRATION_DIR, target)
end
status() click to toggle source
# File lib/pliny/db_support.rb, line 167
def status
  migrations_in_database = get_migrations_from_database
  migrations_on_disk = Dir["#{MIGRATION_DIR}/*.rb"].map { |f| File.basename(f) }
  total_set_of_migrations = (migrations_in_database | migrations_on_disk).sort_by(&:to_i)

  migration_statuses = total_set_of_migrations.map { |filename|
    status = MigrationStatus.new(filename: filename)
    if migrations_on_disk.include?(filename)
      status.present_on_disk = true
    end

    if migrations_in_database.include?(filename)
      status.present_in_database = true
    end
    status
  }

  MigrationStatusPresenter.new(migration_statuses: migration_statuses).to_s
end
version() click to toggle source
# File lib/pliny/db_support.rb, line 53
def version
  return 0 unless db.table_exists?(:schema_migrations)

  current = db[:schema_migrations].order(Sequel.desc(:filename)).first

  return 0 unless current

  version = current[:filename].match(Sequel::Migrator::MIGRATION_FILE_PATTERN).captures.first
  version ||= 0
  Integer(version)
end