Commit 068a53dd authored by Gabriel Mazetto's avatar Gabriel Mazetto

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

parent 477dc9fb
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
......@@ -145,15 +148,15 @@ namespace :geo do
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
......@@ -174,11 +177,14 @@ namespace :geo do
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?
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 "Warning: Cannot refresh foreign tables, there is no foreign server configured."
end
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