Skip to content

Commit

Permalink
handle schema prefix (python version)
Browse files Browse the repository at this point in the history
  • Loading branch information
freddez committed Jan 31, 2022
1 parent 04b9c81 commit ea07b91
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions python/pg-dump2insert.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import fileinput
import re
start_table_pattern = re.compile("^COPY (\w+) \(([\w, ]+)\) FROM stdin;")

start_table_pattern = re.compile("^COPY ([\w\.]+) \(([\w, ]+)\) FROM stdin;")
table_name = None
fields = None
insert_mode = False
Expand All @@ -11,16 +12,18 @@
if line == "\\.\n":
insert_mode = False
continue
values = [ v == '\\N' and 'NULL' or "'%s'" % v \
for v in line[:-1].replace("'", "''").split("\t") ]
print "INSERT INTO %s (%s) VALUES (%s);" \
% (table_name, fields, ", ".join(values))
values = [
v == "\\N" and "NULL" or "'%s'" % v
for v in line[:-1].replace("'", "''").split("\t")
]
print(
"INSERT INTO %s (%s) VALUES (%s);" % (table_name, fields, ", ".join(values))
)
else:
start_table = start_table_pattern.match(line)
if start_table:
table_name = start_table.group(1)
fields = start_table.group(2)
insert_mode = True
continue
print line,

print(line, end=" ")

0 comments on commit ea07b91

Please sign in to comment.