Changeset View
Changeset View
Standalone View
Standalone View
swh/core/cli/db.py
Show First 20 Lines • Show All 77 Lines • ▼ Show 20 Lines | Example: | ||||
swh db create -d postgresql://superuser:passwd@pghost:5433/swh-storage storage | swh db create -d postgresql://superuser:passwd@pghost:5433/swh-storage storage | ||||
""" | """ | ||||
logger.debug("db_create %s dn_name=%s", module, db_name) | logger.debug("db_create %s dn_name=%s", module, db_name) | ||||
create_database_for_package(module, db_name, template) | create_database_for_package(module, db_name, template) | ||||
@db.command(name="init-admin", context_settings=CONTEXT_SETTINGS) | |||||
@click.argument("module", required=True) | |||||
@click.option( | |||||
"--db-name", | |||||
"-d", | |||||
help="Database name.", | |||||
default="softwareheritage-dev", | |||||
show_default=True, | |||||
) | |||||
def db_init_admin(module: str, db_name: str) -> None: | |||||
"""Execute superuser-level initialization steps (e.g pg extensions , admin functions, | |||||
...) | |||||
Example: | |||||
PGPASSWORD=... swh db init-admin -d swh-test scheduler | |||||
If you want to specify non-default postgresql connection parameters, please | |||||
provide them using standard environment variables or by the mean of a | |||||
properly crafted libpq connection URI. See psql(1) man page (section | |||||
ENVIRONMENTS) for details. | |||||
Note: this command requires a postgresql connection with superuser permissions (e.g | |||||
postgres, swh-admin, ...) | |||||
Example: | |||||
PGPORT=5434 swh db init-admin scheduler | |||||
swh db init-admin -d postgresql://superuser:passwd@pghost:5433/swh-scheduler \ | |||||
scheduler | |||||
""" | |||||
logger.debug("db_init_admin %s dn_name=%s", module, db_name) | |||||
logger.debug("db_init_admin db_name=%s", db_name) | |||||
init_admin_extensions(module, db_name) | |||||
@db.command(name="init", context_settings=CONTEXT_SETTINGS) | @db.command(name="init", context_settings=CONTEXT_SETTINGS) | ||||
@click.argument("module", required=True) | @click.argument("module", required=True) | ||||
@click.option( | @click.option( | ||||
"--db-name", | "--db-name", | ||||
"-d", | "-d", | ||||
help="Database name.", | help="Database name.", | ||||
default="softwareheritage-dev", | default="softwareheritage-dev", | ||||
show_default=True, | show_default=True, | ||||
▲ Show 20 Lines • Show All 108 Lines • ▼ Show 20 Lines | def parse_dsn_or_dbname(dsn_or_dbname: str) -> Dict[str, str]: | ||||
try: | try: | ||||
return _parse_dsn(dsn_or_dbname) | return _parse_dsn(dsn_or_dbname) | ||||
except psycopg2.ProgrammingError: | except psycopg2.ProgrammingError: | ||||
# psycopg2 failed to parse the DSN; it's probably a database name, | # psycopg2 failed to parse the DSN; it's probably a database name, | ||||
# handle it as such | # handle it as such | ||||
return _parse_dsn(f"dbname={dsn_or_dbname}") | return _parse_dsn(f"dbname={dsn_or_dbname}") | ||||
def init_admin_extensions(modname: str, conninfo: str) -> None: | |||||
"""The remaining initialization process -- running -superuser- SQL files -- is done | |||||
using the given conninfo, thus connecting to the newly created database | |||||
""" | |||||
sqlfiles = get_sql_for_package(modname) | |||||
sqlfiles = [fname for fname in sqlfiles if "-superuser-" in fname] | |||||
execute_sqlfiles(sqlfiles, conninfo) | |||||
def create_database_for_package( | def create_database_for_package( | ||||
modname: str, conninfo: str, template: str = "template1" | modname: str, conninfo: str, template: str = "template1" | ||||
): | ): | ||||
"""Create the database pointed at with `conninfo`, and initialize it using | """Create the database pointed at with `conninfo`, and initialize it using | ||||
-superuser- SQL files found in the package `modname`. | -superuser- SQL files found in the package `modname`. | ||||
Args: | Args: | ||||
modname: Name of the module of which we're loading the files | modname: Name of the module of which we're loading the files | ||||
Show All 20 Lines | subprocess.check_call( | ||||
"-v", | "-v", | ||||
"ON_ERROR_STOP=1", | "ON_ERROR_STOP=1", | ||||
"-d", | "-d", | ||||
make_dsn(**creation_dsn), | make_dsn(**creation_dsn), | ||||
"-c", | "-c", | ||||
f'CREATE DATABASE "{db_name}"', | f'CREATE DATABASE "{db_name}"', | ||||
] | ] | ||||
) | ) | ||||
init_admin_extensions(modname, conninfo) | |||||
# the remaining initialization process -- running -superuser- SQL files -- | |||||
# is done using the given conninfo, thus connecting to the newly created | |||||
# database | |||||
sqlfiles = get_sql_for_package(modname) | |||||
sqlfiles = [fname for fname in sqlfiles if "-superuser-" in fname] | |||||
execute_sqlfiles(sqlfiles, conninfo) | |||||
def execute_sqlfiles( | def execute_sqlfiles( | ||||
sqlfiles: Collection[str], conninfo: str, flavor: Optional[str] = None | sqlfiles: Collection[str], conninfo: str, flavor: Optional[str] = None | ||||
): | ): | ||||
"""Execute a list of SQL files on the database pointed at with `conninfo`. | """Execute a list of SQL files on the database pointed at with `conninfo`. | ||||
Args: | Args: | ||||
Show All 31 Lines |