We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Set plugin migration version in between each migration
https://github.com/CTFd/CTFd/blob/e1991e16963b10302baa7cc50d52071a5053bf2f/CTFd/plugins/migrations.py#L72-L77

This code here probably should be setting the plugin version in between each migration so that if a migration fails it doesn't need to be started from the beginning again. 

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `CTFd/plugins/migrations.py`
Content:
```
1 import inspect
2 import os
3 
4 from alembic.config import Config
5 from alembic.migration import MigrationContext
6 from alembic.operations import Operations
7 from alembic.script import ScriptDirectory
8 from flask import current_app
9 from sqlalchemy import create_engine, pool
10 
11 from CTFd.utils import get_config, set_config
12 
13 
14 def current(plugin_name=None):
15     if plugin_name is None:
16         # Get the directory name of the plugin if unspecified
17         # Doing it this way doesn't waste the rest of the inspect.stack call
18         frame = inspect.currentframe()
19         caller_info = inspect.getframeinfo(frame.f_back)
20         caller_path = caller_info[0]
21         plugin_name = os.path.basename(os.path.dirname(caller_path))
22 
23     return get_config(plugin_name + "_alembic_version")
24 
25 
26 def upgrade(plugin_name=None, revision=None, lower="current"):
27     database_url = current_app.config.get("SQLALCHEMY_DATABASE_URI")
28     if database_url.startswith("sqlite"):
29         current_app.db.create_all()
30         return
31 
32     if plugin_name is None:
33         # Get the directory name of the plugin if unspecified
34         # Doing it this way doesn't waste the rest of the inspect.stack call
35         frame = inspect.currentframe()
36         caller_info = inspect.getframeinfo(frame.f_back)
37         caller_path = caller_info[0]
38         plugin_name = os.path.basename(os.path.dirname(caller_path))
39 
40     # Check if the plugin has migraitons
41     migrations_path = os.path.join(current_app.plugins_dir, plugin_name, "migrations")
42     if os.path.isdir(migrations_path) is False:
43         return
44 
45     engine = create_engine(database_url, poolclass=pool.NullPool)
46     conn = engine.connect()
47     context = MigrationContext.configure(conn)
48     op = Operations(context)
49 
50     # Find the list of migrations to run
51     config = Config()
52     config.set_main_option("script_location", migrations_path)
53     config.set_main_option("version_locations", migrations_path)
54     script = ScriptDirectory.from_config(config)
55 
56     # Choose base revision for plugin upgrade
57     # "current" points to the current plugin version stored in config
58     # None represents the absolute base layer (e.g. first installation)
59     if lower == "current":
60         lower = get_config(plugin_name + "_alembic_version")
61 
62     # Do we upgrade to head or to a specific revision
63     if revision is None:
64         upper = script.get_current_head()
65     else:
66         upper = revision
67 
68     # Apply from lower to upper
69     revs = list(script.iterate_revisions(lower=lower, upper=upper))
70     revs.reverse()
71 
72     try:
73         for r in revs:
74             with context.begin_transaction():
75                 r.module.upgrade(op=op)
76     finally:
77         conn.close()
78 
79     # Set the new latest revision
80     set_config(plugin_name + "_alembic_version", upper)
81
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/CTFd/plugins/migrations.py b/CTFd/plugins/migrations.py
--- a/CTFd/plugins/migrations.py
+++ b/CTFd/plugins/migrations.py
@@ -73,6 +73,9 @@
         for r in revs:
             with context.begin_transaction():
                 r.module.upgrade(op=op)
+            # Set revision that succeeded so we don't need
+            # to start from the beginning on failure
+            set_config(plugin_name + "_alembic_version", r.revision)
     finally:
         conn.close()

{"golden_diff": "diff --git a/CTFd/plugins/migrations.py b/CTFd/plugins/migrations.py\n--- a/CTFd/plugins/migrations.py\n+++ b/CTFd/plugins/migrations.py\n@@ -73,6 +73,9 @@\n         for r in revs:\n             with context.begin_transaction():\n                 r.module.upgrade(op=op)\n+            # Set revision that succeeded so we don't need\n+            # to start from the beginning on failure\n+            set_config(plugin_name + \"_alembic_version\", r.revision)\n     finally:\n         conn.close()\n", "issue": "Set plugin migration version in between each migration\nhttps://github.com/CTFd/CTFd/blob/e1991e16963b10302baa7cc50d52071a5053bf2f/CTFd/plugins/migrations.py#L72-L77\r\n\r\nThis code here probably should be setting the plugin version in between each migration so that if a migration fails it doesn't need to be started from the beginning again. \n", "before_files": [{"content": "import inspect\nimport os\n\nfrom alembic.config import Config\nfrom alembic.migration import MigrationContext\nfrom alembic.operations import Operations\nfrom alembic.script import ScriptDirectory\nfrom flask import current_app\nfrom sqlalchemy import create_engine, pool\n\nfrom CTFd.utils import get_config, set_config\n\n\ndef current(plugin_name=None):\n    if plugin_name is None:\n        # Get the directory name of the plugin if unspecified\n        # Doing it this way doesn't waste the rest of the inspect.stack call\n        frame = inspect.currentframe()\n        caller_info = inspect.getframeinfo(frame.f_back)\n        caller_path = caller_info[0]\n        plugin_name = os.path.basename(os.path.dirname(caller_path))\n\n    return get_config(plugin_name + \"_alembic_version\")\n\n\ndef upgrade(plugin_name=None, revision=None, lower=\"current\"):\n    database_url = current_app.config.get(\"SQLALCHEMY_DATABASE_URI\")\n    if database_url.startswith(\"sqlite\"):\n        current_app.db.create_all()\n        return\n\n    if plugin_name is None:\n        # Get the directory name of the plugin if unspecified\n        # Doing it this way doesn't waste the rest of the inspect.stack call\n        frame = inspect.currentframe()\n        caller_info = inspect.getframeinfo(frame.f_back)\n        caller_path = caller_info[0]\n        plugin_name = os.path.basename(os.path.dirname(caller_path))\n\n    # Check if the plugin has migraitons\n    migrations_path = os.path.join(current_app.plugins_dir, plugin_name, \"migrations\")\n    if os.path.isdir(migrations_path) is False:\n        return\n\n    engine = create_engine(database_url, poolclass=pool.NullPool)\n    conn = engine.connect()\n    context = MigrationContext.configure(conn)\n    op = Operations(context)\n\n    # Find the list of migrations to run\n    config = Config()\n    config.set_main_option(\"script_location\", migrations_path)\n    config.set_main_option(\"version_locations\", migrations_path)\n    script = ScriptDirectory.from_config(config)\n\n    # Choose base revision for plugin upgrade\n    # \"current\" points to the current plugin version stored in config\n    # None represents the absolute base layer (e.g. first installation)\n    if lower == \"current\":\n        lower = get_config(plugin_name + \"_alembic_version\")\n\n    # Do we upgrade to head or to a specific revision\n    if revision is None:\n        upper = script.get_current_head()\n    else:\n        upper = revision\n\n    # Apply from lower to upper\n    revs = list(script.iterate_revisions(lower=lower, upper=upper))\n    revs.reverse()\n\n    try:\n        for r in revs:\n            with context.begin_transaction():\n                r.module.upgrade(op=op)\n    finally:\n        conn.close()\n\n    # Set the new latest revision\n    set_config(plugin_name + \"_alembic_version\", upper)\n", "path": "CTFd/plugins/migrations.py"}], "after_files": [{"content": "import inspect\nimport os\n\nfrom alembic.config import Config\nfrom alembic.migration import MigrationContext\nfrom alembic.operations import Operations\nfrom alembic.script import ScriptDirectory\nfrom flask import current_app\nfrom sqlalchemy import create_engine, pool\n\nfrom CTFd.utils import get_config, set_config\n\n\ndef current(plugin_name=None):\n    if plugin_name is None:\n        # Get the directory name of the plugin if unspecified\n        # Doing it this way doesn't waste the rest of the inspect.stack call\n        frame = inspect.currentframe()\n        caller_info = inspect.getframeinfo(frame.f_back)\n        caller_path = caller_info[0]\n        plugin_name = os.path.basename(os.path.dirname(caller_path))\n\n    return get_config(plugin_name + \"_alembic_version\")\n\n\ndef upgrade(plugin_name=None, revision=None, lower=\"current\"):\n    database_url = current_app.config.get(\"SQLALCHEMY_DATABASE_URI\")\n    if database_url.startswith(\"sqlite\"):\n        current_app.db.create_all()\n        return\n\n    if plugin_name is None:\n        # Get the directory name of the plugin if unspecified\n        # Doing it this way doesn't waste the rest of the inspect.stack call\n        frame = inspect.currentframe()\n        caller_info = inspect.getframeinfo(frame.f_back)\n        caller_path = caller_info[0]\n        plugin_name = os.path.basename(os.path.dirname(caller_path))\n\n    # Check if the plugin has migraitons\n    migrations_path = os.path.join(current_app.plugins_dir, plugin_name, \"migrations\")\n    if os.path.isdir(migrations_path) is False:\n        return\n\n    engine = create_engine(database_url, poolclass=pool.NullPool)\n    conn = engine.connect()\n    context = MigrationContext.configure(conn)\n    op = Operations(context)\n\n    # Find the list of migrations to run\n    config = Config()\n    config.set_main_option(\"script_location\", migrations_path)\n    config.set_main_option(\"version_locations\", migrations_path)\n    script = ScriptDirectory.from_config(config)\n\n    # Choose base revision for plugin upgrade\n    # \"current\" points to the current plugin version stored in config\n    # None represents the absolute base layer (e.g. first installation)\n    if lower == \"current\":\n        lower = get_config(plugin_name + \"_alembic_version\")\n\n    # Do we upgrade to head or to a specific revision\n    if revision is None:\n        upper = script.get_current_head()\n    else:\n        upper = revision\n\n    # Apply from lower to upper\n    revs = list(script.iterate_revisions(lower=lower, upper=upper))\n    revs.reverse()\n\n    try:\n        for r in revs:\n            with context.begin_transaction():\n                r.module.upgrade(op=op)\n            # Set revision that succeeded so we don't need\n            # to start from the beginning on failure\n            set_config(plugin_name + \"_alembic_version\", r.revision)\n    finally:\n        conn.close()\n\n    # Set the new latest revision\n    set_config(plugin_name + \"_alembic_version\", upper)\n", "path": "CTFd/plugins/migrations.py"}]}