Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ fn idents_semantically_eq(a: &Ident, b: &Ident) -> bool {

// Compares two expressions that have already been normalized by `StripPgNormalization`.
fn exprs_semantically_eq(a: &Expr, b: &Expr) -> bool {
use sqlparser::ast::CastKind;
use sqlparser::ast::{CastKind, DataType};

if a == b {
return true;
Expand Down Expand Up @@ -839,6 +839,29 @@ fn exprs_semantically_eq(a: &Expr, b: &Expr) -> bool {
Expr::Value(va),
) if matches!(expr.as_ref(), Expr::Value(vb) if vb == va) => true,

// PG normalizes varchar column references with an implicit ::text cast.
(
inner,
Expr::Cast {
kind: CastKind::DoubleColon,
expr,
data_type: DataType::Text,
..
},
)
| (
Expr::Cast {
kind: CastKind::DoubleColon,
expr,
data_type: DataType::Text,
..
},
inner,
) if matches!(
expr.as_ref(),
Expr::Identifier(_) | Expr::CompoundIdentifier(_)
) => exprs_semantically_eq(inner, expr),

// Unwrap outer :: cast when inner is also a :: cast (PG annotation wrapping user cast).
(
inner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,35 @@ fn empty_migrations_should_not_be_created(api: TestApi) {
.assert_migration_directories_count(1);
}

#[test_connector(tags(Postgres), exclude(CockroachDb), preview_features("partialIndexes"))]
fn partial_unique_index_on_varchar_object_predicate_is_not_recreated(mut api: TestApi) {
let dm = api.datamodel_with_provider_and_features(
r#"
model ExternalMappingData {
tenantId String @db.VarChar(255)
mappingType String @db.VarChar(255)
externalId String @db.VarChar(4096)
caralegalId String @db.VarChar(4096)

@@id([tenantId, mappingType, externalId, caralegalId])
@@unique([tenantId, externalId], map: "ExternalMappingData_userId_tenantId_externalId_key", where: { mappingType: "userId", externalId: { not: "" } })
}
"#,
&[],
&["partialIndexes"],
);

let dir = api.create_migrations_directory();

api.create_migration("initial", &dm, &dir)
.send_sync()
.assert_migration_directories_count(1);

api.create_migration("no-op", &dm, &dir)
.send_sync()
.assert_migration_directories_count(1);
}

#[test_connector]
fn migration_name_length_is_validated(api: TestApi) {
let dm = api.datamodel_with_provider(
Expand Down