Skip to content

Commit

Permalink
postgres(normalize types to eliminate noop migrations) (#1518)
Browse files Browse the repository at this point in the history
* postgres(normalize types to eliminate noop migrations)

* add test and update changelog
  • Loading branch information
HugoPeters1024 authored Sep 21, 2023
1 parent a3a25eb commit dfd87cc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
10 changes: 9 additions & 1 deletion persistent-postgresql/Database/Persist/Postgresql.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,15 @@ getColumn _ _ columnName _ =
-- | Intelligent comparison of SQL types, to account for SqlInt32 vs SqlOther integer
sqlTypeEq :: SqlType -> SqlType -> Bool
sqlTypeEq x y =
T.toCaseFold (showSqlType x) == T.toCaseFold (showSqlType y)
let
-- Non exhaustive helper to map postgres aliases to the same name. Based on
-- https://www.postgresql.org/docs/9.5/datatype.html.
-- This prevents needless `ALTER TYPE`s when the type is the same.
normalize "int8" = "bigint"
normalize "serial8" = "bigserial"
normalize v = v
in
normalize (T.toCaseFold (showSqlType x)) == normalize (T.toCaseFold (showSqlType y))

findAlters
:: [EntityDef]
Expand Down
4 changes: 2 additions & 2 deletions persistent-postgresql/test/EquivalentTypeTestPostgres.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import PgInit

share [mkPersist sqlSettings, mkMigrate "migrateAll1"] [persistLowerCase|
EquivalentType sql=equivalent_types
field1 Int
field1 Int sqltype=bigint
field2 T.Text sqltype=text
field3 T.Text sqltype=us_postal_code
deriving Eq Show
|]

share [mkPersist sqlSettings, mkMigrate "migrateAll2"] [persistLowerCase|
EquivalentType2 sql=equivalent_types
field1 Int
field1 Int sqltype=int8
field2 T.Text
field3 T.Text sqltype=us_postal_code
deriving Eq Show
Expand Down
7 changes: 6 additions & 1 deletion persistent/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Changelog for persistent
# Changelog for persistentChan

## 2.14.6.1 (unreleased)

* [#1518](https://github.com/yesodweb/persistent/pull/1518)
* Normalize postgres type aliases to prevent noop migrations

## 2.14.6.0

Expand Down

0 comments on commit dfd87cc

Please sign in to comment.