This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_dump_splitsort.py
138 lines (114 loc) · 4.44 KB
/
pg_dump_splitsort.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
import os
import sys
import re
COPY_RE = re.compile(r'COPY .*? \(.*?\) FROM stdin;\n$')
def try_float(s):
if not s or s[0] not in '0123456789.-':
# optimization
return s
try:
return float(s)
except ValueError:
return s
def linecomp(l1, l2):
p1 = l1.split('\t', 1)
p2 = l2.split('\t', 1)
result = cmp(try_float(p1[0]), try_float(p2[0]))
if not result and len(p1) == len(p2) == 2:
return linecomp(p1[1], p2[1])
return result
DATA_COMMENT_RE = re.compile('-- Data for Name: (?P<table>.*?); '
'Type: TABLE DATA; '
'Schema: (?P<schema>.*?);')
SEQUENCE_SET_RE = re.compile(r'-- Name: .+; Type: SEQUENCE SET; Schema: |'
r"SELECT pg_catalog\.setval\('")
class Matcher(object):
def __init__(self):
self._match = None
def match(self, pattern, data):
self._match = pattern.match(data)
return self._match
def __getattr__(self, attname):
if not self._match:
raise ValueError('Pattern did not match')
return getattr(self._match, attname)
def split_sql_file(sql_filepath):
directory = os.path.dirname(sql_filepath)
output = None
buf = []
def flush():
output.writelines(buf)
buf[:] = []
def new_output(filename):
if output:
output.close()
return open(os.path.join(directory, filename), 'w')
copy_lines = None
counter = 0
output = new_output('0000_prologue.sql')
matcher = Matcher()
for line in open(sql_filepath):
if copy_lines is None:
if line in ('\n', '--\n'):
buf.append(line)
elif line.startswith('SET search_path = '):
flush()
buf.append(line)
else:
if matcher.match(DATA_COMMENT_RE, line):
counter += 1
output = new_output(
'{counter:04}_{schema}.{table}.sql'.format(
counter=counter,
schema=matcher.group('schema'),
table=matcher.group('table')))
elif COPY_RE.match(line):
copy_lines = []
elif SEQUENCE_SET_RE.match(line):
pass
elif 1 <= counter < 9999:
counter = 9999
output = new_output('%04d_epilogue.sql' % counter)
buf.append(line)
flush()
else:
if line == '\\.\n':
copy_lines.sort(cmp=linecomp)
buf.extend(copy_lines)
buf.append(line)
flush()
copy_lines = None
else:
copy_lines.append(line)
def main():
split_sql_file(sys.argv[1])
if __name__ == '__main__':
main()
# Copyright (c) 2010, Antti Kaihola
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of pgtricks nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.