forked from liam-mitchell/inne
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Rakefile
141 lines (116 loc) · 4.31 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require 'active_record'
require 'database_cleaner'
require 'factory_bot'
require 'rails'
require 'yaml'
require 'yaml_db'
require_relative 'src/constants.rb'
require_relative 'src/utils.rb'
require_relative 'src/models.rb'
require_relative 'src/messages.rb'
# Supress warnings, too spammy in migrations!
$VERBOSE = nil
module Rails
def Rails.env
DATABASE_ENV
end
end
namespace :db do
desc "Sets database environment and migrations directory"
task :environment do
DATABASE_ENV = ENV['DATABASE_ENV'] || DATABASE
MIGRATIONS_DIR = ENV['MIGRATIONS_DIR'] || DIR_MIGRATION
end
desc "Loads database connection configuration from YAML file"
task :configuration => :environment do
@config = YAML.load_file(CONFIG)[DATABASE_ENV]
end
desc "Establish connection with database using YAML configuration"
task :configure_connection => :configuration do
ActiveRecord::Base.establish_connection(@config)
end
desc "Alias for 'configure_connection'"
task :create => :configure_connection do
ActiveRecord::Base.establish_connection(@config)
end
desc "Deletes the database"
task :drop => :configure_connection do
ActiveRecord::Base.connection.drop_database(@config['database'])
end
desc "Execute new migrations"
task :migrate => :configure_connection do
require_relative "#{DIR_SOURCE}/models.rb"
version = ENV['MIGRATION_VERSION'] ? ENV['MIGRATION_VERSION'].to_i : ENV['VERSION'] ? ENV['VERSION'].to_i : nil
if rails_at_most('5.2.0')
ActiveRecord::Migrator.migrate(MIGRATIONS_DIR, version)
else
ActiveRecord::MigrationContext.new(MIGRATIONS_DIR).migrate(version)
end
end
desc "Rollback latest migration, or a fixed number of them"
task :rollback => :configure_connection do
if rails_at_most('5.2.0')
ActiveRecord::Migrator.rollback(MIGRATIONS_DIR, (ENV['STEP'] || 1).to_i)
else
ActiveRecord::MigrationContext.new(MIGRATIONS_DIR).rollback((ENV['STEP'] || 1).to_i)
end
end
desc "Seed database with initial records stored in seeds.rb"
task :seed => :configure_connection do
require_relative "#{DIR_SOURCE}/models.rb"
require_relative "#{DIR_DB}/seeds.rb"
end
desc "Run tests"
task :test => :configure_connection do
require "#{DIR_TEST}/unit"
require "#{DIR_TEST}/unit/ui/console/testrunner"
require 'mocha/test_unit'
class Test::Unit::TestCase
include FactoryBot::Syntax::Methods
end
FactoryBot.find_definitions
require_relative "#{DIR_TEST}/test_models.rb"
require_relative "#{DIR_TEST}/test_messages.rb"
DatabaseCleaner.strategy = :transaction
[TestScores, TestRankings, TestMessages].each do |suite|
Test::Unit::UI::Console::TestRunner.run(suite)
end
end
desc "Dump schema and data to db/schema.rb and db/data.yml"
task(:dump => [ "db:schema:dump", "db:data:dump" ])
desc "Load schema and data from db/schema.rb and db/data.yml"
task(:load => [ "db:schema:load", "db:data:load" ])
namespace :data do
desc "Dump contents of database to db/data.extension (defaults to yaml)"
task :dump => :environment do
YamlDb::RakeTasks.data_dump_task
end
desc "Dump contents of database to curr_dir_name/tablename.extension (defaults to yaml)"
task :dump_dir => :environment do
YamlDb::RakeTasks.data_dump_dir_task
end
desc "Load contents of db/data.extension (defaults to yaml) into database"
task :load => [:environment, :configure_connection] do
YamlDb::RakeTasks.data_load_task
end
desc "Load contents of db/data_dir into database"
task :load_dir => :environment do
YamlDb::RakeTasks.data_load_dir_task
end
end
namespace :schema do
desc "Creates a db/schema.rb file that is portable against any DB supported by Active Record"
task dump: [:environment, :configure_connection] do
require "active_record/schema_dumper"
filename = ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema.rb")
File.open(filename, "w:utf-8") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
# db_namespace["schema:dump"].reenable
end
desc "Loads a schema.rb file into the database"
task load: [:environment, :configure_connection] do
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV["SCHEMA"])
end
end
end