Changeset View
Changeset View
Standalone View
Standalone View
swh/core/cli/db.py
Show First 20 Lines • Show All 192 Lines • ▼ Show 20 Lines | def db_init(ctx, module, dbname, flavor, initial_version): | ||||
if dbversion is None: | if dbversion is None: | ||||
if cfg is not None: | if cfg is not None: | ||||
# db version has not been populated by sql init scripts (new style), | # db version has not been populated by sql init scripts (new style), | ||||
# let's do it; instantiate the data source to retrieve the current | # let's do it; instantiate the data source to retrieve the current | ||||
# (expected) db version | # (expected) db version | ||||
datastore_factory = getattr(import_swhmodule(module), "get_datastore", None) | datastore_factory = getattr(import_swhmodule(module), "get_datastore", None) | ||||
if datastore_factory: | if datastore_factory: | ||||
datastore = datastore_factory(**cfg) | datastore = datastore_factory(**cfg) | ||||
if not hasattr(datastore, "current_version"): | try: | ||||
get_current_version = datastore.get_current_version | |||||
except AttributeError: | |||||
logger.warning( | logger.warning( | ||||
"Datastore %s does not declare the " | "Datastore %s does not implement the " | ||||
"'current_version' attribute", | "'get_current_version()' method", | ||||
datastore, | datastore, | ||||
) | ) | ||||
else: | else: | ||||
code_version = datastore.current_version | code_version = get_current_version() | ||||
logger.info( | logger.info( | ||||
"Initializing database version to %s from the %s datastore", | "Initializing database version to %s from the %s datastore", | ||||
code_version, | code_version, | ||||
module, | module, | ||||
) | ) | ||||
swh_set_db_version(dbname, code_version, desc="DB initialization") | swh_set_db_version(dbname, code_version, desc="DB initialization") | ||||
dbversion = get_database_info(dbname)[1] | dbversion = get_database_info(dbname)[1] | ||||
▲ Show 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | def db_version(ctx, module, show_all, module_config_key=None): | ||||
if db_flavor is not None: | if db_flavor is not None: | ||||
click.secho(f"flavor: {db_flavor}", fg="green", bold=True) | click.secho(f"flavor: {db_flavor}", fg="green", bold=True) | ||||
# instantiate the data source to retrieve the current (expected) db version | # instantiate the data source to retrieve the current (expected) db version | ||||
datastore_factory = getattr(import_swhmodule(db_module), "get_datastore", None) | datastore_factory = getattr(import_swhmodule(db_module), "get_datastore", None) | ||||
if datastore_factory: | if datastore_factory: | ||||
datastore = datastore_factory(**cfg) | datastore = datastore_factory(**cfg) | ||||
code_version = datastore.current_version | code_version = datastore.get_current_version() | ||||
click.secho( | click.secho( | ||||
f"current code version: {code_version}", | f"current code version: {code_version}", | ||||
fg="green" if code_version == db_version else "red", | fg="green" if code_version == db_version else "red", | ||||
bold=True, | bold=True, | ||||
) | ) | ||||
if not show_all: | if not show_all: | ||||
click.secho(f"version: {db_version}", fg="green", bold=True) | click.secho(f"version: {db_version}", fg="green", bold=True) | ||||
▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | def db_upgrade(ctx, module, to_version, interactive, module_config_key): | ||||
# instantiate the data source to retrieve the current (expected) db version | # instantiate the data source to retrieve the current (expected) db version | ||||
datastore_factory = getattr(import_swhmodule(db_module), "get_datastore", None) | datastore_factory = getattr(import_swhmodule(db_module), "get_datastore", None) | ||||
if not datastore_factory: | if not datastore_factory: | ||||
raise click.UsageError( | raise click.UsageError( | ||||
"You cannot use this command on old-style datastore backend {db_module}" | "You cannot use this command on old-style datastore backend {db_module}" | ||||
) | ) | ||||
datastore = datastore_factory(**cfg) | datastore = datastore_factory(**cfg) | ||||
ds_version = datastore.current_version | ds_version = datastore.get_current_version() | ||||
if to_version is None: | if to_version is None: | ||||
to_version = ds_version | to_version = ds_version | ||||
if to_version > ds_version: | if to_version > ds_version: | ||||
raise click.UsageError( | raise click.UsageError( | ||||
f"The target version {to_version} is larger than the current version " | f"The target version {to_version} is larger than the current version " | ||||
f"{ds_version} of the datastore backend {db_module}" | f"{ds_version} of the datastore backend {db_module}" | ||||
) | ) | ||||
Show All 25 Lines |