Sha256: 774cc1d7548098475fb6a9c4560dca46eaa8790b6bd2fc235b7e634267ccde5b

Contents?: true

Size: 1.64 KB

Versions: 14

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

require 'sequel'
require_relative '../../config/db'

namespace :db do
  desc 'Create database'
  task create: :config do
    cmd = "PGPASSWORD=#{DB_CONF[:password]} createdb" \
          " --username=#{DB_CONF[:username]}" \
          " --host=#{DB_CONF[:host]}" \
          " #{DB_CONF[:database]}"
    puts "Database `#{DB_CONF[:database]}` successfully created" if system(cmd)
  end

  desc 'Drop database'
  task drop: :config do
    cmd = "PGPASSWORD=#{DB_CONF[:password]} dropdb" \
          " --username=#{DB_CONF[:username]}" \
          " --host=#{DB_CONF[:host]}" \
          " #{DB_CONF[:database]}"
    puts "Database `#{DB_CONF[:database]}` successfully dropped" if system(cmd)
  end

  desc 'Apply migrations'
  task :migrate, [:version] => :config do |_, args|
    require 'logger'
    require 'sequel/core'

    Sequel.extension :migration
    version = args[:version] ? args[:version].to_i : nil
    migrations_path = "#{__dir__}/../../db/migrate/"

    Sequel.connect(**DB_CONF, logger: Logger.new($stdout)) do |db|
      Sequel::Migrator.run(db, migrations_path, target: version)
    end
  end

  desc 'Database console'
  task console: :config do
    cmd = "PGPASSWORD=#{DB_CONF[:password]} psql" \
          " --username=#{DB_CONF[:username]}" \
          " --host=#{DB_CONF[:host]}" \
          " --port=#{DB_CONF[:port]}" \
          " #{DB_CONF[:database]}"
    puts "Database `#{DB_CONF[:database]}` says 'bye-bye'" if system(cmd)
  end

  desc 'Reset database - drop, create, & migrate'
  task :reset do
    Rake::Task['db:drop'].invoke
    Rake::Task['db:create'].invoke
    Rake::Task['db:migrate'].invoke
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
cyclone_lariat-0.3.10 lib/tasks/db.rake
cyclone_lariat-0.3.9 lib/tasks/db.rake
cyclone_lariat-0.3.8 lib/tasks/db.rake
cyclone_lariat-0.3.7 lib/tasks/db.rake
cyclone_lariat-0.3.6 lib/tasks/db.rake
cyclone_lariat-0.3.5 lib/tasks/db.rake
cyclone_lariat-0.3.4 lib/tasks/db.rake
cyclone_lariat-0.3.3 lib/tasks/db.rake
cyclone_lariat-0.3.2 lib/tasks/db.rake
cyclone_lariat-0.3.1 lib/tasks/db.rake
cyclone_lariat-0.3.0 lib/tasks/db.rake
cyclone_lariat-0.2.3 lib/tasks/db.rake
cyclone_lariat-0.2.2 lib/tasks/db.rake
cyclone_lariat-0.2.1 lib/tasks/db.rake