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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Lythenas
Copy link

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
Author

Yes thanks, I didn't think about that.

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