Module: Hanami::Sequel::Command

Defined in:
lib/hanami/sequel/commands/drop.rb,
lib/hanami/sequel/commands/seed.rb,
lib/hanami/sequel/commands/create.rb,
lib/hanami/sequel/commands/migrate.rb

Class Method Summary collapse

Class Method Details

.createObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hanami/sequel/commands/create.rb', line 12

def self.create
  env = Hanami::Environment.new
  if env.environment == 'production'
    raise 'Command unavailable in the production environment.'
  end

  db_url = ENV.fetch('DATABASE_URL')
  db_conn, _, db_name = db_url.rpartition('/')

  require 'sequel'

  db = ::Sequel.connect("#{db_conn}/postgres",
                        loggers: Logger.new($stdout))
  db.run("CREATE DATABASE #{db_name}")
end

.dropObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hanami/sequel/commands/drop.rb', line 12

def self.drop
  env = Hanami::Environment.new
  if env.environment == 'production'
    raise 'Command unavailable in the production environment.'
  end

  db_url = ENV.fetch('DATABASE_URL')
  db_conn, _, db_name = db_url.rpartition('/')

  require 'sequel'

  db = ::Sequel.connect("#{db_conn}/postgres",
                        loggers: Logger.new($stdout))
  db.run("DROP DATABASE IF EXISTS #{db_name}")
end

.migrate(**options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hanami/sequel/commands/migrate.rb', line 17

def self.migrate(**options)
  Hanami::Environment.new             # load DATABASE_URL

  args = {}

  if (v = options[:version])
    case
    when v == 'up' then;
    when v == 'down' then args[:target] = 0
    else args[:target] = Integer(v)
    end
  end

  require 'sequel'
  ::Sequel.extension :migration

  db = ::Sequel.connect(ENV.fetch('DATABASE_URL'),
                        loggers: Logger.new($stdout))

  ::Sequel::Migrator.run(db, CLI.config.migrations, **args)
end

.seed(**options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hanami/sequel/commands/seed.rb', line 12

def self.seed(**options)
  Hanami::Environment.new             # load DATABASE_URL

  require 'sequel'

  log = Logger.new($stdout)
  db = ::Sequel.connect(ENV.fetch('DATABASE_URL'),
                        loggers: log)
  ::Sequel::Model.db = db

  path = File.join('.', CLI.models_path, '*_model.rb')
  log.level = Logger::WARN
  Dir[path].each { |m| require m }
  log.level = Logger::INFO

  db.transaction do
    s = Hanami::Sequel::Seed
    s.methods(false).each { |m| s.send(m) }
  end
end