Skip to content

Commit f058390

Browse files
richardscholtensmwatts15
authored andcommitted
Update SQLAlchemy, fix tests
* Not all tests fixed yet * Fix all `test_aggregate_graphs` * Fix all `test_store_performance` * Fix all `test_type_to_term_combination` * Update SQLAlchemy to version 2.0.23 * Refactor sqlalchemy statements * Add docker-compose.yml * Add .env.sample
1 parent 912ad4a commit f058390

File tree

6 files changed

+48
-24
lines changed

6 files changed

+48
-24
lines changed

.env.sample

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
POSTGRES_USERNAME=username
2+
POSTGRES_PASSWORD=password
3+
POSTGRES_HOST=127.0.0.1
4+
POSTGRES_PORT=5432
5+
POSTGRES_DBNAME=name
6+
DB='pgsql'
7+
DBURI='postgresql+psycopg2://username:[email protected]/name'

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ docs/api
1818

1919
# Test outcomes
2020
test/tmpdb.sqlite
21+
22+
.env

docker-compose.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
version: '3.8'
3+
services:
4+
db:
5+
image: postgres:14.1-alpine
6+
restart: always
7+
environment:
8+
- POSTGRES_USER=${POSTGRES_USERNAME}
9+
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
10+
- POSTGRES_HOST=${POSTGRES_HOST}
11+
- POSTGRES_DB=${POSTGRES_DBNAME}
12+
ports:
13+
- "${POSTGRES_PORT}:${POSTGRES_PORT}"
14+
volumes:
15+
- db:/var/lib/postgresql/data
16+
volumes:
17+
db:
18+
driver: local

rdflib_sqlalchemy/sql.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,31 +62,28 @@ def union_select(select_components, distinct=False, select_type=TRIPLE_SELECT):
6262
cols = [c.subject, c.predicate, c.object]
6363
else:
6464
raise ValueError('Unrecognized table type {}'.format(tableType))
65-
select_clause = expression.select([functions.count().label('aCount')]).select_from(
66-
expression.select(cols, whereClause).distinct().select_from(table))
65+
select_clause = expression.select(*[functions.count().label('aCount')]).select_from(
66+
expression.select(*cols).where(whereClause).distinct().select_from(table))
6767
elif select_type == CONTEXT_SELECT:
68-
select_clause = expression.select([table.c.context], whereClause)
68+
select_clause = expression.select(table.c.context)
69+
if whereClause is not None:
70+
select_clause = expression.select(table.c.context).where(whereClause)
6971
elif tableType in FULL_TRIPLE_PARTITIONS:
70-
select_clause = table.select(whereClause)
72+
select_clause = table.select().where(whereClause)
7173
elif tableType == ASSERTED_TYPE_PARTITION:
7274
select_clause = expression.select(
73-
[table.c.id.label("id"),
75+
*[table.c.id.label("id"),
7476
table.c.member.label("subject"),
7577
expression.literal(text_type(RDF.type)).label("predicate"),
7678
table.c.klass.label("object"),
7779
table.c.context.label("context"),
7880
table.c.termComb.label("termcomb"),
7981
expression.literal_column("NULL").label("objlanguage"),
80-
expression.literal_column("NULL").label("objdatatype")],
82+
expression.literal_column("NULL").label("objdatatype")]).where(
8183
whereClause)
8284
elif tableType == ASSERTED_NON_TYPE_PARTITION:
83-
select_clause = expression.select(
84-
[c for c in table.columns] +
85-
[expression.literal_column("NULL").label("objlanguage"),
86-
expression.literal_column("NULL").label("objdatatype")],
87-
whereClause,
88-
from_obj=[table])
89-
85+
all_table_columns = [c for c in table.columns] + [expression.literal_column("NULL").label("objlanguage"), expression.literal_column("NULL").label("objdatatype")]
86+
select_clause = expression.select(*all_table_columns).select_from(table).where(whereClause)
9087
selects.append(select_clause)
9188

