Skip to content

Support explicit schema selection for the migration table #386

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions refinery/tests/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ mod mysql {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.unwrap();

Expand Down Expand Up @@ -483,6 +484,7 @@ mod mysql {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.unwrap_err();

Expand Down Expand Up @@ -519,6 +521,7 @@ mod mysql {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.unwrap_err();

Expand Down Expand Up @@ -568,6 +571,7 @@ mod mysql {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.unwrap_err();
match err.kind() {
Expand Down
4 changes: 4 additions & 0 deletions refinery/tests/mysql_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ mod mysql_async {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.await
.unwrap();
Expand Down Expand Up @@ -494,6 +495,7 @@ mod mysql_async {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.await
.unwrap_err();
Expand Down Expand Up @@ -539,6 +541,7 @@ mod mysql_async {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.await
.unwrap_err();
Expand Down Expand Up @@ -591,6 +594,7 @@ mod mysql_async {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.await
.unwrap_err();
Expand Down
4 changes: 4 additions & 0 deletions refinery/tests/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ mod postgres {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.unwrap();

Expand Down Expand Up @@ -455,6 +456,7 @@ mod postgres {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.unwrap_err();

Expand Down Expand Up @@ -488,6 +490,7 @@ mod postgres {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.unwrap_err();

Expand Down Expand Up @@ -534,6 +537,7 @@ mod postgres {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.unwrap_err();
match err.kind() {
Expand Down
4 changes: 4 additions & 0 deletions refinery/tests/rusqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ mod rusqlite {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.unwrap();

Expand Down Expand Up @@ -583,6 +584,7 @@ mod rusqlite {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.unwrap_err();

Expand Down Expand Up @@ -614,6 +616,7 @@ mod rusqlite {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.unwrap_err();

Expand Down Expand Up @@ -658,6 +661,7 @@ mod rusqlite {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.unwrap_err();
match err.kind() {
Expand Down
4 changes: 4 additions & 0 deletions refinery/tests/tiberius.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ mod tiberius {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.await
.unwrap_err();
Expand Down Expand Up @@ -189,6 +190,7 @@ mod tiberius {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.await
.unwrap_err();
Expand Down Expand Up @@ -253,6 +255,7 @@ mod tiberius {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.await
.unwrap_err();
Expand Down Expand Up @@ -523,6 +526,7 @@ mod tiberius {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.await
.unwrap();
Expand Down
13 changes: 10 additions & 3 deletions refinery/tests/tokio_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ mod tokio_postgres {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.await
.unwrap();
Expand Down Expand Up @@ -631,6 +632,7 @@ mod tokio_postgres {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.await
.unwrap_err();
Expand Down Expand Up @@ -677,6 +679,7 @@ mod tokio_postgres {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.await
.unwrap_err();
Expand Down Expand Up @@ -735,6 +738,7 @@ mod tokio_postgres {
false,
Target::Latest,
DEFAULT_TABLE_NAME,
None,
)
.await
.unwrap_err();
Expand All @@ -757,7 +761,8 @@ mod tokio_postgres {
.set_db_name("postgres")
.set_db_user("postgres")
.set_db_host("localhost")
.set_db_port("5432");
.set_db_port("5432")
.set_db_schema("public");

let migrations = get_migrations();
let runner = Runner::new(&migrations)
Expand Down Expand Up @@ -801,7 +806,8 @@ mod tokio_postgres {
.set_db_name("postgres")
.set_db_user("postgres")
.set_db_host("localhost")
.set_db_port("5432");
.set_db_port("5432")
.set_db_schema("public");

let migrations = get_migrations();
let runner = Runner::new(&migrations)
Expand Down Expand Up @@ -842,7 +848,8 @@ mod tokio_postgres {
.set_db_name("postgres")
.set_db_user("postgres")
.set_db_host("localhost")
.set_db_port("5432");
.set_db_port("5432")
.set_db_schema("public");

let migrations = get_migrations();
let runner = Runner::new(&migrations)
Expand Down
4 changes: 4 additions & 0 deletions refinery_cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ pub struct MigrateArgs {
#[clap(long, default_value = "refinery_schema_history")]
pub table_name: String,

/// Set explicit migration table schema
#[clap(long)]
pub table_schema: Option<String>,

/// Should abort if divergent migrations are found
#[clap(short)]
pub divergent: bool,
Expand Down
4 changes: 4 additions & 0 deletions refinery_cli/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn handle_migration_command(args: MigrateArgs) -> anyhow::Result<()> {
args.env_var.as_deref(),
&args.path,
&args.table_name,
args.table_schema.as_deref(),
)?;
Ok(())
}
Expand All @@ -34,6 +35,7 @@ fn run_migrations(
env_var_opt: Option<&str>,
path: &Path,
table_name: &str,
table_schema: Option<&str>,
) -> anyhow::Result<()> {
let migration_files_path = find_migration_files(path, MigrationType::Sql)?;
let mut migrations = Vec::new();
Expand Down Expand Up @@ -79,6 +81,7 @@ fn run_migrations(
.set_abort_divergent(divergent)
.set_abort_missing(missing)
.set_migration_table_name(table_name)
.set_migration_table_schema(table_schema.or(config.db_schema()))
.run_async(&mut config)
.await
})?;
Expand All @@ -96,6 +99,7 @@ fn run_migrations(
.set_abort_missing(missing)
.set_target(target)
.set_migration_table_name(table_name)
.set_migration_table_schema(table_schema.or(config.db_schema()))
.run(&mut config)?;
} else {
panic!("tried to migrate async from config for a {:?} database, but it's matching feature was not enabled!", _db_type);
Expand Down
8 changes: 8 additions & 0 deletions refinery_cli/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,13 @@ fn get_config_from_input() -> Result<Config> {
io::stdin().read_line(&mut db_name)?;
config = config.set_db_name(db_name.trim());

print!("Enter optional schema name (empty to use the default schema): ");
io::stdout().flush()?;
let mut db_schema = String::new();
io::stdin().read_line(&mut db_schema)?;
db_schema = db_schema.trim().to_string();
if !db_schema.is_empty() {
config = config.set_db_schema(&db_schema);
}
Ok(config)
}
21 changes: 20 additions & 1 deletion refinery_core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl Config {
db_user: None,
db_pass: None,
db_name: None,
db_schema: None,
#[cfg(feature = "tiberius-config")]
trust_cert: false,
},
Expand Down Expand Up @@ -139,6 +140,10 @@ impl Config {
self.main.db_port.as_deref()
}

pub fn db_schema(&self) -> Option<&str> {
self.main.db_schema.as_deref()
}

pub fn set_db_user(self, db_user: &str) -> Config {
Config {
main: Main {
Expand Down Expand Up @@ -183,6 +188,15 @@ impl Config {
},
}
}

pub fn set_db_schema(self, db_schema: &str) -> Config {
Config {
main: Main {
db_schema: Some(db_schema.into()),
..self.main
},
}
}
}

impl TryFrom<Url> for Config {
Expand Down Expand Up @@ -238,6 +252,9 @@ impl TryFrom<Url> for Config {
db_user: Some(url.username().to_string()),
db_pass: url.password().map(|r| r.to_string()),
db_name: Some(url.path().trim_start_matches('/').to_string()),
db_schema: url
.query_pairs()
.find_map(|(name, value)| (name == "currentSchema").then(|| value.to_string())),
#[cfg(feature = "tiberius-config")]
trust_cert,
},
Expand Down Expand Up @@ -270,6 +287,7 @@ struct Main {
db_user: Option<String>,
db_pass: Option<String>,
db_name: Option<String>,
db_schema: Option<String>,
#[cfg(feature = "tiberius-config")]
#[serde(default)]
trust_cert: bool,
Expand Down Expand Up @@ -421,7 +439,8 @@ mod tests {
db_port = \"5432\" \n
db_user = \"root\" \n
db_pass = \"1234\" \n
db_name = \"refinery\"";
db_name = \"refinery\" \n
db_schema = \"public\"";

let config: Config = toml::from_str(config).unwrap();

Expand Down
4 changes: 4 additions & 0 deletions refinery_core/src/drivers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ impl crate::Migrate for Config {
grouped: bool,
target: Target,
migration_table_name: &str,
migration_table_schema: Option<&str>,
) -> Result<Report, Error> {
with_connection!(self, |mut conn| {
crate::Migrate::migrate(
Expand All @@ -214,6 +215,7 @@ impl crate::Migrate for Config {
grouped,
target,
migration_table_name,
migration_table_schema,
)
})
}
Expand Down Expand Up @@ -267,6 +269,7 @@ impl crate::AsyncMigrate for Config {
grouped: bool,
target: Target,
migration_table_name: &str,
migration_table_schema: Option<&str>,
) -> Result<Report, Error> {
with_connection_async!(self, move |mut conn| async move {
crate::AsyncMigrate::migrate(
Expand All @@ -277,6 +280,7 @@ impl crate::AsyncMigrate for Config {
grouped,
target,
migration_table_name,
migration_table_schema,
)
.await
})
Expand Down
Loading
Loading