-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile.rb
executable file
·55 lines (44 loc) · 1.69 KB
/
Rakefile.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'active_record'
require 'pathname'
require 'fileutils'
include ActiveRecord::Tasks
class SeedLoader
def load_seed
load File.expand_path("../db/seeds.rb", __FILE__)
end
end
DatabaseTasks.env = ENV['RACK_ENV'] || :development
DatabaseTasks.root = File.dirname(__FILE__)
DatabaseTasks.db_dir = File.expand_path('../db', __FILE__)
DatabaseTasks.database_configuration = YAML.load(File.read('config/database.yml'))
DatabaseTasks.migrations_paths = File.join(DatabaseTasks.db_dir, 'migrate')
DatabaseTasks.seed_loader = SeedLoader.new
task :environment do
ActiveRecord::Base.configurations = DatabaseTasks.database_configuration
ActiveRecord::Base.establish_connection DatabaseTasks.env
end
namespace :db do
desc 'Generate a migration (parameters: NAME)'
task :generate_migration do
name = ENV['NAME']
raise('No NAME specified. Example usage: `rake db:generate_migration NAME=create_authors`') unless name
ActiveRecord::Migrator.migrations_paths.each do |directory|
next unless File.exist?(directory)
migration_files = Pathname(directory).children
duplicate = migration_files.find { |path| path.basename.to_s.include?(name) }
raise("Another migration is already named \"#{name}\": #{duplicate}") if duplicate
end
filename = "#{Time.now.utc.strftime('%Y%m%d%H%M%S')}_#{name}.rb"
dirname = ActiveRecord::Migrator.migrations_path
path = File.join(dirname, filename)
FileUtils.mkdir_p(dirname)
File.write path, <<-MIGRATION.strip_heredoc
class #{name.camelize} < ActiveRecord::Migration
def change
end
end
MIGRATION
puts "Generated Migration: #{path}"
end
end
load 'active_record/railties/databases.rake'