def checkout_pg(self): """create postgres working copy (schema) from versioned database layers""" # for each connection, we need the list of tables tables_for_conninfo = [] uri = None conn_info = '' for layer_id in self.current_layers: layer = QgsMapLayerRegistry.instance().mapLayer(layer_id) uri = QgsDataSourceURI(layer.source()) if not conn_info: conn_info = uri.connectionInfo() else: assert (conn_info == uri.connectionInfo()) table = uri.schema() + "." + uri.table() tables_for_conninfo.append(table) dlg = QDialog() dlg.setWindowTitle('Enter working copy schema name') layout = QVBoxLayout(dlg) button_box = QDialogButtonBox(dlg) button_box.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Ok) button_box.accepted.connect(dlg.accept) button_box.rejected.connect(dlg.reject) line_edit = QLineEdit(dlg) layout.addWidget(line_edit) layout.addWidget(button_box) if not dlg.exec_(): return working_copy_schema = line_edit.text() if not working_copy_schema: print "aborted" return print "checking out ", tables_for_conninfo, " from ", uri.connectionInfo( ) versioning_base.pg_checkout(self.pg_conn_info(), tables_for_conninfo, working_copy_schema) # add layers from offline version grp_idx = self.iface.legendInterface().addGroup(working_copy_schema) for layer_id in reversed(self.current_layers): layer = QgsMapLayerRegistry.instance().mapLayer(layer_id) new_uri = QgsDataSourceURI(layer.source()) new_uri.setDataSource(working_copy_schema, new_uri.table() + "_view", new_uri.geometryColumn(), new_uri.sql(), new_uri.keyColumn()) display_name = QgsMapLayerRegistry.instance().mapLayer( layer_id).name() print "replacing ", display_name src = new_uri.uri().replace('()', '') new_layer = self.iface.addVectorLayer(src, display_name, 'postgres') self.iface.legendInterface().moveLayer(new_layer, grp_idx)
def checkout_pg(self): """create postgres working copy (schema) from versioned database layers""" # for each connection, we need the list of tables tables_for_conninfo = [] uri = None conn_info = '' for layer_id in self.current_layers: layer = QgsMapLayerRegistry.instance().mapLayer( layer_id ) uri = QgsDataSourceURI(layer.source()) if not conn_info: conn_info = uri.connectionInfo() else: assert(conn_info == uri.connectionInfo()) table = uri.schema()+"."+uri.table() tables_for_conninfo.append(table) dlg = QDialog() dlg.setWindowTitle('Enter working copy schema name') layout = QVBoxLayout(dlg) button_box = QDialogButtonBox(dlg) button_box.setStandardButtons( QDialogButtonBox.Cancel|QDialogButtonBox.Ok) button_box.accepted.connect(dlg.accept) button_box.rejected.connect(dlg.reject) line_edit = QLineEdit( dlg ) layout.addWidget( line_edit ) layout.addWidget( button_box ) if not dlg.exec_() : return working_copy_schema = line_edit.text() if not working_copy_schema: print "aborted" return print "checking out ", tables_for_conninfo, " from ", uri.connectionInfo() versioning_base.pg_checkout( self.pg_conn_info(), tables_for_conninfo, working_copy_schema ) # add layers from offline version grp_idx = self.iface.legendInterface().addGroup( working_copy_schema ) for layer_id in reversed(self.current_layers): layer = QgsMapLayerRegistry.instance().mapLayer( layer_id ) new_uri = QgsDataSourceURI(layer.source()) new_uri.setDataSource(working_copy_schema, new_uri.table()+"_view", new_uri.geometryColumn(), new_uri.sql(), new_uri.keyColumn()) display_name = QgsMapLayerRegistry.instance().mapLayer(layer_id).name() print "replacing ", display_name src = new_uri.uri().replace('()','') new_layer = self.iface.addVectorLayer(src, display_name, 'postgres') self.iface.legendInterface().moveLayer( new_layer, grp_idx)
print r test_data_dir = os.path.dirname(os.path.realpath(__file__)) # 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.sql") # chechout #tables = ['epanet_trunk_rev_head.junctions','epanet_trunk_rev_head.pipes'] tables = ['epanet_trunk_rev_head.junctions', 'epanet_trunk_rev_head.pipes'] versioning_base.pg_checkout("dbname=epanet_test_db", tables, "epanet_working_copy") versioning_base.pg_checkout("dbname=epanet_test_db", tables, "epanet_working_copy_cflt") pcur = versioning_base.Db(psycopg2.connect("dbname=epanet_test_db")) pcur.execute( "INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('2','1','2',ST_GeometryFromText('LINESTRING(1 1,0 1)',2154))" ) pcur.execute( "INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('3','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))" ) pcur.commit() prtHid(pcur, 'epanet_working_copy.pipes_view')
def checkout_pg(self): """create postgres working copy (schema) from versioned database layers""" # for each connection, we need the list of tables tables_for_conninfo = [] uri = None conn_info = '' for layer_id in self.current_layers: layer = QgsMapLayerRegistry.instance().mapLayer( layer_id ) uri = QgsDataSourceURI(layer.source()) if not conn_info: conn_info = uri.connectionInfo() else: assert(conn_info == uri.connectionInfo()) table = uri.schema()+"."+uri.table() tables_for_conninfo.append(table) dlg = QDialog() dlg.setWindowTitle('Enter working copy schema name') layout = QVBoxLayout(dlg) button_box = QDialogButtonBox(dlg) button_box.setStandardButtons( QDialogButtonBox.Cancel|QDialogButtonBox.Ok) button_box.accepted.connect(dlg.accept) button_box.rejected.connect(dlg.reject) line_edit = QLineEdit( dlg ) layout.addWidget( line_edit ) layout.addWidget( button_box ) if not dlg.exec_() : return working_copy_schema = line_edit.text() if not working_copy_schema: print "Name not provided; aborted" self.iface.messageBar().pushMessage("Warning", "Please provide a schema name.", duration=5) return # Check if name is valid for a PG object; only characters and max length # are checked; use of reserved words is not checked if len(working_copy_schema) > 63: print "Name too long; aborted" self.iface.messageBar().pushMessage("Warning", "\""+working_copy_schema+"\" is "+str(len(working_copy_schema))+ " characters long; maximum is 63.", duration=5) return valid_name = re.match('^[a-z_][a-z_0-9$]*$', str(working_copy_schema)) if not(valid_name): print "Not a valid name" self.iface.messageBar().pushMessage("Warning", "\""+working_copy_schema+"\" is not valid; first character must be " "<b>lowercase</b> letter or underscore; other characters may be " "<b>lowercase</b> letters, underscore or digits.", duration=10) return print "checking out ", tables_for_conninfo, " from ", uri.connectionInfo() versioning_base.pg_checkout( self.pg_conn_info(), tables_for_conninfo, working_copy_schema ) # add layers from offline version grp_idx = self.iface.legendInterface().addGroup( working_copy_schema ) for layer_id in reversed(self.current_layers): layer = QgsMapLayerRegistry.instance().mapLayer( layer_id ) new_uri = QgsDataSourceURI(layer.source()) new_uri.setDataSource(working_copy_schema, new_uri.table()+"_view", new_uri.geometryColumn(), new_uri.sql(), new_uri.keyColumn()) display_name = QgsMapLayerRegistry.instance().mapLayer(layer_id).name() print "replacing ", display_name src = new_uri.uri().replace('()','') new_layer = self.iface.addVectorLayer(src, display_name, 'postgres') self.iface.legendInterface().moveLayer( new_layer, grp_idx)
for r in pcur.fetchall(): t = [] for i in r: t.append(str(i)) print '\t| '.join(t) def prtHid( cur, tab ): print "--- ",tab," ---" pcur.execute("SELECT pid FROM "+tab) for [r] in pcur.fetchall(): print r test_data_dir = os.path.dirname(os.path.realpath(__file__)) # 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.sql") # chechout versioning_base.pg_checkout("dbname=epanet_test_db",['epanet_trunk_rev_head.junctions','epanet_trunk_rev_head.pipes'], "epanet_working_copy") pcur = versioning_base.Db(psycopg2.connect("dbname=epanet_test_db")) pcur.execute("UPDATE epanet_working_copy.pipes_view SET length = 4 WHERE pid = 1") prtTab(pcur, 'epanet_working_copy.pipes_diff') prtHid( pcur, 'epanet_working_copy.pipes_view') pcur.execute("SElECT COUNT(pid) FROM epanet_working_copy.pipes_view") assert( 1 == pcur.fetchone()[0] )
print "--- ", tab, " ---" pcur.execute("SELECT pid FROM " + tab) for [r] in pcur.fetchall(): print r test_data_dir = os.path.dirname(os.path.realpath(__file__)) # 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.sql") # chechout versioning_base.pg_checkout( "dbname=epanet_test_db", ['epanet_trunk_rev_head.junctions', 'epanet_trunk_rev_head.pipes'], "epanet_working_copy") pcur = versioning_base.Db(psycopg2.connect("dbname=epanet_test_db")) pcur.execute( "UPDATE epanet_working_copy.pipes_view SET length = 4 WHERE pid = 1") prtTab(pcur, 'epanet_working_copy.pipes_diff') prtHid(pcur, 'epanet_working_copy.pipes_view') pcur.execute("SElECT COUNT(pid) FROM epanet_working_copy.pipes_view") assert (1 == pcur.fetchone()[0])
for [r] in pcur.fetchall(): print r test_data_dir = os.path.dirname(os.path.realpath(__file__)) # create the test database for resolution in ['theirs','mine']: 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.sql") pcur = versioning_base.Db(psycopg2.connect("dbname=epanet_test_db")) tables = ['epanet_trunk_rev_head.junctions', 'epanet_trunk_rev_head.pipes'] versioning_base.pg_checkout("dbname=epanet_test_db",tables, "wc1") versioning_base.pg_checkout("dbname=epanet_test_db",tables, "wc2") pcur.execute("UPDATE wc1.pipes_view SET length = 4 WHERE pid = 1") pcur.commit() prtTab( pcur, "wc1.pipes_diff") versioning_base.pg_commit("dbname=epanet_test_db","wc1","msg1") pcur.execute("UPDATE wc2.pipes_view SET length = 5 WHERE pid = 1") pcur.commit() prtTab( pcur, "wc2.pipes_diff") versioning_base.pg_update("dbname=epanet_test_db","wc2") prtTab( pcur, "wc2.pipes_diff") prtTab( pcur, "wc2.pipes_conflicts") pcur.execute("SELECT COUNT(*) FROM wc2.pipes_conflicts WHERE origin = 'mine'")
pcur.execute("SELECT pid FROM "+tab) for [r] in pcur.fetchall(): print r test_data_dir = os.path.dirname(os.path.realpath(__file__)) # 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.sql") # chechout #tables = ['epanet_trunk_rev_head.junctions','epanet_trunk_rev_head.pipes'] tables = ['epanet_trunk_rev_head.junctions', 'epanet_trunk_rev_head.pipes'] versioning_base.pg_checkout("dbname=epanet_test_db",tables, "epanet_working_copy") versioning_base.pg_checkout("dbname=epanet_test_db",tables, "epanet_working_copy_cflt") pcur = versioning_base.Db(psycopg2.connect("dbname=epanet_test_db")) pcur.execute("INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('2','1','2',ST_GeometryFromText('LINESTRING(1 1,0 1)',2154))") pcur.execute("INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('3','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))") pcur.commit() prtHid(pcur, 'epanet_working_copy.pipes_view') pcur.execute("SELECT pid FROM epanet_working_copy.pipes_view") assert( len(pcur.fetchall()) == 3 )