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 @@ -396,7 +396,11 @@ fn render_mysql_modify(

fn render_column_type(column: TableColumnWalker<'_>) -> Cow<'static, str> {
if let ColumnTypeFamily::Enum(enum_id) = column.column_type_family() {
let variants: String = column.walk(*enum_id).values().map(Quoted::mysql_string).join(", ");
let variants: String = column
.walk(*enum_id)
.values()
.map(|value| Quoted::mysql_string(escape_string_literal(value)))
.join(", ");

return format!("ENUM({variants})").into();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ impl SqlRenderer for PostgresRenderer {
stmt.push_str(" AS ENUM (");
let mut values = enm.values().peekable();
while let Some(value) = values.next() {
stmt.push_display(&Quoted::postgres_string(value));
stmt.push_display(&Quoted::postgres_string(escape_string_literal(value)));
if values.peek().is_some() {
stmt.push_str(", ");
}
Expand Down Expand Up @@ -902,7 +902,7 @@ fn render_postgres_alter_enum(
format!(
"ALTER TYPE {enum_name} ADD VALUE {value}",
enum_name = quoted_alter_enum_name(schemas.walk(alter_enum.id)),
value = Quoted::postgres_string(created_value)
value = Quoted::postgres_string(escape_string_literal(created_value))
)
})
.collect();
Expand Down Expand Up @@ -939,7 +939,11 @@ fn render_postgres_alter_enum(
let create_new_enum = format!(
"CREATE TYPE {enum_name} AS ENUM ({variants})",
enum_name = tmp_name,
variants = enums.next.values().map(Quoted::postgres_string).join(", ")
variants = enums
.next
.values()
.map(|value| Quoted::postgres_string(escape_string_literal(value)))
.join(", ")
);

stmts.push(create_new_enum);
Expand Down Expand Up @@ -1078,18 +1082,16 @@ fn render_cockroach_alter_enum(
for variant in &alter_enum.created_variants {
renderer.render_statement(&mut |stmt| {
stmt.push_str(&prefix);
stmt.push_str(" ADD VALUE '");
stmt.push_str(variant);
stmt.push_str("'");
stmt.push_str(" ADD VALUE ");
stmt.push_display(&Quoted::postgres_string(escape_string_literal(variant)));
});
}

for variant in &alter_enum.dropped_variants {
renderer.render_statement(&mut |stmt| {
stmt.push_str(&prefix);
stmt.push_str("DROP VALUE '");
stmt.push_str(variant);
stmt.push_str("'");
stmt.push_str("DROP VALUE ");
stmt.push_display(&Quoted::postgres_string(escape_string_literal(variant)));
});
}
}
Expand Down
76 changes: 76 additions & 0 deletions schema-engine/sql-migration-tests/tests/migrations/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,3 +695,79 @@ fn alter_enum_and_change_default_must_work(api: TestApi) {
migration.expect_contents(expected_script)
});
}

#[test_connector(capabilities(Enums))]
fn enum_values_with_single_quotes_can_be_created_added_and_removed(api: TestApi) {
let dm1 = r#"
model Cat {
id Int @id
mood CatMood
}

enum CatMood {
HAPPY @map("it's fine")
HUNGRY
}
"#;

api.schema_push_w_datasource(dm1).send().assert_green();

let enum_name = if api.lower_cases_table_names() {
"cat_mood"
} else if api.is_mysql() {
"Cat_mood"
} else {
"CatMood"
};

if !api.is_sqlite() {
api.assert_schema()
.assert_enum(enum_name, |enm| enm.assert_values(&["it's fine", "HUNGRY"]));
}

api.insert("Cat").value("id", 1).value("mood", "it's fine").result_raw();

let dm2 = r#"
model Cat {
id Int @id
mood CatMood
}

enum CatMood {
HAPPY @map("it's fine")
HUNGRY
SLEEPY @map("o'clock")
}
"#;

api.schema_push_w_datasource(dm2).send().assert_green();

if !api.is_sqlite() {
api.assert_schema()
.assert_enum(enum_name, |enm| enm.assert_values(&["it's fine", "HUNGRY", "o'clock"]));
}

let dm3 = r#"
model Cat {
id Int @id
mood CatMood
}

enum CatMood {
HAPPY @map("it's fine")
SLEEPY @map("o'clock")
}
"#;

api.schema_push_w_datasource(dm3).force(true).send().assert_executable();

if !api.is_sqlite() {
api.assert_schema()
.assert_enum(enum_name, |enm| enm.assert_values(&["it's fine", "o'clock"]));
}

api.schema_push_w_datasource(dm3)
.send()
.assert_green()
.assert_no_steps();
}