Skip to content

Make the Orca migration safe to run against a live database #321

Done

Description

@NathanTarbert

scripts/migrate-from-orca.ts copies Orca accounts, users, tickets and messages into Outpost. It works for a clean, one-off migration into an empty database. Running it into a database that already has tickets, or recovering from a run that failed partway, can go wrong in four ways. These came up while reviewing #306, and none of them were introduced there.

The ticket counter can restart at 1. getNextDisplayIdCounter finds the highest display ID by sorting displayId descending, but that sort is on text, not numbers. Tickets created by the bots and the web app have random IDs from generateTicketId(), such as TKT-K7QX2M9P, and those sort above any numeric ID. The ^TKT-(\d+)$ match then fails, and the counter starts again at 1. The same happens with numbers alone once they pass four digits, because TKT-9999 sorts above TKT-10000. Either way the migration reissues display IDs that already exist, and since displayId is unique, those creates fail. Using generateTicketId() here too would remove the counter altogether, which is the same fix #307 proposes for the Linear importer.

One failed message stops every ticket after it. In migrateTickets, the ticket is created first and its messages are written one by one afterwards. If a message write throws, the catch block counts the ticket as failed, but counter++ at line 294 never runs. The ticket itself was already saved with that display ID, so the next ticket tries the same ID, fails on the unique constraint, and also skips the increment. Every remaining ticket in the run fails the same way. A rerun does not repair it either: the duplicate check finds the saved ticket by sourceId and skips it, so its missing messages never arrive. Writing each ticket and its messages in one prisma.$transaction keeps them together.

A mistyped flag runs a real migration. parseArgs has no default case, so an unknown argument is ignored. Someone who types --dryrun instead of --dry-run gets a full migration that writes to the database. Exiting with the usage text on any unknown flag would catch that before anything is written.

Rollback leaves the accounts behind. rollback deletes the Orca messages, tickets and users, but not the accounts the migration created. The Account model has no source column, and migrateAccounts reuses an existing account when the name and domain match, so the script has no way to tell its own accounts from ones that were already there. Recording the IDs of the accounts it creates, or adding a source to Account, would let rollback remove exactly what the migration added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions