yesodweb / yesodweb/persistent

Unnecessary migration when no schema change

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

Nobody has claimed this yet.

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

Description

Persistent tries this migration and fails on existing FOREIGN KEY

Migrating: CREATE TEMP TABLE "user_backup"("id" INTEGER PRIMARY KEY,"stellar_address" VARCHAR NOT NULL,"name" VARCHAR NULL,CONSTRAINT "unique_user" UNIQUE ("stellar_address"))
07/Feb/2022:13:00:39 +0300 [Debug#SQL] CREATE TEMP TABLE "user_backup"("id" INTEGER PRIMARY KEY,"stellar_address" VARCHAR NOT NULL,"name" VARCHAR NULL,CONSTRAINT "unique_user" UNIQUE ("stellar_address")); []
Migrating: INSERT INTO "user_backup"("id","stellar_address","name") SELECT "id","stellar_address","name" FROM "user"
Migrating: DROP TABLE "user"
devel.hs: SQLite3 returned ErrorConstraint while attempting to perform step: FOREIGN KEY constraint failed

but why?

oldSql = "CREATE TABLE \"user\"(\"id\" INTEGER PRIMARY KEY,\"stellar_address\" VARCHAR NOT NULL, \"name\" VARCHAR NULL,CONSTRAINT \"unique_user\" UNIQUE (\"stellar_address\"))"
newSql = "CREATE TABLE \"user\"(\"id\" INTEGER PRIMARY KEY,\"stellar_address\" VARCHAR NOT NULL,\"name\" VARCHAR NULL,CONSTRAINT \"unique_user\" UNIQUE (\"stellar_address\"))"

Yes, Sqlite inserts extra space. Is there a way to normalize SQL schema code got from Sqlite?

How to reproduce: first I created Persist schema without name field, then added it by hand (ALTER TABLE ADD COLUMN) on the production database.

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

No files or tests are named. Start by reproducing the shown oldSql/newSql comparison with the SQLite schema that was altered by hand, then trace Persistent's SQLite migration decision and foreign-key handling. Done means semantically identical schemas do not trigger an unnecessary migration and the existing foreign key remains valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell, sqlite
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.