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

Prepare to migrate to the new versioning system #65

Merged
merged 10 commits into from
Feb 1, 2023
20 changes: 18 additions & 2 deletions scripts/database/create_names_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,26 @@

CREATE TABLE names (
name VARCHAR(128) NOT NULL PRIMARY KEY,
pointer UUID NOT NULL REFERENCES packages(pointer),
pointer UUID NULL,
-- constraints
CONSTRAINT lowercase_names CHECK (name = LOWER(name))
CONSTRAINT lowercase_names CHECK (name = LOWER(name)),
CONSTRAINT package_names_fkey FOREIGN KEY (pointer) REFERENCES packages(pointer) ON DELETE SET NULL
);

-- Lowercase constraint added upon the following issue:
-- https://github.com/confused-Techie/atom-backend/issues/90

/*
-- `pointer` was NOT NULL, then we made it nullable.
-- The previous foreign key has been dropped and a new `package_names_fkey`
-- has need added to avoid supply chain attacks.
-- `pointer` is set to NULL when a row in packages table is deleted.
-- Steps made to apply this change:

ALTER TABLE names ALTER COLUMN pointer DROP NOT NULL;

ALTER TABLE names DROP CONSTRAINT previous_foreign_key_name;

ALTER TABLE names
ADD CONSTRAINT package_names_fkey FOREIGN KEY (pointer) REFERENCES packages(pointer) ON DELETE SET NULL;
*/
14 changes: 13 additions & 1 deletion scripts/database/create_versions_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ CREATE TABLE versions (
semver VARCHAR(256) NOT NULL,
license VARCHAR(128) NOT NULL,
engine JSONB NOT NULL,
created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
meta JSONB,
-- generated columns
semver_v1 INTEGER GENERATED ALWAYS AS
Expand All @@ -21,5 +23,15 @@ CREATE TABLE versions (
(CAST ((regexp_match(semver, '^(\d+)\.(\d+)\.(\d+)'))[3] AS INTEGER)) STORED,
-- constraints
CONSTRAINT semver2_format CHECK (semver ~ '^\d+\.\d+\.\d+'),
CONSTRAINT unique_pack_version UNIQUE(package, semver_v1, semver_v2, semver_v3)
CONSTRAINT unique_pack_version UNIQUE(package, semver)
);

-- Create a function and a trigger to set the current timestamp
-- in the `updated` column of the updated row.
-- The function now_on_updated_package() is the same defined in
-- the script for the `packages` table.

CREATE TRIGGER trigger_now_on_updated_versions
BEFORE UPDATE ON versions
FOR EACH ROW
EXECUTE PROCEDURE now_on_updated_package();
86 changes: 0 additions & 86 deletions scripts/tools/duplicateVersions.js

This file was deleted.

Loading