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

Update postgres table to use identity instead of serial #556

Open
mfridman opened this issue Jul 6, 2023 · 0 comments · May be fixed by #694
Open

Update postgres table to use identity instead of serial #556

mfridman opened this issue Jul 6, 2023 · 0 comments · May be fixed by #694

Comments

@mfridman
Copy link
Collaborator

mfridman commented Jul 6, 2023

In Postgres versions 10 and above, the recommendation is to use identity instead of serial,

https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_serial

We should update the code to create new tables with:

CREATE TABLE goose_db_version (
-       id serial NOT NULL,
+	id int GENERATED ALWAYS AS IDENTITY,
	version_id bigint NOT NULL,
	is_applied boolean NOT NULL,
	tstamp timestamp NULL DEFAULT now(),
	PRIMARY KEY (id)
);

And publish a blog post / update release notes on how users can update existing goose tables.

Note, we're using GENERATED ALWAYS instead of GENERATED BY DEFAULT because these sequence ids should not be manipulated by end-users.

Although I could see an argument for making this GENERATED BY DEFAULT as an escape hatch. But I can't think of a scenario where the id needs to be manually changed, so GENERATED ALWAYS seems like the most sane solution.

EDIT: after a bit of thought, settled on GENERATED BY DEFAULT, as this avoids locking users.

@mfridman mfridman changed the title Update new postgres table to use identity instead of serial Update postgres table to use identity instead of serial Jul 6, 2023
@mfridman mfridman linked a pull request Feb 3, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant