Skip to content

Commit

Permalink
upgrade-modules-db: support commons alembic args
Browse files Browse the repository at this point in the history
  • Loading branch information
bouttier committed Jan 18, 2023
1 parent ae2c089 commit 194440b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
28 changes: 23 additions & 5 deletions backend/geonature/core/command/create_gn_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@


@main.command()
@click.option(
"-x", "--x-arg", multiple=True, help="Additional arguments consumed by custom env.py scripts"
)
@click.argument("module_path")
@click.argument("module_code")
@click.option("--build", type=bool, required=False, default=True)
@click.option("--upgrade-db", type=bool, required=False, default=True)
def install_gn_module(module_path, module_code, build, upgrade_db):
def install_gn_module(x_arg, module_path, module_code, build, upgrade_db):
click.echo("Installation du backend…")
subprocess.run(f"pip install -e '{module_path}'", shell=True, check=True)

Expand Down Expand Up @@ -66,13 +69,28 @@ def install_gn_module(module_path, module_code, build, upgrade_db):
click.secho("Rebuild du frontend terminé.", fg="green")

if upgrade_db:
click.echo("Installation de la basse de données…")
module_db_upgrade(module_dist)
click.echo("Installation de la base de données…")
module_db_upgrade(module_dist, x_arg=x_arg)


@main.command()
@click.option(
"-d",
"--directory",
default=None,
help=('Migration script directory (default is "migrations")'),
)
@click.option(
"--sql", is_flag=True, help=("Don't emit SQL to database - dump to standard output " "instead")
)
@click.option(
"--tag", default=None, help=('Arbitrary "tag" name - can be used by custom env.py ' "scripts")
)
@click.option(
"-x", "--x-arg", multiple=True, help="Additional arguments consumed by custom env.py scripts"
)
@click.argument("module_codes", metavar="[MODULE_CODE]...", nargs=-1)
def upgrade_modules_db(module_codes):
def upgrade_modules_db(directory, sql, tag, x_arg, module_codes):
for module_code_entry in iter_entry_points("gn_module", "code"):
module_code = module_code_entry.resolve()
if module_codes and module_code not in module_codes:
Expand All @@ -82,4 +100,4 @@ def upgrade_modules_db(module_codes):
continue
click.echo(f"Mise-à-jour du module {module_code}…")
module_dist = module_code_entry.dist
module_db_upgrade(module_dist)
module_db_upgrade(module_dist, directory, sql, tag, x_arg)
5 changes: 3 additions & 2 deletions backend/geonature/utils/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_dist_from_code(module_code):
raise Exception(f"Module with code {module_code} not installed in venv")


def module_db_upgrade(module_dist):
def module_db_upgrade(module_dist, directory=None, sql=False, tag=None, x_arg=[]):
module_code = module_dist.load_entry_point("gn_module", "code")
module = TModules.query.filter_by(module_code=module_code).one_or_none()
if module is None:
Expand Down Expand Up @@ -74,4 +74,5 @@ def module_db_upgrade(module_dist):
alembic_branch = module_dist.load_entry_point("gn_module", "alembic_branch")
except ImportError:
alembic_branch = module_code.lower()
db_upgrade(revision=alembic_branch + "@head")
revision = alembic_branch + "@head"
db_upgrade(directory, revision, sql, tag, x_arg)

0 comments on commit 194440b

Please sign in to comment.