-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add a script for fixing schema (public) of plugin's objects (#299)
this script allows to set the plugin's schema (instead of "public") to the database objects created before v2.2 (postgresql v16)... it aims to avoid errors caused by using the "public" schema....<hr>This is an automatic backport of pull request #298 done by [Mergify](https://mergify.com).
- Loading branch information
1 parent
97cc2cc
commit f590e07
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
import psycopg2 | ||
import argparse | ||
|
||
USERNAME = os.environ["MFBASE_POSTGRESQL_USERNAME"] | ||
PASSWORD = os.environ["MFBASE_POSTGRESQL_PASSWORD"] | ||
HOST = "localhost" | ||
PORT = int(os.environ["MFBASE_POSTGRESQL_PORT"]) | ||
TIMEOUT = 30 | ||
DESCRIPTION = "fix plugin database objects schema. " \ | ||
"from 'public' to plugin's schema" | ||
|
||
parser = argparse.ArgumentParser(description=DESCRIPTION) | ||
parser.add_argument("plugin_dbname", help="plugin dbname") | ||
parser.add_argument("plugin_user", help="plugin user") | ||
args = parser.parse_args() | ||
|
||
dbname = args.plugin_dbname | ||
conn = psycopg2.connect(host=HOST, port=PORT, user=USERNAME, dbname=dbname, | ||
password=PASSWORD, connect_timeout=TIMEOUT) | ||
conn.autocommit = True | ||
cur = conn.cursor() | ||
|
||
# TABLES | ||
sql1 = "SELECT tablename FROM pg_tables WHERE schemaname='public' " \ | ||
"AND tablename <> 'spatial_ref_sys'" | ||
cur.execute(sql1) | ||
for table in cur.fetchall(): | ||
sql2 = "ALTER TABLE public.%s SET SCHEMA %s" % (table[0], args.plugin_user) | ||
print(sql2) | ||
cur.execute(sql2) | ||
|
||
# SEQUENCES | ||
sql3 = "SELECT sequencename FROM pg_sequences WHERE schemaname='public'" | ||
cur.execute(sql3) | ||
for sequence in cur.fetchall(): | ||
sql4 = "ALTER SEQUENCE public.%s SET SCHEMA %s" % (sequence[0], args.plugin_user) | ||
print(sql4) | ||
cur.execute(sql4) | ||
|
||
# INDEXES | ||
sql5 = "SELECT indexname FROM pg_indexes WHERE schemaname='public' " \ | ||
"AND tablename <> 'spatial_ref_sys'" | ||
cur.execute(sql5) | ||
for index in cur.fetchall(): | ||
sql6 = "ALTER INDEX public.%s SET SCHEMA %s" % (index[0], args.plugin_user) | ||
print(sql6) | ||
cur.execute(sql6) |