feat: introduce migrate.create-schemas

This commit is contained in:
Austin Bonander
2025-01-22 15:32:50 -08:00
parent 45c0b85b4c
commit 3765f67aba
17 changed files with 383 additions and 185 deletions

View File

@@ -208,18 +208,18 @@ impl PgConnection {
attribute_no: i16,
should_fetch: bool,
) -> Result<ColumnOrigin, Error> {
if let Some(origin) =
self.inner
.cache_table_to_column_names
.get(&relation_id)
.and_then(|table_columns| {
let column_name = table_columns.columns.get(&attribute_no).cloned()?;
if let Some(origin) = self
.inner
.cache_table_to_column_names
.get(&relation_id)
.and_then(|table_columns| {
let column_name = table_columns.columns.get(&attribute_no).cloned()?;
Some(ColumnOrigin::Table(TableColumn {
table: table_columns.table_name.clone(),
name: column_name,
}))
})
Some(ColumnOrigin::Table(TableColumn {
table: table_columns.table_name.clone(),
name: column_name,
}))
})
{
return Ok(origin);
}

View File

@@ -149,7 +149,8 @@ impl PgConnection {
cache_type_info: HashMap::new(),
cache_elem_type_to_array: HashMap::new(),
cache_table_to_column_names: HashMap::new(),
log_settings: options.log_settings.clone(),}),
log_settings: options.log_settings.clone(),
}),
})
}
}

View File

@@ -111,11 +111,27 @@ impl MigrateDatabase for Postgres {
}
impl Migrate for PgConnection {
fn ensure_migrations_table<'e>(&'e mut self, table_name: &'e str) -> BoxFuture<'e, Result<(), MigrateError>> {
fn create_schema_if_not_exists<'e>(
&'e mut self,
schema_name: &'e str,
) -> BoxFuture<'e, Result<(), MigrateError>> {
Box::pin(async move {
// language=SQL
self.execute(
&*format!(r#"
self.execute(&*format!(r#"CREATE SCHEMA IF NOT EXISTS {schema_name};"#))
.await?;
Ok(())
})
}
fn ensure_migrations_table<'e>(
&'e mut self,
table_name: &'e str,
) -> BoxFuture<'e, Result<(), MigrateError>> {
Box::pin(async move {
// language=SQL
self.execute(&*format!(
r#"
CREATE TABLE IF NOT EXISTS {table_name} (
version BIGINT PRIMARY KEY,
description TEXT NOT NULL,
@@ -124,20 +140,23 @@ CREATE TABLE IF NOT EXISTS {table_name} (
checksum BYTEA NOT NULL,
execution_time BIGINT NOT NULL
);
"#),
)
"#
))
.await?;
Ok(())
})
}
fn dirty_version<'e>(&'e mut self, table_name: &'e str) -> BoxFuture<'e, Result<Option<i64>, MigrateError>> {
fn dirty_version<'e>(
&'e mut self,
table_name: &'e str,
) -> BoxFuture<'e, Result<Option<i64>, MigrateError>> {
Box::pin(async move {
// language=SQL
let row: Option<(i64,)> = query_as(
&*format!("SELECT version FROM {table_name} WHERE success = false ORDER BY version LIMIT 1"),
)
let row: Option<(i64,)> = query_as(&*format!(
"SELECT version FROM {table_name} WHERE success = false ORDER BY version LIMIT 1"
))
.fetch_optional(self)
.await?;
@@ -145,13 +164,17 @@ CREATE TABLE IF NOT EXISTS {table_name} (
})
}
fn list_applied_migrations<'e>(&'e mut self, table_name: &'e str) -> BoxFuture<'e, Result<Vec<AppliedMigration>, MigrateError>> {
fn list_applied_migrations<'e>(
&'e mut self,
table_name: &'e str,
) -> BoxFuture<'e, Result<Vec<AppliedMigration>, MigrateError>> {
Box::pin(async move {
// language=SQL
let rows: Vec<(i64, Vec<u8>)> =
query_as(&*format!("SELECT version, checksum FROM {table_name} ORDER BY version"))
.fetch_all(self)
.await?;
let rows: Vec<(i64, Vec<u8>)> = query_as(&*format!(
"SELECT version, checksum FROM {table_name} ORDER BY version"
))
.fetch_all(self)
.await?;
let migrations = rows
.into_iter()
@@ -230,13 +253,13 @@ CREATE TABLE IF NOT EXISTS {table_name} (
// language=SQL
#[allow(clippy::cast_possible_truncation)]
let _ = query(
&*format!(r#"
let _ = query(&*format!(
r#"
UPDATE {table_name}
SET execution_time = $1
WHERE version = $2
"#),
)
"#
))
.bind(elapsed.as_nanos() as i64)
.bind(migration.version)
.execute(self)
@@ -283,12 +306,12 @@ async fn execute_migration(
.map_err(|e| MigrateError::ExecuteMigration(e, migration.version))?;
// language=SQL
let _ = query(
&*format!(r#"
let _ = query(&*format!(
r#"
INSERT INTO {table_name} ( version, description, success, checksum, execution_time )
VALUES ( $1, $2, TRUE, $3, -1 )
"#),
)
"#
))
.bind(migration.version)
.bind(&*migration.description)
.bind(&*migration.checksum)