Skip to content

Commit

Permalink
Merge pull request #672 from paul-rouse/master
Browse files Browse the repository at this point in the history
Fix #671 - revise handling of 'default null' in mysql
  • Loading branch information
snoyberg committed May 16, 2017
2 parents 04a7a15 + 84f7b49 commit 6968640
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
4 changes: 4 additions & 0 deletions persistent-mysql/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
##2.6.0.2

Prevent spurious no-op migrations when `default=NULL` is specified - revised version [#672](https://github.com/yesodweb/persistent/pull/672) (which fixes bug [#671](https://github.com/yesodweb/persistent/issues/671) introduced by the earlier attempt [#641](https://github.com/yesodweb/persistent/pull/641))

## 2.6

Compatibility for backend-specific upsert functionality.
Expand Down
12 changes: 8 additions & 4 deletions persistent-mysql/Database/Persist/MySQL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ getColumns connectInfo getter def = do
stmtIdClmn <- getter "SELECT COLUMN_NAME, \
\IS_NULLABLE, \
\DATA_TYPE, \
\IF(IS_NULLABLE='YES', COALESCE(COLUMN_DEFAULT, 'NULL'), COLUMN_DEFAULT) \
\COLUMN_DEFAULT \
\FROM INFORMATION_SCHEMA.COLUMNS \
\WHERE TABLE_SCHEMA = ? \
\AND TABLE_NAME = ? \
Expand All @@ -473,7 +473,7 @@ getColumns connectInfo getter def = do
\CHARACTER_MAXIMUM_LENGTH, \
\NUMERIC_PRECISION, \
\NUMERIC_SCALE, \
\IF(IS_NULLABLE='YES', COALESCE(COLUMN_DEFAULT, 'NULL'), COLUMN_DEFAULT) \
\COLUMN_DEFAULT \
\FROM INFORMATION_SCHEMA.COLUMNS \
\WHERE TABLE_SCHEMA = ? \
\AND TABLE_NAME = ? \
Expand Down Expand Up @@ -691,10 +691,12 @@ findAlters tblName allDefs col@(Column name isNull type_ def _defConstraintName
modType | showSqlType type_ maxLen False `ciEquals` showSqlType type_' maxLen' False && isNull == isNull' = []
| otherwise = [(name, Change col)]
-- Default value
-- Avoid DEFAULT NULL, since it is always unnecessary, and is an error for text/blob fields
modDef | def == def' = []
| otherwise = case def of
Nothing -> [(name, NoDefault)]
Just s -> [(name, Default $ T.unpack s)]
Just s -> if T.toUpper s == "NULL" then []
else [(name, Default $ T.unpack s)]
in ( refDrop ++ modType ++ modDef ++ refAdd
, filter ((name /=) . cName) cols )

Expand All @@ -715,7 +717,9 @@ showColumn (Column n nu t def _defConstraintName maxLen ref) = concat
, if nu then "NULL" else "NOT NULL"
, case def of
Nothing -> ""
Just s -> " DEFAULT " ++ T.unpack s
Just s -> -- Avoid DEFAULT NULL, since it is always unnecessary, and is an error for text/blob fields
if T.toUpper s == "NULL" then ""
else " DEFAULT " ++ T.unpack s
, case ref of
Nothing -> ""
Just (s, _) -> " REFERENCES " ++ escapeDBName s
Expand Down
2 changes: 1 addition & 1 deletion persistent-mysql/persistent-mysql.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: persistent-mysql
version: 2.6.0.1
version: 2.6.0.2
license: MIT
license-file: LICENSE
author: Felipe Lessa <felipe.lessa@gmail.com>, Michael Snoyman
Expand Down

0 comments on commit 6968640

Please sign in to comment.