---
title: "Write a SQL migration and explain it: 7 AI models, one prompt | Straitly"
description: "Seven AI models wrote the same SQLite migration once each. 15 checks on a seeded database, plus time, tokens and cost per run."
url: https://straitly.ai/shootouts/sql-migration
updated: 2026-11-26
publisher: Straitly (https://straitly.ai)
---

# Write a SQL migration and explain it: 7 AI models, one prompt

All 7 models passed all 15 checks, so speed and price decide. GPT-6.2 Astra wins: done in 6.2 s. Gemini 4 Neon cost the least at the maker's list price, $0.0112\. Kimi K3.1 Pro took the longest, 227 s.

Updated 26 November 2026

[See the results](#results) [Read every output ](#outputs)

The task

Asked

Write a SQLite migration that merges duplicate user emails, keeps every order, adds a status CHECK and an index, then explain it in 150 words.

Graded by

15 checks run against a seeded SQLite database

## Which model did best?

Every answer was run, not read. Here is who passed, how long each took and what each run cost.

- Pick this one
### GPT-6.2 Astra
15 of 15 checks, done in 6.2 s: the fastest full pass. $0.0250 at list price.
- Smallest bill
### Gemini 4 Neon
15 of 15 checks for $0.0112 at list price, in 77.3 s.
- Biggest bill
### Claude Fable 5.2
15 of 15 checks too, at $0.175 at list price for the same result.
- Longest wait
### Kimi K3.1 Pro
Passed, after 227 s and 7,752 output tokens. Fine in a batch job, slow in an editor.
- Most tokens
### DeepSeek V4.1 Flash 1017
13.0K output tokens, 12.4K of them reasoning, for the same pass.

### Results

| Model                             | Tests      | First token | Total time | Output tokens | Reasoning | List cost | Straitly price                                                 |
| --------------------------------- | ---------- | ----------- | ---------- | ------------- | --------- | --------- | -------------------------------------------------------------- |
| Claude Opus 5.5 Anthropic         | Pass 15/15 | 3.1 s       | 36.9 s     | 4,398         | 2,400     | $0.0915   | [$0.0549](https://straitly.ai/models/claude-opus-5-5)          |
| GPT-6.2 Astra OpenAI              | Pass 15/15 | 2.2 s       | 6.2 s      | 520           | 0         | $0.0250   | [$0.0150](https://straitly.ai/models/gpt-6.2-astra)            |
| Gemini 4 Neon Google              | Pass 15/15 | 70.6 s      | 77.3 s     | 639           | 0         | $0.0112   | [See model page](https://straitly.ai/models/gemini-4-neon)     |
| Gemini 4 Carbon Google            | Pass 15/15 | 6.8 s       | 44.8 s     | 2,561         | 1,867     | $0.0268   | [$0.0268](https://straitly.ai/models/gemini-4-carbon)          |
| Claude Fable 5.2 Anthropic        | Pass 15/15 | 25.1 s      | 38.8 s     | 4,164         | 2,471     | $0.175    | [$0.105](https://straitly.ai/models/claude-fable-5.2)          |
| DeepSeek V4.1 Flash 1017 DeepSeek | Pass 15/15 | 2.1 s       | 78.4 s     | 13.0K         | 12.4K     | $0.0158   | [$0.0047](https://straitly.ai/models/deepseek-v4-1-flash-1017) |
| Kimi K3.1 Pro Moonshot            | Pass 15/15 | 3.6 s       | 227 s      | 7,752         | 7,083     | $0.0788   | [$0.0449](https://straitly.ai/models/kimi-k3.1-pro)            |

One run per model, 26 November 2026, 5:55 PM to 6:01 PM PT. Reasoning tokens are part of output tokens. List cost is the run's input and output tokens at the maker's list price. The Straitly price column is the same tokens at the Straitly catalog price that day. Gemini 4 Neon: see its model page.

## What did each model write?

The prompt and all seven replies, exactly as they came back. Open one to read it.

**The prompt Sent word for word to every model**

System: You are a senior software engineer. Follow the requested output format exactly.

schema.sql:

```
-- Current schema (SQLite 3). Foreign keys are enforced (PRAGMA foreign_keys = ON).
CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  email TEXT NOT NULL,
  full_name TEXT,
  created_at TEXT NOT NULL            -- ISO 8601 UTC, e.g. '2026-03-01T10:00:00Z'
);

CREATE TABLE orders (
  id INTEGER PRIMARY KEY,
  user_id INTEGER NOT NULL REFERENCES users(id),
  status TEXT NOT NULL,               -- free text today: 'paid', 'PAID', 'Refunded', 'pending', ...
  total_cents INTEGER NOT NULL,
  created_at TEXT NOT NULL
);
```

Write one SQLite migration script, \`migration.sql\`, for the schema in \`schema.sql\`, then explain it. Requirements: 1\. Emails: some users share an email that differs only by case or surrounding spaces. Merge them: keep the user with the lowest id in each group, move every order of the other users in the group to the kept user, then delete the other users. Store the kept user's email trimmed and lower-cased. 2\. After the merge, emails must be unique case-insensitively, enforced by the database (a unique index on the lower-cased email, or an equivalent constraint), so inserting 'Ana@x.com ' after 'ana@x.com' fails. 3\. Orders: normalize \`status\` to lower case and trimmed. Allowed values are exactly 'pending', 'paid', 'refunded', 'cancelled'. Map 'canceled' (US spelling) to 'cancelled'. Any other value becomes 'pending'. After the migration the database must reject an insert or update with a status outside the allowed list (a CHECK constraint, which in SQLite means rebuilding the table, or triggers). 4\. Add an index that serves "orders for a user, newest first" (user\_id, created\_at). 5\. Keep every order row (no order is lost) and keep order ids unchanged. Foreign keys must still be valid (\`PRAGMA foreign\_key\_check\` returns nothing). 6\. The script must run as a single transaction and must be runnable with \`sqlite3 db < migration.sql\` on a database that already has data. Reply with the full \`migration.sql\` in one \`\`\`sql code block, then a short explanation (at most 150 words) of the order of steps and why it is safe.

**Claude Opus 5.5 Pass, 15 of 15, 36.9 s, 4,398 output tokens**

```
-- migration.sql
-- Run with: sqlite3 db < migration.sql
-- Stop at the first error. The open transaction is then rolled back, so nothing is half-applied.
.bail on

-- foreign_keys cannot be changed inside a transaction, so set it first.
-- Keeping it ON means every step below is checked against users(id).
PRAGMA foreign_keys = ON;

BEGIN IMMEDIATE;

-- Assertion helper: inserting 0 violates the CHECK, and .bail aborts the script.
CREATE TEMP TABLE _assert (ok INTEGER NOT NULL CHECK (ok = 1));

------------------------------------------------------------------------
-- 1. Merge users whose emails differ only by case or surrounding spaces
------------------------------------------------------------------------
-- Map every duplicate user id to the lowest id in its group.
CREATE TEMP TABLE _user_map AS
SELECT u.id      AS old_id,
       g.keep_id AS keep_id
FROM users AS u
JOIN (
  SELECT lower(trim(email)) AS norm_email, MIN(id) AS keep_id
  FROM users
  GROUP BY lower(trim(email))
) AS g ON lower(trim(u.email)) = g.norm_email
WHERE u.id <> g.keep_id;

-- Move orders to the kept user first, so no order ever points at a deleted user.
UPDATE orders
SET user_id = (SELECT m.keep_id FROM _user_map AS m WHERE m.old_id = orders.user_id)
WHERE user_id IN (SELECT old_id FROM _user_map);

-- Delete the merged-away users. No orders reference them any more.
DELETE FROM users WHERE id IN (SELECT old_id FROM _user_map);

-- Store the canonical email: trimmed and lower-cased.
UPDATE users SET email = lower(trim(email));

------------------------------------------------------------------------
-- 2. Case- and whitespace-insensitive unique email
------------------------------------------------------------------------
-- The index uses the same expression as the grouping above, so it cannot
-- fail on the merged data. It also rejects 'Ana@x.com ' after 'ana@x.com'.
CREATE UNIQUE INDEX users_email_norm_uq ON users (lower(trim(email)));

------------------------------------------------------------------------
-- 3. Rebuild orders with normalized status and a CHECK constraint
------------------------------------------------------------------------
CREATE TABLE orders_new (
  id          INTEGER PRIMARY KEY,
  user_id     INTEGER NOT NULL REFERENCES users(id),
  status      TEXT NOT NULL
              CHECK (status IN ('pending', 'paid', 'refunded', 'cancelled')),
  total_cents INTEGER NOT NULL,
  created_at  TEXT NOT NULL
);

INSERT INTO orders_new (id, user_id, status, total_cents, created_at)
SELECT id,
       user_id,
       CASE
         WHEN lower(trim(status)) IN ('pending', 'paid', 'refunded', 'cancelled')
           THEN lower(trim(status))
         WHEN lower(trim(status)) = 'canceled'
           THEN 'cancelled'
         ELSE 'pending'
       END,
       total_cents,
       created_at
FROM orders;

-- No order lost, and ids unchanged.
INSERT INTO _assert
SELECT (SELECT COUNT(*) FROM orders) = (SELECT COUNT(*) FROM orders_new);
INSERT INTO _assert
SELECT NOT EXISTS (
  SELECT 1 FROM orders AS o
  LEFT JOIN orders_new AS n ON n.id = o.id
  WHERE n.id IS NULL
);

-- No table references orders, so dropping it is safe with FKs on.
DROP TABLE orders;
ALTER TABLE orders_new RENAME TO orders;

------------------------------------------------------------------------
-- 4. Index for "orders for a user, newest first"
------------------------------------------------------------------------
CREATE INDEX orders_user_created_idx ON orders (user_id, created_at DESC);

------------------------------------------------------------------------
-- 5. Final integrity assertions
------------------------------------------------------------------------
-- Every order points at an existing user.
INSERT INTO _assert
SELECT NOT EXISTS (
  SELECT 1 FROM orders AS o
  LEFT JOIN users AS u ON u.id = o.user_id
  WHERE u.id IS NULL
);

DROP TABLE _assert;
DROP TABLE _user_map;

COMMIT;

-- Should print nothing.
PRAGMA foreign_key_check;
```

\*\*Explanation\*\* The script uses \`.bail on\` and wraps everything in one \`BEGIN IMMEDIATE ... COMMIT\`, so any error rolls back the whole migration. It sets \`foreign\_keys = ON\` before the transaction because SQLite ignores that pragma inside one. The steps run in this order: 1\. Build a map from each duplicate user to the lowest id in its group, using \`lower(trim(email))\`. 2\. Re-point those users' orders to the kept user. 3\. Delete the duplicates. 4\. Normalize the emails. 5\. Create the unique index on that same expression. Because orders move before users are deleted, foreign keys are never violated. \`orders\` is then rebuilt with a CHECK constraint. Statuses are mapped during the copy and ids are copied explicitly. Count and id assertions run before the old table is dropped. Nothing references \`orders\`, so the drop and rename are safe. Caveat: \`lower()\` folds only ASCII letters unless SQLite is built with ICU.

**GPT-6.2 Astra Pass, 15 of 15, 6.2 s, 520 output tokens**

```
.bail on
PRAGMA foreign_keys = ON;
BEGIN IMMEDIATE;

CREATE TEMP TABLE user_merge (
  old_id  INTEGER PRIMARY KEY,
  keep_id INTEGER NOT NULL
);

INSERT INTO user_merge (old_id, keep_id)
SELECT u.id, g.keep_id
FROM users AS u
JOIN (
  SELECT lower(trim(email)) AS email_key, min(id) AS keep_id
  FROM users
  GROUP BY lower(trim(email))
) AS g ON lower(trim(u.email)) = g.email_key
WHERE u.id <> g.keep_id;

UPDATE orders
SET user_id = (
  SELECT keep_id FROM user_merge WHERE old_id = orders.user_id
)
WHERE user_id IN (SELECT old_id FROM user_merge);

DELETE FROM users WHERE id IN (SELECT old_id FROM user_merge);
DROP TABLE temp.user_merge;

UPDATE users SET email = lower(trim(email));
CREATE UNIQUE INDEX users_email_key ON users (lower(trim(email)));

CREATE TABLE orders_new (
  id          INTEGER PRIMARY KEY,
  user_id     INTEGER NOT NULL REFERENCES users(id),
  status      TEXT NOT NULL CHECK (status IN ('pending', 'paid', 'refunded', 'cancelled')),
  total_cents INTEGER NOT NULL,
  created_at  TEXT NOT NULL
);

INSERT INTO orders_new (id, user_id, status, total_cents, created_at)
SELECT id, user_id,
       CASE lower(trim(status))
         WHEN 'paid'      THEN 'paid'
         WHEN 'refunded'  THEN 'refunded'
         WHEN 'cancelled' THEN 'cancelled'
         WHEN 'canceled'  THEN 'cancelled'
         ELSE 'pending'
       END,
       total_cents, created_at
FROM orders;

DROP TABLE orders;
ALTER TABLE orders_new RENAME TO orders;
CREATE INDEX orders_user_newest ON orders (user_id, created_at DESC);

PRAGMA foreign_key_check;
COMMIT;
```

The merge map identifies each duplicate’s lowest-id survivor. Orders are reassigned before duplicates are deleted, so their foreign keys remain valid. The unique index uses the same normalized key as the merge, rejecting future case or surrounding-space duplicates under SQLite’s built-in ASCII case folding. The rebuild copies every order with its original id and normalized status; the new constraint rejects invalid statuses on insert or update. The final index supports newest-first retrieval for each user. \`BEGIN IMMEDIATE\` prevents concurrent writers, foreign keys are enforced throughout, and \`.bail on\` makes the CLI exit on error rather than commit partial changes.

**Gemini 4 Neon Pass, 15 of 15, 77.3 s, 639 output tokens**

```
PRAGMA foreign_keys = OFF;

BEGIN IMMEDIATE;

-- 1. Reassign orders from duplicate users (same trimmed, lower-cased email) to lowest id
UPDATE orders
SET user_id = (
  SELECT MIN(u_same.id)
  FROM users AS u_dup
  JOIN users AS u_same
    ON LOWER(TRIM(u_same.email)) = LOWER(TRIM(u_dup.email))
  WHERE u_dup.id = orders.user_id
)
WHERE EXISTS (
  SELECT 1
  FROM users AS u_dup
  WHERE u_dup.id = orders.user_id
    AND u_dup.id > (
      SELECT MIN(u_same.id)
      FROM users AS u_same
      WHERE LOWER(TRIM(u_same.email)) = LOWER(TRIM(u_dup.email))
    )
);

-- 2. Remove duplicate users and normalize kept emails
DELETE FROM users
WHERE id NOT IN (
  SELECT MIN(id)
  FROM users
  GROUP BY LOWER(TRIM(email))
);

UPDATE users
SET email = LOWER(TRIM(email));

-- 3. Rebuild orders with normalized status and allowed-value CHECK constraint
CREATE TABLE orders_new (
  id INTEGER PRIMARY KEY,
  user_id INTEGER NOT NULL REFERENCES users(id),
  status TEXT NOT NULL CHECK (status IN ('pending', 'paid', 'refunded', 'cancelled')),
  total_cents INTEGER NOT NULL,
  created_at TEXT NOT NULL
);

INSERT INTO orders_new (id, user_id, status, total_cents, created_at)
SELECT
  id,
  user_id,
  CASE
    WHEN LOWER(TRIM(status)) = 'canceled' THEN 'cancelled'
    WHEN LOWER(TRIM(status)) IN ('pending', 'paid', 'refunded', 'cancelled') THEN LOWER(TRIM(status))
    ELSE 'pending'
  END,
  total_cents,
  created_at
FROM orders;

DROP TABLE orders;
ALTER TABLE orders_new RENAME TO orders;

-- 4. Enforce case/whitespace-insensitive email uniqueness and newest-first user order lookups
CREATE UNIQUE INDEX idx_users_email_lower ON users(LOWER(TRIM(email)));
CREATE INDEX idx_orders_user_id_created_at ON orders(user_id, created_at DESC);

COMMIT;

PRAGMA foreign_keys = ON;
PRAGMA foreign_key_check;
```

Disabling foreign keys before \`BEGIN IMMEDIATE\` lets us delete merged users and drop \`orders\` inside one atomic transaction without cascade or drop errors. Reassigning \`orders.user\_id\` to each email group's lowest \`id\` before deleting duplicates preserves every order and keeps \`order.id\` unchanged. Normalizing emails and deleting duplicates before creating \`idx\_users\_email\_lower\` prevents unique-index creation failures. Rebuilding \`orders\` with a \`CHECK\` constraint sanitizes legacy \`status\` values on insert and blocks invalid statuses afterward. Creating \`idx\_orders\_user\_id\_created\_at\` with \`created\_at DESC\` optimizes per-user newest-first queries, and re-enabling foreign keys with \`PRAGMA foreign\_key\_check\` verifies referential integrity.

**Gemini 4 Carbon Pass, 15 of 15, 44.8 s, 2,561 output tokens**

```
PRAGMA foreign_keys = ON;

BEGIN TRANSACTION;

-- 1. Rebuild orders with the status CHECK constraint, remapped user_ids, and normalized statuses
CREATE TABLE orders_new (
  id INTEGER PRIMARY KEY,
  user_id INTEGER NOT NULL REFERENCES users(id),
  status TEXT NOT NULL CHECK (status IN ('pending', 'paid', 'refunded', 'cancelled')),
  total_cents INTEGER NOT NULL,
  created_at TEXT NOT NULL
);

WITH canonical_users AS (
  SELECT lower(trim(email)) AS norm_email, MIN(id) AS kept_id
  FROM users
  GROUP BY lower(trim(email))
)
INSERT INTO orders_new (id, user_id, status, total_cents, created_at)
SELECT
  o.id,
  cu.kept_id,
  CASE lower(trim(o.status))
    WHEN 'pending'   THEN 'pending'
    WHEN 'paid'      THEN 'paid'
    WHEN 'refunded'  THEN 'refunded'
    WHEN 'cancelled' THEN 'cancelled'
    WHEN 'canceled'  THEN 'cancelled'
    ELSE 'pending'
  END,
  o.total_cents,
  o.created_at
FROM orders o
JOIN users u ON u.id = o.user_id
JOIN canonical_users cu ON cu.norm_email = lower(trim(u.email));

DROP TABLE orders;
ALTER TABLE orders_new RENAME TO orders;

-- 2. Remove merged duplicate users and normalize kept emails
DELETE FROM users
WHERE id NOT IN (
  SELECT MIN(id)
  FROM users
  GROUP BY lower(trim(email))
);

UPDATE users
SET email = lower(trim(email));

-- 3. Enforce case- and space-insensitive email uniqueness and index user orders
CREATE UNIQUE INDEX idx_users_email_unique
  ON users (lower(trim(email)));

CREATE INDEX idx_orders_user_created_at
  ON orders (user_id, created_at DESC);

COMMIT;
```

\### Explanation First, \`PRAGMA foreign\_keys = ON\` ensures referential integrity is enforced throughout the \`sqlite3\` CLI session, and \`BEGIN TRANSACTION\` makes the entire migration atomic - any failure rolls back all changes cleanly. 1\. \*\*\`orders\` is rebuilt first\*\*: Because no other table references \`orders\`, recreating and replacing it works safely while foreign keys remain enabled. In one pass, every order keeps its original \`id\`, gets reassigned to the lowest \`users.id\` for its normalized email, and has its \`status\` normalized and validated against the new \`CHECK\` constraint. 2\. \*\*Duplicate \`users\` are deleted second\*\*: Because all orders already point to the kept users, deleting redundant user rows cannot violate foreign keys. Kept emails are then trimmed and lower-cased. 3\. \*\*Indexes are created last\*\*: Building the unique expression index on \`lower(trim(email))\` after deduplication guarantees uniqueness (blocking inputs like \`'Ana@x.com '\`), while \`(user\_id, created\_at DESC)\` optimizes newest-first user order queries.

**Claude Fable 5.2 Pass, 15 of 15, 38.8 s, 4,164 output tokens**

```
-- migration.sql
-- Run with: sqlite3 db < migration.sql
-- Stop at the first error; the open transaction is then rolled back when the CLI exits.
.bail on

-- Must be set outside a transaction (it is a no-op inside one).
PRAGMA foreign_keys = ON;

BEGIN IMMEDIATE;

----------------------------------------------------------------------
-- 1. Merge users whose emails differ only by case / surrounding spaces
----------------------------------------------------------------------
CREATE TEMP TABLE _user_merge (
  old_id  INTEGER PRIMARY KEY,
  keep_id INTEGER NOT NULL
);

INSERT INTO _user_merge (old_id, keep_id)
SELECT u.id, k.keep_id
FROM users AS u
JOIN (
  SELECT lower(trim(email)) AS norm_email, MIN(id) AS keep_id
  FROM users
  GROUP BY lower(trim(email))
) AS k ON lower(trim(u.email)) = k.norm_email
WHERE u.id <> k.keep_id;

-- Move orders of duplicate users to the kept user (lowest id in the group).
UPDATE orders
SET user_id = (SELECT m.keep_id FROM _user_merge AS m WHERE m.old_id = orders.user_id)
WHERE user_id IN (SELECT old_id FROM _user_merge);

-- Duplicates now have no orders, so deleting them satisfies the foreign key.
DELETE FROM users
WHERE id IN (SELECT old_id FROM _user_merge);

DROP TABLE _user_merge;

-- Store the surviving emails trimmed and lower-cased.
UPDATE users
SET email = lower(trim(email))
WHERE email <> lower(trim(email));

----------------------------------------------------------------------
-- 2. Enforce case-insensitive (and space-insensitive) unique emails
----------------------------------------------------------------------
CREATE UNIQUE INDEX users_email_unique ON users (lower(trim(email)));

----------------------------------------------------------------------
-- 3. Rebuild orders with normalized status and a CHECK constraint
----------------------------------------------------------------------
CREATE TABLE orders_new (
  id          INTEGER PRIMARY KEY,
  user_id     INTEGER NOT NULL REFERENCES users(id),
  status      TEXT    NOT NULL
              CHECK (status IN ('pending', 'paid', 'refunded', 'cancelled')),
  total_cents INTEGER NOT NULL,
  created_at  TEXT    NOT NULL
);

INSERT INTO orders_new (id, user_id, status, total_cents, created_at)
SELECT
  id,
  user_id,
  CASE lower(trim(status))
    WHEN 'pending'   THEN 'pending'
    WHEN 'paid'      THEN 'paid'
    WHEN 'refunded'  THEN 'refunded'
    WHEN 'cancelled' THEN 'cancelled'
    WHEN 'canceled'  THEN 'cancelled'
    ELSE 'pending'
  END,
  total_cents,
  created_at
FROM orders;

-- Nothing references orders, so dropping and renaming is safe with foreign keys on.
DROP TABLE orders;
ALTER TABLE orders_new RENAME TO orders;

----------------------------------------------------------------------
-- 4. Index for "orders for a user, newest first"
----------------------------------------------------------------------
CREATE INDEX orders_user_created_idx ON orders (user_id, created_at DESC);

----------------------------------------------------------------------
-- 5. Verify: prints nothing when all foreign keys are valid
----------------------------------------------------------------------
PRAGMA foreign_key_check;

COMMIT;
```

\*\*Order and safety\*\* 1\. \*\*Merge users first.\*\* Duplicates are mapped to the lowest id per \`lower(trim(email))\`. Orders are repointed before the duplicate users are deleted, so the foreign key is never violated. Only then are emails normalized. 2\. \*\*Unique index after the merge\*\*, on \`lower(trim(email))\`, so it cannot fail on existing data and \`'Ana@x.com '\` is rejected after \`'ana@x.com'\`. 3\. \*\*Rebuild \`orders\`.\*\* Every row is copied with its original id and a mapped status into a table with the CHECK constraint, then swapped in. \`orders\` is a child table only, so drop and rename are safe with foreign keys on. 4\. \*\*Index\*\* on \`(user\_id, created\_at DESC)\` is created on the final table. Everything runs in one \`BEGIN IMMEDIATE … COMMIT\`. \`.bail on\` matters: without it, the \`sqlite3\` CLI continues after an error and would commit a partial migration. I have not run this against a database. The kept user's other columns (such as \`full\_name\`) are left as they were.

**DeepSeek V4.1 Flash 1017 Pass, 15 of 15, 78.4 s, 13.0K output tokens**

```
PRAGMA foreign_keys = ON;

BEGIN;

DROP TABLE IF EXISTS temp._user_merge;
CREATE TEMP TABLE _user_merge AS
SELECT
  u.id AS old_id,
  (
    SELECT MIN(u2.id)
    FROM users u2
    WHERE lower(trim(u2.email)) = lower(trim(u.email))
  ) AS keep_id
FROM users u;

UPDATE orders
SET user_id = (
  SELECT m.keep_id
  FROM _user_merge m
  WHERE m.old_id = orders.user_id
)
WHERE user_id IN (
  SELECT old_id
  FROM _user_merge
  WHERE old_id <> keep_id
);

DELETE FROM users
WHERE id IN (
  SELECT old_id
  FROM _user_merge
  WHERE old_id <> keep_id
);

UPDATE users
SET email = lower(trim(email))
WHERE id IN (
  SELECT keep_id
  FROM _user_merge
);

CREATE UNIQUE INDEX users_email_lower_trim_unique
ON users(lower(trim(email)));

CREATE TABLE orders_new (
  id INTEGER PRIMARY KEY,
  user_id INTEGER NOT NULL REFERENCES users(id),
  status TEXT NOT NULL CHECK (
    status IN ('pending', 'paid', 'refunded', 'cancelled')
  ),
  total_cents INTEGER NOT NULL,
  created_at TEXT NOT NULL
);

INSERT INTO orders_new (id, user_id, status, total_cents, created_at)
SELECT
  id,
  user_id,
  CASE lower(trim(status))
    WHEN 'pending'  THEN 'pending'
    WHEN 'paid'     THEN 'paid'
    WHEN 'refunded' THEN 'refunded'
    WHEN 'cancelled' THEN 'cancelled'
    WHEN 'canceled' THEN 'cancelled'
    ELSE 'pending'
  END,
  total_cents,
  created_at
FROM orders;

DROP TABLE orders;
ALTER TABLE orders_new RENAME TO orders;

CREATE INDEX idx_orders_user_created
ON orders(user_id, created_at);

DROP TABLE temp._user_merge;

PRAGMA foreign_key_check;

COMMIT;
```

All data changes are inside one transaction. First I build a temp mapping of each user to the lowest id in its \`lower(trim(email))\` group, reassign orders from duplicate users to that keeper, then delete the duplicates. Reassigning orders before deleting users keeps foreign keys valid and loses no order rows. Kept emails are normalized, then a unique expression index on \`lower(trim(email))\` enforces case- and space-insensitive uniqueness. Next I rebuild \`orders\` with unchanged ids, normalized statuses, and a CHECK constraint for the four allowed values; \`canceled\` maps to \`cancelled\`, and anything else becomes \`pending\`. Finally I add the \`(user\_id, created\_at)\` index and run \`foreign\_key\_check\` before committing.

**Kimi K3.1 Pro Pass, 15 of 15, 227 s, 7,752 output tokens**

```
.bail on
PRAGMA foreign_keys = ON;

BEGIN IMMEDIATE;

-- Map every user to the lowest-id user in the same normalized-email group.
CREATE TEMP TABLE _user_merge_map (
  old_user_id  INTEGER PRIMARY KEY,
  keep_user_id INTEGER NOT NULL
);

INSERT INTO _user_merge_map (old_user_id, keep_user_id)
SELECT u.id, k.keep_user_id
FROM users AS u
JOIN (
  SELECT lower(trim(email)) AS normalized_email,
         MIN(id) AS keep_user_id
  FROM users
  GROUP BY lower(trim(email))
) AS k
  ON k.normalized_email = lower(trim(u.email));

-- Repoint orders before deleting any referenced users.
UPDATE orders
SET user_id = (
  SELECT m.keep_user_id
  FROM _user_merge_map AS m
  WHERE m.old_user_id = orders.user_id
)
WHERE user_id IN (
  SELECT old_user_id
  FROM _user_merge_map
  WHERE old_user_id <> keep_user_id
);

DELETE FROM users
WHERE id IN (
  SELECT old_user_id
  FROM _user_merge_map
  WHERE old_user_id <> keep_user_id
);

-- Every survivor is now a group's kept user.
UPDATE users
SET email = lower(trim(email));

DROP TABLE _user_merge_map;

-- Also rejects future case/whitespace variants, including 'Ana@x.com '.
CREATE UNIQUE INDEX users_email_normalized_uq
  ON users(lower(trim(email)));

-- Rebuild orders with an insert/update CHECK constraint.
CREATE TABLE orders_new (
  id INTEGER PRIMARY KEY,
  user_id INTEGER NOT NULL REFERENCES users(id),
  status TEXT NOT NULL CHECK (
    status IN ('pending', 'paid', 'refunded', 'cancelled')
  ),
  total_cents INTEGER NOT NULL,
  created_at TEXT NOT NULL
);

INSERT INTO orders_new (id, user_id, status, total_cents, created_at)
SELECT
  id,
  user_id,
  CASE lower(trim(status))
    WHEN 'pending'   THEN 'pending'
    WHEN 'paid'      THEN 'paid'
    WHEN 'refunded'  THEN 'refunded'
    WHEN 'cancelled' THEN 'cancelled'
    WHEN 'canceled'  THEN 'cancelled'
    ELSE 'pending'
  END,
  total_cents,
  created_at
FROM orders;

DROP TABLE orders;
ALTER TABLE orders_new RENAME TO orders;

CREATE INDEX orders_user_created_at_idx
  ON orders(user_id, created_at DESC);

PRAGMA foreign_key_check;

COMMIT;
```

Foreign keys are enabled before \`BEGIN IMMEDIATE\`; all schema and data changes are transactional, and \`.bail on\` prevents partial migration after an error. The temporary mapping identifies each normalized-email group’s lowest id. Orders are repointed before duplicate users are deleted, then survivors’ emails are normalized and protected by a unique expression index that also trims future index keys. Rebuilding \`orders\` adds the status \`CHECK\`, while explicitly copying every row and id and canonicalizing statuses. Finally, the composite descending index supports per-user newest-first queries, and \`PRAGMA foreign\_key\_check\` verifies referential integrity before commit.

## How was this run?

One run per model on the same day, through one API, graded by the same tests.

- **Run** 26 November 2026, 5:55 PM to 6:01 PM PT. Each model got each task once, with the same system prompt and the same user prompt, word for word.
- **How it was called** Through the Straitly API (OpenAI chat completions format, streaming) with our own test key. Each model's default temperature, max\_tokens 32,000, no tools.
- **Grading** The code block in the reply was saved and run against 15 checks run against a seeded SQLite database. Pass means every check passed.
- **Time** First token is the time from sending the request to the first streamed token, reasoning tokens included. Total time runs to the last token. Both were timed on the calling machine, so network time is included.
- **Tokens** From the usage block each reply ended with. Reasoning tokens are part of output tokens and are billed as output. The reasoning figure is what that block reported: Gemini 4 Neon reported none on all three tasks, though its first token took over a minute each time.
- **List cost** Input and output tokens times the maker's list price per 1M tokens, read on the run day. DeepSeek ran in its peak hours. Gemini 4 Carbon's rate is the one in force until 30 November 2026.
- **Straitly price** The same tokens at the Straitly catalog price on the run day. Gemini 4 Neon shows a link to its model page instead.
- **Reruns** One. Claude Fable 5.2's first request on the failing-test task timed out while connecting, before any token came back. It was sent again, and the second run is the one shown. Nothing was rerun for a better answer.

### Rates used

| Model                             | List           | Straitly price                                                       |
| --------------------------------- | -------------- | -------------------------------------------------------------------- |
| Claude Opus 5.5 Anthropic         | $4.00 / $20.00 | [$2.40 / $12.00](https://straitly.ai/models/claude-opus-5-5)         |
| GPT-6.2 Astra OpenAI              | $8.00 / $40.00 | [$4.80 / $24.00](https://straitly.ai/models/gpt-6.2-astra)           |
| Gemini 4 Neon Google              | $3.00 / $15.00 | [See model page](https://straitly.ai/models/gemini-4-neon)           |
| Gemini 4 Carbon Google            | $2.00 / $10.00 | [$2.00 / $10.00](https://straitly.ai/models/gemini-4-carbon)         |
| Claude Fable 5.2 Anthropic        | $8.00 / $40.00 | [$4.80 / $24.00](https://straitly.ai/models/claude-fable-5.2)        |
| DeepSeek V4.1 Flash 1017 DeepSeek | $0.30 / $1.20  | [$0.09 / $0.36](https://straitly.ai/models/deepseek-v4-1-flash-1017) |
| Kimi K3.1 Pro Moonshot            | $2.00 / $10.00 | [$1.14 / $5.70](https://straitly.ai/models/kimi-k3.1-pro)            |

List prices from each maker's pricing page, read on the run day. DeepSeek ran in its peak hours, so its peak rate applies.

## What else do people ask?

[Ask a question](https://straitly.ai/contact)

- ### Which AI model is best at this task?
On this run all 7 models passed all 15 checks, so pick on speed and cost: GPT-6.2 Astra was fastest at 6.2 s and Gemini 4 Neon cost the least at list price, $0.0112.
- ### How was each answer graded?
By running it: 15 checks run against a seeded SQLite database. A model passes only with every check green. Nobody scored the answers by reading them.
- ### Is one run enough to rank models?
No. Each model ran once, on 26 November 2026, so a gap of a few seconds can flip on another run. Gaps of a minute or more showed up on all three tasks.
- ### What does time to first token mean here?
Seconds from sending the request to the first streamed token, reasoning tokens included. Total time runs to the last token.

## Sources

List prices quoted from each maker's own pricing page on the run day. Spot something out of date? Tell us at [straitly.ai/contact](https://straitly.ai/contact).

1. \[1\] [Anthropic pricing](https://platform.claude.com/docs/en/about-claude/pricing) platform.claude.com, checked 26 November 2026
2. \[2\] [OpenAI pricing](https://developers.openai.com/api/docs/pricing) developers.openai.com, checked 26 November 2026
3. \[3\] [Gemini API pricing](https://ai.google.dev/gemini-api/docs/pricing) ai.google.dev, checked 26 November 2026
4. \[4\] [DeepSeek pricing](https://api-docs.deepseek.com/quick%5Fstart/pricing) api-docs.deepseek.com, checked 26 November 2026
5. \[5\] [Kimi API pricing](https://platform.kimi.ai/docs/pricing/chat) platform.kimi.ai, checked 26 November 2026
6. \[6\] [Straitly model catalog](https://straitly.ai/models) straitly.ai/models, checked 26 November 2026

**What each source says**

Anthropic pricing

Claude Opus 5.5 $4 / $20 and Claude Fable 5.2 $8 / $40 per 1M input / output tokens, base rate.

OpenAI pricing

gpt-6.2-astra $8 / $40 per 1M input / output tokens, Standard, prompts up to 272K tokens.

Gemini API pricing

Gemini 4 Neon $3 / $15 per 1M input / output tokens, paid tier. Gemini 4 Carbon $2 / $10 through 30 November 2026 and $4 / $20 from 1 December 2026.

DeepSeek pricing

V4.1 Flash $0.30 / $1.20 per 1M input (cache miss) / output tokens in peak hours, 01:00 to 04:00 and 06:00 to 10:00 UTC on weekdays; half that off-peak.

Kimi API pricing

kimi-k3.1-pro $2 / $10 per 1M input (cache miss) / output tokens.

Straitly model catalog

The per-model price list /models prints, read on the run day.

Read next: [Fix a failing test](https://straitly.ai/shootouts/fix-failing-test) , [Build a React component](https://straitly.ai/shootouts/react-component) , [All shootouts](https://straitly.ai/shootouts) , [All comparisons](https://straitly.ai/compare).

## Start with one request.

Run the same prompt on every model with one key. [See pricing](https://straitly.ai/pricing).

[Console](https://straitly.ai/app) [Talk to sales ](https://straitly.ai/contact)

---
Markdown version of https://straitly.ai/shootouts/sql-migration.
