in src/hpc/autoscale/node/nodehistory.py [0:0]
def initialize_db(path: str, read_only: bool, uri: bool = False) -> sqlite3.Connection:
file_uri = path
try:
if read_only:
path = os.path.abspath(path)
# just use an in memory db if this is the first time this is run
if not os.path.exists(path):
file_uri = "file:memory"
else:
file_uri = "file://{}?mode=ro".format(path)
conn = sqlite3.connect(file_uri, uri=True)
# uninitialized file conns will fail here, so just
# use memory instead
try:
conn = sqlite3.connect(file_uri, uri=True)
except sqlite3.OperationalError:
conn = sqlite3.connect("file:memory", uri=True)
else:
conn = sqlite3.connect(path, uri=uri)
except sqlite3.OperationalError as e:
logging.exception("Error while opening %s - %s", file_uri, e)
raise
try:
conn.execute("CREATE TABLE metadata (version)")
except sqlite3.OperationalError as e:
if "table metadata already exists" not in e.args:
raise
cursor = conn.execute("SELECT version from metadata")
version_result = list(cursor)
if version_result:
version = version_result[0][0]
else:
conn.execute(
"INSERT INTO metadata (version) VALUES ('{}')".format(SQLITE_VERSION)
)
version = SQLITE_VERSION
if version != SQLITE_VERSION:
if SQLITE_VERSION > "0.0.6":
upgrade_database(version, conn)
conn.close()
new_path = "{}.{}".format(path, version)
print("Invalid version - moving to {}".format(new_path))
shutil.move(path, new_path)
return initialize_db(path, read_only)
try:
conn.execute(
"""CREATE TABLE nodes (node_id TEXT PRIMARY KEY, hostname TEXT,
instance_id TEXT,
create_time REAL, last_match_time REAL,
ready_time REAL, delete_time REAL,
ignore BOOL)"""
)
except sqlite3.OperationalError as e:
if "table nodes already exists" not in e.args:
raise
return conn