DatriseETL com IA

Gerador de upsert

Upsert no PlanetScale: INSERT … ON DUPLICATE KEY UPDATE

Nomeie tabela, chave e colunas e receba a instrução que o PlanetScale realmente aceita, com uma guarda para que uma linha antiga nunca sobrescreva uma nova. Abaixo: como a mecânica funciona, o que quebra e como a Datrise carrega o PlanetScale de forma incremental.

As notas técnicas desta página estão em inglês.

Gere a instrução

A URL é atualizada enquanto você digita; compartilhe para entregar o formulário exato.

Instrução · INSERT … ON DUPLICATE KEY UPDATE

INSERT INTO `deals` (`id`, `name`, `stage`, `amount`, `owner_id`, `updated_at`)
SELECT s.`id`, s.`name`, s.`stage`, s.`amount`, s.`owner_id`, s.`updated_at`
FROM `deals_staging` AS s
ON DUPLICATE KEY UPDATE
  `name` = IF(`deals`.`updated_at` < s.`updated_at`, s.`name`, `deals`.`name`),
  `stage` = IF(`deals`.`updated_at` < s.`updated_at`, s.`stage`, `deals`.`stage`),
  `amount` = IF(`deals`.`updated_at` < s.`updated_at`, s.`amount`, `deals`.`amount`),
  `owner_id` = IF(`deals`.`updated_at` < s.`updated_at`, s.`owner_id`, `deals`.`owner_id`),
  `updated_at` = IF(`deals`.`updated_at` < s.`updated_at`, s.`updated_at`, `deals`.`updated_at`);

Como o upsert funciona no PlanetScale

PlanetScale speaks MySQL over Vitess, so INSERT … ON DUPLICATE KEY UPDATE runs as written, including the IF() watermark guard. On a sharded keyspace the statement must be routable: the unique key you upsert on should be, or include, the sharding key, otherwise Vitess has to scatter the write across shards and may reject it.

Foreign key constraints are disabled by default, so relationships between synced entities are modelled with stable id columns rather than enforced FKs; the upsert does not need them. Schema changes go through deploy requests, which means the staging table should be created once and reused, not created and dropped per batch.

Antes de executar

  • "Duplicate" means any PRIMARY KEY or UNIQUE index, not just the one you think of as the business key. A second unique index on email will silently merge rows you meant to keep apart.
  • The IF() guard keeps the existing row when the incoming updated-at is older. Without it the last write wins, whatever its timestamp.
  • Avoid REPLACE INTO for upserts: it deletes and re-inserts, which fires DELETE triggers, cascades foreign keys and burns auto-increment values.

Perguntas frequentes

Does ON DUPLICATE KEY UPDATE work on sharded tables?

Yes, when the key includes the sharding column so the row maps to one shard. Cross-shard upserts on a secondary unique index are the case to avoid.

Can I use foreign keys to keep deals and contacts consistent?

Not by default. Land both entities with stable ids and join on them; referential integrity is checked at read time or in dbt tests, not by the database.

Why keep a permanent staging table?

Because DDL is a deploy request on PlanetScale. TRUNCATE the staging table between batches instead of recreating it.

O mesmo gerador para outros destinos

Pule a escrita do merge

A Datrise entrega entidades de CRM e SaaS no PlanetScale com exatamente esta mecânica, uma marca d'água sobre updated-at e colunas tipadas, então a instrução acima é a que roda por você. Entre na lista de espera para acesso antecipado.

Explorar o catálogo de integrações