Skip to content
This repository has been archived by the owner on Jun 4, 2020. It is now read-only.

Commit

Permalink
adds partial checkout for postgres working copies
Browse files Browse the repository at this point in the history
fix #63
  • Loading branch information
vmora committed Apr 26, 2017
1 parent 718ca4a commit 0494166
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
11 changes: 11 additions & 0 deletions test/partial_checkout_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def test():
pcon.close()

versioning.historize('dbname=epanet_test_db', 'epanet')

# spatialite working copy
versioning.checkout("dbname=epanet_test_db",["epanet_trunk_rev_head.junctions","epanet_trunk_rev_head.pipes"], sqlite_test_filename, [[1, 2, 3], []])
assert( os.path.isfile(sqlite_test_filename) and "sqlite file must exist at this point" )

Expand All @@ -47,5 +49,14 @@ def test():
scur.execute("SELECT * from junctions")
assert len(scur.fetchall()) == 3

# postgres working copy
versioning.pg_checkout("dbname=epanet_test_db",["epanet_trunk_rev_head.junctions","epanet_trunk_rev_head.pipes"], 'my_working_copy', [[1, 2, 3], []])

pcon = psycopg2.connect("dbname=epanet_test_db")
pcur = pcon.cursor()
pcur.execute("SELECT * from my_working_copy.junctions_view")
assert len(pcur.fetchall()) == 3


if __name__ == "__main__":
test()
58 changes: 38 additions & 20 deletions versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ def revisions(pg_conn_info, schema):
# we need the initial_revision table all the same
# for each table we need a diff and a view and triggers

def pg_checkout(pg_conn_info, pg_table_names, working_copy_schema):
def pg_checkout(pg_conn_info, pg_table_names, working_copy_schema, selected_feature_lists = []):
"""create postgres working copy from versioned database tables
pg_table_names must be complete schema.table names
the schema name must end with _branch_rev_head
Expand All @@ -1310,7 +1310,7 @@ def pg_checkout(pg_conn_info, pg_table_names, working_copy_schema):
pcur.execute("CREATE SCHEMA "+wcs)

first_table = True
for pg_table_name in pg_table_names:
for pg_table_name, feature_list in list(izip_longest(pg_table_names, selected_feature_lists)):
[schema, table] = pg_table_name.split('.')
[schema, sep, branch] = schema[:-9].rpartition('_')
del sep
Expand Down Expand Up @@ -1371,26 +1371,44 @@ def pg_checkout(pg_conn_info, pg_table_names, working_copy_schema):
"REFERENCES "+wcs+"."+table+"_diff("+pkey+") "
"ON UPDATE CASCADE ON DELETE CASCADE")

if feature_list:
additional_filter = "AND t.{pkey} IN ({features})".format(
pkey=pkey,
features = ','.join(str(f) for f in feature_list)
)
else:
additional_filter = ""

current_rev_sub = "(SELECT MAX(rev) FROM "+wcs+".initial_revision)"
pcur.execute("CREATE VIEW "+wcs+"."+table+"_view AS "
"SELECT "+pkey+", "+cols+" "
"FROM (SELECT "+cols+", "+hcols+" FROM "+wcs+"."+table+"_diff "
"WHERE ("+branch+"_rev_end IS NULL "
"OR "+branch+"_rev_end >= "+current_rev_sub+"+1 ) "
"AND "+branch+"_rev_begin IS NOT NULL "
"UNION "
"(SELECT DISTINCT ON ("+pkey+") "+cols+", t."+hcols+" "
"FROM "+schema+"."+table+" AS t "
"LEFT JOIN (SELECT "+pkey+" FROM "+wcs+"."+table+"_diff) "
"AS d "
"ON t."+pkey+" = d."+pkey+" "
"WHERE d."+pkey+" IS NULL "
"AND t."+branch+"_rev_begin <= "+current_rev_sub+" "
"AND ((t."+branch+"_rev_end IS NULL "
"OR t."+branch+"_rev_end >= "+current_rev_sub+") "
"AND t."+branch+"_rev_begin IS NOT NULL ))"
") AS src ")
pcur.execute("""
CREATE VIEW {wcs}.{table}_view AS
SELECT {pkey}, {cols}
FROM (
SELECT {cols}, {hcols}
FROM {wcs}.{table}_diff
WHERE ({branch}_rev_end IS NULL OR {branch}_rev_end >= {current_rev_sub}+1 )
AND {branch}_rev_begin IS NOT NULL
UNION
SELECT DISTINCT ON ({pkey}) {cols}, t.{hcols}
FROM {schema}.{table} AS t
LEFT JOIN (SELECT {pkey} FROM {wcs}.{table}_diff) AS d ON t.{pkey} = d.{pkey}
WHERE d.{pkey} IS NULL
AND t.{branch}_rev_begin <= {current_rev_sub}
AND ((t.{branch}_rev_end IS NULL
OR t.{branch}_rev_end >= {current_rev_sub})
AND t.{branch}_rev_begin IS NOT NULL)
{additional_filter}
) AS src """.format(
wcs=wcs,
schema=schema,
table=table,
pkey=pkey,
cols=cols,
hcols=hcols,
branch=branch,
current_rev_sub=current_rev_sub,
additional_filter=additional_filter
))

max_fid_sub = ("( SELECT MAX(max_fid) FROM ( SELECT MAX("+pkey+") "
"AS max_fid FROM "+wcs+"."+table+"_diff "
Expand Down

0 comments on commit 0494166

Please sign in to comment.