Skip to content

Commit f99c56e

Browse files
committed
refactor: don't match on all of the Column fields
1 parent 9715b1a commit f99c56e

File tree

1 file changed

+26
-26
lines changed
  • persistent-postgresql/Database/Persist/Postgresql/Internal

1 file changed

+26
-26
lines changed

persistent-postgresql/Database/Persist/Postgresql/Internal/Migration.hs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,61 +1104,61 @@ findAlters
11041104
-> [Column]
11051105
-- ^ The columns for this table, as they currently exist in the database.
11061106
-> ([AlterColumn], [Column])
1107-
findAlters defs edef col@(Column name isNull sqltype def _gen _defConstraintName _maxLen ref) cols =
1108-
case List.find (\c -> cName c == name) cols of
1107+
findAlters defs edef newCol oldCols =
1108+
case List.find (\c -> cName c == cName newCol) oldCols of
11091109
Nothing ->
1110-
([AddColumn col] ++ refAdd ref, cols)
1110+
([AddColumn newCol] ++ refAdd (cReference newCol), oldCols)
11111111
Just
1112-
(Column _oldName isNull' sqltype' def' _gen' _defConstraintName' _maxLen' ref') ->
1112+
oldCol ->
11131113
let
11141114
refDrop Nothing = []
11151115
refDrop (Just ColumnReference{crConstraintName = cname}) =
11161116
[DropReference cname]
11171117

11181118
modRef =
1119-
if equivalentRef ref ref'
1119+
if equivalentRef (cReference oldCol) (cReference newCol)
11201120
then []
1121-
else refDrop ref' ++ refAdd ref
1122-
modNull = case (isNull, isNull') of
1121+
else refDrop (cReference oldCol) ++ refAdd (cReference newCol)
1122+
modNull = case (cNull newCol, cNull oldCol) of
11231123
(True, False) -> do
1124-
guard $ Just name /= fmap fieldDB (getEntityIdField edef)
1125-
pure (IsNull col)
1124+
guard $ Just (cName newCol) /= fmap fieldDB (getEntityIdField edef)
1125+
pure (IsNull newCol)
11261126
(False, True) ->
11271127
let
1128-
up = case def of
1128+
up = case cDefault newCol of
11291129
Nothing -> id
1130-
Just s -> (:) (UpdateNullToValue col s)
1130+
Just s -> (:) (UpdateNullToValue newCol s)
11311131
in
1132-
up [NotNull col]
1132+
up [NotNull newCol]
11331133
_ -> []
11341134
modType
1135-
| sqlTypeEq sqltype sqltype' = []
1135+
| sqlTypeEq (cSqlType newCol) (cSqlType oldCol) = []
11361136
-- When converting from Persistent pre-2.0 databases, we
11371137
-- need to make sure that TIMESTAMP WITHOUT TIME ZONE is
11381138
-- treated as UTC.
1139-
| sqltype == SqlDayTime && sqltype' == SqlOther "timestamp" =
1140-
[ ChangeType col sqltype $
1139+
| cSqlType newCol == SqlDayTime && cSqlType oldCol == SqlOther "timestamp" =
1140+
[ ChangeType newCol (cSqlType newCol) $
11411141
T.concat
11421142
[ " USING "
1143-
, escapeF name
1143+
, escapeF (cName newCol)
11441144
, " AT TIME ZONE 'UTC'"
11451145
]
11461146
]
1147-
| otherwise = [ChangeType col sqltype ""]
1147+
| otherwise = [ChangeType newCol (cSqlType newCol) ""]
11481148
modDef =
1149-
if def == def'
1150-
|| isJust (T.stripPrefix "nextval" =<< def')
1149+
if cDefault newCol == cDefault oldCol
1150+
|| isJust (T.stripPrefix "nextval" =<< cDefault oldCol)
11511151
then []
1152-
else case def of
1153-
Nothing -> [NoDefault col]
1154-
Just s -> [Default col s]
1152+
else case cDefault newCol of
1153+
Nothing -> [NoDefault newCol]
1154+
Just s -> [Default newCol s]
11551155
dropSafe =
1156-
if safeToRemove edef name
1157-
then error "wtf" [Drop col (SafeToRemove True)]
1156+
if safeToRemove edef (cName newCol)
1157+
then error "wtf" [Drop newCol (SafeToRemove True)]
11581158
else []
11591159
in
11601160
( modRef ++ modDef ++ modNull ++ modType ++ dropSafe
1161-
, filter (\c -> cName c /= name) cols
1161+
, filter (\c -> cName c /= cName newCol) oldCols
11621162
)
11631163
where
11641164
refAdd Nothing = []
@@ -1168,7 +1168,7 @@ findAlters defs edef col@(Column name isNull sqltype def _gen _defConstraintName
11681168
[ AddReference
11691169
(crTableName colRef)
11701170
(crConstraintName colRef)
1171-
(name NEL.:| [])
1171+
(cName newCol NEL.:| [])
11721172
(NEL.toList $ Util.dbIdColumnsEsc escapeF refdef)
11731173
(crFieldCascade colRef)
11741174
]

0 commit comments

Comments
 (0)