Changeset View
Changeset View
Standalone View
Standalone View
swh/provenance/postgresql_nopath/provenance.py
Show All 9 Lines | |||||
from ..model import DirectoryEntry, FileEntry | from ..model import DirectoryEntry, FileEntry | ||||
from ..origin import OriginEntry | from ..origin import OriginEntry | ||||
from ..postgresql.db_utils import connect, execute_sql | from ..postgresql.db_utils import connect, execute_sql | ||||
from ..provenance import ProvenanceInterface | from ..provenance import ProvenanceInterface | ||||
from ..revision import RevisionEntry | from ..revision import RevisionEntry | ||||
def create_database(conn: psycopg2.extensions.connection, conninfo: dict, name: str): | |||||
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) | |||||
# Normalize dbname to avoid issues when reconnecting below | |||||
name = name.casefold() | |||||
# Create new database dropping previous one if exists | |||||
cursor = conn.cursor() | |||||
cursor.execute(f"""DROP DATABASE IF EXISTS {name}""") | |||||
cursor.execute(f"""CREATE DATABASE {name}""") | |||||
conn.close() | |||||
# Reconnect to server selecting newly created database to add tables | |||||
conninfo["dbname"] = name | |||||
conn = connect(conninfo) | |||||
sqldir = os.path.dirname(os.path.realpath(__file__)) | |||||
execute_sql(conn, os.path.join(sqldir, "provenance.sql")) | |||||
######################################################################################## | ######################################################################################## | ||||
######################################################################################## | ######################################################################################## | ||||
######################################################################################## | ######################################################################################## | ||||
class ProvenancePostgreSQLNoPath(ProvenanceInterface): | class ProvenancePostgreSQLNoPath(ProvenanceInterface): | ||||
def __init__(self, conn: psycopg2.extensions.connection): | def __init__(self, conn: psycopg2.extensions.connection): | ||||
# TODO: consider adding a mutex for thread safety | # TODO: consider adding a mutex for thread safety | ||||
▲ Show 20 Lines • Show All 397 Lines • Show Last 20 Lines |