Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reverting doesn't seem to work #29

Open
compufox opened this issue Nov 6, 2024 · 7 comments
Open

reverting doesn't seem to work #29

compufox opened this issue Nov 6, 2024 · 7 comments

Comments

@compufox
Copy link

compufox commented Nov 6, 2024

I've tried both the Fluent.revert and FluentMigrations.revertLast functions and neither seem to be working on my SQLite3 database.

no errors are thrown, and the single migration (a table creation) does not get reverted.

i've been testing this by running migrations then immediately trying to revert them, through separate invocations (I run my program with a migrate flag, then once it ends I run it with a revert flag)

i'm using Swift 6.0.1, with hummingbird-fluent version 2.0.0-beta.5

@adam-fowler
Copy link
Member

I'm away for a few days so won't be able to look into this. But all revert does is call into the FluentKit revert function.

I assume you have added the database to Fluent.databases before calling revert.

@adam-fowler
Copy link
Member

Did you add all the migrations to the migrator first? eg

await fluent.migrations.add(CreatePlanet())
try await fluent.revert()

@compufox
Copy link
Author

Yes, the database and migrations have been added before I attempt the call to revert(). I basically just copied my code for running migrations and changed the call from migrate() to revert(). The migrations do work, it's just reverting them that doesn't.

On a side note, I also checked the _fluent_migrations table in my SQLite database and I'm not seeing any data in the table.

@adam-fowler
Copy link
Member

Is the _fluent_migrations table empty after running a migrate?

@adam-fowler
Copy link
Member

Also can you post the code where you set up fluent and your migrations

@compufox
Copy link
Author

I deleted my database and re-applied the migration I've been testing with and yes, after running the migrations the new table is created and the _fluent_migrations table is empty.

Apply Migration Command:

minisoc.fluent.databases.use(.sqlite(.file("test.db")), as: .sqlite)
await minisoc.fluent.migrations.add(CreateAccountsTable20241106140124())
try await minisoc.fluent.migrate()

Revert command:

minisoc.fluent.databases.use(.sqlite(.file("test.db")), as: .sqlite)
await minisoc.fluent.migrations.add(CreateAccountsTable20241106140124())
try await minisoc.fluent.revert()

minisoc.fluent is a initialized as such: static let fluent = Fluent(logger: Logger(label: "minisoc-fluent"))

The migration is defined as follows:

import FluentKit

struct CreateAccountsTable20241106140124: AsyncMigration {
    func prepare(on database: Database) async throws {
        try await database.schema(Account.schema)
          .id()
          .field("username", .string)
          .field("email", .string)
          .field("password", .string)
          .create()
    }

    func revert(on database: Database) async throws {
        try await database.schema(Account.schema)
          .delete()
    }
}

@adam-fowler
Copy link
Member

When using the todos-fluent example in hummingbird-examples I can see in the log the migrations being migrated and reverted as expected and those changes actually being applied to the database.

What are you using to view the database?
When using DB Browser for SQLite I found I had to reload the database to get it to refresh the database structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants