Skip to content

Commit b9df232

Browse files
committed
Merge branch 'feature/ah-open-23' into develop
2 parents c195405 + 55cea8b commit b9df232

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

rdflib_sqlalchemy/store.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from rdflib.graph import Graph, QuotedGraph
1515
from rdflib.namespace import RDF
1616
from rdflib.plugins.stores.regexmatching import PYTHON_REGEX, REGEXTerm
17-
from rdflib.store import Store
17+
from rdflib.store import CORRUPTED_STORE, VALID_STORE, Store
1818
from six import text_type
1919
from six.moves import reduce
2020
from sqlalchemy import MetaData
@@ -582,9 +582,9 @@ def open(self, configuration, create=True):
582582
store.
583583
584584
Returns:
585-
int: 0 if database exists but is empty,
586-
1 if database exists and tables are all there,
587-
-1 if nothing exists
585+
int: CORRUPTED_STORE (0) if database exists but is empty,
586+
VALID_STORE (1) if database exists and tables are all there,
587+
NO_STORE (-1) if nothing exists
588588
589589
"""
590590
# Close any existing engine connection
@@ -593,26 +593,31 @@ def open(self, configuration, create=True):
593593
self.engine = sqlalchemy.create_engine(configuration)
594594
with self.engine.connect():
595595
if create:
596-
return self.create_all()
596+
# Create all of the database tables (idempotent)
597+
self.metadata.create_all(self.engine)
597598

598-
def create_all(self):
599+
ret_value = self.verify_store_exists()
600+
601+
if ret_value != VALID_STORE and not create:
602+
raise RuntimeError("open() - create flag was set to False, but store was not created previously.")
603+
604+
return ret_value
605+
606+
def verify_store_exists(self):
599607
"""
600-
Create all of the database tables.
601-
Will not re-create any tables that already exist.
608+
Verify store (e.g. all tables) exist.
602609
603610
"""
604-
self.metadata.create_all(self.engine)
605611

606612
inspector = reflection.Inspector.from_engine(self.engine)
607613
existing_table_names = inspector.get_table_names()
608614
for table_name in self.table_names:
609615
if table_name not in existing_table_names:
610616
_logger.critical("create_all() - table %s Doesn't exist!", table_name)
611617
# The database exists, but one of the tables doesn't exist
612-
return 0
618+
return CORRUPTED_STORE
613619

614-
# Everything is there (the database and all of the tables)
615-
return 1
620+
return VALID_STORE
616621

617622
def close(self, commit_pending_transaction=False):
618623
"""

test/test_sqlalchemy_mysql.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
from nose import SkipTest
66
from rdflib import Literal
7+
from rdflib.graph import ConjunctiveGraph as Graph
78
from rdflib.py3compat import PY3
9+
from rdflib.store import NO_STORE, VALID_STORE
10+
from rdflib.term import URIRef
11+
812

913
from . import context_case
1014
from . import graph_case
1115

16+
1217
if PY3:
1318
try:
1419
import mysql
@@ -30,13 +35,10 @@
3035

3136
_logger = logging.getLogger(__name__)
3237

33-
# Specific to Travis-ci continuous integration and testing ...
38+
3439
sqlalchemy_url = Literal(os.environ.get(
3540
"DBURI",
3641
"mysql+%s://[email protected]:3306/test?charset=utf8" % dialect))
37-
# Generally ...
38-
# sqlalchemy_url = Literal(
39-
# "mysql+mysqldb://user:password@hostname:port/database?charset=utf8")
4042

4143

4244
class SQLAMySQLGraphTestCase(graph_case.GraphTestCase):
@@ -76,10 +78,6 @@ class SQLAMySQLIssueTestCase(unittest.TestCase):
7678
uri = sqlalchemy_url
7779

7880
def test_issue_4(self):
79-
from rdflib.graph import ConjunctiveGraph as Graph
80-
from rdflib.store import NO_STORE, VALID_STORE
81-
from rdflib.term import URIRef
82-
8381
ident = URIRef("rdflib_test")
8482
g = Graph(store="SQLAlchemy", identifier=ident)
8583
rt = g.open(self.uri, create=True)

0 commit comments

Comments
 (0)