-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathdatabase_connection.rb
More file actions
93 lines (85 loc) · 3.36 KB
/
database_connection.rb
File metadata and controls
93 lines (85 loc) · 3.36 KB
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
# frozen_string_literal: true
module DatabaseConnection
class << self
def db_name
ENV.fetch("DB", "mysql2")
end
def setup
db_config = DEFAULT_CONFIG.fetch(db_name)
begin
ActiveRecord::Base.establish_connection(db_config)
ActiveRecord::Base.connection
rescue
raise unless db_config.is_a?(Hash)
ActiveRecord::Base.establish_connection(db_config.merge("database" => nil))
ActiveRecord::Base.connection.create_database(db_config["database"])
ActiveRecord::Base.establish_connection(db_config)
end
end
def drop_tables
TABLES.keys.each do |table|
ActiveRecord::Base.connection.drop_table(table) if table_exists?(table)
end
end
def table_exists?(table)
if ActiveRecord::Base.connection.respond_to?(:data_source_exists?)
ActiveRecord::Base.connection.data_source_exists?(table)
else
ActiveRecord::Base.connection.table_exists?(table)
end
end
def create_tables
TABLES.each do |table, fields|
fields = fields.dup
options = fields.last.is_a?(Hash) ? fields.pop : {}
ActiveRecord::Base.connection.create_table(table, **options) do |t|
fields.each do |column_type, *args|
if args.last.is_a?(Hash)
kwargs = args.pop
t.send(column_type, *args, **kwargs)
else
t.send(column_type, *args)
end
end
end
end
end
end
TABLES = {
polymorphic_records: [[:string, :owner_type], [:integer, :owner_id], [:timestamps, null: true]],
deeply_associated_records: [
[:string, :name], [:integer, :associated_record_id], [:integer, :item_id], [:timestamps, null: true],
],
associated_records: [[:string, :name], [:integer, :item_id], [:integer, :item_two_id], [:timestamps, null: true]],
normalized_associated_records: [[:string, :name], [:integer, :item_id], [:timestamps, null: true]],
no_inverse_of_records: [[:integer, :owner_id], [:timestamps, null: true]],
not_cached_records: [[:string, :name], [:integer, :item_id], [:timestamps, null: true]],
items: [[:integer, :item_id], [:string, :title], [:timestamps, null: true]],
items2: [[:integer, :item_id], [:string, :title], [:timestamps, null: true]],
related_items: [[:integer, :owner_id], [:string, :owner_type], [:integer, :item_id], [:timestamps, null: true]],
keyed_records: [[:string, :value], primary_key: "hashed_key"],
sti_records: [[:string, :type], [:string, :name]],
custom_parent_records: [[:integer, :parent_primary_key], id: false, primary_key: "parent_primary_key"],
custom_child_records: [
[:integer, :child_primary_key], [:integer, :parent_id], id: false, primary_key: "child_primary_key",
],
}
DEFAULT_CONFIG = {
"mysql2" => {
"adapter" => "mysql2",
"database" => "identity_cache_test",
"host" => ENV["MYSQL_HOST"] || "127.0.0.1",
"username" => "root",
"port" => ENV["MYSQL_PORT"] ? Integer(ENV["MYSQL_PORT"]) : 3306,
"password" => ENV["MYSQL_PASSWORD"] || "my-secret-pw",
},
"postgresql" => {
"adapter" => "postgresql",
"database" => "identity_cache_test",
"host" => ENV["POSTGRES_HOST"] || "127.0.0.1",
"username" => "postgres",
"password" => ENV["POSTGRES_PASSWORD"],
"prepared_statements" => false,
},
}
end