DEV Community

Hasnan Khan
Hasnan Khan

Posted on

Prove a CRM migration with Postgres views and one Next.js page

This article was drafted with AI help and reviewed by the author.

The import finished. HubSpot shows a green "completed" banner. Someone asks "did everything come across?" and the honest answer is "probably".

"Probably" isn't good enough when the next step is switching off the old CRM. Once it's gone, a missing deal or a broken company link has no source to recover from.

This post describes a small check you can rerun as often as you like. It has three parts:

  1. Two tables in Supabase Postgres. One holds the old CRM export. The other holds what HubSpot has right now.
  2. Five views that compare them: counts, missing records, owners, open pipeline and company links.
  3. One server-rendered Next.js page that shows the views to the people who have to sign off.

It's a developer's version of the validation step, built from tools many web teams already run. It doesn't replace field-by-field samples checked by the people who use the data.

The comparison examples below were built from a small set of planted sample records, used only to check the logic. They have not been run against a live HubSpot account or a hosted Supabase project.

The one thing the migration must have done

All of this depends on a legacy ID. Every migrated record in HubSpot needs a property that holds the old system's record ID. A typical name is "legacycrmid". Without it, you're matching on names and emails, and the comparison turns into guesswork.

If your migration didn't set one, add it before you do anything else. It's also what makes reimports and fixes safe later.

Two tables

The first table is the old CRM export. Load it once, then treat it as read-only. It's your evidence of what existed. A CSV import in the Supabase table editor is enough. Each row needs:

  • Which object it is (company, contact or deal)
  • Legacy ID
  • The owner's email from the old system
  • For deals, the old stage name, the amount, and whether the deal was open
  • For contacts and deals, the parent company's legacy ID

The second table is what HubSpot holds. A pull script refills it every time you want a fresh comparison. Each row needs:

  • Object type
  • HubSpot record ID
  • The legacy ID property, which can be missing
  • The HubSpot owner ID
  • For deals, the HubSpot stage and amount
  • The associated company IDs, when the list endpoint returns them
  • The time this pull wrote the row

Two small mapping tables sit beside them. They are your mapping sheet in table form. One maps an old owner email to a HubSpot owner ID. The other maps an old deal stage name to a HubSpot stage. If the migration decided that "Negotiation" becomes "contractsent", that decision lives here, and the views use it.

Pull HubSpot into Postgres

HubSpot's list endpoint for the 2026-09 API pages through every record of a type. A few details from the docs shape the pull:

  • Each page can ask for up to 100 records. You follow the next-page cursor until it's gone.
  • You choose which properties come back. Ask only for what the views need, such as the legacy ID, the owner, and for deals the stage and amount.
  • Asking for company associations returns associated company IDs inline with each contact or deal.
  • The CRM search endpoint looks tempting, but HubSpot caps any search query at 10,000 total results and five requests per second per account. Listing doesn't have that 10,000 cap, so it's the safer choice for a full copy.

The pull itself goes through companies first, then contacts, then deals. For every page it upserts the rows into the HubSpot table. A few choices are worth explaining.

Upsert, then delete what wasn't seen. Every row from this run gets the same pulled-at timestamp. After a complete pass, rows with an older timestamp belong to records that were deleted or archived in HubSpot since the last pull, so they go. If the run fails halfway, the delete never happens and you keep the old snapshot.

Daily 429 stops the run. HubSpot's 429 body includes a policy name. DAILY means you're done until the limit resets, so there's no point retrying. Other 429s get a 10-second wait, which matches HubSpot's 10-second burst window. Server errors get a short backoff and a handful of retries. Any other client error should fail the run, because retrying a bad request won't change it.

Server-side only. The script uses a HubSpot token and the Supabase secret key. Neither belongs in a browser bundle or a mobile app. Run it from your machine, a CI job, or a scheduled function.

The legacy ID can be missing. That's deliberate. A HubSpot record with no legacy ID is either something created after go-live or a migration bug. Both are worth seeing.

A worked example of a partial pull. The contacts pass starts at 10:00 and writes every contact with the timestamp 10:00. At 10:07 the job hits a daily 429 and stops, halfway through the contact pages. The cleanup that would delete older contact rows does not run. The table still holds the previous complete contact snapshot, plus the pages that were written at 10:00. You can trust yesterday's complete picture, and you can run the pull again once the daily limit resets. You should not sign off on a half-finished refresh.

Five views

Plain SQL views keep the logic in one place. The page just reads them. Anyone with database access can query them too.

To show what each view answers, the rest of this section uses a planted sample. These records are examples, not a real migration.

The source export has three contacts and two deals:

  • Contact C-101, Ana Ruiz, owner sam@example.com, company CO-10
  • Contact C-102, Ben Cole, owner sam@example.com, company CO-10
  • Contact C-103, Priya Shah, owner lea@example.com, company CO-10
  • Deal D-40, open, old stage "Proposal", amount 12,000, company CO-10
  • Deal D-41, open, old stage "Negotiation", amount 8,500, company CO-10

The owner map has one row: sam@example.com maps to HubSpot owner 812. Lea has left, so lea@example.com has no mapping.

The stage map has one row: "Proposal" maps to "contractsent". "Negotiation" was forgotten.

