Last active
September 27, 2016 18:50
-
-
Save vlad-at-work/405e16628729f88ae405acf168e9cfcc to your computer and use it in GitHub Desktop.
This is an updated Rakefile to for those who want to use Sequel with Hanami-RB, using the nifty Rails-like commands.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# You might need dotenv and colorize gems for this one to play nice | |
# | |
# The original work by Rodrigo Panachi: | |
# http://rpanachi.com/2016/04/11/from-rails-to-hanami-part2-sequel-migrations-model-validations-specs-fixtures | |
# | |
# For some reason I couldn't get his solution to work for the life of me, so | |
# I include the Hanami environment and used slightly more concise forms of `exec` | |
# as well as Sequel DB directly here and there. hanami/rake_tasks seems to be loading something which | |
# breaks migrations, so I advise you to keep that one commented out. Doesn't affect minitest. | |
# | |
require 'rake' | |
# require 'hanami/rake_tasks' # Uncomment this line if you don't want to use this rakefile | |
require 'rake/testtask' | |
Rake::TestTask.new do |t| | |
t.pattern = 'spec/**/*_spec.rb' | |
t.libs << 'spec' | |
t.warning = false | |
end | |
task default: :test | |
task spec: :test | |
# require_relative 'config/environment' | |
require 'colorize' | |
require 'dotenv' | |
ENV['DATABASE_URL'] = Dotenv.load('.env.development')['DATABASE_URL'] | |
APP_NAME = Dotenv.load('.hanamirc')['project'] | |
require_relative 'lib/config/sequel.rb' # This is where sequel is setup and defined | |
throw "ENV['DATABASE_URL'] is missing. Can't work with Sequel or PostgreSQL. \nCheck .env.development or .env.test" unless ENV['DATABASE_URL'] | |
namespace :db do | |
require 'sequel' | |
Sequel.extension(:migration) | |
MIGRATIONS_PATH = 'db/migrations' | |
# Show available schema versions | |
def database_versions | |
DB[:schema_migrations].order(:filename).select_map(:filename) || [] | |
end | |
# Split postgres URI into pieces | |
def split_database_url(url) | |
user, pass, host, database = url.scan(/postgres:\/\/(.+?):(.+?)@(.+?)\/(.+?)$/).first | |
data = { | |
user: user, | |
pass: pass, | |
host: host, | |
database: database | |
} | |
end | |
# Connection URL without the DB | |
def database_connection_url | |
db = split_database_url ENV['DATABASE_URL'] | |
db_conn = "postgres://#{db[:user]}:#{db[:pass]}@#{db[:host]}" | |
end | |
# Sorted list of migrations | |
def db_migrations | |
Dir[MIGRATIONS_PATH + "/*.rb"].map { |f| File.basename(f) }.sort | |
end | |
# Run DB Migrator | |
def db_migrate(version = db_migrations.last) | |
Sequel::Migrator.run(DB, MIGRATIONS_PATH, target: version.to_i) | |
end | |
# # Check if the version has been migrated | |
# def db_migrated?(version) | |
# db_versions.include?(version.to_s) | |
# end | |
desc 'Seed the database with pre-defined data from db/seeds.rb' | |
task :seed do | |
load 'db/seeds.rb' | |
end | |
desc 'Print current schema/migration version.' | |
task :version do | |
puts "\t\nCurrent Schema Version: " + "#{database_versions.last}\n".yellow | |
end | |
desc "Perform migration up to latest migration available." | |
task :migrate do |t, args| | |
db_migrate(args[:version] || db_migrations.last) | |
Rake::Task['db:version'].execute | |
end | |
desc "Perform migration reset (full rollback and migration). Doesn't work in production." | |
task :reset do | |
if (ENV["HANAMI_ENV"] || ENV["RACK_ENV"]) == "production" | |
abort "You can't run this rake on production environment" | |
end | |
db_migrate(0) | |
db_migrate(db_migrations.last) | |
Rake::Task['db:version'].execute | |
end | |
desc "Perform rollback to specified target or previous version as default" | |
task :rollback, [:version] do |t, args| | |
version = args[:version] || database_versions[-2] | |
db_migrate(version) | |
Rake::Task['db:version'].execute | |
end | |
desc "Creates a database specified in the ENV['DATABASE_URL']" | |
task :create do | |
db = split_database_url ENV['DATABASE_URL'] | |
puts "\t\nAttempting to create #{db[:database]}... ".green | |
result = system("PGPASSWORD=#{db[:pass]} createdb -w -h localhost -p 5432 -U #{db[:user]} #{db[:database]}") | |
if result | |
print "success!\n".bold.green | |
else | |
puts | |
puts result | |
end | |
end | |
desc "Drops database if it exists. Doesn't work in production." | |
task :drop do | |
if (ENV["HANAMI_ENV"] || ENV["RACK_ENV"]) == "production" | |
abort "You can't run this rake on production environment" | |
end | |
db = split_database_url ENV['DATABASE_URL'] | |
puts "\n\tWARNING! Dropping ".red + "#{db[:database]}\n".red.bold | |
result = exec("PGPASSWORD=#{db[:pass]} dropdb -w -h localhost -p 5432 -U #{db[:user]} #{db[:database]}") | |
puts result | |
end | |
desc "Start a PostgreSQL console environment." | |
task :console do | |
exec "psql #{ENV['DATABASE_URL']}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment