async def METHOD_NAME(conn: Connection, scheme: Scheme) -> None:
    legacy_version = await conn.fetchval(legacy_version_query)
    if legacy_version != last_legacy_version:
        raise RuntimeError(
            "Legacy database is not on last version. "
            "Please upgrade the old database with alembic or drop it completely first."
        )
    if scheme != Scheme.SQLITE:
        await drop_constraints(conn, "contact", contype="f")
        await conn.execute(
            """
            ALTER TABLE contact
              ADD CONSTRAINT contact_user_fkey FOREIGN KEY (contact) REFERENCES puppet(id)
                ON DELETE CASCADE ON UPDATE CASCADE,
              ADD CONSTRAINT contact_contact_fkey FOREIGN KEY ("user") REFERENCES "user"(tgid)
                ON DELETE CASCADE ON UPDATE CASCADE
            """
        )
        await drop_constraints(conn, "telethon_sessions", contype="p")
        await conn.execute(
            """
            ALTER TABLE telethon_sessions
              ADD CONSTRAINT telethon_sessions_pkey PRIMARY KEY (session_id)
            """
        )
        await drop_constraints(conn, "telegram_file", contype="f")
        await conn.execute(
            """
            ALTER TABLE telegram_file
              ADD CONSTRAINT fk_file_thumbnail
                FOREIGN KEY (thumbnail) REFERENCES telegram_file(id)
                ON UPDATE CASCADE ON DELETE SET NULL
            """
        )
        await conn.execute("ALTER TABLE puppet ALTER COLUMN id DROP IDENTITY IF EXISTS")
        await conn.execute("ALTER TABLE puppet ALTER COLUMN id DROP DEFAULT")
        await conn.execute("DROP SEQUENCE IF EXISTS puppet_id_seq")
        await conn.execute("ALTER TABLE bot_chat ALTER COLUMN id DROP IDENTITY IF EXISTS")
        await conn.execute("ALTER TABLE bot_chat ALTER COLUMN id DROP DEFAULT")
        await conn.execute("DROP SEQUENCE IF EXISTS bot_chat_id_seq")
        await conn.execute("ALTER TABLE portal ALTER COLUMN config TYPE jsonb USING config::jsonb")
        await conn.execute(
            "ALTER TABLE telegram_file ALTER COLUMN decryption_info TYPE jsonb "
            "USING decryption_info::jsonb"
        )
        await varchar_to_text(conn)
    else:
        await conn.execute(
            """CREATE TABLE telethon_sessions_new (
                session_id     TEXT PRIMARY KEY,
                dc_id          INTEGER,
                server_address TEXT,
                port           INTEGER,
                auth_key       bytea
            )"""
        )
        await conn.execute(
            """
            INSERT INTO telethon_sessions_new (session_id, dc_id, server_address, port, auth_key)
            SELECT session_id, dc_id, server_address, port, auth_key FROM telethon_sessions
            """
        )
        await conn.execute("DROP TABLE telethon_sessions")
        await conn.execute("ALTER TABLE telethon_sessions_new RENAME TO telethon_sessions")
    await update_state_store(conn, scheme)
    await conn.execute('ALTER TABLE "user" ADD COLUMN is_bot BOOLEAN NOT NULL DEFAULT false')
    await conn.execute("ALTER TABLE puppet RENAME COLUMN matrix_registered TO is_registered")
    await conn.execute("DROP TABLE telethon_version")
    await conn.execute("DROP TABLE alembic_version")