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

Allow modifying the connection in migrations #2397

Merged
merged 3 commits into from
Jan 9, 2025

Conversation

Lythenas
Copy link
Contributor

New Features

Add a function to allow modifying the database connection in migrations.

This allows configuring the database connection like you do in your real application. I'm currently using this to register sqlite functions.

Usage would be something like this:

#[async_std::main]
async fn main() {
    cli::run_cli_with_connection(migration::Migrator, |connect_options| async {
        let db = Database::connect(connect_options)
             .await?;
        if db.get_database_backend() == DatabaseBackend::Sqlite {
            db::register_sqlite_functions(&db).await;
        }
        return Ok(db);
    }).await;
}

Note: I didn't see a test for run_cli except in the examples, so I wasn't sure if/where to add a test.

I also looked at adding it as a function to MigratorTrait but there we could at most accept a &DbConn (which would allow my use case) but I think this way is more flexible. The closure is accepting ConnectOptions so we can continue to use the same cli options and DATABASE_URL var. But it still allows to modify it before creating the connection or throw it away and create your own, if needed.

Copy link
Member

@tyt2y3 tyt2y3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for your contribution. I think this is a nice idea, however can you make run_cli delegate to run_cli_with_connection ? I assume the following would work?

async fn run_cli(migrator: M) {
    run_cli_with_connection(migrator, Database::connect)
}

@Lythenas
Copy link
Contributor Author

Yes thanks, I didn't think about that.

@tyt2y3 tyt2y3 merged commit 5db8cb0 into SeaQL:master Jan 9, 2025
36 checks passed
Copy link

🎉 Released In 1.1.4 🎉

Thank you everyone for the contribution!
This feature is now available in the latest release. Now is a good time to upgrade!
Your participation is what makes us unique; your adoption is what drives us forward.
You can support SeaQL 🌊 by starring our repos, sharing our libraries and becoming a sponsor ⭐.

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

Successfully merging this pull request may close these issues.

2 participants