-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
61 lines (51 loc) · 1.54 KB
/
Rakefile
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
56
57
58
59
60
61
require "bundler/setup"
Bundler.require
require "active_record"
require "yaml"
namespace(:db) do
db_config = YAML::load(File.open("config/database.yml"))
desc("Create the database")
task(:create) do
ActiveRecord::Base.establish_connection(db_config)
puts("Database created")
end
desc("Migrate the database")
task(:migrate => :create) do
ActiveRecord::Base.establish_connection(db_config)
ActiveRecord::MigrationContext.new("data/migrate/", ActiveRecord::SchemaMigration).migrate
Rake::Task["db:schema"].invoke
puts("Database migrated")
end
desc("Create a data/schema.rb file")
task(:schema) do
ActiveRecord::Base.establish_connection(db_config)
require "active_record/schema_dumper"
filename = "data/schema.rb"
File.open(filename, "w:utf-8") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
puts("Schema dumped")
end
end
namespace(:g) do
desc("Generate migration")
task(:migration) do
name = ARGV[1] || raise("Specify name: rake g:migration migration_name")
timestamp = Time.now.strftime("%Y%m%d%H%M%S")
path = File.expand_path("../data/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[7.0]
def change
end
end
EOF
)
end
puts("Migration #{path} created")
# needed stop other tasks
abort
end
end