From ab4c37b31fece8786a3c257aa0c2a03a40fc46bb Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Sun, 6 Dec 2020 22:24:37 -0500 Subject: [PATCH] Add a lower parameter to the plugin upgrade() function to help importing (#1755) - The plugin `upgrade()` function now accepts a `lower` parameter which specifies what lower revision should be used to start from. - This is used to support plugin migrations during import so that we can import data directly at the point that the import was taken from - `lower="current"` means to use the current revision and `lower=None` would mean to use the absolute base revision (e.g. plugin's first installation) - By default this doesn't change `upgrade()` behavior --- CHANGELOG.md | 4 ++++ CTFd/plugins/migrations.py | 9 ++++++--- CTFd/utils/exports/__init__.py | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fb5ba10..c51f8ca6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,10 @@ - Don't run `db.create_all()` as much during plugin upgrade or during imports - By avoiding this we can let alembic and migrations do more of the table creation work but this means that plugins specifically opt into `app.db.create_all()` and will not implicitly get it through `upgrade()`. - This means plugins that run `upgrade()` without a migrations folder (no idea who would do this really) will need to upgrade their code. +- The plugin `upgrade()` function now accepts a `lower` parameter which specifies what lower revision should be used to start from. + - This is used to support plugin migrations during import so that we can import data directly at the point that the import was taken from + - `lower="current"` means to use the current revision and `lower=None` would mean to use the absolute base revision (e.g. plugin's first installation) + - By default this doesn't change `upgrade()` behavior **Admin Panel** diff --git a/CTFd/plugins/migrations.py b/CTFd/plugins/migrations.py index 77fed28f..a3661de2 100644 --- a/CTFd/plugins/migrations.py +++ b/CTFd/plugins/migrations.py @@ -23,7 +23,7 @@ def current(plugin_name=None): return get_config(plugin_name + "_alembic_version") -def upgrade(plugin_name=None, revision=None): +def upgrade(plugin_name=None, revision=None, lower="current"): database_url = current_app.config.get("SQLALCHEMY_DATABASE_URI") if database_url.startswith("sqlite"): current_app.db.create_all() @@ -53,8 +53,11 @@ def upgrade(plugin_name=None, revision=None): config.set_main_option("version_locations", migrations_path) script = ScriptDirectory.from_config(config) - # get current revision for plugin - lower = get_config(plugin_name + "_alembic_version") + # Choose base revision for plugin upgrade + # "current" points to the current plugin version stored in config + # None represents the absolute base layer (e.g. first installation) + if lower == "current": + lower = get_config(plugin_name + "_alembic_version") # Do we upgrade to head or to a specific revision if revision is None: diff --git a/CTFd/utils/exports/__init__.py b/CTFd/utils/exports/__init__.py index f2729d8f..b4b98bae 100644 --- a/CTFd/utils/exports/__init__.py +++ b/CTFd/utils/exports/__init__.py @@ -306,7 +306,7 @@ def import_ctf(backup, erase=True): plugins = get_plugin_names() for plugin in plugins: revision = plugin_current(plugin_name=plugin) - plugin_upgrade(plugin_name=plugin, revision=revision) + plugin_upgrade(plugin_name=plugin, revision=revision, lower=None) # Insert data for plugin tables insertion(members)