-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_sqlite_export.py
104 lines (87 loc) · 3.18 KB
/
pg_sqlite_export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
import datetime
import psycopg2 as pg
from psycopg2 import sql
from psycopg2.extensions import AsIs
class PgSqliteExport:
def __init__(self, db_name, user, pw, host, port):
self.cxn_string = '''dbname={db}
user={user}
password={pw}
host={host}
port={port}
'''.format(db=db_name,
user=user,
pw=pw,
host=host,
port=port)
self.db = pg.connect(self.cxn_string)
self.db.autocommit = True
def __name__(self):
return 'pg_sqlite_export'
def _close(self):
self.db.close()
def _create_output_file(self):
cwd = os.getcwd()
self.output_file = '{cwd}/pgdb_export.sql'.format(cwd=cwd)
self._write_header()
def _finish_output_file(self):
self._write_footer()
def export_pg_data(self):
self._create_output_file()
self._get_all_tables()
self._export_all_table_data()
self._finish_output_file()
self._close()
def _get_all_tables(self):
curs = self.db.cursor()
stmt = """
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_type = 'BASE TABLE';
"""
curs.execute(stmt)
self.tables = [row[0] for row in curs]
curs.close()
def _export_all_table_data(self):
curs = self.db.cursor()
stmt = """
SELECT *
FROM %(tbl)s;
"""
for table in self.tables:
self._write_deletes(table)
curs.execute(stmt, {'tbl': AsIs(table)})
results = curs.fetchall()
for row in results:
insert_stmt = self._format_line(table, row)
self._write_sql(insert_stmt)
curs.close()
def _convert_dates(self, value):
if isinstance(value, datetime.date):
value = value.strftime('%Y-%m-%d')
# TODO: Add other checks here, like datetime, timestamp, etc
return value
def _format_line(self, table, values):
stmt = sql.SQL("INSERT INTO {tbl} VALUES ({vals});\n").format(
tbl=sql.Identifier(table),
vals=sql.SQL(', ').join(
sql.Literal(self._convert_dates(element)) for element in values)
)
return stmt.as_string(self.db)
def _write_sql(self, stmt):
with open(self.output_file, 'a') as file:
file.write(stmt)
def _write_header(self):
stmt = 'PRAGMA foreign_keys=0;\nBEGIN;\n'
with open(self.output_file, 'w+') as file:
file.write(stmt)
def _write_deletes(self, table):
stmt = sql.SQL('DELETE FROM {tbl};\n').format(tbl=sql.Identifier(table)).as_string(self.db)
with open(self.output_file, 'a') as file:
file.write(stmt)
def _write_footer(self):
stmt = 'COMMIT;'
with open(self.output_file, 'a') as file:
file.write(stmt)