After a pull, HubSpot has:

  • Company CO-10, HubSpot ID 22088
  • Contact C-101, linked to company 22088, owner 812
  • Contact C-103, no company link, owner 812
  • A new contact with HubSpot ID 99001 and no legacy ID
  • Deal D-40, stage "contractsent", amount 11,500, which does not match the source amount
  • Deal D-41 is missing

Contact C-102 never arrived.

Counts. For each object type the view reports how many records the source has, how many HubSpot has, how many match by legacy ID, how many are missing in HubSpot, and how many exist only in HubSpot. A full join is what makes "only in HubSpot" visible. An inner join would quietly hide it.

On the planted sample, contacts would read: 3 in the source, 3 in HubSpot, 2 matched (C-101 and C-103), 1 missing in HubSpot (C-102), and 1 only in HubSpot (record 99001). Deals would read: 2 in the source, 1 in HubSpot, 1 matched (D-40), 1 missing in HubSpot (D-41), and none only in HubSpot.

Gaps. The actual rows behind those numbers. A count of "1 missing" isn't useful until someone can look at which one. On the sample, the gaps list would show:

  • C-102, missing in HubSpot
  • D-41, missing in HubSpot
  • HubSpot record 99001, no legacy ID

Owners. Records per owner, after translating old owners through the owner map. An old owner with no mapping shows up as unmapped, plus their email. That's usually someone who left, and their records need a holding owner. The view only returns rows where the counts differ, so an empty result is good news.

On the sample, Priya's source owner is lea@example.com, which has no mapping, so she is counted under an unmapped owner. In HubSpot she sits under owner 812. The view shows that difference. Sam's other contacts may also be off by one, because C-102 is missing and Priya was assigned to the wrong owner.

Open pipeline. Open deal count and value per stage, with old stages mapped to new ones. The HubSpot side only counts deals that were open in the source. That stops deals created after go-live from muddying the totals. An unmapped stage means a stage the mapping forgot.

On the sample, "Proposal" becomes "contractsent". The source has one such deal, D-40, worth 12,000. HubSpot has the same deal in that stage, worth 11,500. The view shows one deal on each side and an amount that doesn't match. "Negotiation" has no mapping, so D-41 would have appeared under an unmapped stage on the source side, and under nothing on the HubSpot side because the deal is missing.

Company links. Contacts whose source company didn't end up associated in HubSpot. This catches the common failure where contacts were imported before their companies, or the association step was skipped for part of a file.

On the sample, C-101 is fine: its expected company is 22088, and that ID is in the contact's company list. C-103 should also point at CO-10, which is HubSpot company 22088, but the contact has no company IDs. C-103 appears on this view. C-102 does not, because the contact itself is missing and already shows up in the gaps list.

Each of those differences was planted on purpose to check the comparison. In a real run, you would fix or explain every row before sign-off.

One page

The page is a server component. It reads the five views with the Supabase secret key on the server and renders a plain report.

Why this shape:

  • Render on every request. The page calls the Next.js connection helper so nothing is served from a cache between a fix and the next check. That also works with Cache Components turned on, where the older dynamic segment config isn't allowed.
  • No client JavaScript needed. It's a report, not an app. Plain HTML tables print well and paste into a sign-off email.
  • The banner is careful. Even when every view is empty, it says samples still need a person. Matching counts don't prove that field values are right.

On the planted sample the banner would say that differences were found, and each section would list the rows above. After you reimport C-102 and D-41, map "Negotiation", assign Lea's contacts to a holding owner, correct D-40's amount, link C-103 to its company, and decide what to do with HubSpot record 99001, a later refresh should show empty sections. The banner would then say that no differences were found in these checks, and that field samples still need a person.

Put the route behind your normal auth, or deploy it somewhere only the migration team can reach. That key skips row level security, so the page must not be public.

A rerun loop

In practice the check runs many times:

  1. Run the pull script.
  2. Open the migration page.
  3. Fix what it shows. Reimport missing rows, map the forgotten stage, assign the orphaned owner, repair links.
  4. Repeat until every section is empty or every remaining difference has a written reason.

Then take the last screenshot, or export the views, as the evidence for sign-off.

Limitations

  • Inline associations on the list endpoint suit normal records. A company with thousands of contacts is better checked through HubSpot's associations batch read endpoint.
  • The views compare IDs, owners, stages and amounts. They don't compare every field. Add columns for the fields your team cares about, and keep human samples for the rest.
  • Amounts are compared as stored. If the migration converted currency, compare in one currency.
  • Activity history (notes, calls, emails) isn't covered. Counting them per record is possible with the same pattern, but it's a much bigger pull.
  • A pull uses API calls from the account's shared daily limit. Run it off-peak if live integrations share the portal.

Takeaways

  • Keep the old export in Postgres as read-only evidence, and pull HubSpot into a second table.
  • Match on a legacy ID property. Without it, reconciliation becomes guesswork.
  • Use the list endpoint for full copies. Search stops at 10,000 results per query.
  • Let views that join both sides show gaps, and turn counts into the rows behind them.
  • Keep the page server-rendered and private. It holds a secret key.
  • Treat an empty report as "ready for human samples", not "done".

Reconciliation is the last step of a longer process. For the earlier parts, from deciding what to migrate to import order and rebuilding automation, there's a HubSpot migration playbook that lays out each stage.

References

Top comments (0)