Skip to content

Instantly share code, notes, and snippets.

@nandooliveira
Forked from schickling/Rakefile
Created August 8, 2019 05:47
Show Gist options
  • Save nandooliveira/963396a4bd09ab10ddb09c613472f7d7 to your computer and use it in GitHub Desktop.
Save nandooliveira/963396a4bd09ab10ddb09c613472f7d7 to your computer and use it in GitHub Desktop.

Revisions

  1. @schickling schickling revised this gist Sep 30, 2013. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions Usage.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    ## Usage

    ```sh
    $ rake db:create # create the db
    $ rake db:migrate # run migrations
    $ rake db:drop # delete the db
    $ rake db:reset # combination of the upper three
    $ rake db:schema # creates a schema file of the current database
    $ rake g:migration your_migration # generates a new migration file
    ```
  2. @schickling schickling created this gist Sep 30, 2013.
    70 changes: 70 additions & 0 deletions Rakefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    require "active_record"

    namespace :db do

    db_config = YAML::load(File.open('config/database.yml'))
    db_config_admin = db_config.merge({'database' => 'postgres', 'schema_search_path' => 'public'})

    desc "Create the database"
    task :create do
    ActiveRecord::Base.establish_connection(db_config_admin)
    ActiveRecord::Base.connection.create_database(db_config["database"])
    puts "Database created."
    end

    desc "Migrate the database"
    task :migrate do
    ActiveRecord::Base.establish_connection(db_config)
    ActiveRecord::Migrator.migrate("db/migrate/")
    Rake::Task["db:schema"].invoke
    puts "Database migrated."
    end

    desc "Drop the database"
    task :drop do
    ActiveRecord::Base.establish_connection(db_config_admin)
    ActiveRecord::Base.connection.drop_database(db_config["database"])
    puts "Database deleted."
    end

    desc "Reset the database"
    task :reset => [:drop, :create, :migrate]

    desc 'Create a db/schema.rb file that is portable against any DB supported by AR'
    task :schema do
    ActiveRecord::Base.establish_connection(db_config)
    require 'active_record/schema_dumper'
    filename = "db/schema.rb"
    File.open(filename, "w:utf-8") do |file|
    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
    end
    end

    end

    namespace :g do
    desc "Generate migration"
    task :migration do
    name = ARGV[1] || raise("Specify name: rake g:migration your_migration")
    timestamp = Time.now.strftime("%Y%m%d%H%M%S")
    path = File.expand_path("../db/migrate/#{timestamp}_#{name}.rb", __FILE__)
    migration_class = name.split("_").map(&:capitalize).join

    File.open(path, 'w') do |file|
    file.write <<-EOF
    class #{migration_class} < ActiveRecord::Migration
    def self.up
    end
    def self.down
    end
    end
    EOF
    end

    puts "Migration #{path} created"
    abort # needed stop other tasks
    end
    end
    5 changes: 5 additions & 0 deletions database.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    # config/database.yml
    host: 'localhost'
    adapter: 'postgresql'
    encoding: utf-8
    database: 'test'