Skip to content

Commit

Permalink
Fix incorrect behavior when adding an autoincrement column to a Postg… (
Browse files Browse the repository at this point in the history
#697)

* Fix incorrect behavior when adding an autoincrement column to a Postgres table.

* rustfmt
  • Loading branch information
opusbopus authored Sep 8, 2023
1 parent 36c03c3 commit 375b7dc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/backend/postgres/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,15 @@ impl TableBuilder for PostgresQueryBuilder {
let f = |column_def: &ColumnDef, sql: &mut dyn SqlWriter| {
if let Some(column_type) = &column_def.types {
write!(sql, " ").unwrap();
self.prepare_column_type(column_type, sql);
if column_def
.spec
.iter()
.any(|v| matches!(v, ColumnSpec::AutoIncrement))
{
self.prepare_column_auto_increment(column_type, sql);
} else {
self.prepare_column_type(column_type, sql);
}
}
};
self.prepare_column_def_common(column, sql, f);
Expand Down
4 changes: 2 additions & 2 deletions tests/postgres/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ fn alter_9() {

#[test]
fn alter_10() {
// https://dbfiddle.uk/PQksflGf
// https://dbfiddle.uk/BeiZPvBe
assert_eq!(
Table::alter()
.table(Glyph::Table)
Expand All @@ -534,7 +534,7 @@ fn alter_10() {
.to_string(PostgresQueryBuilder),
[
r#"ALTER TABLE "glyph""#,
r#"ADD COLUMN "aspect" integer NOT NULL UNIQUE PRIMARY KEY"#,
r#"ADD COLUMN "aspect" serial NOT NULL UNIQUE PRIMARY KEY"#,
]
.join(" ")
);
Expand Down

0 comments on commit 375b7dc

Please sign in to comment.