diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/database/schema_cleaner.rb | 43 | ||||
-rw-r--r-- | lib/gitlab/fake_application_settings.rb | 2 | ||||
-rw-r--r-- | lib/tasks/gitlab/db.rake | 17 |
3 files changed, 60 insertions, 2 deletions
diff --git a/lib/gitlab/database/schema_cleaner.rb b/lib/gitlab/database/schema_cleaner.rb new file mode 100644 index 00000000000..c1436d3e7ca --- /dev/null +++ b/lib/gitlab/database/schema_cleaner.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +module Gitlab + module Database + class SchemaCleaner + attr_reader :original_schema + + def initialize(original_schema) + @original_schema = original_schema + end + + def clean(io) + structure = original_schema.dup + + # Postgres compat fix for PG 9.6 (which doesn't support (AS datatype) syntax for sequences) + structure.gsub!(/CREATE SEQUENCE [^.]+\.\S+\n(\s+AS integer\n)/) { |m| m.gsub(Regexp.last_match[1], '') } + + # Also a PG 9.6 compatibility fix, see below. + structure.gsub!(/^CREATE EXTENSION IF NOT EXISTS plpgsql.*/, '') + structure.gsub!(/^COMMENT ON EXTENSION.*/, '') + + # Remove noise + structure.gsub!(/^SET.+/, '') + structure.gsub!(/^SELECT pg_catalog\.set_config\('search_path'.+/, '') + structure.gsub!(/^--.*/, "\n") + structure.gsub!(/\n{3,}/, "\n\n") + + io << "SET search_path=public;\n\n" + + # Adding plpgsql explicitly is again a compatibility fix for PG 9.6 + # In more recent versions of pg_dump, the extension isn't explicitly dumped anymore. + # We use PG 9.6 still on CI and for schema checks - here this is still the case. + io << <<~SQL.strip + CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; + SQL + + io << structure + + nil + end + end + end +end diff --git a/lib/gitlab/fake_application_settings.rb b/lib/gitlab/fake_application_settings.rb index 74b91277dd7..71d2b2396f8 100644 --- a/lib/gitlab/fake_application_settings.rb +++ b/lib/gitlab/fake_application_settings.rb @@ -4,7 +4,7 @@ # ActiveRecord access. We rely on the initial values being true or false to # determine whether to define a predicate method because for a newly-added # column that has not been migrated yet, there is no way to determine the -# column type without parsing db/schema.rb. +# column type without parsing db/structure.sql. module Gitlab class FakeApplicationSettings < OpenStruct include ApplicationSettingImplementation diff --git a/lib/tasks/gitlab/db.rake b/lib/tasks/gitlab/db.rake index e72c5f51ada..69d542a4f02 100644 --- a/lib/tasks/gitlab/db.rake +++ b/lib/tasks/gitlab/db.rake @@ -50,7 +50,7 @@ namespace :gitlab do else # Add post-migrate paths to ensure we mark all migrations as up Gitlab::Database.add_post_migrate_path_to_rails(force: true) - Rake::Task['db:schema:load'].invoke + Rake::Task['db:structure:load'].invoke Rake::Task['db:seed_fu'].invoke end end @@ -78,5 +78,20 @@ namespace :gitlab do else task :setup_ee end + + desc 'This adjusts and cleans db/structure.sql - it runs after db:structure:dump' + task :clean_structure_sql do + structure_file = 'db/structure.sql' + schema = File.read(structure_file) + + File.open(structure_file, 'wb+') do |io| + Gitlab::Database::SchemaCleaner.new(schema).clean(io) + end + end + + # Inform Rake that gitlab:schema:fix_structure_sql should be run every time rake db:structure:dump is run + Rake::Task['db:structure:dump'].enhance do + Rake::Task['gitlab:db:clean_structure_sql'].invoke + end end end |