10637.3.7
by Guilherme Salgado
merge devel |
1 |
#!/usr/bin/python -S
|
8687.15.9
by Karl Fogel
Add the copyright header block to more files (everything under database/). |
2 |
#
|
3 |
# Copyright 2009 Canonical Ltd. This software is licensed under the
|
|
4 |
# GNU Affero General Public License version 3 (see the file LICENSE).
|
|
1649
by Canonical.com Patch Queue Manager
SQL sorting script (r=Andrew) |
5 |
|
1841
by Canonical.com Patch Queue Manager
second-generation SQL sorter [r=Salgado] |
6 |
"""Script to sort SQL dumps.
|
7 |
||
8 |
This script reads a series of SQL statements on standard input, and prints
|
|
9 |
them out, possibly in a different order, on standard output.
|
|
10 |
||
11 |
When we dump data from the database, we do it in the form of SQL statements.
|
|
12 |
Most of these statements are INSERTs. We keep a dump in this form in revision
|
|
13 |
control for use as sample data.
|
|
14 |
||
15 |
The problem is that the statements are not dumped in a consistent order. This
|
|
16 |
means that it is much more likely for conflicts to occur when more than one
|
|
17 |
person works on the sample data at the same time. It also means the conflicts
|
|
18 |
are harder to resolve when they happen.
|
|
19 |
||
20 |
This script fixes the problem by sorting INSERT statements by the numeric
|
|
21 |
value of the first column. This works because the first column is the numeric
|
|
22 |
id of the row being inserted. Statements are sorted in blocks; i.e. contiguous
|
|
23 |
groups of statements separated by empty lines. This works because the dumps
|
|
24 |
happen by table, with one block of statements for each table.
|
|
25 |
"""
|
|
1649
by Canonical.com Patch Queue Manager
SQL sorting script (r=Andrew) |
26 |
|
27 |
__metaclass__ = type |
|
28 |
||
8677.1.3
by Stuart Bishop
All scripts need to import _pythonpath to function correctly with buildout |
29 |
# pylint: disable-msg=W0403
|
30 |
import _pythonpath |
|
31 |
||
32 |
import sys |
|
1649
by Canonical.com Patch Queue Manager
SQL sorting script (r=Andrew) |
33 |
|
1841
by Canonical.com Patch Queue Manager
second-generation SQL sorter [r=Salgado] |
34 |
from canonical.launchpad.scripts.sort_sql import Parser, print_lines_sorted |
1649
by Canonical.com Patch Queue Manager
SQL sorting script (r=Andrew) |
35 |
|
36 |
def main(argv): |
|
1717
by Canonical.com Patch Queue Manager
[r=spiv] Misc pending changes, including ProductSeries.name changing for bazaar imports, wiring up Daf's sampledata sorting script, enhancing the PostgreSQL test harness to ensure better test isolation (solves the issue of not resetting sequence values between tests, a possible case of occasional test failures). |
37 |
if len(argv) > 1: |
1841
by Canonical.com Patch Queue Manager
second-generation SQL sorter [r=Salgado] |
38 |
input = open(argv[1]) |
1717
by Canonical.com Patch Queue Manager
[r=spiv] Misc pending changes, including ProductSeries.name changing for bazaar imports, wiring up Daf's sampledata sorting script, enhancing the PostgreSQL test harness to ensure better test isolation (solves the issue of not resetting sequence values between tests, a possible case of occasional test failures). |
39 |
else: |
1841
by Canonical.com Patch Queue Manager
second-generation SQL sorter [r=Salgado] |
40 |
input = sys.stdin |
41 |
||
42 |
parser = Parser() |
|
43 |
||
44 |
for line in input: |
|
45 |
parser.feed(line) |
|
46 |
||
47 |
print_lines_sorted(sys.stdout, parser.lines) |
|
1649
by Canonical.com Patch Queue Manager
SQL sorting script (r=Andrew) |
48 |
|
49 |
return 0 |
|
50 |
||
51 |
if __name__ == '__main__': |
|
52 |
sys.exit(main(sys.argv)) |
|
53 |