Skip to content
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

Improve Postgres Enum Default Recognition #794

Merged
merged 2 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,37 @@ async fn introspecting_a_table_with_enum_default_values_should_work(api: &TestAp
custom_assert(&result, dm);
}

#[test_each_connector(tags("postgres"))]
async fn introspecting_a_table_with_enum_default_values_should_work_2(api: &TestApi) {
let sql = format!("CREATE Type color as ENUM ('black', 'white')");

api.database().execute_raw(&sql, &[]).await.unwrap();

api.barrel()
.execute(|migration| {
migration.create_table("Book", |t| {
t.add_column("id", types::primary());
t.inject_custom("color color Not Null default 'black'::\"color\"");
});
})
.await;

let dm = r#"
model Book {
color color @default(black)
id Int @default(autoincrement()) @id
}

enum color{
black
white
}
"#;

let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}

#[test_each_connector(tags("postgres"))]
async fn introspecting_a_table_with_enum_default_values_that_look_like_booleans_should_work(api: &TestApi) {
let sql = format!("CREATE Type Truth as ENUM ( 'true', 'false', 'rumor')");
Expand Down
18 changes: 12 additions & 6 deletions libs/sql-schema-describer/src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,18 @@ impl SqlSchemaDescriber {
ColumnTypeFamily::TextSearch => DefaultValue::DBGENERATED(default_string),
ColumnTypeFamily::TransactionId => DefaultValue::DBGENERATED(default_string),
ColumnTypeFamily::Enum(enum_name) => {
let enum_suffix = format!("::{}", enum_name);
match default_string.ends_with(&enum_suffix) {
true => DefaultValue::VALUE(PrismaValue::Enum(unquote_string(
default_string.replace(&enum_suffix, ""),
))),
false => DefaultValue::DBGENERATED(default_string),
let enum_suffix_without_quotes = format!("::{}", enum_name);
let enum_suffix_with_quotes = format!("::\"{}\"", enum_name);
if default_string.ends_with(&enum_suffix_with_quotes) {
DefaultValue::VALUE(PrismaValue::Enum(unquote_string(
default_string.replace(&enum_suffix_with_quotes, ""),
)))
} else if default_string.ends_with(&enum_suffix_without_quotes) {
DefaultValue::VALUE(PrismaValue::Enum(unquote_string(
default_string.replace(&enum_suffix_without_quotes, ""),
)))
} else {
DefaultValue::DBGENERATED(default_string)
}
}
ColumnTypeFamily::Unsupported(_) => DefaultValue::DBGENERATED(default_string),
Expand Down