Skip to content

Instantly share code, notes, and snippets.

@robertsosinski
Created December 25, 2012 15:23
Show Gist options
  • Save robertsosinski/4373709 to your computer and use it in GitHub Desktop.
Save robertsosinski/4373709 to your computer and use it in GitHub Desktop.

Revisions

  1. Robert Sosinski created this gist Dec 25, 2012.
    87 changes: 87 additions & 0 deletions db.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    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