()
| 8 | |
| 9 | #[async_std::test] |
| 10 | async fn test_fresh_with_extension() -> Result<(), DbErr> { |
| 11 | let url = |
| 12 | &std::env::var("DATABASE_URL").expect("Environment variable 'DATABASE_URL' not set"); |
| 13 | let db_name = "test_fresh_with_extension"; |
| 14 | |
| 15 | let db_connect = |url: String| async { |
| 16 | let connect_options = ConnectOptions::new(url).to_owned(); |
| 17 | Database::connect(connect_options).await |
| 18 | }; |
| 19 | |
| 20 | let db = db_connect(url.to_owned()).await?; |
| 21 | if !matches!(db.get_database_backend(), DbBackend::Postgres) { |
| 22 | return Ok(()); |
| 23 | } |
| 24 | |
| 25 | db.execute_unprepared(&format!(r#"DROP DATABASE IF EXISTS "{db_name}""#)) |
| 26 | .await?; |
| 27 | db.execute_unprepared(&format!(r#"CREATE DATABASE "{db_name}""#)) |
| 28 | .await?; |
| 29 | |
| 30 | let url = format!("{url}/{db_name}"); |
| 31 | let db = db_connect(url).await?; |
| 32 | |
| 33 | // Create the extension and a custom type |
| 34 | db.execute_unprepared("CREATE EXTENSION IF NOT EXISTS citext") |
| 35 | .await?; |
| 36 | db.execute_unprepared("CREATE TYPE \"UserFruit\" AS ENUM ('Apple', 'Banana')") |
| 37 | .await?; |
| 38 | |
| 39 | // Run the fresh migration |
| 40 | Migrator::fresh(&db).await?; |
| 41 | |
| 42 | // Check that the custom type was dropped and the extension's type was not |
| 43 | let citext_exists: Option<i32> = db |
| 44 | .query_one(Statement::from_string( |
| 45 | DbBackend::Postgres, |
| 46 | r#"SELECT 1 as "value" FROM pg_type WHERE typname = 'citext'"#.to_owned(), |
| 47 | )) |
| 48 | .await? |
| 49 | .map(|row| row.try_get("", "value").unwrap()); |
| 50 | |
| 51 | assert_eq!(citext_exists, Some(1), "the citext type should still exist"); |
| 52 | |
| 53 | let user_fruit_exists: Option<i32> = db |
| 54 | .query_one(Statement::from_string( |
| 55 | DbBackend::Postgres, |
| 56 | r#"SELECT 1 as "value" FROM pg_type WHERE typname = 'UserFruit'"#.to_owned(), |
| 57 | )) |
| 58 | .await? |
| 59 | .map(|row| row.try_get("", "value").unwrap()); |
| 60 | |
| 61 | assert_eq!( |
| 62 | user_fruit_exists, None, |
| 63 | "the UserFruit type should have been dropped" |
| 64 | ); |
| 65 | |
| 66 | Ok(()) |
| 67 | } |
nothing calls this directly
no test coverage detected