14
14
from rdflib .graph import Graph , QuotedGraph
15
15
from rdflib .namespace import RDF
16
16
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
18
18
from six import text_type
19
19
from six .moves import reduce
20
20
from sqlalchemy import MetaData
@@ -582,9 +582,9 @@ def open(self, configuration, create=True):
582
582
store.
583
583
584
584
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
588
588
589
589
"""
590
590
# Close any existing engine connection
@@ -593,26 +593,31 @@ def open(self, configuration, create=True):
593
593
self .engine = sqlalchemy .create_engine (configuration )
594
594
with self .engine .connect ():
595
595
if create :
596
- return self .create_all ()
596
+ # Create all of the database tables (idempotent)
597
+ self .metadata .create_all (self .engine )
597
598
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 ):
599
607
"""
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.
602
609
603
610
"""
604
- self .metadata .create_all (self .engine )
605
611
606
612
inspector = reflection .Inspector .from_engine (self .engine )
607
613
existing_table_names = inspector .get_table_names ()
608
614
for table_name in self .table_names :
609
615
if table_name not in existing_table_names :
610
616
_logger .critical ("create_all() - table %s Doesn't exist!" , table_name )
611
617
# The database exists, but one of the tables doesn't exist
612
- return 0
618
+ return CORRUPTED_STORE
613
619
614
- # Everything is there (the database and all of the tables)
615
- return 1
620
+ return VALID_STORE
616
621
617
622
def close (self , commit_pending_transaction = False ):
618
623
"""
0 commit comments