-
Notifications
You must be signed in to change notification settings - Fork 7
/
Rakefile
58 lines (51 loc) · 1.94 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
# frozen_string_literal: true
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'rubocop/rake_task'
RSpec::Core::RakeTask.new
RuboCop::RakeTask.new
namespace :db do
mysql_spec = {
adapter: 'mysql2',
host: (ENV['MYSQL_HOST'] || 'localhost'),
username: (ENV['MYSQL_USER'] || 'root'),
encoding: 'utf8mb4',
}
desc 'Build the databases for tests'
task :build_databases do
require 'active_record'
# Connect to MYSQL
ActiveRecord::Base.establish_connection(mysql_spec)
(1..5).map do |i|
# drop the old database (if it exists)
ActiveRecord::Base.connection.drop_database("octoball_shard_#{i}")
# create new database
ActiveRecord::Base.connection.create_database("octoball_shard_#{i}", charset: 'utf8mb4')
end
end
desc 'Create tables on tests databases'
task :create_tables do
ActiveRecord::Base.configurations = {
"test" => {
shard1: mysql_spec.merge(database: 'octoball_shard_1'),
shard2: mysql_spec.merge(database: 'octoball_shard_2'),
shard3: mysql_spec.merge(database: 'octoball_shard_3'),
shard4: mysql_spec.merge(database: 'octoball_shard_4'),
shard5: mysql_spec.merge(database: 'octoball_shard_5'),
}
}
require './spec/models/application_record'
ActiveRecord::Base.configurations.configs_for(env_name: "test").each do |config|
ActiveRecord::Base.establish_connection(config)
schema_migration = if ActiveRecord.gem_version >= Gem::Version.new(7.2)
ActiveRecord::Base.connection.pool.schema_migration
else
ActiveRecord::Base.connection.schema_migration
end
ActiveRecord::MigrationContext.new("spec/migration", schema_migration)
.migrate(config.database == 'octoball_shard_5' ? 2 : 1)
end
end
desc 'Prepare the test databases'
task prepare: [:build_databases, :create_tables]
end