Skip to content

Commit

Permalink
Add sequence setval to light dump script, remove PII
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanw committed Oct 18, 2023
1 parent dc03d0d commit 4b22d36
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions scripts/create_light_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
r'ALTER TABLE ONLY (?P<table>public\.\w+)\s+ADD CONSTRAINT (?P<constraint>["\w]+) FOREIGN KEY \((?P<fk>\w+)\) REFERENCES (?P<fk_table>public\.\w+)\((?P<field>\w+)\)(?P<tail>[^;]*);'
)
TABLE_RE = re.compile(r"CREATE TABLE (public\.\w+) ")
SEQUENCE_RE = re.compile(
r"ALTER TABLE ONLY (?P<table>public\.\w+) ALTER COLUMN (?P<column>\w+) SET DEFAULT nextval\('(?P<sequence>public\.\w+)'::regclass\);"
)

SQL_TEMPLATE = """
ALTER TABLE ONLY {table}
Expand Down Expand Up @@ -206,8 +209,26 @@ def generate_copy_script(
outfile.write(cmd)
outfile.write("unset PGPASSWORD\n")

EXTRA_SQL = [
"UPDATE account_user SET password = ''",
"UPDATE public.publicbody_publicbody SET _created_by_id = NULL",
"UPDATE public.publicbody_publicbody SET _updated_by_id = NULL",
]
for sql in EXTRA_SQL:
outfile.write(
f"""psql -c "{escape_quote(sql)}" {target_connection} {target_db}\n"""
)

outfile.write(f"psql {target_connection} {target_db} < constraints.sql\n")

matches = SEQUENCE_RE.findall(schema)
for match in matches:
table, column, sequence = match
sql = f"""SELECT SETVAL('{sequence}', COALESCE(MAX({column}), 1) ) FROM {table};"""
outfile.write(
f"""psql -c "{escape_quote(sql)}" {target_connection} {target_db}\n"""
)


def show_unsafe(safe_tables="safe_tables.txt", schema_file="schema.sql"):
with open(schema_file) as f:
Expand Down

0 comments on commit 4b22d36

Please sign in to comment.