Created
December 25, 2012 15:23
-
-
Save robertsosinski/4373709 to your computer and use it in GitHub Desktop.
Database rake tasks for Sequel
This file contains hidden or 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
namespace :db do | |
desc "Migrates the database to the target version, or to the lastest version if no target is given" | |
task :migrate, [:target, :current] => :environment do |t, args| | |
opts = {} | |
opts[:target] = args[:target].to_i if args[:target] | |
opts[:current] = args[:current].to_i if args[:current] | |
Sequel::Migrator.run(DB, "db/migrate", opts) | |
Rake::Task["db:dump"].invoke if Rails.env.development? | |
end | |
desc "Migrates the databse back one step from the current version" | |
task :rollback => :environment do | |
version = DB[:schema_info].first.try(:[], :version) | |
Rake::Task["db:migrate"].invoke(version - 1) if version | |
end | |
desc "Creates the database for the current environment" | |
task :create do | |
line = Cocaine::CommandLine.new("createdb", ":database " \ | |
"--host :host " \ | |
"--port :port " \ | |
"--username :username") | |
line.run(:database => dbconfig['database'], | |
:host => dbconfig['host'], | |
:port => dbconfig['port'].to_s, | |
:username => dbconfig['username']) | |
Rake::Task["db:set_public_schema_owner"].invoke(dbconfig['username']) | |
end | |
desc "Drops the database for the current environment" | |
task :drop do | |
line = Cocaine::CommandLine.new("dropdb", ":database " \ | |
"--host :host " \ | |
"--port :port " \ | |
"--username :username") | |
line.run(:database => dbconfig['database'], | |
:host => dbconfig['host'], | |
:port => dbconfig['port'].to_s, | |
:username => dbconfig['username']) | |
end | |
desc "Dumps the database schema into 'db/structure.sql'" | |
task :dump do | |
line = Cocaine::CommandLine.new("pg_dump", "--schema-only :database " \ | |
"--host :host " \ | |
"--port :port " \ | |
"--role :username " \ | |
"--username :username " \ | |
"--file :file") | |
line.run(:database => dbconfig['database'], | |
:host => dbconfig['host'], | |
:port => dbconfig['port'].to_s, | |
:username => dbconfig['username'], | |
:file => (Rails.root + 'db' + 'structure.sql').to_s) | |
end | |
desc "Loads the database schema from 'db/structure.sql'" | |
task :load => [:drop, :create] do | |
line = Cocaine::CommandLine.new("psql", ":database " \ | |
"--host :host " \ | |
"--port :port " \ | |
"--username :username " \ | |
"--file :file") | |
line.run(:database => dbconfig['database'], | |
:host => dbconfig['host'], | |
:port => dbconfig['port'].to_s, | |
:username => dbconfig['username'], | |
:file => (Rails.root + 'db' + 'structure.sql').to_s) | |
end | |
task :set_public_schema_owner, [:owner] do |t, args| | |
DB.execute_ddl("alter schema public owner to #{args[:owner]}") | |
end | |
private | |
def dbconfig | |
DB.opts[:orig_opts] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment