forked from Oslandia/qgis-versioning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversioning_base.py
1699 lines (1507 loc) · 72.1 KB
/
versioning_base.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
""" This module provides functions to version a prostgis DB and interact
with this DB. User can checkout a working copy, update and commit.
"""
import re
import os
import getpass
from pyspatialite import dbapi2
import psycopg2
import codecs
def escape_quote(msg):
"""quote single quotes"""
return str.replace(str(msg),"'","''");
def quote_ident(ident):
"""Add quotes around identifier if it contains spaces"""
if ident.find(' '):
return '"'+ident+'"'
else:
return ident
class Db:
"""Basic wrapper arround DB cursor that allows for logging SQL commands"""
def __init__(self, con, filename = ''):
"""The passed connection must be closed with close()"""
self.con = con
if isinstance(con, dbapi2.Connection):
self.db_type = 'sp : '
else :
self.db_type = 'pg : '
self.cur = self.con.cursor()
if filename :
self.log = codecs.open( filename, 'w', 'utf-8' )
self.log.write('-- openning connection\n')
else :
self.log = None
self.begun = False
self._verbose = False
def hasrow(self):
"""Test if previous execute returned rows"""
if self._verbose:
print self.db_type, self.cur.rowcount, ' rows returned'
return self.cur.rowcount > 0
def verbose(self, verbose):
"""Set verbose level"""
self._verbose = verbose
def execute(self, sql):
"""Execute SQL command"""
if not self.begun:
self.begun = True
if self._verbose:
print self.db_type, 'BEGIN;'
if self.log :
self.log.write( 'BEGIN;\n')
if self._verbose:
print self.db_type, sql, ';'
if self.log :
self.log.write(sql+';\n')
self.cur.execute( sql )
def fetchall(self):
"""Returns the result of the previous execute as a list of tuples"""
return self.cur.fetchall()
def fetchone(self):
"""Returns on row of result of the previous execute as a tuple"""
return self.cur.fetchone()
def commit(self):
"""Commit previous SQL command to DB, not necessary for SELECT"""
if self._verbose:
print self.db_type, 'END;'
if self.log :
self.log.write('END;\n')
self.begun = False
self.con.commit()
def close(self):
"""Close DB connection"""
if self.begun :
if self._verbose:
print self.db_type, 'END;'
if self.log :
self.log.write('END;\n')
if self.log :
self.log.write('-- closing connection\n')
self.con.close()
def get_username():
"""Returns user name"""
return getpass.getuser()
def pg_pk( cur, schema_name, table_name ):
"""Fetch the primary key of the specified postgis table"""
cur.execute("SELECT c.column_name "
"FROM information_schema.table_constraints tc "
"JOIN information_schema.constraint_column_usage AS ccu "
"USING (constraint_schema, constraint_name) "
"JOIN information_schema.columns AS c "
"ON c.table_schema = tc.constraint_schema "
"AND tc.table_name = c.table_name "
"AND ccu.column_name = c.column_name "
"WHERE constraint_type = 'PRIMARY KEY' "
"AND tc.table_schema = '"+schema_name+"' "
"AND tc.table_name = '"+table_name+"'")
if not cur.hasrow():
raise RuntimeError("table "+schema_name+"."+table_name+
" does not have a primary key")
[pkey] = cur.fetchone()
return pkey
def pg_array_elem_type( cur, schema, table, column ):
"""Fetch type of elements of a column of type ARRAY"""
cur.execute("SELECT e.data_type FROM information_schema.columns c "
"LEFT JOIN information_schema.element_types e "
"ON ((c.table_catalog, c.table_schema, c.table_name, "
"'TABLE', c.dtd_identifier) "
"= (e.object_catalog, e.object_schema, e.object_name, "
"e.object_type, e.collection_type_identifier)) "
"WHERE c.table_schema = '"+schema+"' "
"AND c.table_name = '"+table+"' "
"AND c.column_name = '"+column+"'")
if not cur.hasrow():
raise RuntimeError('column '+column+' of '
+schema+'.'+table+' is not an ARRAY')
[res] = cur.fetchone()
return res
def pg_geoms( cur, schema_name, table_name ):
"""Fetch the list of geometry column of the specified postgis table, empty if none"""
cur.execute("SELECT f_geometry_column FROM geometry_columns "
"WHERE f_table_schema = '"+schema_name+"' "
"AND f_table_name = '"+table_name+"'")
return [ geo[0] for geo in cur.fetchall() ]
def pg_geom( cur, schema_name, table_name ):
"""Fetch the first geometry column of the specified postgis table, empty string if none"""
geoms = pg_geoms( cur, schema_name, table_name )
if not geoms:
return ''
elif len(geoms) == 1:
return geoms[0]
elif 'VERSIONING_GEOMETRY_COLUMN' in os.environ:
if os.environ['VERSIONING_GEOMETRY_COLUMN'] in geoms:
return os.environ['VERSIONING_GEOMETRY_COLUMN']
else:
raise RuntimeError('more than one geometry column in '
+schema_name+'.'+table_name+' but none is '
+os.environ['VERSIONING_GEOMETRY_COLUMN']+
' (i.e. the value of VERSIONING_GEOMETRY_COLUMN) ')
elif 'geometry' in geoms:
return 'geometry'
else:
raise RuntimeError('more than one geometry column in '
+schema_name+'.'+table_name+
' but the environment variable VERSIONING_GEOMETRY_COLUMN '
'is not defined and the geometry column name is not geometry')
def unresolved_conflicts(sqlite_filename):
"""return a list of tables with unresolved conflicts"""
found = []
scur = Db(dbapi2.connect(sqlite_filename))
scur.execute("SELECT tbl_name FROM sqlite_master "
"WHERE type='table' AND tbl_name LIKE '%_conflicts'")
for table_conflicts in scur.fetchall():
print 'table_conflicts:', table_conflicts[0]
scur.execute("SELECT * FROM "+table_conflicts[0])
if scur.fetchone():
found.append( table_conflicts[0][:-10] )
scur.commit()
scur.close()
return found
def checkout(pg_conn_info, pg_table_names, sqlite_filename):
"""create working copy from versioned database tables
pg_table_names must be complete schema.table names
the schema name must end with _branch_rev_head
the file sqlite_filename must not exists
the views and trigger for local edition will be created
along with the tables and triggers for conflict resolution"""
if os.path.isfile(sqlite_filename):
raise RuntimeError("File "+sqlite_filename+" already exists")
for pg_table_name in pg_table_names:
[schema, table] = pg_table_name.split('.')
if not ( schema and table and schema[-9:] == "_rev_head"):
raise RuntimeError("Schema names must end with "
"suffix _branch_rev_head")
pcur = Db(psycopg2.connect(pg_conn_info))
first_table = True
for pg_table_name in pg_table_names:
[schema, table] = pg_table_name.split('.')
[schema, sep, branch] = schema[:-9].rpartition('_')
del sep
# fetch the current rev
pcur.execute("SELECT MAX(rev) FROM "+schema+".revisions")
current_rev = int(pcur.fetchone()[0])
# max pkey for this table
pkey = pg_pk( pcur, schema, table )
pcur.execute("SELECT MAX("+pkey+") FROM "+schema+"."+table)
[max_pg_pk] = pcur.fetchone()
if not max_pg_pk :
max_pg_pk = 0
# use ogr2ogr to create spatialite db
if first_table:
first_table = False
cmd = ['ogr2ogr',
'-preserve_fid',
'-f', 'SQLite',
'-dsco', 'SPATIALITE=yes',
sqlite_filename,
'PG:"'+pg_conn_info+'"', schema+'.'+table,
'-nln', table]
#print ' '.join(cmd)
os.system(' '.join(cmd))
# save target revision in a table
scur = Db(dbapi2.connect(sqlite_filename))
scur.execute("CREATE TABLE initial_revision AS SELECT "+
str(current_rev)+" AS rev, '"+
branch+"' AS branch, '"+
schema+"' AS table_schema, '"+
table+"' AS table_name, "+
str(max_pg_pk)+" AS max_pk")
scur.commit()
scur.close()
else:
cmd = ['ogr2ogr',
'-preserve_fid',
'-f', 'SQLite',
'-update',
sqlite_filename,
'PG:"'+pg_conn_info+'"', schema+'.'+table,
'-nln', table]
#print ' '.join(cmd)
os.system(' '.join(cmd))
# save target revision in a table if not in there
scur = Db(dbapi2.connect(sqlite_filename))
scur.execute("INSERT INTO initial_revision"
"(rev, branch, table_schema, table_name, max_pk) "
"VALUES ("+str(current_rev)+", '"+branch+"', '"+
schema+"', '"+table+"', "+str(max_pg_pk)+")" )
scur.commit()
scur.close()
scur = Db(dbapi2.connect(sqlite_filename))
# create views and triggers in spatilite db
scur.execute("PRAGMA table_info("+table+")")
cols = ""
newcols = ""
hcols = ['OGC_FID'] + sum([[brch+'_rev_begin', brch+'_rev_end',
brch+'_parent', brch+'_child'] for brch in pg_branches( pcur, schema ) ],[])
for res in scur.fetchall():
if res[1] not in hcols :
cols += quote_ident(res[1]) + ", "
newcols += "new."+quote_ident(res[1])+", "
cols = cols[:-2]
newcols = newcols[:-2] # remove last coma
scur.execute( "CREATE VIEW "+table+"_view "+"AS "
"SELECT ROWID AS ROWID, OGC_FID, "+cols+" "
"FROM "+table+" WHERE "+branch+"_rev_end IS NULL "
"AND "+branch+"_rev_begin IS NOT NULL")
max_fid_sub = ("( SELECT MAX(max_fid) FROM ( SELECT MAX(OGC_FID) AS "
"max_fid FROM "+table+" UNION SELECT max_pk AS max_fid "
"FROM initial_revision WHERE table_name = '"+table+"') )")
current_rev_sub = ("(SELECT rev FROM initial_revision "
"WHERE table_name = '"+table+"')")
scur.execute("DELETE FROM views_geometry_columns "
"WHERE view_name = '"+table+"_view'")
if 'GEOMETRY' in cols:
scur.execute("INSERT INTO views_geometry_columns "
"(view_name, view_geometry, view_rowid, "
"f_table_name, f_geometry_column, read_only) "
"VALUES"+"('"+table+"_view', 'geometry', 'rowid', '"
+table+"', 'geometry', 0)")
# when we edit something old, we insert and update parent
scur.execute(
"CREATE TRIGGER update_old_"+table+" "
"INSTEAD OF UPDATE ON "+table+"_view "
"WHEN (SELECT COUNT(*) FROM "+table+" "
"WHERE OGC_FID = new.OGC_FID "
"AND ("+branch+"_rev_begin <= "+current_rev_sub+" ) ) \n"
"BEGIN\n"
"INSERT INTO "+table+" "
"(OGC_FID, "+cols+", "+branch+"_rev_begin, "
+branch+"_parent) "
"VALUES "
"("+max_fid_sub+"+1, "+newcols+", "+current_rev_sub+"+1, "
"old.OGC_FID);\n"
"UPDATE "+table+" SET "+branch+"_rev_end = "+current_rev_sub+", "
+branch+"_child = "+max_fid_sub+" WHERE OGC_FID = old.OGC_FID;\n"
"END")
# when we edit something new, we just update
scur.execute("CREATE TRIGGER update_new_"+table+" "
"INSTEAD OF UPDATE ON "+table+"_view "
"WHEN (SELECT COUNT(*) FROM "+table+" "
"WHERE OGC_FID = new.OGC_FID AND ("+branch+"_rev_begin > "
+current_rev_sub+" ) ) \n"
"BEGIN\n"
"REPLACE INTO "+table+" "
"(OGC_FID, "+cols+", "+branch+"_rev_begin, "+branch+"_parent) "
"VALUES "
"(new.OGC_FID, "+newcols+", "+current_rev_sub+"+1, (SELECT "
+branch+"_parent FROM "+table+
" WHERE OGC_FID = new.OGC_FID));\n"
"END")
scur.execute("CREATE TRIGGER insert_"+table+" "
"INSTEAD OF INSERT ON "+table+"_view\n"
"BEGIN\n"
"INSERT INTO "+table+" "+
"(OGC_FID, "+cols+", "+branch+"_rev_begin) "
"VALUES "
"("+max_fid_sub+"+1, "+newcols+", "+current_rev_sub+"+1);\n"
"END")
scur.execute("CREATE TRIGGER delete_"+table+" "
"INSTEAD OF DELETE ON "+table+"_view\n"
"BEGIN\n"
# update it if its old
"UPDATE "+table+" "
"SET "+branch+"_rev_end = "+current_rev_sub+" "
"WHERE OGC_FID = old.OGC_FID "
"AND "+branch+"_rev_begin < "+current_rev_sub+"+1;\n"
# delete it if its new and remove it from child
"UPDATE "+table+" "
"SET "+branch+"_child = NULL "
"WHERE "+branch+"_child = old.OGC_FID "
"AND "+branch+"_rev_begin = "+current_rev_sub+"+1;\n"
"DELETE FROM "+table+" "
"WHERE OGC_FID = old.OGC_FID "
"AND "+branch+"_rev_begin = "+current_rev_sub+"+1;\n"
"END")
scur.commit()
scur.close()
pcur.close()
def update(sqlite_filename, pg_conn_info):
"""merge modifications since last update into working copy"""
print "update"
if unresolved_conflicts(sqlite_filename):
raise RuntimeError("There are unresolved conflicts in "
+sqlite_filename)
# get the target revision from the spatialite db
# create the diff in postgres
# load the diff in spatialite
# detect conflicts and create conflict layers
# merge changes and update target_revision
# delete diff
scur = Db(dbapi2.connect(sqlite_filename))
scur.execute("SELECT rev, branch, table_schema, table_name, max_pk "
"FROM initial_revision")
versioned_layers = scur.fetchall()
for [rev, branch, table_schema, table, current_max_pk] in versioned_layers:
pcur = Db(psycopg2.connect(pg_conn_info))
pcur.execute("SELECT MAX(rev) FROM "+table_schema+".revisions "
"WHERE branch = '"+branch+"'")
[max_rev] = pcur.fetchone()
if max_rev == rev:
print ("Nothing new in branch "+branch+" in "+table_schema+"."
+table+" since last update")
pcur.close()
continue
# get the max pkey
pkey = pg_pk( pcur, table_schema, table )
pgeom = pg_geom( pcur, table_schema, table )
pcur.execute("SELECT MAX("+pkey+") FROM "+table_schema+"."+table)
[max_pg_pk] = pcur.fetchone()
if not max_pg_pk :
max_pg_pk = 0
# create the diff
diff_schema = (table_schema+"_"+branch+"_"+str(rev)+
"_to_"+str(max_rev)+"_diff")
pcur.execute("SELECT schema_name FROM information_schema.schemata "
"WHERE schema_name = '"+diff_schema+"'")
if not pcur.fetchone():
pcur.execute("CREATE SCHEMA "+diff_schema)
other_branches = pg_branches( pcur, table_schema ).remove(branch)
other_branches = other_branches if other_branches else []
other_branches_columns = sum([
[brch+'_rev_begin', brch+'_rev_end',
brch+'_parent', brch+'_child']
for brch in other_branches], [])
pcur.execute("SELECT column_name "
"FROM information_schema.columns "
"WHERE table_schema = '"+table_schema+"' "
"AND table_name = '"+table+"'")
cols = ""
for col in pcur.fetchall():
if col[0] != pgeom and col[0] not in other_branches_columns:
cols += quote_ident(col[0])+", "
cols = cols[:-2] # remove last coma and space
pcur.execute("SELECT srid, type "
"FROM geometry_columns "
"WHERE f_table_schema = '"+table_schema+
"' AND f_table_name ='"+table+"' AND f_geometry_column = '"+pgeom+"'")
srid_type = pcur.fetchone()
[srid, geom_type] = srid_type if srid_type else [None, None]
pcur.execute( "DROP TABLE IF EXISTS "+diff_schema+"."+table+"_diff")
geom = (", "+pgeom+"::geometry('"+geom_type+"', "+str(srid)+") "
"AS "+pgeom) if pgeom else ''
pcur.execute( "CREATE TABLE "+diff_schema+"."+table+"_diff AS "
"SELECT "+cols+geom+" "
"FROM "+table_schema+"."+table+" "
"WHERE "+branch+"_rev_end = "+str(rev)+" "
"OR "+branch+"_rev_begin > "+str(rev))
pcur.execute( "ALTER TABLE "+diff_schema+"."+table+"_diff "
"ADD CONSTRAINT "+table+"_"+branch+"_pk_pk "
"PRIMARY KEY ("+pkey+")")
pcur.commit()
scur.execute("DROP TABLE IF EXISTS "+table+"_diff")
scur.execute("DROP TABLE IF EXISTS idx_"+table+"_diff_GEOMETRY")
scur.execute("DELETE FROM geometry_columns "
"WHERE f_table_name = '"+table+"_diff'")
scur.commit()
# import the diff to spatialite
cmd = ['ogr2ogr',
'-preserve_fid',
'-f', 'SQLite',
'-update',
sqlite_filename,
'PG:"'+pg_conn_info+'"',
diff_schema+'.'+table+"_diff",
'-nln', table+"_diff"]
print ' '.join(cmd)
os.system(' '.join(cmd))
# cleanup in postgis
pcur.execute("DROP SCHEMA "+diff_schema+" CASCADE")
pcur.commit()
pcur.close()
scur.execute("PRAGMA table_info("+table+")")
cols = ""
for col in scur.fetchall():
cols += quote_ident(col[1])+", "
cols = cols[:-2] # remove last coma and space
# update the initial revision
scur.execute("UPDATE initial_revision "
"SET rev = "+str(max_rev)+", max_pk = "+str(max_pg_pk)+" "
"WHERE table_name = '"+table+"'")
scur.execute("UPDATE "+table+" "
"SET "+branch+"_rev_end = "+str(max_rev)+" "
"WHERE "+branch+"_rev_end = "+str(rev))
scur.execute("UPDATE "+table+" "
"SET "+branch+"_rev_begin = "+str(max_rev+1)+" "
"WHERE "+branch+"_rev_begin = "+str(rev+1))
# we cannot add constrain to the spatialite db in order to have
# spatialite update parent and child when we bump inserted pkey
# above the max pkey in the diff we must do this manually
bump = max_pg_pk - current_max_pk
assert( bump >= 0)
# now bump the pks of inserted rows in working copy
# note that to do that, we need to set a negative value because
# the UPDATE is not implemented correctly according to:
# http://stackoverflow.com/questions/19381350/simulate-order-by-in-sqlite-update-to-handle-uniqueness-constraint
scur.execute("UPDATE "+table+" "
"SET OGC_FID = -OGC_FID "
"WHERE "+branch+"_rev_begin = "+str(max_rev+1))
scur.execute("UPDATE "+table+" "
"SET OGC_FID = "+str(bump)+"-OGC_FID WHERE OGC_FID < 0")
# and bump the pkey in the child field
# not that we don't care for nulls since adding something
# to null is null
scur.execute("UPDATE "+table+" "
"SET "+branch+"_child = "+branch+"_child + "+str(bump)+" "
"WHERE "+branch+"_rev_end = "+str(max_rev))
# detect conflicts: conflict occur if two lines with the same pkey have
# been modified (i.e. have a non null child) or one has been removed
# and the other modified
scur.execute("DROP VIEW IF EXISTS "+table+"_conflicts_ogc_fid")
scur.execute("CREATE VIEW "+table+"_conflicts_ogc_fid AS "
"SELECT DISTINCT sl.OGC_FID as conflict_deleted_fid "
"FROM "+table+" AS sl, "+table+"_diff AS pg "
"WHERE sl.OGC_FID = pg.OGC_FID "
"AND sl."+branch+"_child != pg."+branch+"_child")
scur.execute("SELECT conflict_deleted_fid "
"FROM "+table+"_conflicts_ogc_fid" )
if scur.fetchone():
print "there are conflicts"
# add layer for conflicts
scur.execute("DROP TABLE IF EXISTS "+table+"_conflicts ")
scur.execute("CREATE TABLE "+table+"_conflicts AS "
# insert new features from mine
"SELECT "+branch+"_parent AS conflict_id, 'mine' AS origin, "
"'modified' AS action, "+cols+" "
"FROM "+table+", "+table+"_conflicts_ogc_fid AS cflt "
"WHERE OGC_FID = (SELECT "+branch+"_child FROM "+table+" "
"WHERE OGC_FID = conflict_deleted_fid) "
"UNION ALL "
# insert new features from theirs
"SELECT "+branch+"_parent AS conflict_id, 'theirs' AS origin, "
"'modified' AS action, "+cols+" "
"FROM "+table+"_diff "+", "+table+"_conflicts_ogc_fid AS cflt "
"WHERE OGC_FID = (SELECT "+branch+"_child FROM "+table+"_diff "
"WHERE OGC_FID = conflict_deleted_fid) "
# insert deleted features from mine
"UNION ALL "
"SELECT "+branch+"_parent AS conflict_id, 'mine' AS origin, "
"'deleted' AS action, "+cols+" "
"FROM "+table+", "+table+"_conflicts_ogc_fid AS cflt "
"WHERE OGC_FID = conflict_deleted_fid "
"AND "+branch+"_child IS NULL "
# insert deleted features from theirs
"UNION ALL "
"SELECT "+branch+"_parent AS conflict_id, 'theirs' AS origin, "
"'deleted' AS action, "+cols+" "
"FROM "+table+"_diff, "+table+"_conflicts_ogc_fid AS cflt "
"WHERE OGC_FID = conflict_deleted_fid "
"AND "+branch+"_child IS NULL" )
# identify conflicts for deleted
scur.execute("UPDATE "+table+"_conflicts "
"SET conflict_id = OGC_FID "+ "WHERE action = 'deleted'")
# now follow child if any for 'theirs' 'modified' since several
# edition could be made we want the very last child
while True:
scur.execute("SELECT conflict_id, OGC_FID, "+branch+"_child "
"FROM "+table+"_conflicts WHERE origin='theirs' "
"AND action='modified' AND "+branch+"_child IS NOT NULL")
res = scur.fetchall()
if not res :
break
# replaces each entries by it's child
for [cflt_id, fid, child] in res:
scur.execute("DELETE FROM "+table+"_conflicts "
"WHERE OGC_FID = "+str(fid))
scur.execute("INSERT INTO "+table+"_conflicts "
"SELECT "+str(cflt_id)+" AS conflict_id, "
"'theirs' AS origin, 'modified' AS action, "+cols+" "
"FROM "+table+"_diff "
"WHERE OGC_FID = "+str(child)+" "
"AND "+branch+"_rev_end IS NULL" )
scur.execute("INSERT INTO "+table+"_conflicts "
"SELECT "+str(cflt_id)+" AS conflict_id, "
"'theirs' AS origin, 'deleted' AS action, "+cols+" "
"FROM "+table+"_diff "
"WHERE OGC_FID = "+str(child)+" "
"AND "+branch+"_rev_end IS NOT NULL" )
scur.execute("DELETE FROM geometry_columns "
"WHERE f_table_name = '"+table+"_conflicts'")
if geom:
scur.execute("SELECT RecoverGeometryColumn("
"'"+table+"_conflicts', 'GEOMETRY', "
"(SELECT srid FROM geometry_columns "
"WHERE f_table_name='"+table+"'), "
"(SELECT GeometryType(geometry) FROM "+table+" LIMIT 1), "
"'XY')")
scur.execute("CREATE UNIQUE INDEX IF NOT EXISTS "
+table+"_conflicts_idx ON "+table+"_conflicts(OGC_FID)")
# create trigers such that on delete the conflict is resolved
# if we delete 'theirs', we set their child to our fid and
# their rev_end if we delete 'mine'... well, we delete 'mine'
scur.execute("DROP TRIGGER IF EXISTS delete_"+table+"_conflicts")
scur.execute("CREATE TRIGGER delete_"+table+"_conflicts "
"AFTER DELETE ON "+table+"_conflicts\n"
"BEGIN\n"
"DELETE FROM "+table+" "
"WHERE OGC_FID = old.OGC_FID AND old.origin = 'mine';\n"
"UPDATE "+table+" "
"SET "+branch+"_child = (SELECT OGC_FID "
"FROM "+table+"_conflicts "
"WHERE origin = 'mine' "
"AND conflict_id = old.conflict_id), "
+branch+"_rev_end = "+str(max_rev)+" "
"WHERE OGC_FID = old.OGC_FID AND old.origin = 'theirs';\n"
"UPDATE "+table+" "
"SET "+branch+"_parent = old.OGC_FID "
"WHERE OGC_FID = (SELECT OGC_FID "
"FROM "+table+"_conflicts WHERE origin = 'mine' "
"AND conflict_id = old.conflict_id) "
"AND old.origin = 'theirs';\n"
"DELETE FROM "+table+"_conflicts "
"WHERE conflict_id = old.conflict_id;\n"
"END")
scur.commit()
scur.execute("CREATE UNIQUE INDEX IF NOT EXISTS "
+table+"_diff_idx ON "+table+"_diff(OGC_FID)")
# insert and replace all in diff
scur.execute("INSERT OR REPLACE INTO "+table+" ("+cols+") "
"SELECT "+cols+" FROM "+table+"_diff")
scur.commit()
scur.close()
def late(sqlite_filename, pg_conn_info):
"""Return 0 if up to date, the number of commits in between otherwize"""
scur = Db(dbapi2.connect(sqlite_filename))
scur.execute("SELECT rev, branch, table_schema "
"FROM initial_revision")
versioned_layers = scur.fetchall()
if not versioned_layers:
raise RuntimeError("Cannot find versioned layer in "+sqlite_filename)
late_by = 0
for [rev, branch, table_schema] in versioned_layers:
pcur = Db(psycopg2.connect(pg_conn_info))
pcur.execute("SELECT MAX(rev) FROM "+table_schema+".revisions "
"WHERE branch = '"+branch+"'")
[max_rev] = pcur.fetchone()
late_by = max(max_rev - rev, late_by)
return late_by
def revision( sqlite_filename ):
"""returns the revision the working copy was created from plus one"""
scur = Db(dbapi2.connect(sqlite_filename))
scur.execute("SELECT rev "+ "FROM initial_revision")
rev = 0
for [res] in scur.fetchall():
if rev:
assert( res == rev)
else :
rev = res
scur.close()
return rev+ 1
def commit(sqlite_filename, commit_msg, pg_conn_info):
"""merge modifications into database
returns the number of updated layers"""
# get the target revision from the spatialite db
# create the diff in postgres
# load the diff in spatialite
# detect conflicts
# merge changes and update target_revision
# delete diff
unresolved = unresolved_conflicts(sqlite_filename)
if unresolved:
raise RuntimeError("There are unresolved conflicts in "
+sqlite_filename+" for table(s) "+', '.join(unresolved) )
late_by = late(sqlite_filename, pg_conn_info)
if late_by:
raise RuntimeError("Working copy "+sqlite_filename+
" is not up to date. "
"It's late by "+str(late_by)+" commit(s).\n\n"
"Please update before commiting your modifications")
scur = Db(dbapi2.connect(sqlite_filename))
scur.execute("SELECT rev, branch, table_schema, table_name "
"FROM initial_revision")
versioned_layers = scur.fetchall()
if not versioned_layers:
raise RuntimeError("Cannot find a versioned layer in "+sqlite_filename)
schema_list = {} # for final cleanup
nb_of_updated_layer = 0
next_rev = 0
for [rev, branch, table_schema, table] in versioned_layers:
diff_schema = (table_schema+"_"+branch+"_"+str(rev)+
"_to_"+str(rev+1)+"_diff")
if next_rev:
assert( next_rev == rev + 1 )
else:
next_rev = rev + 1
scur.execute( "DROP TABLE IF EXISTS "+table+"_diff")
# note, creating the diff table dirrectly with
# CREATE TABLE... AS SELECT won't work
# types get fubared in the process
# therefore we copy the creation statement from
# spatialite master and change the
# table name ta obtain a similar table, we add the geometry column
# to geometry_columns manually and we insert the diffs
scur.execute("SELECT sql FROM sqlite_master "
"WHERE tbl_name = '"+table+"' AND type = 'table'")
[sql] = scur.fetchone()
sql = unicode.replace(sql, table, table+"_diff", 1)
scur.execute(sql)
geom = (sql.find('GEOMETRY') != -1)
scur.execute("DELETE FROM geometry_columns "
"WHERE f_table_name = '"+table+"_diff'")
if geom:
scur.execute("INSERT INTO geometry_columns "
"SELECT '"+table+"_diff', 'geometry', geometry_type, "
"coord_dimension, srid, spatial_index_enabled "
"FROM geometry_columns WHERE f_table_name = '"+table+"'")
scur.execute( "INSERT INTO "+table+"_diff "
"SELECT * "
"FROM "+table+" "
"WHERE "+branch+"_rev_end = "+str(rev)+" "
"OR "+branch+"_rev_begin > "+str(rev))
scur.execute( "SELECT OGC_FID FROM "+table+"_diff")
there_is_something_to_commit = scur.fetchone()
print "there_is_something_to_commit ", there_is_something_to_commit
scur.commit()
pcur = Db(psycopg2.connect(pg_conn_info))
pkey = pg_pk( pcur, table_schema, table )
pgeom = pg_geom( pcur, table_schema, table )
# import layers in postgis schema
pcur.execute("SELECT schema_name FROM information_schema.schemata "
"WHERE schema_name = '"+diff_schema+"'")
if not pcur.fetchone():
schema_list[diff_schema] = pg_conn_info
pcur.execute("CREATE SCHEMA "+diff_schema)
pcur.execute( "DROP TABLE IF EXISTS "+diff_schema+"."+table+"_diff")
pcur.commit()
cmd = ['ogr2ogr',
'-preserve_fid',
'-f',
'PostgreSQL',
'PG:"'+pg_conn_info+'"',
'-lco',
'FID='+pkey,
sqlite_filename, table+"_diff",
'-nln', diff_schema+'.'+table+"_diff"]
if pgeom:
cmd.insert(5, '-lco')
cmd.insert(6, 'GEOMETRY_NAME='+pgeom)
print ' '.join(cmd)
os.system(' '.join(cmd))
# remove dif table and geometry column
scur.execute("DELETE FROM geometry_columns "
"WHERE f_table_name = '"+table+"_diff'")
scur.execute("DROP TABLE "+table+"_diff")
if not there_is_something_to_commit:
print "nothing to commit for ", table
pcur.close()
continue
nb_of_updated_layer += 1
pcur.execute("SELECT rev FROM "+table_schema+".revisions "
"WHERE rev = "+str(rev+1))
if not pcur.fetchone():
print "inserting rev ", str(rev+1)
pcur.execute("INSERT INTO "+table_schema+".revisions "
"(rev, commit_msg, branch, author) "
"VALUES ("+str(rev+1)+", '"+escape_quote(commit_msg)+"', '"+branch+"',"
"'"+get_username()+"')")
# TODO remove when ogr2ogr will be able to convert multiple geom column
# from postgis to spatialite
geoms = pg_geoms( pcur, table_schema, table )
if len(geoms) > 1:
dest_geom = ''
src_geom = ''
for geo in geoms:
if geo != pgeom:
dest_geom += geo+', '
src_geom += 'src.'+geo+', '
dest_geom = dest_geom[:-2]
src_geom = src_geom[:-2]
pgeom = pg_geom( pcur, table_schema, table )
pcur.execute("SELECT AddGeometryColumn('"+diff_schema+"', '"+table+"_diff', "
"'"+geo+"', srid, type, coord_dimension) FROM geometry_columns "
"WHERE f_table_name = '"+table+"' "
"AND f_table_schema = '"+table_schema+"' "
"AND f_geometry_column != '"+pgeom+"'")
pcur.execute("UPDATE "+diff_schema+"."+table+"_diff AS dest "
"SET ("+dest_geom+") = ("+src_geom+") "
"FROM "+table_schema+"."+table+" AS src "
"WHERE dest."+branch+"_rev_begin = "+str(rev+1)+" "
"AND src."+pkey+" = dest."+branch+"_parent")
other_branches = pg_branches( pcur, table_schema ).remove(branch)
other_branches = other_branches if other_branches else []
other_branches_columns = sum([
[brch+'_rev_begin', brch+'_rev_end',
brch+'_parent', brch+'_child']
for brch in other_branches], [])
pcur.execute("SELECT column_name, data_type "
"FROM information_schema.columns "
"WHERE table_schema = '"+table_schema+"' "
"AND table_name = '"+table+"'")
cols = ""
cols_cast = ""
for col in pcur.fetchall():
if col[0] not in other_branches_columns:
cols += quote_ident(col[0])+", "
if col[1] != 'ARRAY':
cast = "::"+col[1] if col[1] != 'USER-DEFINED' else ""
cols_cast += quote_ident(col[0])+cast+", "
else :
cols_cast += ("regexp_replace(regexp_replace("
+col[0]+",'^\(.*:','{'),'\)$','}')::"
+pg_array_elem_type(pcur,
table_schema, table, col[0])+"[], ")
cols = cols[:-2] # remove last coma and space
cols_cast = cols_cast[:-2] # remove last coma and space
# insert inserted and modified
pcur.execute("INSERT INTO "+table_schema+"."+table+" ("+cols+") "
"SELECT "+cols_cast+" FROM "+diff_schema+"."+table+"_diff "
"WHERE "+branch+"_rev_begin = "+str(rev+1))
# apdate deleted and modified
pcur.execute("UPDATE "+table_schema+"."+table+" AS dest "
"SET ("+branch+"_rev_end, "+branch+"_child)"
"=(src."+branch+"_rev_end, src."+branch+"_child) "
"FROM "+diff_schema+"."+table+"_diff AS src "
"WHERE dest."+pkey+" = src."+pkey+" "
"AND src."+branch+"_rev_end = "+str(rev))
pcur.commit()
pcur.close()
scur.commit()
if nb_of_updated_layer:
for [rev, branch, table_schema, table] in versioned_layers:
pcur = Db(psycopg2.connect(pg_conn_info))
pkey = pg_pk( pcur, table_schema, table )
pcur.execute("SELECT MAX(rev) FROM "+table_schema+".revisions")
[rev] = pcur.fetchone()
pcur.execute("SELECT MAX("+pkey+") FROM "+table_schema+"."+table)
[max_pk] = pcur.fetchone()
if not max_pk :
max_pk = 0
pcur.close()
scur.execute("UPDATE initial_revision "
"SET rev = "+str(rev)+", max_pk = "+str(max_pk)+" "
"WHERE table_schema = '"+table_schema+"' "
"AND table_name = '"+table+"' "
"AND branch = '"+branch+"'")
scur.commit()
scur.close()
# cleanup diffs in postgis
for schema, conn_info in schema_list.iteritems():
pcur = Db(psycopg2.connect(conn_info))
pcur.execute("DROP SCHEMA "+schema+" CASCADE")
pcur.commit()
pcur.close()
return nb_of_updated_layer
def historize( pg_conn_info, schema ):
"""Create historisation for the given schema"""
if not schema:
raise RuntimeError("no schema specified")
pcur = Db(psycopg2.connect(pg_conn_info))
pcur.execute("CREATE TABLE "+schema+".revisions ("
"rev serial PRIMARY KEY, "
"commit_msg varchar, "
"branch varchar DEFAULT 'trunk', "
"date timestamp DEFAULT current_timestamp, "
"author varchar)")
pcur.commit()
pcur.close()
add_branch( pg_conn_info, schema, 'trunk', 'initial commit' )
def add_branch( pg_conn_info, schema, branch, commit_msg,
base_branch='trunk', base_rev='head' ):
"""Create a new branch (add 4 columns to tables)"""
pcur = Db(psycopg2.connect(pg_conn_info))
# check that branch doesn't exist and that base_branch exists
# and that base_rev is ok
pcur.execute("SELECT * FROM "+schema+".revisions "
"WHERE branch = '"+branch+"'")
if pcur.fetchone():
pcur.close()
raise RuntimeError("Branch "+branch+" already exists")
pcur.execute("SELECT * FROM "+schema+".revisions "
"WHERE branch = '"+base_branch+"'")
if branch != 'trunk' and not pcur.fetchone():
pcur.close()
raise RuntimeError("Base branch "+base_branch+" doesn't exist")
pcur.execute("SELECT MAX(rev) FROM "+schema+".revisions")
[max_rev] = pcur.fetchone()
if not max_rev:
max_rev = 0
if base_rev != 'head' and (int(base_rev) > max_rev or int(base_rev) <= 0):
pcur.close()
raise RuntimeError("Revision "+str(base_rev)+" doesn't exist")
print 'max rev = ', max_rev
pcur.execute("INSERT INTO "+schema+".revisions(rev, branch, commit_msg ) "
"VALUES ("+str(max_rev+1)+", '"+branch+"', '"+escape_quote(commit_msg)+"')")
pcur.execute("CREATE SCHEMA "+schema+"_"+branch+"_rev_head")
history_columns = sum([
[brch+'_rev_end', brch+'_rev_begin',
brch+'_child', brch+'_parent' ] for brch in pg_branches( pcur, schema )],[])
security = ' WITH (security_barrier)'
pcur.execute("SELECT version()")
[version] = pcur.fetchone()
mtch = re.match( r'^PostgreSQL (\d+)\.(\d+)\.(\d+) ', version )
if mtch and int(mtch.group(1)) <= 9 and int(mtch.group(2)) <= 2 :
security = ''
# note: do not version views
pcur.execute("SELECT table_name FROM information_schema.tables "
"WHERE table_schema = '"+schema+"' "
"AND table_type = 'BASE TABLE'")
for [table] in pcur.fetchall():
if table == 'revisions':
continue
try:
pkey = pg_pk( pcur, schema, table )
except:
if 'VERSIONING_NO_PK' in os.environ and os.environ['VERSIONING_NO_PK'] == 'skip':
print schema+'.'+table+' has no primary key, skipping'
else:
raise RuntimeError(schema+'.'+table+' has no primary key')
pcur.execute("ALTER TABLE "+schema+"."+table+" "
"ADD COLUMN "+branch+"_rev_begin integer "
"REFERENCES "+schema+".revisions(rev), "
"ADD COLUMN "+branch+"_rev_end integer "
"REFERENCES "+schema+".revisions(rev), "
"ADD COLUMN "+branch+"_parent integer "
"REFERENCES "+schema+"."+table+"("+pkey+"),"
"ADD COLUMN "+branch+"_child integer "
"REFERENCES "+schema+"."+table+"("+pkey+")")
if branch == 'trunk': # initial versioning
pcur.execute("UPDATE "+schema+"."+table+" "
"SET "+branch+"_rev_begin = (SELECT MAX(rev) "
"FROM "+schema+".revisions)")
elif base_rev == "head":
pcur.execute("UPDATE "+schema+"."+table+" "
"SET "+branch+"_rev_begin = (SELECT MAX(rev) "
"FROM "+schema+".revisions "
"WHERE "+base_branch+"_rev_end IS NULL "
"AND "+base_branch+"_rev_begin IS NOT NULL)")
else:
pcur.execute("UPDATE "+schema+"."+table+" "
"SET "+branch+"_rev_begin = (SELECT MAX(rev) "
"FROM "+schema+".revisions)"
"WHERE ("+base_branch+"_rev_end IS NULL "
"OR "+base_branch+"_rev_end > "+base_rev+") "
"AND "+base_branch+"_rev_begin IS NOT NULL")
pcur.execute("SELECT column_name "
"FROM information_schema.columns "
"WHERE table_schema = '"+schema+"' "
"AND table_name = '"+table+"'")
cols = ""
for [col] in pcur.fetchall():
if col not in history_columns:
cols = quote_ident(col)+", "+cols
cols = cols[:-2] # remove last coma and space
pcur.execute("CREATE VIEW "+schema+"_"+branch+"_rev_head."+table+" "
+security+" AS "
"SELECT "+cols+" FROM "+schema+"."+table+" "
"WHERE "+branch+"_rev_end IS NULL "
"AND "+branch+"_rev_begin IS NOT NULL")
pcur.commit()
pcur.close()
def add_revision_view(pg_conn_info, schema, branch, rev):
"""Create schema with views of the specified revision"""
pcur = Db(psycopg2.connect(pg_conn_info))
pcur.execute("SELECT * FROM "+schema+".revisions "
"WHERE branch = '"+branch+"'")