Skip to content

Instantly share code, notes, and snippets.

@ambethia
Created September 17, 2018 14:37
Show Gist options
  • Save ambethia/28448b43a0e510c2208a62f7d1cb8884 to your computer and use it in GitHub Desktop.
Save ambethia/28448b43a0e510c2208a62f7d1cb8884 to your computer and use it in GitHub Desktop.
ActiveRecord Starter Kit
Metrics/LineLength:
Max: 120
AllCops:
TargetRubyVersion: 2.5
Exclude:
- 'db/schema.rb'
- 'db/migrate/*'
mkdir -p db/migrate app/models config
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'irb'
require 'active_record'
Dir[File.join(__dir__, '..', '/app/**/*.rb')].each { |file| require file }
def reload!
Dir[File.join(__dir__, '..', '/app/**/*.rb')].each { |file| load file }
end
db_config = YAML.safe_load(File.open('config/database.yml'))
ActiveRecord::Base.establish_connection(db_config)
binding.irb
host: localhost
adapter: postgresql
encoding: utf-8
database: bg_library
# frozen_string_literal: true
source 'https://rubygems.org'
gem 'activerecord'
gem 'pg'
# frozen_string_literal: true
require 'active_record'
# rubocop:disable Metrics/BlockLength
namespace :db do
db_config = YAML.safe_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::MigrationContext.new('db/migrate/').migrate
Rake::Task['db:schema'].invoke
puts 'Database migrated.'
end
desc 'Migrate the database'
task :rollback do
ActiveRecord::Base.establish_connection(db_config)
ActiveRecord::MigrationContext.new('db/migrate/').rollback
Rake::Task['db:schema'].invoke
puts 'Last migration has been reverted.'
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: %i[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
# rubocop:enable Metrics/BlockLength
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 <<~END_MIGRATION
# frozen_string_literal: true
class #{migration_class} < ActiveRecord::Migration[5.2]
def up; end
def down; end
end
END_MIGRATION
end
puts "Migration #{path} created"
abort # needed stop other tasks
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment