Convert ee/geo/db/schema.rb to ee/geo/db/structure.sql

Please refer to https://gitlab.com/gitlab-org/gitlab/-/issues/341758 for details.

Changelog: changed
EE: true
parent 5eaaf70a
......@@ -109,7 +109,7 @@ Geo secondary sites have a [Geo tracking database](https://gitlab.com/gitlab-org
bin/rake geo:db:migrate
```
- [ ] Be sure to commit the relevant changes in `ee/db/geo/schema.rb`
- [ ] Be sure to commit the relevant changes in `ee/db/geo/structure.sql`
### Add verification state fields on the Geo primary site
......
......@@ -110,7 +110,7 @@ Geo secondary sites have a [Geo tracking database](https://gitlab.com/gitlab-org
bin/rake geo:db:migrate
```
- [ ] Be sure to commit the relevant changes in `ee/db/geo/schema.rb`
- [ ] Be sure to commit the relevant changes in `ee/db/geo/structure.sql`
### Add verification state fields on the Geo primary site
......
......@@ -28,7 +28,6 @@ AllCops:
- 'node_modules/**/*'
- 'db/fixtures/**/*'
- 'db/schema.rb'
- 'ee/db/geo/schema.rb'
- 'tmp/**/*'
- 'bin/**/*'
- 'generator_templates/**/*'
......
......@@ -33,7 +33,7 @@ MSG
DATABASE_APPROVED_LABEL = 'database::approved'
non_geo_db_schema_updated = !git.modified_files.grep(%r{\Adb/structure\.sql}).empty?
geo_db_schema_updated = !git.modified_files.grep(%r{\Aee/db/geo/schema\.rb}).empty?
geo_db_schema_updated = !git.modified_files.grep(%r{\Aee/db/geo/structure\.sql}).empty?
non_geo_migration_created = !git.added_files.grep(%r{\A(db/(post_)?migrate)/}).empty?
geo_migration_created = !git.added_files.grep(%r{\Aee/db/geo/(post_)?migrate/}).empty?
......@@ -45,7 +45,7 @@ if non_geo_migration_created && !non_geo_db_schema_updated
end
if geo_migration_created && !geo_db_schema_updated
warn format(format_str, migrations: 'Geo migrations', schema: helper.html_link("ee/db/geo/schema.rb"))
warn format(format_str, migrations: 'Geo migrations', schema: helper.html_link("ee/db/geo/structure.sql"))
end
return unless helper.ci?
......
This diff is collapsed.
fc56ad577659aeb35893c644f404cbe226ef5c4b64d46bd7a6979917170e8379
\ No newline at end of file
03f26e06b4a8ba1d7f4793ed59f2790bc782de059a377777adeaa05645d940bc
\ No newline at end of file
1ee3e2ecb36df2c136430b7a302121cb8b916b54966a1d9c53fb959058b1101c
\ No newline at end of file
ffdbcf4ba41532961d8245078c5c80997fc8a7494181e9aae22d8d8eb0d84fae
\ No newline at end of file
6ce74aada3515fe681d55c19f3b483d44dae6878bfdc8713377a0eece3591701
\ No newline at end of file
7585f2682dc0b35f3bc5e105bd338ce3602cec7e6301a1a59c3b8342e6dd10bd
\ No newline at end of file
3d715f80eeda6d29cb4bfb725dca4303160d6be515371015726d86d4cfc92b58
\ No newline at end of file
ddcfa3398a102a01eda15bb3fa0b6a796011186084b291efd3a0766d327cd22e
\ No newline at end of file
158f354fa311961e328951fc365c8d0a87e6c55fbc8bd477b623006abaf18dfe
\ No newline at end of file
44f717e6b7bd87909ea978cdd74991babd0dd3a8e44dbfa4a6b7ec0874dfeeff
\ No newline at end of file
89faad46515d44af495b75b7c6ebcdc2c282fbb965cf5f6b34c4df534378b4c5
\ No newline at end of file
cb9da5bc60b2d4699c9117e3e384575e393d0c98b6f669606d98781d22dbc3ac
\ No newline at end of file
a6cc590283c5160a325ddd66a8139ac9da973670a5e1ec6c13772535da973237
\ No newline at end of file
b44a91c540b476b9c63ba247ef9f0e3ee2b696737cc3a59f1b60436dd0d8107f
\ No newline at end of file
2fb6f33c9d2942d0c83f550963447e414616ded09718e59b9e657480aa8cdfd5
\ No newline at end of file
871f226decc06e96cfb5f8156c7e87accc9475e22cd97d6c41fa27c8eb5855b7
\ No newline at end of file
427f9c742870563003a6f7728b8523faed8ad061b00fc01d307d4f2df83867c1
\ No newline at end of file
86fd97eff85491d242c8d38acadd383be052175d9e0dcfcecb32aeb152183c34
\ No newline at end of file
842585c0203ead10143bba55cb34d2ab360831661db4519f9083306cde9041f0
\ No newline at end of file
df6168850c19c25ac7a177cf91bc5c8ee73fca93724f171d427bf9937e9a6a6c
\ No newline at end of file
c818e0fc91534f407567d6ee1f9e5cd858392c7afbd22c35463c617ec57296f0
\ No newline at end of file
dc1fbc3291af7c261d686747d0985fee6fdc65cce98f262eca496af087020a8c
\ No newline at end of file
701f46d9972f184c27e2e8bc3631296138b522035ff35624db9688de9ae3d34b
\ No newline at end of file
322e4466d309ccfd8f24064c7835d9b271268d290e8204af06587fa2f5313272
\ No newline at end of file
72c5a2f8a94566a34fcc3df16b72a4239a37a9b1098ce31284d42bd5fefa52c0
\ No newline at end of file
43b9134bcd2c244859497764ae902da51e69320827301be4033b358cd2be3cfe
\ No newline at end of file
646959b39da5bb35f03941af129444c4364218ecc72ba9ffcfb869fa1d83e030
\ No newline at end of file
497de32713debe7016d643f0d9a1eafd6d4dd982c6f56374e26fc91647a61c64
\ No newline at end of file
6616c486d58e39cc4452f4d28a9f47e5c376ee5e6e9450d9659412467569e9b3
\ No newline at end of file
07196b409c0c9e73e4707a8137716f025f09238869e1b319efa045a0a8a97f74
\ No newline at end of file
8c8454796921d35b59261c9d3a9a29d5c3b52dd85a0171a36b81c0316563d984
\ No newline at end of file
f555f44342f489f4633fde2961c98c857ba9d673b4bde78b278d8a112858fe3d
\ No newline at end of file
01b19219e46985ac5dc1df2e6afb45fb9d006f082fadf1565eb4ac9206afb01a
\ No newline at end of file
853357855ebdc76d39a991b402764bc8009f47473f6f03e74c6ec26602695442
\ No newline at end of file
b035dbc1bafa92ab1595cb416314f23052b09fac4aff7d8e0556cdae752300fa
\ No newline at end of file
dc5fe525d8ebb0088f4b704b01b4568e2d8cb487156b4bbc6c90c16d53c501fc
\ No newline at end of file
a4a38efe967d1bf4a9b29e1545e32b3ea59d3373920b0989f65ccd1e79b79543
\ No newline at end of file
79c814920903ea9b92a2540dc9078a2b119ed63cbe02756d16f2c482ca1889ec
\ No newline at end of file
309fceeed92c7454495742644158f9cd2c31d75fc489cffdf18992c69f9b10c7
\ No newline at end of file
9c00de916aaa87b205fede9ae8c4decc03a1987fd038cd888abc905556da6334
\ No newline at end of file
67b34fe78c0d2f25fde2d7a981bc2de1053d46dad9d911df16d238ea02749197
\ No newline at end of file
805651c9c9f4fcc7ee69be2a774334bd3dea59475f9234da01b8ddc2badb7fb3
\ No newline at end of file
5766c582effb731ce3346ccf335eba9350d671b41ab9697c0c81ce901065f33e
\ No newline at end of file
a6804d3592cfde2b9541a1cfa20fb67762fa82cf9ebc3f703a98668f742995b0
\ No newline at end of file
15e06328a76772499ce08936d7e11e272ca57c3a5e04246a9b2ddab5e651d0c4
\ No newline at end of file
e1a58a3bc183214d280b253d0171cb97fca3c065a67a746214cf5d096acf0834
\ No newline at end of file
d7c8a2d1a8a4f36781a56475f8d96e830a33a7e94cfe4f622f28c6c43b76323d
\ No newline at end of file
2a6439cf81ef55b063b277112847b436f7daf37f1fc1d3e76261dc49e8ddba9b
\ No newline at end of file
4fbed5a9d9fb34137db151b78efdc9cf7ffbeb445728a1dbf536f1f8d621f066
\ No newline at end of file
2447efe0b351ce5f18aca8c9193541bbd83cdc8a6d009306b57c4158d80525b8
\ No newline at end of file
91b9f4eece930583053a215792537f8288e8d7226d1cb619eff167999d2c63d9
\ No newline at end of file
73adf0b2df8b314fa4911087d54649e2b2d83310516dd1b7557f8195875744d6
\ No newline at end of file
7dc9e31a729e11dfc4888d681f21b4260d4345d63ec2142aa3e78889d4c60c87
\ No newline at end of file
b99486cc34650ba7f735f04813cfcb3af24f8cebe8b293d4ef8db7f08b99fb04
\ No newline at end of file
e0c194fd28eb186918b71e5d90517cab6bc63d6ae0bdce69ea3bf3e89596e124
\ No newline at end of file
7b11f57bd5e4e96676c78335096b2e12b83fdfbc7b7df04fa18fb5b64ce963d1
\ No newline at end of file
faf6f0c60a3a0151995bf0d7973820de649aad4e6d708575e9ae79317ad893eb
\ No newline at end of file
7123ecf2a4b6cd114173126eb9375f050a1bc9b17df4a434f03b42ff902fe407
\ No newline at end of file
f9e1bca1a7a0c6642359689fa3340313d1d3fdadf31c338c971cb9313cf2b983
\ No newline at end of file
84bd01454296401f86c1866a33c6b45091333c6b3d638f5384b9c474bd7ab290
\ No newline at end of file
27d0aa487d569f90cf5f408fca0040b4bd679394520f41c602c2179891ffbb3b
\ No newline at end of file
0f364cf251d7fcda281145d84dda51cd67d33e4dfe87a805b3b188227c0dddbd
\ No newline at end of file
2b4bb29e09a1cc599c7b6e2b455bd6ea3175f3b7470ff8cbfef8be686dfb2abc
\ No newline at end of file
11f9ae81865a4315d6b46d90c1636252333747c095de567a056ebb344bc0e702
\ No newline at end of file
392afdcfb817a88a3cd35afa4db87749bc70418913dd90dbca616c06cdb24b1d
\ No newline at end of file
d009ec5b2c7ec6b1f54969dc182c47400e0a6a14f77174852ea42bc5c7823a27
\ No newline at end of file
b63d6fbfdcf0caf5399cb0c7579831d9187a18de88c7e516a84d21625e5fc963
\ No newline at end of file
823dc59c6b24cfd1784d1dd45e26f8e365e2e812001aa94b9a59daa1e1999f16
\ No newline at end of file
10bbe723769c6bf30f11406eb7f336f55b5b7b0e805c2fdeb24b018895315fd6
\ No newline at end of file
88a3355eb13da8ebccffeef580371a9ed6913baa0755ec4fe84a477eb02de432
\ No newline at end of file
710ade13436e31c2d8170d85e8890475db8f07fd60c2b89a14ef8824be005bc2
\ No newline at end of file
4cb0e40afc41a81af078e1afc53773fcee508bd4c4d6447ec432d8a28f743177
\ No newline at end of file
2af1ff7554c41a412ee92bea810127c5f38485a49e93c7da021b2590544e5150
\ No newline at end of file
ce8b83b1233035e24a58a79755fc062bc699f1b5337dfc74de0666a968ac484d
\ No newline at end of file
7376c4a2de86ac8879209571757319c9dc1c1b08292f55b904b4e60041d91e56
\ No newline at end of file
6c974f3987f274ec7cd5ce2225a7cc4170c87a487c981c65b94df4c3a7bd25c9
\ No newline at end of file
08472c4eefc6f596e2e705c62ea3aedc3c2db40ad8c6e169f597da6504ddfaa1
\ No newline at end of file
c57c851e7a2c7f393ae93df48cda9021b49cfb4bc75093462fd49f6502d02175
\ No newline at end of file
This diff is collapsed.
......@@ -64,10 +64,7 @@ module Gitlab
require 'active_record/schema_dumper'
Gitlab::Geo::DatabaseTasks.with_geo_db do
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
ActiveRecord::Tasks::DatabaseTasks.dump_schema(Gitlab::Geo::DatabaseTasks.db_config)
end
end
end
......@@ -132,10 +129,10 @@ module Gitlab
Gitlab::Geo::DatabaseTasks.with_geo_db do
should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
ActiveRecord::Schema.verbose = false
ActiveRecord::Tasks::DatabaseTasks.load_schema(ActiveRecord::Base.configurations.configs_for(env_name: 'test').first, :ruby, ENV['SCHEMA'])
ActiveRecord::Tasks::DatabaseTasks.load_schema(ActiveRecord::Base.configurations.configs_for(env_name: 'test').first, :sql, ENV['SCHEMA'])
ensure
if should_reconnect
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).first)
ActiveRecord::Base.establish_connection(Gitlab::Geo::DatabaseTasks.db_config)
end
end
end
......@@ -152,6 +149,7 @@ module Gitlab
database_config: YAML.load_file(GEO_DATABASE_CONFIG),
db_dir: GEO_DB_DIR,
migrations_paths: geo_migrations_paths,
schema_migrations_path: geo_schema_migrations_path,
seed_loader: SeedLoader.new
}
end
......@@ -170,6 +168,10 @@ module Gitlab
Rails.root.join(GEO_DB_DIR, 'post_migrate')
end
def geo_schema_migrations_path
Rails.root.join(GEO_DB_DIR, 'schema_migrations').to_s
end
def with_geo_db
abort_if_no_geo_config!
......@@ -177,7 +179,8 @@ module Gitlab
database_config: ActiveRecord::Tasks::DatabaseTasks.database_configuration&.dup || YAML.load_file(DATABASE_CONFIG),
db_dir: ActiveRecord::Tasks::DatabaseTasks.db_dir,
migrations_paths: ActiveRecord::Tasks::DatabaseTasks.migrations_paths,
seed_loader: ActiveRecord::Tasks::DatabaseTasks.seed_loader
seed_loader: ActiveRecord::Tasks::DatabaseTasks.seed_loader,
schema_migrations_path: Gitlab::Database::SchemaMigrations::Context.default_schema_migrations_path
}
set_db_env(geo_settings)
......@@ -187,16 +190,21 @@ module Gitlab
set_db_env(original_settings)
end
def db_config
ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).first
end
def set_db_env(settings)
ActiveRecord::Tasks::DatabaseTasks.database_configuration = settings[:database_config]
ActiveRecord::Tasks::DatabaseTasks.db_dir = settings[:db_dir]
ActiveRecord::Tasks::DatabaseTasks.migrations_paths = settings[:migrations_paths]
ActiveRecord::Tasks::DatabaseTasks.seed_loader = settings[:seed_loader]
ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration || {}
ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration || {}
ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
Gitlab::Database::SchemaMigrations::Context.default_schema_migrations_path = settings[:schema_migrations_path]
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).first)
ActiveRecord::Base.establish_connection(db_config)
end
class SeedLoader
......
......@@ -38,7 +38,7 @@ namespace :geo do
puts "Current version: #{Gitlab::Geo::DatabaseTasks.version}"
end
desc 'GitLab | Geo | DB | Drops and recreates the database from ee/db/geo/schema.rb for the current environment and loads the seeds.'
desc 'GitLab | Geo | DB | Drops and recreates the database from ee/db/geo/structure.sql for the current environment and loads the seeds.'
task reset: [:environment] do
ns['drop'].invoke
ns['create'].invoke
......@@ -77,12 +77,12 @@ namespace :geo do
end
namespace :schema do
desc 'GitLab | Geo | DB | Schema | Load a schema.rb file into the database'
desc 'GitLab | Geo | DB | Schema | Load a structure.sql file into the database'
task load: [:environment] do
Gitlab::Geo::DatabaseTasks.load_schema_current(:ruby, ENV['SCHEMA'])
Gitlab::Geo::DatabaseTasks.load_schema_current(ActiveRecord::Base.schema_format, ENV['SCHEMA'])
end
desc 'GitLab | Geo | DB | Schema | Create a ee/db/geo/schema.rb file that is portable against any DB supported by AR'
desc 'GitLab | Geo | DB | Schema | Create a ee/db/geo/structure.sql file that is portable against any DB supported by AR'
task dump: [:environment] do
Gitlab::Geo::DatabaseTasks::Schema.dump
......@@ -90,6 +90,13 @@ namespace :geo do
end
end
# Inform Rake that custom tasks should be run every time rake db:schema:dump is run
Rake::Task['geo:db:schema:dump'].enhance do
Gitlab::Geo::DatabaseTasks.with_geo_db do
Rake::Task['gitlab:db:clean_structure_sql'].invoke
end
end
namespace :migrate do
desc 'GitLab | Geo | DB | Migrate | Runs the "up" for a given migration VERSION.'
task up: [:environment] do
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
RSpec.describe Gitlab::Geo::DatabaseTasks, :reestablished_active_record_base do
let(:schema_file) { Rails.root.join('tmp', 'tests', 'geo_schema.rb').to_s }
let(:schema_file) { Rails.root.join('tmp', 'tests', 'geo_structure.sql').to_s }
subject { described_class }
......@@ -59,8 +59,8 @@ RSpec.describe Gitlab::Geo::DatabaseTasks, :reestablished_active_record_base do
describe described_class::Schema do
describe '.dump' do
it 'calls ActiveRecord::SchemaDumper.dump' do
expect(ActiveRecord::SchemaDumper).to receive(:dump)
it 'calls ActiveRecord::Tasks::DatabaseTasks.dump_schema' do
expect(ActiveRecord::Tasks::DatabaseTasks).to receive(:dump_schema)
subject.dump
end
......
......@@ -6,7 +6,13 @@ RSpec.describe 'geo rake tasks', :geo, :silence_stdout do
include ::EE::GeoHelpers
before do
Rake.application.rake_require 'active_record/railties/databases'
Rake.application.rake_require 'tasks/gitlab/db'
Rake.application.rake_require 'tasks/geo'
# empty task as env is already loaded
Rake::Task.define_task :environment
stub_licensed_features(geo: true)
end
......@@ -133,15 +139,16 @@ RSpec.describe 'geo rake tasks', :geo, :silence_stdout do
describe 'geo:db:schema_load' do
it 'loads schema file into database' do
allow(ENV).to receive(:[]).with('SCHEMA')
expect(Gitlab::Geo::DatabaseTasks).to receive(:load_schema_current).with(:ruby, ENV['SCHEMA'])
expect(Gitlab::Geo::DatabaseTasks).to receive(:load_schema_current).with(:sql, ENV['SCHEMA'])
run_rake_task('geo:db:schema:load')
end
end
describe 'geo:db:schema_dump' do
it 'creates schema.rb file' do
it 'creates a clean structure.sql file', :reestablished_active_record_base do
expect(Gitlab::Geo::DatabaseTasks::Schema).to receive(:dump)
expect(Rake::Task['gitlab:db:clean_structure_sql']).to receive(:invoke)
run_rake_task('geo:db:schema:dump')
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment