def commit(self): """merge modifiactions into database""" print "commit" if self.unresolved_conflicts(): return layer = QgsMapLayerRegistry.instance().mapLayer(self.current_layers[0]) uri = QgsDataSourceURI(layer.source()) late_by = 0 if layer.providerType() == "spatialite": late_by = versioning_base.late(uri.database(), self.pg_conn_info()) else: #postgres late_by = versioning_base.pg_late(self.pg_conn_info(), uri.schema()) if late_by: QMessageBox.warning( self.iface.mainWindow(), "Warning", "This working copy is not up to date (late by " + str(late_by) + " commit(s)).\n\n" "Please update before commiting your modifications") print "aborted" return # time to get the commit message if not self.q_commit_msg_dlg.exec_(): return commit_msg = self.commit_msg_dlg.commitMessage.document().toPlainText() if not commit_msg: QMessageBox.warning(self.iface.mainWindow(), "Warning", "No commit message, aborting commit") print "aborted" return nb_of_updated_layer = 0 rev = 0 if layer.providerType() == "spatialite": nb_of_updated_layer = versioning_base.commit( uri.database(), commit_msg, self.pg_conn_info()) rev = versioning_base.revision(uri.database()) else: # postgres nb_of_updated_layer = versioning_base.pg_commit( uri.connectionInfo(), uri.schema(), commit_msg) rev = versioning_base.pg_revision(uri.connectionInfo(), uri.schema()) if nb_of_updated_layer: QMessageBox.information( self.iface.mainWindow(), "Info", "You have successfully commited revision " + str(rev)) else: QMessageBox.information(self.iface.mainWindow(), "Info", "There was no modification to commit")
def commit(self): """merge modifiactions into database""" print "commit" if self.unresolved_conflicts(): return layer = QgsMapLayerRegistry.instance().mapLayer( self.current_layers[0] ) uri = QgsDataSourceURI(layer.source()) late_by = 0 if layer.providerType() == "spatialite": late_by = versioning_base.late( uri.database(), self.pg_conn_info() ) else:#postgres late_by = versioning_base.pg_late( self.pg_conn_info(), uri.schema() ) if late_by: QMessageBox.warning(self.iface.mainWindow(), "Warning", "This working copy is not up to date (late by " +str(late_by)+" commit(s)).\n\n" "Please update before commiting your modifications") print "aborted" return # time to get the commit message if not self.q_commit_msg_dlg.exec_(): return commit_msg = self.commit_msg_dlg.commitMessage.document().toPlainText() if not commit_msg: QMessageBox.warning(self.iface.mainWindow(), "Warning", "No commit message, aborting commit") print "aborted" return nb_of_updated_layer = 0 rev = 0 if layer.providerType() == "spatialite": nb_of_updated_layer = versioning_base.commit( uri.database(), commit_msg, self.pg_conn_info() ) rev = versioning_base.revision(uri.database()) else: # postgres nb_of_updated_layer = versioning_base.pg_commit( uri.connectionInfo(), uri.schema(), commit_msg ) rev = versioning_base.pg_revision( uri.connectionInfo(), uri.schema()) if nb_of_updated_layer: QMessageBox.information(self.iface.mainWindow(), "Info", "You have successfully commited revision "+str( rev ) ) else: QMessageBox.information(self.iface.mainWindow(), "Info", "There was no modification to commit")
pcur.execute("SELECT * FROM epanet_mybranch_rev_2.junctions") assert( len(pcur.fetchall()) == 2 ) pcur.execute("SELECT * FROM epanet_mybranch_rev_2.pipes") assert( len(pcur.fetchall()) == 1 ) pcur.execute("SELECT ST_AsText(geometry), ST_AsText(geometry_schematic) FROM epanet_mybranch_rev_2.junctions") res = pcur.fetchall() assert( res[0][0] == 'POINT(0 0)' ) assert( res[1][1] == 'POLYGON((0 0,2 0,2 2,0 2,0 0))' ) wc = tmp_dir+'/wc_multiple_geometry_test.sqlite' if os.path.isfile(wc): os.remove(wc) versioning_base.checkout( pg_conn_info, ['epanet_trunk_rev_head.pipes','epanet_trunk_rev_head.junctions'], wc ) scur = versioning_base.Db( dbapi2.connect(wc) ) scur.execute("UPDATE junctions_view SET GEOMETRY = GeometryFromText('POINT(3 3)',2154)") scur.commit() scur.close() versioning_base.commit( wc, 'a commit msg', 'dbname=epanet_test_db' ) pcur.execute("SELECT ST_AsText(geometry), ST_AsText(geometry_schematic) FROM epanet_trunk_rev_head.junctions") res = pcur.fetchall() for r in res: print r assert( res[0][0] == 'POINT(3 3)' ) assert( res[1][1] == 'POLYGON((0 0,2 0,2 2,0 2,0 0))' ) pcur.close()
pcur.execute("SELECT ST_AsText(geometry), ST_AsText(geometry_schematic) FROM epanet.junctions") res = pcur.fetchall() assert( res[0][0] == 'POINT(0 0)' ) assert( res[1][1] == 'POLYGON((0 0,2 0,2 2,0 2,0 0))' ) wc = tmp_dir+'/wc_multiple_geometry_test.sqlite' if os.path.isfile(wc): os.remove(wc) versioning_base.checkout( pg_conn_info, ['epanet_trunk_rev_head.pipes','epanet_trunk_rev_head.junctions'], wc ) scur = versioning_base.Db( dbapi2.connect(wc) ) scur.execute("UPDATE junctions_view SET GEOMETRY = GeometryFromText('POINT(3 3)',2154) WHERE OGC_FID = 1") scur.commit() scur.close() versioning_base.commit( wc, 'moved a junction', 'dbname=epanet_test_db' ) pcur.execute("SELECT ST_AsText(geometry), ST_AsText(geometry_schematic) FROM epanet_trunk_rev_head.junctions ORDER BY hid DESC") res = pcur.fetchall() for r in res: print r assert( res[0][0] == 'POINT(3 3)' ) assert( res[0][1] == 'POLYGON((-1 -1,1 -1,1 1,-1 1,-1 -1))' ) pcur.close() # now we branch from head versioning_base.add_branch( pg_conn_info, 'epanet', 'b1', 'add branch b1' ) pcur = versioning_base.Db(psycopg2.connect(pg_conn_info)) pcur.execute("SELECT hid, trunk_rev_begin, trunk_rev_end, b1_rev_begin, b1_rev_end FROM epanet.junctions ORDER BY hid") for r in pcur.fetchall(): print r
import psycopg2 import os import shutil test_data_dir = os.path.dirname(os.path.realpath(__file__)) tmp_dir = "/tmp" # create the test database os.system("dropdb epanet_test_db") os.system("createdb epanet_test_db") os.system("psql epanet_test_db -c 'CREATE EXTENSION postgis'") os.system("psql epanet_test_db -f "+test_data_dir+"/epanet_test_db_unversioned.sql") versioning_base.historize("dbname=epanet_test_db","epanet") # try the update wc = tmp_dir+"/bug_in_branch_after_commit_wc.sqlite" if os.path.isfile(wc): os.remove(wc) versioning_base.checkout("dbname=epanet_test_db", ['epanet_trunk_rev_head.junctions', 'epanet_trunk_rev_head.pipes'], wc) scur = versioning_base.Db( dbapi2.connect( wc ) ) scur.execute("SELECT * FROM pipes") scur.execute("UPDATE pipes_view SET length = 1 WHERE OGC_FID = 1") scur.commit() versioning_base.commit(wc,'test', "dbname=epanet_test_db" ) versioning_base.add_branch("dbname=epanet_test_db","epanet","mybranch","add 'branch")
def commit(self): """merge modifications into database""" print "commit" if self.unresolved_conflicts(): return layer = QgsMapLayerRegistry.instance().mapLayer( self.current_layers[0] ) uri = QgsDataSourceURI(layer.source()) late_by = 0 if layer.providerType() == "spatialite": late_by = versioning_base.late( uri.database(), self.pg_conn_info() ) else:#postgres late_by = versioning_base.pg_late( self.pg_conn_info(), uri.schema() ) if late_by: QMessageBox.warning(self.iface.mainWindow(), "Warning", "This working copy is not up to date (late by " +str(late_by)+" commit(s)).\n\n" "Please update before committing your modifications") print "aborted" return # Make sure the combobox is visible; could be made invisible by a # previous call to branch self.q_commit_msg_dlg.pg_users_combobox.setVisible(True) self.q_commit_msg_dlg.pg_username_label.setVisible(True) # Populate combobox with list of pg usernames nb_items_in_list = self.q_commit_msg_dlg.pg_users_combobox.count() if not(nb_items_in_list) : self.get_pg_users_list() # Better if we could have a QgsDataSourceURI.username() but no such # thing in spatialite. Next block is for the case the username cannot # be found in the connection info string (mainly for plugin tests) try: pg_username = self.pg_conn_info().split(' ')[3].replace("'","").split('=')[1] current_user_index = self.q_commit_msg_dlg.pg_users_combobox.findText(pg_username) # sets the current pg_user in the combobox to come current_user_combobox_item = self.q_commit_msg_dlg.pg_users_combobox.setCurrentIndex(current_user_index) except (IndexError): pg_username = '' # time to get the commit message if not self.q_commit_msg_dlg.exec_(): return commit_msg = self.q_commit_msg_dlg.commitMessage.document().toPlainText() commit_pg_user = self.q_commit_msg_dlg.pg_users_combobox.itemText(self.q_commit_msg_dlg.pg_users_combobox.currentIndex()) if not commit_msg: QMessageBox.warning(self.iface.mainWindow(), "Warning", "No commit message, aborting commit") print "aborted" return nb_of_updated_layer = 0 rev = 0 if layer.providerType() == "spatialite": nb_of_updated_layer = versioning_base.commit( uri.database(), commit_msg, self.pg_conn_info(),commit_pg_user ) rev = versioning_base.revision(uri.database()) else: # postgres nb_of_updated_layer = versioning_base.pg_commit( uri.connectionInfo(), uri.schema(), commit_msg ) rev = versioning_base.pg_revision( uri.connectionInfo(), uri.schema()) if nb_of_updated_layer: #self.iface.messageBar().pushMessage("Info", #"You have successfully committed revision "+str( rev ), duration=10) QMessageBox.information(self.iface.mainWindow(), "Info", "You have successfully committed remote revision "+str( rev-1 ) ) # Force refresh of rev number in menu text if layer.providerType() == "spatialite": self.info.setText( uri.database().split("/")[-1] +' <b>working rev</b>='+str(rev)) else: self.info.setText( uri.database() +' <b>working rev</b>='+str(rev)) else: #self.iface.messageBar().pushMessage("Info", #"There was no modification to commit", duration=10) QMessageBox.information(self.iface.mainWindow(), "Info", "There was no modification to commit")
wc = tmp_dir + '/wc_multiple_geometry_test.sqlite' if os.path.isfile(wc): os.remove(wc) versioning_base.checkout( pg_conn_info, ['epanet_trunk_rev_head.pipes', 'epanet_trunk_rev_head.junctions'], wc) scur = versioning_base.Db(dbapi2.connect(wc)) scur.execute( "UPDATE junctions_view SET GEOMETRY = GeometryFromText('POINT(3 3)',2154) WHERE OGC_FID = 1" ) scur.commit() scur.execute("SELECT * from junctions_view") print "--------------" for res in scur.fetchall(): print res scur.close() versioning_base.commit(wc, 'moved a junction', 'dbname=epanet_test_db') pcur.execute( "SELECT ST_AsText(geometry), ST_AsText(geometry_schematic), printmap FROM epanet_trunk_rev_head.junctions ORDER BY hid DESC" ) res = pcur.fetchall() for r in res: print r assert (res[0][0] == 'POINT(3 3)') assert (res[0][1] == 'POLYGON((-1 -1,1 -1,1 1,-1 1,-1 -1))') assert (res[0][2] == [1, 2, 3]) pcur.close()
# try the update wc = [tmp_dir+"/issue357_wc0.sqlite", tmp_dir+"/issue357_wc1.sqlite"] for f in wc: if os.path.isfile(f): os.remove(f) versioning_base.checkout("dbname=epanet_test_db", ['epanet_trunk_rev_head.junctions', 'epanet_trunk_rev_head.pipes'], f) scur = [] for f in wc: scur.append(versioning_base.Db( dbapi2.connect( f ) )) scur[0].execute("INSERT INTO pipes_view(id, start_node, end_node, GEOMETRY) VALUES ('2','1','2',GeomFromText('LINESTRING(1 1,0 1)',2154))") scur[0].execute("INSERT INTO pipes_view(id, start_node, end_node, GEOMETRY) VALUES ('3','1','2',GeomFromText('LINESTRING(1 -1,0 1)',2154))") scur[0].commit() versioning_base.commit( wc[0], 'commit 1 wc0', "dbname=epanet_test_db") versioning_base.update( wc[1], "dbname=epanet_test_db" ) scur[0].execute("UPDATE pipes_view SET length = 1") scur[0].commit() scur[1].execute("UPDATE pipes_view SET length = 2") scur[1].execute("UPDATE pipes_view SET length = 3") scur[1].commit() versioning_base.commit( wc[0], "commit 2 wc0", "dbname=epanet_test_db" ) scur[0].execute("SELECT OGC_FID,length,trunk_rev_begin,trunk_rev_end,trunk_parent,trunk_child FROM pipes") print '################' for r in scur[0].fetchall(): print r scur[0].execute("UPDATE pipes_view SET length = 2")
def commit(self): """merge modifications into database""" print "commit" if self.unresolved_conflicts(): return layer = QgsMapLayerRegistry.instance().mapLayer(self.current_layers[0]) uri = QgsDataSourceURI(layer.source()) late_by = 0 if layer.providerType() == "spatialite": late_by = versioning_base.late(uri.database(), self.pg_conn_info()) else: #postgres late_by = versioning_base.pg_late(self.pg_conn_info(), uri.schema()) if late_by: QMessageBox.warning( self.iface.mainWindow(), "Warning", "This working copy is not up to date (late by " + str(late_by) + " commit(s)).\n\n" "Please update before committing your modifications") print "aborted" return # Make sure the combobox is visible; could be made invisible by a # previous call to branch self.q_commit_msg_dlg.pg_users_combobox.setVisible(True) self.q_commit_msg_dlg.pg_username_label.setVisible(True) # Populate combobox with list of pg usernames nb_items_in_list = self.q_commit_msg_dlg.pg_users_combobox.count() if not (nb_items_in_list): self.get_pg_users_list() # Better if we could have a QgsDataSourceURI.username() but no such # thing in spatialite. Next block is for the case the username cannot # be found in the connection info string (mainly for plugin tests) try: pg_username = self.pg_conn_info().split(' ')[3].replace( "'", "").split('=')[1] current_user_index = self.q_commit_msg_dlg.pg_users_combobox.findText( pg_username) # sets the current pg_user in the combobox to come current_user_combobox_item = self.q_commit_msg_dlg.pg_users_combobox.setCurrentIndex( current_user_index) except (IndexError): pg_username = '' # time to get the commit message if not self.q_commit_msg_dlg.exec_(): return commit_msg = self.q_commit_msg_dlg.commitMessage.document( ).toPlainText() commit_pg_user = self.q_commit_msg_dlg.pg_users_combobox.itemText( self.q_commit_msg_dlg.pg_users_combobox.currentIndex()) if not commit_msg: QMessageBox.warning(self.iface.mainWindow(), "Warning", "No commit message, aborting commit") print "aborted" return nb_of_updated_layer = 0 rev = 0 if layer.providerType() == "spatialite": nb_of_updated_layer = versioning_base.commit( uri.database(), commit_msg, self.pg_conn_info(), commit_pg_user) rev = versioning_base.revision(uri.database()) else: # postgres nb_of_updated_layer = versioning_base.pg_commit( uri.connectionInfo(), uri.schema(), commit_msg) rev = versioning_base.pg_revision(uri.connectionInfo(), uri.schema()) if nb_of_updated_layer: #self.iface.messageBar().pushMessage("Info", #"You have successfully committed revision "+str( rev ), duration=10) QMessageBox.information( self.iface.mainWindow(), "Info", "You have successfully committed remote revision " + str(rev - 1)) # Force refresh of rev number in menu text if layer.providerType() == "spatialite": self.info.setText(uri.database().split("/")[-1] + ' <b>working rev</b>=' + str(rev)) else: self.info.setText(uri.database() + ' <b>working rev</b>=' + str(rev)) else: #self.iface.messageBar().pushMessage("Info", #"There was no modification to commit", duration=10) QMessageBox.information(self.iface.mainWindow(), "Info", "There was no modification to commit")
["epanet_trunk_rev_head.junctions", "epanet_trunk_rev_head.pipes"], sqlite_test_filename1) assert (False and "trying to checkout on an existing file must fail") except RuntimeError: pass # edit one table and commit changes; rev = 2 scon = dbapi2.connect(sqlite_test_filename1) scur = scon.cursor() scur.execute("UPDATE junctions_view SET elevation = '8' WHERE id = '1'") scon.commit() scur.execute("SELECT COUNT(*) FROM junctions") assert (scur.fetchone()[0] == 3) scon.close() versioning_base.commit(sqlite_test_filename1, 'first edit commit', "dbname=epanet_test_db") pcon = psycopg2.connect("dbname=epanet_test_db") pcur = pcon.cursor() pcur.execute("SELECT COUNT(*) FROM epanet.junctions") assert (pcur.fetchone()[0] == 3) pcur.execute("SELECT COUNT(*) FROM epanet.revisions") assert (pcur.fetchone()[0] == 2) # add revision : edit one table and commit changes; rev = 3 versioning_base.checkout("dbname=epanet_test_db", ["epanet_trunk_rev_head.junctions"], sqlite_test_filename2) scon = dbapi2.connect(sqlite_test_filename2) scur = scon.cursor()
sqlite_test_filename1) assert (os.path.isfile(sqlite_test_filename1) and "sqlite file must exist at this point") try: versioning_base.checkout( "dbname=epanet_test_db", ["epanet_trunk_rev_head.junctions", "epanet_trunk_rev_head.pipes"], sqlite_test_filename1) assert (False and "trying to checkout on an existing file must fail") except RuntimeError: pass # edit one table and commit changes scon = dbapi2.connect(sqlite_test_filename1) scur = scon.cursor() scur.execute("UPDATE junctions_view SET elevation = '8' WHERE id = '1'") scon.commit() scur.execute("SELECT COUNT(*) FROM junctions") assert (scur.fetchone()[0] == 3) scon.close() versioning_base.commit(sqlite_test_filename1, 'first test commit', "dbname=epanet_test_db") pcon = psycopg2.connect("dbname=epanet_test_db") pcur = pcon.cursor() pcur.execute("SELECT COUNT(*) FROM epanet.junctions") assert (pcur.fetchone()[0] == 3) pcur.execute("SELECT COUNT(*) FROM epanet.revisions") assert (pcur.fetchone()[0] == 2)
#!/usr/bin/python import versioning_base from pyspatialite import dbapi2 import psycopg2 import os import shutil test_data_dir = os.path.dirname(os.path.realpath(__file__)) tmp_dir = "/tmp" # create the test database os.system("dropdb epanet_test_db") os.system("createdb epanet_test_db") os.system("psql epanet_test_db -c 'CREATE EXTENSION postgis'") os.system("psql epanet_test_db -f " + test_data_dir + "/issue287_pg_dump.sql") # try the update shutil.copyfile(test_data_dir + "/issue287_wc.sqlite", tmp_dir + "/issue287_wc.sqlite") versioning_base.update(tmp_dir + "/issue287_wc.sqlite", "dbname=epanet_test_db") versioning_base.commit(tmp_dir + "/issue287_wc.sqlite", "test message", "dbname=epanet_test_db")
try: versioning_base.checkout("dbname=epanet_test_db",["epanet_trunk_rev_head.junctions","epanet_trunk_rev_head.pipes"], sqlite_test_filename1) assert(False and "trying to checkout on an existing file must fail") except RuntimeError: pass # edit one table and commit changes; rev = 2 scon = dbapi2.connect(sqlite_test_filename1) scur = scon.cursor() scur.execute("UPDATE junctions_view SET elevation = '8' WHERE id = '1'") scon.commit() scur.execute("SELECT COUNT(*) FROM junctions") assert( scur.fetchone()[0] == 3 ) scon.close() versioning_base.commit(sqlite_test_filename1, 'first edit commit', "dbname=epanet_test_db") pcon = psycopg2.connect("dbname=epanet_test_db") pcur = pcon.cursor() pcur.execute("SELECT COUNT(*) FROM epanet.junctions") assert( pcur.fetchone()[0] == 3 ) pcur.execute("SELECT COUNT(*) FROM epanet.revisions") assert( pcur.fetchone()[0] == 2 ) # add revision : edit one table and commit changes; rev = 3 versioning_base.checkout("dbname=epanet_test_db",["epanet_trunk_rev_head.junctions"], sqlite_test_filename2) scon = dbapi2.connect(sqlite_test_filename2) scur = scon.cursor() scur.execute("UPDATE junctions_view SET elevation = '22' WHERE id = '1'") scon.commit()
"dbname=epanet_test_db", ['epanet_trunk_rev_head.junctions', 'epanet_trunk_rev_head.pipes'], f) scur = [] for f in wc: scur.append(versioning_base.Db(dbapi2.connect(f))) scur[0].execute( "INSERT INTO pipes_view(id, start_node, end_node, GEOMETRY) VALUES ('2','1','2',GeomFromText('LINESTRING(1 1,0 1)',2154))" ) scur[0].execute( "INSERT INTO pipes_view(id, start_node, end_node, GEOMETRY) VALUES ('3','1','2',GeomFromText('LINESTRING(1 -1,0 1)',2154))" ) scur[0].commit() versioning_base.commit(wc[0], 'commit 1 wc0', "dbname=epanet_test_db") versioning_base.update(wc[1], "dbname=epanet_test_db") scur[0].execute("UPDATE pipes_view SET length = 1") scur[0].commit() scur[1].execute("UPDATE pipes_view SET length = 2") scur[1].execute("UPDATE pipes_view SET length = 3") scur[1].commit() versioning_base.commit(wc[0], "commit 2 wc0", "dbname=epanet_test_db") scur[0].execute( "SELECT OGC_FID,length,trunk_rev_begin,trunk_rev_end,trunk_parent,trunk_child FROM pipes" ) print '################' for r in scur[0].fetchall(): print r
assert( not os.path.isfile(sqlite_test_filename1) and "sqlite file must not exist at this point" ) versioning_base.checkout("dbname=epanet_test_db",["epanet_trunk_rev_head.junctions","epanet_trunk_rev_head.pipes"], sqlite_test_filename1) assert( os.path.isfile(sqlite_test_filename1) and "sqlite file must exist at this point" ) try: versioning_base.checkout("dbname=epanet_test_db",["epanet_trunk_rev_head.junctions","epanet_trunk_rev_head.pipes"], sqlite_test_filename1) assert(False and "trying to checkout on an existing file must fail") except RuntimeError: pass # edit one table and commit changes scon = dbapi2.connect(sqlite_test_filename1) scur = scon.cursor() scur.execute("UPDATE junctions_view SET elevation = '8' WHERE id = '1'") scon.commit() scur.execute("SELECT COUNT(*) FROM junctions") assert( scur.fetchone()[0] == 3 ) scon.close() versioning_base.commit(sqlite_test_filename1, 'first test commit', "dbname=epanet_test_db") pcon = psycopg2.connect("dbname=epanet_test_db") pcur = pcon.cursor() pcur.execute("SELECT COUNT(*) FROM epanet.junctions") assert( pcur.fetchone()[0] == 3 ) pcur.execute("SELECT COUNT(*) FROM epanet.revisions") assert( pcur.fetchone()[0] == 2 )