yesodweb / yesodweb/persistent

Migration with lowercase 'current_timestamp' is not idempotent

Open
#1,532 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Haskell
Stars
486
Forks
306
PR merge metrics
No merged PRs in 30d

Description

On the project that I work on, we have a default field similar to this:

SomeEntity
  added    UTCTime    default=current_timestamp

When we run migrations on the project, we always get:

ALTER TABLE "some_entity" ALTER COLUMN "added" SET DEFAULT current_timestamp"

The column default gets set to CURRENT_TIMESTAMP, but every time we run migrations this ALTER TABLE appears.

I had a bit of a look at this, and I think this is happening because current_timestamp is in lowercase. We have other fields in our project like this, but the default is written like: CURRENT_TIMESTAMP.

I added some logging here:

https://github.com/yesodweb/persistent/blob/a4b489032b9bb14a3637522aae89014b714494a4/persistent-postgresql/Database/Persist/Postgresql.hs#L1568

And looked in newcols and fst old' (the new columns vs existing columns), showing the field name and default value on the Column.

Here are the values that were logged out:

-- this is from `newcols`
(FieldNameDB {unFieldNameDB = "time_field"},Just "current_timestamp")
-- this is from `fst old`
(FieldNameDB {unFieldNameDB = "time_field"},Just "CURRENT_TIMESTAMP")

Ultimately these fields get compared here I think:

https://github.com/yesodweb/persistent/blob/a4b489032b9bb14a3637522aae89014b714494a4/persistent-postgresql/Database/Persist/Postgresql.hs#L1178

So in this case, these are not matching.

I created a test to recreate this, there is an open PR with failing test here: https://github.com/yesodweb/persistent/pull/1533

I'm happy to work on resolving this, but the solution isn't super obvious to me. We could make this comparison case insensitive, and obviously that would need to be fixed across all backends (I've only looked at postgres, quite possibly other backends have a similar issue), but I'm not sure if that approach is the best, or if there would be possible issues introduced there.

Another approach I was thinking of would be to changing the cDefault field to parse certain fields, such as current_timestamp. Instead of:

cDefault :: Maybe Text
cDefault :: FieldDefault

-- where

data FieldDefault
  = FieldDefaultCurrentTime
  | FieldDefaultOther Text

And we parse both CURRENT_TIME / current_time (and any other similar) to a single value in this type.

Not sure about this approach either.

Happy to look to fix this with the right guidance, I don't think there is any actual issue here it's just an annoyance when running migrations, and I decided to actually look into this tonight 😄

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in persistent-postgresql/Database/Persist/Postgresql.hs at the comparison near line 1178 and migration logic near line 1568. Review the failing reproduction test in PR 1533 and inspect how defaults are represented across backends. Done means equivalent lowercase and uppercase timestamp defaults no longer produce repeated ALTER TABLE statements.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell, postgresql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.