Commit ee2198d0 authored by Nick Thomas's avatar Nick Thomas

Merge branch 'geo-fdw-support' into 'master'

Add FDW schema refresh to Geo rake task and refactor some code

See merge request gitlab-org/gitlab-ee!3627
parents 6ecc9db1 bef38d1c
---
title: 'Geo: rake task to refresh foreign table schema (FDW support)'
merge_request: 3626
author:
type: added
module Gitlab
module Geo
module GeoTasks
extend self
def set_primary_geo_node
node = GeoNode.new(primary: true, url: GeoNode.current_node_url)
$stdout.puts "Saving primary GeoNode with URL #{node.url}".color(:green)
node.save
$stdout.puts "Error saving GeoNode:\n#{node.errors.full_messages.join("\n")}".color(:red) unless node.persisted?
end
def refresh_foreign_tables!
sql = <<~SQL
DROP SCHEMA IF EXISTS gitlab_secondary CASCADE;
CREATE SCHEMA gitlab_secondary;
IMPORT FOREIGN SCHEMA public
FROM SERVER gitlab_secondary
INTO gitlab_secondary;
SQL
Gitlab::Geo::DatabaseTasks.with_geo_db do
ActiveRecord::Base.transaction do
ActiveRecord::Base.connection.execute(sql)
end
end
end
def foreign_server_configured?
sql = <<~SQL
SELECT count(1)
FROM pg_foreign_server
WHERE srvname = '#{Gitlab::Geo::FDW_SCHEMA}';
SQL
Gitlab::Geo::DatabaseTasks.with_geo_db do
ActiveRecord::Base.connection.execute(sql).first.fetch('count').to_i == 1
end
end
end
end
end
require 'gitlab/geo'
require 'gitlab/geo/database_tasks'
require 'gitlab/geo/geo_tasks'
task spec: ['geo:db:test:prepare']
namespace :geo do
GEO_LICENSE_ERROR_TEXT = 'GitLab Geo is not supported with this license. Please contact sales@gitlab.com.'.freeze
namespace :db do |ns|
desc 'Drops the Geo tracking database from config/database_geo.yml for the current RAILS_ENV.'
task :drop do
......@@ -56,6 +59,18 @@ namespace :geo do
puts Rails.application.secrets.db_key_base
end
desc 'Refresh Foreign Tables definition in Geo Secondary node'
task :refresh_foreign_tables do
if Gitlab::Geo::GeoTasks.foreign_server_configured?
print "\nRefreshing foreign tables for FDW: #{Gitlab::Geo::FDW_SCHEMA} ... "
Gitlab::Geo::GeoTasks.refresh_foreign_tables!
puts 'Done!'
else
puts "Error: Cannot refresh foreign tables, there is no foreign server configured."
exit 1
end
end
# IMPORTANT: This task won't dump the schema if ActiveRecord::Base.dump_schema_after_migration is set to false
task :_dump do
if Gitlab::Geo::DatabaseTasks.dump_schema_after_migration?
......@@ -140,20 +155,29 @@ namespace :geo do
task purge: [:environment] do
Gitlab::Geo::DatabaseTasks::Test.purge
end
task :refresh_foreign_tables do
old_env = ActiveRecord::Tasks::DatabaseTasks.env
ActiveRecord::Tasks::DatabaseTasks.env = 'test'
ns['geo:db:refresh_foreign_tables'].invoke
ActiveRecord::Tasks::DatabaseTasks.env = old_env
end
end
end
desc 'Make this node the Geo primary'
task set_primary_node: :environment do
abort 'GitLab Geo is not supported with this license. Please contact sales@gitlab.com.' unless Gitlab::Geo.license_allows?
abort GEO_LICENSE_ERROR_TEXT unless Gitlab::Geo.license_allows?
abort 'GitLab Geo primary node already present' if Gitlab::Geo.primary_node.present?
set_primary_geo_node
Gitlab::Geo::GeoTasks.set_primary_geo_node
end
desc 'Make this secondary node the primary'
task set_secondary_as_primary: :environment do
abort 'GitLab Geo is not supported with this license. Please contact sales@gitlab.com.' unless Gitlab::Geo.license_allows?
abort GEO_LICENSE_ERROR_TEXT unless Gitlab::Geo.license_allows?
ActiveRecord::Base.transaction do
primary_node = Gitlab::Geo.primary_node
......@@ -173,12 +197,4 @@ namespace :geo do
current_node.update!(primary: true)
end
end
def set_primary_geo_node
node = GeoNode.new(primary: true, url: GeoNode.current_node_url)
puts "Saving primary GeoNode with URL #{node.url}".color(:green)
node.save
puts "Error saving GeoNode:\n#{node.errors.full_messages.join("\n")}".color(:red) unless node.persisted?
end
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