-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRakefile
100 lines (85 loc) · 2.93 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# frozen_string_literal: true
require "bundler/gem_tasks"
require "rake/testtask"
require "active_record"
require "yaml"
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/*_test.rb"]
end
task :default => :test
namespace :db do
env = "development"
db_config = YAML.load_file("lib/puptime/persistence/config/database.yml")[env]
db_config_admin = db_config
desc "Create the database"
task :create do
ActiveRecord::Base.establish_connection(db_config_admin)
ActiveRecord::Base.connection.create_database(db_config["database"]) unless db_config_admin["adapter"] == "sqlite3"
puts "Database created."
end
desc "Migrate the database"
task :migrate do
ActiveRecord::Base.establish_connection(db_config)
ActiveRecord::MigrationContext.new("lib/puptime/persistence/db/migrate/").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 => %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 = "lib/puptime/persistence/db/schema.rb"
File.open(filename, "w:utf-8") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
end
desc "Cleanup database. Truncates all data"
task :clean do
ActiveRecord::Base.establish_connection(db_config)
tables = ActiveRecord::Base.connection.tables - %w[schema_migrations ar_internal_metadata]
case db_config["adapter"]
when "mysql", "postgresql"
tables.each do |table|
ActiveRecord::Base.connection.execute("TRUNCATE #{table}")
end
when "sqlite", "sqlite3"
tables.each do |table|
ActiveRecord::Base.connection.execute("DELETE FROM #{table}")
ActiveRecord::Base.connection.execute("DELETE FROM sqlite_sequence where name='#{table}'")
end
ActiveRecord::Base.connection.execute("VACUUM")
end
puts "Database cleaned."
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("../lib/puptime/persistence/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