9289
order_statement = []
@@ -97,6 +94,6 @@ def union_select(select_components, distinct=False, select_type=TRIPLE_SELECT):
9794
expression.literal_column("object"),
9895
]
9996
if distinct and select_type != COUNT_SELECT:
100-
return expression.union(*selects, **{"order_by": order_statement})
97+
return expression.union(*selects).order_by(*order_statement)
10198
else:
102-
return expression.union_all(*selects, **{"order_by": order_statement})
99+
return expression.union_all(*selects).order_by(*order_statement)

rdflib_sqlalchemy/store.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ def remove(self, triple, context):
396396
if not self.STRONGLY_TYPED_TERMS or isinstance(obj, Literal):
397397
# remove literal triple
398398
clause = self.build_clause(literal_table, subject, predicate, obj, context)
399-
connection.execute(literal_table.delete(clause))
399+
connection.execute(literal_table.delete().where(clause))
400400

401401
for table in [quoted_table, asserted_table]:
402402
# If asserted non rdf:type table and obj is Literal,
@@ -405,16 +405,16 @@ def remove(self, triple, context):
405405
continue
406406
else:
407407
clause = self.build_clause(table, subject, predicate, obj, context)
408-
connection.execute(table.delete(clause))
408+
connection.execute(table.delete().where(clause))
409409

410410
if predicate == RDF.type or predicate is None:
411411
# Need to check rdf:type and quoted partitions (in addition
412412
# perhaps)
413413
clause = self.build_clause(asserted_type_table, subject, RDF.type, obj, context, True)
414-
connection.execute(asserted_type_table.delete(clause))
414+
connection.execute(asserted_type_table.delete().where(clause))
415415

416416
clause = self.build_clause(quoted_table, subject, predicate, obj, context)
417-
connection.execute(quoted_table.delete(clause))
417+
connection.execute(quoted_table.delete().where(clause))
418418
except Exception:
419419
_logger.exception("Removal failed.")
420420
raise
@@ -654,7 +654,7 @@ def prefix(self, namespace):
654654
with self.engine.begin() as connection:
655655
nb_table = self.tables["namespace_binds"]
656656
namespace = text_type(namespace)
657-
s = select([nb_table.c.prefix]).where(nb_table.c.uri == namespace)
657+
s = select(nb_table.c.prefix).where(nb_table.c.uri == namespace)
658658
res = connection.execute(s)
659659
rt = [rtTuple[0] for rtTuple in res.fetchall()]
660660
res.close()
@@ -668,7 +668,7 @@ def namespace(self, prefix):
668668
try:
669669
with self.engine.begin() as connection:
670670
nb_table = self.tables["namespace_binds"]
671-
s = select([nb_table.c.uri]).where(nb_table.c.prefix == prefix_val)
671+
s = select(nb_table.c.uri).where(nb_table.c.prefix == prefix_val)
672672
res = connection.execute(s)
673673
rt = [rtTuple[0] for rtTuple in res.fetchall()]
674674
res.close()
@@ -679,7 +679,7 @@ def namespace(self, prefix):
679679

680680
def namespaces(self):
681681
with self.engine.begin() as connection:
682-
res = connection.execute(self.tables["namespace_binds"].select(distinct=True))
682+
res = connection.execute(self.tables["namespace_binds"].select().distinct())
683683
for prefix, uri in res.fetchall():
684684
yield prefix, uri
685685

@@ -754,7 +754,7 @@ def _remove_context(self, context):
754754
for table in [quoted_table, asserted_table,
755755
asserted_type_table, literal_table]:
756756
clause = self.build_context_clause(context, table)
757-
connection.execute(table.delete(clause))
757+
connection.execute(table.delete().where(clause))
758758
except Exception:
759759
_logger.exception("Context removal failed.")
760760
raise

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"alembic>=0.8.8",
4747
"rdflib>=6,<8",
4848
"six>=1.10.0",
49-
"SQLAlchemy>=1.1.4,<2.0.0",
49+
"SQLAlchemy>=2.0.23",
5050
],
5151
entry_points={
5252
'rdf.plugins.store': [

0 commit comments

Comments
 (0)