Skip to content
Merged
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
21 changes: 15 additions & 6 deletions src/engine/postgres_psql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::collections::HashMap;
use std::collections::HashSet;
use std::io::Write;
use std::process::Stdio;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Instant;
use tokio::io::AsyncReadExt;
Expand All @@ -36,6 +37,8 @@ pub fn migration_lock_key() -> i64 {
pub struct PSQL {
psql_command: Vec<String>,
target_config: TargetConfig,
// Set once update_schema confirms the tracking tables exist.
schema_ready: AtomicBool,
}

static PROJECT_DIR: Dir<'_> = include_dir!("./static/engine-migrations/postgres-psql");
Expand All @@ -53,6 +56,7 @@ impl PSQL {
let eng = Box::new(Self {
psql_command,
target_config: config.clone(),
schema_ready: AtomicBool::new(false),
});

// Ensure we have latest schema:
Expand Down Expand Up @@ -597,6 +601,8 @@ impl PSQL {
}
}

self.schema_ready.store(true, Ordering::Relaxed);

Ok(())
}

Expand Down Expand Up @@ -822,12 +828,15 @@ impl PSQL {
namespace: EscapedLiteral,
retry: bool,
) -> MigrationResult<String> {
// Check if migration already exists in history (skip if table doesn't exist yet)
let existing_status = if self
.migration_history_table_exists()
.await
.map_err(MigrationError::Database)?
{
// If schema is ready, then history table must exist.
let history_table_exists = if self.schema_ready.load(Ordering::Relaxed) {
true
} else {
self.migration_history_table_exists()
.await
.map_err(MigrationError::Database)?
};
let existing_status = if history_table_exists {
self.get_migration_status(migration_name, &namespace)
.await
.map_err(MigrationError::Database)?
Expand Down
Loading