Two Prompts for a Database Migration: What the Second One Adds

Alder Clinic had 1.2 million appointment rows to move off a legacy SQL Server instance and into Postgres, and one weekend with a 4-hour maintenance window to do it. The first prompt produced a tidy script that copied every row in 38 minutes, then held a write lock on the appointments table for the rest of the window while it rebuilt indexes. Nobody had asked about locks, so nobody had budgeted for one.

The script also had no way back. When a spot check found 214 rows with a status value Postgres rejected, there was no rollback step and no verification query. The team spent the last 90 minutes restoring from backup by hand. The data was fine; the plan was not. Here is the prompt that caused that night, the rewrite that would have prevented it, and a skeleton for your own cutover.

What the First Run Cost Alder Clinic

The failure was not a coding error. The script did exactly what it was told: read 1.2 million rows from one database and write them to another. But a migration is not a copy job. It is a scheduled change with a fixed window, a lock budget, a way to reverse course, and a test that proves the result before bookings reopen on Monday morning. A goal-only prompt gives the model none of those, so it optimizes for the visible request: a correct transfer and clean-looking code.

Worse, a short prompt invites the model to fill gaps with assumptions. It picked a default batch size, assumed the column names matched, and treated a finished loop as success. Each was reasonable alone and wrong for a live clinic system:

  • A long index rebuild held the lock past the window.
  • No checkpoint meant restarting all 1.2 million rows after a failed batch.
  • No rollback command left recovery as a manual restore under pressure.
  • No verification query meant the 214 rejected rows were found by luck.

Each is a missing requirement, not a missing line of code. For phrasing query-level checks, the SQL query generator prompt covers the same habit of stating what a correct result must prove.

The Prompt That Only Described the Goal

This is the version the team pasted in. Source, target, row count, and a vague wish that nothing be lost. That is the whole brief.

Write a Python script to migrate appointment data from a legacy
SQL Server database to Postgres for a clinic network. The appointments
table has 1.2 million rows. Copy the source & target schemas, move
all the rows across, and make sure nothing is lost. Keep the script
short and readable.

What came back: about sixty lines of clean Python with a fetch-many loop and a batch insert. It read the live table without a consistent ordering key, so a resume would silently duplicate rows; it wrapped the whole load in one transaction, which is what held the lock; it printed “done”. “Keep it short and readable” actively pushed against a checkpoint table and a rollback section, because those add length. “Make sure nothing is lost” is not testable, so it became an intention rather than a query. A short prompt does not just omit constraints; it steers the model away from them.

The Prompt That Planned the Window

The rewrite changes the deliverable from a script to a runbook. It states the window, caps the lock, demands a resume path, requires a rollback command, and makes verification the gate for success.

You are a database migration engineer planning a one-weekend cutover
for Alder Clinic. Produce a runbook, not just a copy script.

CONTEXT
- Source: legacy SQL Server, table dbo.Appointments, 1,200,000 rows.
- Target: Postgres 15, table appointments, columns: appointment_id,
  patient_id, clinician_id, starts_at, ends_at, status, notes.
- Window: one 4-hour maintenance window, Saturday 22:00 to Sunday 02:00.
- Hard rule: no single table lock may exceed 90 seconds.

DELIVERABLES
1. A batched copy that reads in chunks of 5,000 rows ordered by
   appointment_id, with a checkpoint table so a failed batch resumes.
2. A lock budget table: each step, the lock it takes, estimated duration,
   and a flag on any step over 90 seconds.
3. A rollback path: the exact command to restore the source and remove
   the target load if verification fails, plus the point of no return.
4. A verification query that checks count >= 1,200,000 and compares
   a checksum of appointment_id & starts_at across SQL Server -> Postgres.
5. A go / no-go decision line for the end of the window.

OUTPUT
Plain text, numbered steps, each with an owner and a time estimate.
List every assumption you made about the schema. Do not claim success
without the verification query result.

What came back: a seven-step runbook that split the index build from the load, gave each step a lock estimate, added a checkpoint keyed on appointment_id, and wrote both a restore command and a checksum query. It even flagged one assumption: that starts_at was a plain timestamp, not a SQL Server datetime of different precision. The right prompt is longer, but every added line is a constraint the first night proved was missing. For a second pass over the generated SQL, a set of code review prompts for migration scripts is a reasonable follow-up.

Line by Line: What the Second Prompt Adds

The outputs differ on seven concrete dimensions. The table maps each added requirement to the failure it prevents.

DimensionGoal-only promptRunbook promptFailure it prevents
DeliverableA Python scriptA numbered runbook with owners and estimatesA script nobody can execute at 22:00 without guessing the order
Lock budgetNone90-second cap, per-step estimate, overrun flagThe index rebuild that ate the window
Resume pathSingle transaction, restart from zeroCheckpoint table ordered by appointment_idRe-running 1.2 million rows after a failed batch
RollbackNoneExact restore command and a point of no returnThe manual 90-minute restore under pressure
Verification“Nothing is lost” as an intentionCount check and checksum query as a gateThe 214 rejected status rows found by luck
AssumptionsHiddenExplicitly listedSilent datetime and type mismatches
Success signalPrints “done”A go / no-go line tied to the query resultReopening bookings before the data is proven

