electric-sql / electric

Sync little subsets of your Postgres data into local apps and services.
https://electric-sql.com
Apache License 2.0
6.12k stars 143 forks source link

Electric fails to generate migrations on tables with CHECK constraints #530

Closed gorbak25 closed 7 months ago

gorbak25 commented 11 months ago

In my quest to workaround #524 I added a check constraint manually:

CREATE TABLE "DataTable" (
    "id" UUID NOT NULL,
    "emoji" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "created_at" DOUBLE PRECISION NOT NULL,
    "electric_user_id" TEXT NOT NULL,

    CONSTRAINT "DataTable_pkey" PRIMARY KEY ("id"),
    CONSTRAINT "DataTable_id_is_uuidv7" CHECK (public.get_uuid_version(id) = 7)
);

Electric properly electrified the table and everything works(when using old client migrations). The problem is when one tries to regenerate the migrations:

node@920e676ab3fc ~/workspace (uuidv7) [1]> yarn electric-sql:gen
yarn run v1.22.19
$ electric-sql generate --service electric:5133 --out src/generated/client && echo "// @ts-nocheck
$(cat src/generated/client/index.ts)" > src/generated/client/index.ts
generate command failed: SqliteError: no such function: get_uuid_version
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
node@920e676ab3fc ~/workspace (uuidv7) [1]> 

When looking at the .electric_migrations_tmp folder it looks like electric thinks the CHECK constraint is a Foreign key constraint:

CREATE TABLE "DataTable" (
  "id" TEXT NOT NULL,
  "emoji" TEXT NOT NULL,
  "name" TEXT NOT NULL,
  "created_at" REAL NOT NULL,
  "electric_user_id" TEXT NOT NULL,
  CONSTRAINT "DataTable_id_is_uuidv7" CHECK ((get_uuid_version("id") = 7)),
  CONSTRAINT "DataTable_pkey" PRIMARY KEY ("id")
) WITHOUT ROWID;

Similar to #509 electric should either filter out the constraints or forbid electrifying tables with those constraints.

balegas commented 11 months ago

Yeah, CHECK constraints are very limited for the time being. We're holding increasing the validation surface of the Electrify function until the proxy work is fully integrated. We opened an internal issue to track this.

alco commented 7 months ago

Electric rejects electrification of tables that have unsupported constraints since version v0.9.