def bayesdb_install_schema(db, version=None, compatible=None): # Get the application id. cursor = db.cursor().execute('PRAGMA application_id') application_id = 0 try: row = cursor.next() except StopIteration: raise EnvironmentError('Missing application_id in sqlite3.') else: application_id = row[0] assert isinstance(application_id, int) # Get the user version. cursor = db.cursor().execute('PRAGMA user_version') user_version = 0 try: row = cursor.next() except StopIteration: raise EnvironmentError('Missing user_version in sqlite3.') else: user_version = row[0] assert isinstance(user_version, int) # Check them. If zero, install schema. If mismatch, fail. install = False if application_id == 0 and user_version == 0: # Assume we just created the database. # # XXX What if we opened some random other sqlite file which # did not have an application_id or user_version set? Hope # everyone else sets application_id and user_version too... with sqlite3_transaction(db): db.cursor().execute('PRAGMA application_id = %d' % (0x42594442, )) db.cursor().execute(bayesdb_schema_5) user_version = 5 install = True elif application_id != 0x42594442: raise IOError('Wrong application id: 0x%08x' % (application_id, )) # 5 was the last prerelease version. if user_version == 5: with sqlite3_transaction(db): db.cursor().execute(bayesdb_schema_5to6) user_version = 6 if user_version != 6 and user_version != 7: raise IOError('Unknown bayeslite format version: %d' % (user_version, )) if install or not compatible: _upgrade_schema(db, version=version) db.cursor().execute('PRAGMA foreign_keys = ON') db.cursor().execute('PRAGMA integrity_check') db.cursor().execute('PRAGMA foreign_key_check')
def bayesdb_install_schema(db, version=None, compatible=None): # Get the application id. cursor = db.cursor().execute('PRAGMA application_id') application_id = 0 try: row = cursor.next() except StopIteration: raise EnvironmentError('Missing application_id in sqlite3.') else: application_id = row[0] assert isinstance(application_id, int) # Get the user version. cursor = db.cursor().execute('PRAGMA user_version') user_version = 0 try: row = cursor.next() except StopIteration: raise EnvironmentError('Missing user_version in sqlite3.') else: user_version = row[0] assert isinstance(user_version, int) # Check them. If zero, install schema. If mismatch, fail. install = False if application_id == 0 and user_version == 0: # Assume we just created the database. # # XXX What if we opened some random other sqlite file which # did not have an application_id or user_version set? Hope # everyone else sets application_id and user_version too... with sqlite3_transaction(db): db.cursor().execute('PRAGMA application_id = %d' % (0x42594442,)) db.cursor().execute(bayesdb_schema_5) user_version = 5 install = True elif application_id != 0x42594442: raise IOError('Wrong application id: 0x%08x' % (application_id,)) # 5 was the last prerelease version. if user_version == 5: with sqlite3_transaction(db): db.cursor().execute(bayesdb_schema_5to6) user_version = 6 if user_version != 6 and user_version != 7: raise IOError('Unknown bayeslite format version: %d' % (user_version,)) if install or not compatible: _upgrade_schema(db, version=version) db.cursor().execute('PRAGMA foreign_keys = ON') db.cursor().execute('PRAGMA integrity_check') db.cursor().execute('PRAGMA foreign_key_check')
def bayesdb_transaction(bdb): if bdb._txn_depth != 0: raise BayesDBTxnError(bdb, 'Already in a transaction!') bayesdb_txn_init(bdb) bdb._txn_depth = 1 try: with sqlite3_transaction(bdb._sqlite3): yield finally: assert bdb._txn_depth == 1 bdb._txn_depth = 0 bayesdb_txn_fini(bdb)
def bayesdb_transaction(bdb): if bdb.txn_depth != 0: raise BayesDBTxnError(bdb, 'Already in a transaction!') bayesdb_txn_init(bdb) bdb.txn_depth = 1 try: with sqlite3_transaction(bdb._sqlite3): yield finally: assert bdb.txn_depth == 1 bdb.txn_depth = 0 bayesdb_txn_fini(bdb)
def _upgrade_schema(db, version=None): if version is None: version = 7 assert 6 <= version current_version = _schema_version(db) assert 6 <= current_version if current_version == 6 < version: with sqlite3_transaction(db): db.cursor().execute(bayesdb_schema_6to7) current_version = 7 assert current_version == version db.cursor().execute('PRAGMA integrity_check') db.cursor().execute('PRAGMA foreign_key_check')