The lock ceiling is the biggest addition. A 4-hour window is not 4 hours of copying; it is copying, verifying, and being ready to reverse. Capping any one lock at 90 seconds forces the model to break the work into short, observable steps and move the index build out of the load transaction.

Rollback and verification change the decision, not just the code. A written restore command plus a point of no return gives the operator permission to stop. And “make sure nothing is lost” produces a comment, while “check count >= 1,200,000 and compare a checksum of appointment_id & starts_at” produces an executable gate. The checksum matters more than the count: 1.2 million rows can all arrive with a subtly wrong starts_at and still pass a row count. Stating the expected shape of each gate is the discipline in the structured output format guide.

A Migration Prompt Skeleton You Can Reuse

Swap the bracketed values for your own and keep the five deliverables in order; the order is what stops the model skipping straight to code. Hand the runbook to a DBA before anyone runs it.

You are a [ROLE] planning a [CHANGE_TYPE] for [ORGANIZATION].
Produce a runbook, not just a script.

CONTEXT
- Source: [SOURCE_SYSTEM], [SOURCE_TABLE], [ROW_COUNT] rows.
- Target: [TARGET_SYSTEM], [TARGET_TABLE], columns: [COLUMN_LIST].
- Window: one [WINDOW_LENGTH] maintenance window,
  [WINDOW_START] to [WINDOW_END].
- Hard rule: [HARD_CONSTRAINT], for example no lock over [LOCK_BUDGET].

DELIVERABLES
1. A batched transfer in chunks of [BATCH_SIZE], ordered by [KEY_COLUMN],
   with a checkpoint table so a failed batch resumes.
2. A lock or load budget table per step, flagging anything over [LOCK_BUDGET].
3. A rollback path with the exact restore command and a point of no return.
4. A verification query comparing [CHECKSUM_COLUMNS] across source and
   target, with a pass threshold of [ROW_COUNT].
5. A go / no-go decision line for the end of the window.

OUTPUT
Plain text, numbered steps, each with an owner and a time estimate.
List every assumption. Do not claim success without a verification result.
Fill [COLUMN_LIST] and [CHECKSUM_COLUMNS] from real DDL, not from a guess.

The last line is the one people skip. Let the model guess [COLUMN_LIST] and it produces plausible column names that do not exist, so every step built on them is fiction. Pull the real DDL first, and use a measured [ROW_COUNT], not a count from last quarter. Run the skeleton once with thin inputs: if the assumptions list is empty, add “flag any column whose type differs between source and target” and run it again. A template that names its ignorance is safer than one that sounds confident.

Where the Second Prompt Still Needs a Human

The runbook is a plan, not a rehearsal. A model can estimate that a 5,000-row batch takes seconds; only a timing run on a staging restore of the same 1.2 million rows tells you whether the window holds. Type mapping is the other gap: SQL Server datetime and Postgres timestamp with time zone are not interchangeable, identity columns need attention, and collation differences can reorder comparisons a checksum over appointment_id will not reveal. The prompt can flag these; a DBA still has to resolve them.

  • Time the copy and index build on a staging restore of equal size, not a sample.
  • Have a DBA approve the type mapping for starts_at, status, and the identity key.
  • Execute the rollback once in staging; an untested restore command is not a plan.
  • Confirm the on-call owner and acceptable downtime before the window opens.

Sources & Notes

The prompts and runbook were written for a 1,200,000-row appointments table moving from legacy SQL Server to Postgres 15, and reviewed on 2026-10-12. To check the comparison, we replayed both prompts against three past cutover briefs: a 400,000-row patients table, the 1,200,000-row appointments table, and a 90,000-row billing table. Each output was scored on five checks: a stated lock budget, a rollback command, a verification query, a checkpoint or resume path, and a named go / no-go line. The goal-only prompt produced a rollback command in 0 of 3 replays and a verification query in 1 of 3; the runbook prompt produced all five checks in 3 of 3. Alder Clinic, the 38-minute copy, and the 214 rejected rows are illustrative of the failure, not a logged incident.

The method has limits. It assumes you already know your real row count, window, and column list; with undocumented schemas the model invents column names, so feed it the DDL or the assumptions list becomes guesses. Lock and duration figures in generated runbooks are estimates, never measurements, and must be timed on staging hardware of the same size. The comparison tests whether sections are present, not whether the SQL is correct: a runbook can pass all five checks and still contain a wrong type cast. It also fits planned, one-window cutovers with a fixed source and target; it does not cover online dual-write migrations, zero-downtime replication, or live schema changes during the copy, each of which needs a different prompt and review.

声明:1.本站所有文章,如无特殊说明或标注,均为本站原创发布。本网站中【文本生成/文生图】使用了DeeSseek人工智能生成内容技术, 算法来源说明:本网站未自行开发、训练或部署深度合成算法,所使用的人工智能算法来自:DeepSeek、Chatgpt、Google Gemini。 2.为了网站加载速度优化,本站预览图片进行压缩处理,所以有些不是太清晰,并不影响AI提示词生图、AI设计等内容。3.任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。--GUUNN.COM