From 95a6ef1649f1cd9ba2465fedfb6b58491a964210 Mon Sep 17 00:00:00 2001 From: Varik Matevosyan Date: Fri, 21 Jun 2024 21:28:36 +0400 Subject: [PATCH] reset sequences before failover on logical replica --- model/lantern/lantern_resource.rb | 20 ++++- prog/lantern/lantern_resource_nexus.rb | 1 + spec/model/lantern/lantern_resource_spec.rb | 75 +++++++++++++------ .../lantern/lantern_resource_nexus_spec.rb | 1 + 4 files changed, 72 insertions(+), 25 deletions(-) diff --git a/model/lantern/lantern_resource.rb b/model/lantern/lantern_resource.rb index ac9fb34d4..8aff07c22 100644 --- a/model/lantern/lantern_resource.rb +++ b/model/lantern/lantern_resource.rb @@ -148,6 +148,24 @@ def create_publication(name) representative_server.run_query_all("CREATE PUBLICATION #{name} FOR ALL TABLES") end + def sync_sequences_with_parent + representative_server.list_all_databases.each do |db| + res = parent.representative_server.run_query("SELECT sequence_schema, sequence_name, last_value + FROM information_schema.sequences + JOIN pg_sequences + ON (information_schema.sequences.sequence_schema = pg_sequences.schemaname + AND information_schema.sequences.sequence_name = pg_sequences.sequencename);", db: db) + + statements = res.chomp.strip.split("\n").map do |row| + values = row.split(",") + {schema: values[0], sequence: values[1], last_value: values[2]} + "SELECT setval('#{values[0]}.#{values[1]}', #{values[2]});" + end + + representative_server.run_query(statements, db: db) + end + end + def create_and_enable_subscription representative_server.list_all_databases.each do |db| commands = <