diff --git a/migration-engine/migration-engine-tests/tests/migrations/advisory_locking.rs b/migration-engine/migration-engine-tests/tests/migrations/advisory_locking.rs new file mode 100644 index 000000000000..2452dc3cbe19 --- /dev/null +++ b/migration-engine/migration-engine-tests/tests/migrations/advisory_locking.rs @@ -0,0 +1,119 @@ +use std::sync::Arc; + +use migration_core::{ + commands::{ApplyMigrationsInput, CreateMigrationInput}, + GenericApi, +}; +use tempfile::TempDir; +use test_macros::test_each_connector; +use test_setup::TestAPIArgs; + +type TestResult = Result<(), anyhow::Error>; + +struct TestApi { + args: TestAPIArgs, + source: String, + url: String, +} + +impl TestApi { + /// Create a temporary directory to serve as a test migrations directory. + pub fn create_migrations_directory(&self) -> anyhow::Result { + Ok(tempfile::tempdir()?) + } + + async fn new_engine(&self) -> anyhow::Result> { + Ok(migration_core::migration_api(&self.source).await?) + } + + async fn initialize(&self) -> anyhow::Result<()> { + test_setup::create_postgres_database(&self.url.parse()?).await.unwrap(); + + Ok(()) + } +} + +async fn postgres9_test_api(args: TestAPIArgs) -> TestApi { + TestApi { + source: test_setup::postgres_9_test_config(&args.test_function_name), + url: test_setup::postgres_9_url(&args.test_function_name), + args, + } +} + +async fn postgres_test_api(args: TestAPIArgs) -> TestApi { + TestApi { + source: test_setup::postgres_10_test_config(&args.test_function_name), + url: test_setup::postgres_10_url(&args.test_function_name), + args, + } +} + +async fn postgres11_test_api(args: TestAPIArgs) -> TestApi { + TestApi { + source: test_setup::postgres_11_test_config(&args.test_function_name), + url: test_setup::postgres_11_url(&args.test_function_name), + args, + } +} + +async fn postgres12_test_api(args: TestAPIArgs) -> TestApi { + TestApi { + source: test_setup::postgres_12_test_config(&args.test_function_name), + url: test_setup::postgres_12_url(&args.test_function_name), + args, + } +} + +async fn postgres13_test_api(args: TestAPIArgs) -> TestApi { + TestApi { + source: test_setup::postgres_13_test_config(&args.test_function_name), + url: test_setup::postgres_13_url(&args.test_function_name), + args, + } +} + +#[test_each_connector(tags("postgres"), log = "debug")] +async fn advisory_locking_works(api: &TestApi) -> TestResult { + api.initialize().await?; + + let first_me = api.new_engine().await?; + let migrations_directory = api.create_migrations_directory()?; + let p = migrations_directory.path().to_string_lossy().into_owned(); + + let dm = r#" + model Cat { + id Int @id + inBox Boolean + } + "#; + + let output = first_me + .create_migration(&CreateMigrationInput { + migrations_directory_path: p.clone(), + prisma_schema: dm.into(), + migration_name: "01initial".into(), + draft: true, + }) + .await?; + + let migration_name = output.generated_migration_name.expect("generated no migration"); + + let second_me = api.new_engine().await?; + let third_me = api.new_engine().await?; + + let input = ApplyMigrationsInput { + migrations_directory_path: p, + }; + + let (result_1, result_2, result_3) = tokio::join!( + second_me.apply_migrations(&input), + first_me.apply_migrations(&input), + third_me.apply_migrations(&input), + ); + + panic!("here"); + dbg!(result_1, result_2, result_3); + + Ok(()) +} diff --git a/migration-engine/migration-engine-tests/tests/migrations/mod.rs b/migration-engine/migration-engine-tests/tests/migrations/mod.rs index 120bb0ca8e34..30268a0a101d 100644 --- a/migration-engine/migration-engine-tests/tests/migrations/mod.rs +++ b/migration-engine/migration-engine-tests/tests/migrations/mod.rs @@ -1,3 +1,4 @@ +mod advisory_locking; mod apply_script_tests; mod dev_diagnostic_tests; mod enums;