Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(electric): Reject tables that have columns with DEFAULT expressions #509

Merged
merged 8 commits into from
Oct 16, 2023

Conversation

alco
Copy link
Member

@alco alco commented Sep 29, 2023

Fixes VAX-1100.

  • Add tests
  • Update the type-checking function to use %I
  • Create a follow-up issue in Linear.

@linear
Copy link

linear bot commented Sep 29, 2023

VAX-1100 Electrifying a table with `DEFAULT gen_random_uuid()` yields a broken default "gen_random_uuid" string in SQLite

Reported in Discord: https://discord.com/channels/933657521581858818/1079688869852753981/1156299210400989194

TLDR: The table below electrifies without problems:

CREATE TABLE "public"."DataTable" (
    "id" uuid NOT NULL DEFAULT gen_random_uuid(),
    "name" text NOT NULL,
    "created_at" float8 NOT NULL,
    "electric_user_id" text NOT NULL,
    PRIMARY KEY ("id")
);

However, the default values gets translated to DEFAULT 'gen_random_uuid' in the corresponding SQLite schema. When a client write omits the id field, it gets the default value gen_random_uuid which then fails server-side validation.

Proposed fix: check for DEFAULT clauses in the electrify() function and

  • reject such tables
  • or erase the DEFAULT part when translating the Postgres schema to SQLite

Postgres has a vastly larger set of built-in functions and expressions compared to SQLite. We cannot possibly allow electrification of tables with arbitrary DEFAULT expressions.

@alco alco force-pushed the alco/vax-1100-check-for-default-column-clause branch 3 times, most recently from ff0c722 to 1a93b31 Compare September 30, 2023 20:43
@gorbak25
Copy link

gorbak25 commented Oct 1, 2023

@alco I think this PR might be too strict. I think a better approach would be to remove default expressions from sqlite and keep them in progress. The prisma client can ensure those fields with default expressions are not optional on the client side.

@alco
Copy link
Member Author

alco commented Oct 2, 2023

@gorbak25 I agree that it's strict. But it's aimed at fixing a very particular problem.

Whether we skip DEFAULT clauses from the client schema or add support for limited expressions there, both require some planning and development time. In the time window between now and when we release a version with a proper fix, people who electrify their tables containing DEFAULT clauses will get broken server sync when trying out Electric.

We would prefer to make it known upfront that a given table cannot be electrified and relax this limitation later.

@alco alco marked this pull request as ready for review October 2, 2023 12:59
@alco alco requested review from magnetised and icehaunter October 2, 2023 12:59
@alco alco force-pushed the alco/vax-1100-check-for-default-column-clause branch from 1a93b31 to d7217db Compare October 16, 2023 09:54
Copy link
Contributor

@magnetised magnetised left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good apart from function->procedure switch

@alco alco merged commit aa7c265 into main Oct 16, 2023
4 checks passed
@alco alco deleted the alco/vax-1100-check-for-default-column-clause branch October 16, 2023 12:45
@barbalex
Copy link

barbalex commented Dec 1, 2023

I just stumbled on to this, getting an error when trying to electrify a table.
It seems that this is not documented anywhere nor whether this will be possible in the future.

@thruflo
Copy link
Contributor

thruflo commented Dec 1, 2023

Yes, this should be documented. I've raised an issue [VAX-1405].

It will be relaxed in future. I don't have visibility on timeframe though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants