Option for changing the default migration table name from __diesel_schema_migrations
#3771
Replies: 2 comments 7 replies
-
I think I would be open for such a change, as long as:
From a technical point of view that likely just means that you need to provide another type that implements |
Beta Was this translation helpful? Give feedback.
-
I came here since I wanted to a build a seeding migrations system separate from the schema migrations. The For reference this is my custom harness: use diesel::migration::{Migration, MigrationVersion, Result};
use diesel::sqlite::Sqlite;
use diesel::{Connection, ExpressionMethods, QueryDsl, RunQueryDsl, SqliteConnection};
use diesel_migrations::MigrationHarness;
diesel::table! {
__caido_seed_migrations (version) {
version -> VarChar,
run_on -> Timestamp,
}
}
pub struct SeedHarness<'a> {
connection: &'a mut SqliteConnection,
}
impl<'a> SeedHarness<'a> {
pub fn new(connection: &'a mut SqliteConnection) -> Self {
Self { connection }
}
}
impl<'a> MigrationHarness<Sqlite> for SeedHarness<'a> {
fn run_migration(
&mut self,
migration: &dyn Migration<Sqlite>,
) -> Result<MigrationVersion<'static>> {
let apply_migration = |conn: &mut SqliteConnection| -> Result<()> {
migration.run(conn)?;
diesel::insert_into(__caido_seed_migrations::table)
.values(__caido_seed_migrations::version.eq(migration.name().version().as_owned()))
.execute(conn)?;
Ok(())
};
if migration.metadata().run_in_transaction() {
self.connection.transaction(apply_migration)?;
} else {
apply_migration(self.connection)?;
}
Ok(migration.name().version().as_owned())
}
fn revert_migration(
&mut self,
_migration: &dyn Migration<Sqlite>,
) -> Result<MigrationVersion<'static>> {
Err("Seeds are not revertable".into())
}
fn applied_migrations(&mut self) -> Result<Vec<MigrationVersion<'static>>> {
Ok(__caido_seed_migrations::table
.select(__caido_seed_migrations::version)
.order(__caido_seed_migrations::version.desc())
.load(self.connection)?)
}
} |
Beta Was this translation helpful? Give feedback.
-
Running different applications, or multiple copies of them, in the same database instance, or even the same schema if one is restricted to it, it would be great if
diesel
supported changing the migration table name used, e.g., the request in #1728 may be satisfied by this.Different roles with privileges granted by a higher-level admin allow such a structure to be safely set up so different migrations will not clobber each other. Not too much different than managing infrastructure with Terraform with different modules and devops cloud IAM accounts. A library like Diesel isn't directly responsible for database access security, so the anti-clobber argument against a feature request like this really isn't relevant IMO.
If it is reasonably straightforward, I'd be completely willing to do a PR to get this to work. See launchbadge/sqlx#2725 for example. It's just a matter if project is willing to take on this feature and accept a PR (if it doesn't require a lot of refactoring).
Beta Was this translation helpful? Give feedback.
All reactions