Skip to content

Commit ce43cc0

Browse files
balasankarcRobert Marshall
authored andcommitted
Automatically create additional databases when specified
- when a decomposed database, such as the ci database, is specified it will be created when on the same node as the primary database - external databases will not have decomposed databases created automatically Closes https://gitlab.com/gitlab-org/omnibus-gitlab/-/issues/6638 Changelog: added Signed-off-by: Balasankar "Balu" C <[email protected]>
1 parent 749e907 commit ce43cc0

File tree

4 files changed

+121
-15
lines changed

4 files changed

+121
-15
lines changed

files/gitlab-cookbooks/gitlab/libraries/rails_migration_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ def connection_digest
4141
db_socket
4242
).collect { |attribute| attributes_node[attribute] }
4343

44+
# To trigger a db:migrate run when new databases are introduced, so that
45+
# those schema is populated in them.
46+
database_names = attributes_node['databases']&.select { |_, details| details['enable'] }&.keys
47+
connection_attributes << database_names if database_names
48+
4449
Digest::MD5.hexdigest(Marshal.dump(connection_attributes))
4550
end
4651
end

files/gitlab-cookbooks/gitlab/resources/database_objects.rb

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@
1313
pg_host = node['postgresql']['unix_socket_directory']
1414
pg_port = node['postgresql']['port']
1515
database_name = node['gitlab']['gitlab_rails']['db_database']
16+
database_host = node['gitlab']['gitlab_rails']['db_host']
1617
gitlab_sql_user = node['postgresql']['sql_user']
1718
gitlab_sql_user_password = node['postgresql']['sql_user_password']
1819
sql_replication_user = node['postgresql']['sql_replication_user']
1920
sql_replication_password = node['postgresql']['sql_replication_password']
2021

22+
# Geo Database will be handled separately in gitlab-ee::geo-postgresql
23+
# recipe.
24+
ignored_databases = %w[geo]
25+
# We only create databases that are configured on the same DB host as the main database
26+
databases = node['gitlab']['gitlab_rails']['databases'].select { |db, details| details['enable'] && details['db_host'] == database_host && !ignored_databases.include?(db) }
27+
2128
postgresql_user gitlab_sql_user do
2229
password "md5#{gitlab_sql_user_password}" unless gitlab_sql_user_password.nil?
2330
action :create
@@ -29,23 +36,27 @@
2936
action :create
3037
end
3138

32-
postgresql_database database_name do
33-
database_port pg_port
34-
database_socket pg_host
35-
owner gitlab_sql_user
36-
user postgresql_username
37-
helper new_resource.pg_helper
39+
databases.each do |_, settings|
40+
database_name = settings['db_database']
3841

39-
only_if { rails_enabled }
40-
end
42+
postgresql_database database_name do
43+
database_port pg_port
44+
database_socket pg_host
45+
owner gitlab_sql_user
46+
user postgresql_username
47+
helper new_resource.pg_helper
4148

42-
postgresql_extension 'pg_trgm' do
43-
database database_name
44-
action :enable
45-
end
49+
only_if { rails_enabled }
50+
end
51+
52+
postgresql_extension 'pg_trgm' do
53+
database database_name
54+
action :enable
55+
end
4656

47-
postgresql_extension 'btree_gist' do
48-
database database_name
49-
action :enable
57+
postgresql_extension 'btree_gist' do
58+
database database_name
59+
action :enable
60+
end
5061
end
5162
end
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
require 'chef_helper'
2+
3+
RSpec.describe 'database_objects' do
4+
let(:chef_run) { ChefSpec::SoloRunner.new(step_into: %w(database_objects postgresql_extension)).converge('gitlab::config', 'test_postgresql::postgresql_database_objects') }
5+
6+
before do
7+
allow(Gitlab).to receive(:[]).and_call_original
8+
end
9+
10+
describe 'create' do
11+
context 'by default' do
12+
it 'creates main database' do
13+
expect(chef_run).to create_postgresql_database('gitlabhq_production')
14+
end
15+
end
16+
17+
context 'when additional databases are specified' do
18+
context 'on same host as that of main database' do
19+
before do
20+
stub_gitlab_rb(
21+
gitlab_rails: {
22+
databases: {
23+
ci: {
24+
enable: true,
25+
db_database: 'gitlabhq_production_ci'
26+
}
27+
}
28+
}
29+
)
30+
end
31+
32+
it 'creates specified databases in addition to main database' do
33+
%w[gitlabhq_production_ci gitlabhq_production].each do |db|
34+
expect(chef_run).to create_postgresql_database(db)
35+
end
36+
end
37+
end
38+
39+
context 'on a different host than that of main database' do
40+
before do
41+
stub_gitlab_rb(
42+
gitlab_rails: {
43+
databases: {
44+
ci: {
45+
enable: true,
46+
db_database: 'gitlabhq_production_ci',
47+
db_host: 'different.db.host'
48+
}
49+
}
50+
}
51+
)
52+
end
53+
54+
it 'creates only main database and not CI database' do
55+
expect(chef_run).to create_postgresql_database('gitlabhq_production')
56+
expect(chef_run).not_to create_postgresql_database('gitlabhq_production_ci')
57+
end
58+
end
59+
end
60+
61+
context 'when Geo database is specified' do
62+
before do
63+
stub_gitlab_rb(
64+
gitlab_rails: {
65+
databases: {
66+
ci: {
67+
enable: true,
68+
db_database: 'gitlabhq_production_ci'
69+
}
70+
}
71+
},
72+
geo_secondary_role: {
73+
enable: true
74+
}
75+
)
76+
end
77+
78+
it 'creates specified databases in addition to main database except geo' do
79+
expect(chef_run).to create_postgresql_database('gitlabhq_production')
80+
expect(chef_run).to create_postgresql_database('gitlabhq_production_ci')
81+
82+
expect(chef_run).not_to create_postgresql_database('gitlabhq_geo_production')
83+
end
84+
end
85+
end
86+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
database_objects 'postgresql' do
2+
pg_helper PgHelper.new(node)
3+
account_helper AccountHelper.new(node)
4+
end

0 commit comments

Comments
 (0)