Skip to content

Commit 6dcb301

Browse files
committed
Install: rename node did not update the database
1 parent 1050574 commit 6dcb301

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

install.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,36 @@ def rename_node(config_dir: str, old_name: str, new_name: str):
582582
data[new_name] = data.pop(old_name)
583583
with Path(file).open('w', encoding='utf-8') as f:
584584
yaml.dump(data, f)
585+
main = yaml.load(Path(os.path.join(config_dir, 'main.yaml')).read_text(encoding='utf-8'))
586+
nodes = yaml.load(Path(os.path.join(config_dir, 'nodes.yaml')).read_text(encoding='utf-8'))
587+
url = nodes.get(new_name, {}).get('database', {}).get('url', main.get('database', {}).get('url'))
588+
if not url:
589+
print(_("No database URL found. Please configure the database URL in the main.yaml or nodes.yaml file."))
590+
return
591+
url = url.replace('SECRET', utils.get_password('database', config_dir))
592+
with psycopg.connect(url, autocommit=True) as conn:
593+
# rename nodes in all relevant tables
594+
conn.execute("UPDATE instances SET node = %s WHERE node = %s", (new_name, old_name))
595+
conn.execute("UPDATE nodestats SET node = %s WHERE node = %s", (old_name, new_name))
596+
conn.execute("UPDATE audit SET node = %s WHERE node = %s", (old_name, new_name))
597+
# serverstats might not be there
598+
conn.execute(
599+
sql.SQL(
600+
"""
601+
DO $$
602+
BEGIN
603+
IF to_regclass('public.serverstats') IS NOT NULL THEN
604+
UPDATE serverstats
605+
SET node = {new_val}
606+
WHERE node = {old_val};
607+
END IF;
608+
END $$;
609+
"""
610+
).format(
611+
new_val=sql.Literal(new_name),
612+
old_val=sql.Literal(old_name),
613+
)
614+
)
585615

586616

587617
if __name__ == "__main__":

0 commit comments

Comments
 (0)