Exemplo n.º 1
0
    def get(self):
        """
        Invokes the alembic upgrade process.
        """
        # Note: This method is called after the database configured is saved, but before the
        # database has any tables. Therefore, we only allow it to be run in that unique case.
        if config_provider.config_exists() and not database_is_valid():
            combined = _reload_config()

            app.config["DB_URI"] = combined["DB_URI"]
            db_uri = app.config["DB_URI"]
            escaped_db_uri = db_uri.replace("%", "%%")

            log_handler = _AlembicLogHandler()

            try:
                run_alembic_migration(escaped_db_uri,
                                      log_handler,
                                      setup_app=False)
            except Exception as ex:
                return {"error": str(ex)}

            return {"logs": log_handler.records}

        abort(403)
Exemplo n.º 2
0
def test_alembic_db_uri(db_uri, is_valid):
    """ Test if the given URI is escaped for string interpolation (Python's configparser). """
    with patch("alembic.script.ScriptDirectory.run_env") as m:
        if is_valid:
            run_alembic_migration(db_uri)
        else:
            with pytest.raises(ValueError):
                run_alembic_migration(db_uri)
Exemplo n.º 3
0
def process(resources):
    response = []
    changed = True
    expected_tables = [
        "accesstoken",
        "accesstokenkind",
        "alembic_version",
        "apprblob",
        "apprblobplacement",
        "apprblobplacementlocation",
        "apprmanifest",
        "apprmanifestblob",
        "apprmanifestlist",
        "apprmanifestlistmanifest",
        "apprtag",
        "apprtagkind",
        "appspecificauthtoken",
        "blobupload",
        "buildtriggerservice",
        "deletednamespace",
        "derivedstorageforimage",
        "disablereason",
        "emailconfirmation",
        "externalnotificationevent",
        "externalnotificationmethod",
        "federatedlogin",
        "image",
        "imagestorage",
        "imagestoragelocation",
        "imagestorageplacement",
        "imagestoragesignature",
        "imagestoragesignaturekind",
        "imagestoragetransformation",
        "label",
        "labelsourcetype",
        "logentry",
        "logentry2",
        "logentry3",
        "logentrykind",
        "loginservice",
        "manifest",
        "manifestblob",
        "manifestchild",
        "manifestlabel",
        "manifestlegacyimage",
        "mediatype",
        "messages",
        "namespacegeorestriction",
        "notification",
        "notificationkind",
        "oauthaccesstoken",
        "oauthapplication",
        "oauthauthorizationcode",
        "permissionprototype",
        "quayregion",
        "quayrelease",
        "quayservice",
        "queueitem",
        "repomirrorconfig",
        "repomirrorrule",
        "robotaccountmetadata",
        "repository",
        "repositoryactioncount",
        "repositoryauthorizedemail",
        "repositorybuild",
        "repositorybuildtrigger",
        "repositorykind",
        "repositorynotification",
        "repositorypermission",
        "repositorysearchscore",
        "repositorytag",
        "role",
        "servicekey",
        "servicekeyapproval",
        "star",
        "tag",
        "tagkind",
        "tagmanifest",
        "tagmanifestlabel",
        "tagmanifestlabelmap",
        "tagmanifesttomanifest",
        "tagtorepositorytag",
        "team",
        "teammember",
        "teammemberinvite",
        "teamrole",
        "teamsync",
        "torrentinfo",
        "user",
        "userprompt",
        "userpromptkind",
        "userregion",
        "visibility",
    ]

    for resource in resources:
        p_state = resource["state"]

        if p_state == "reset":
            db.drop_tables(all_models)

        if p_state == "reset" or p_state == "migrate":
            try:
                db_uri = app.config["DB_URI"]
                runmigration.run_alembic_migration(db_uri,
                                                   logger,
                                                   setup_app=False)
            except Exception as ex:
                return (
                    {
                        "failed": True,
                        "msg": "Database migrations failed: %s" % ex
                    },
                    400,
                )
            changed = True

        # Always run 'present' state to confirm 'reset'
        tables = db.get_tables()
        extra_tables = list(set(expected_tables) - set(tables))
        missing_tables = list(set(tables) - set(expected_tables))
        if extra_tables != [] or missing_tables != []:
            return (
                {
                    "failed":
                    True,
                    "msg":
                    "Database exists but has mismatched tables\nMissing tables: %s\nExtra tables: %s"
                    % (missing_tables, extra_tables),
                },
                400,
            )

    return {"failed": False, "changed": changed, "meta": response}, 200