Skip to content

Commit

Permalink
chore: add not null constraint on files.type
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Nov 15, 2023
1 parent df4d554 commit 63f17b6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
31 changes: 31 additions & 0 deletions apps/backend/db/migrations/20231115210334_file-type-not-null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @param {import('knex').Knex} knex
*/
export const up = async (knex) => {
await knex.raw(`
ALTER TABLE files ADD CONSTRAINT files_type_not_null_constraint CHECK (type IS NOT NULL) NOT VALID;
`);

await knex.raw(`
ALTER TABLE files VALIDATE CONSTRAINT files_type_not_null_constraint;
`);

await knex.raw(`
ALTER TABLE files ALTER column type SET NOT NULL;
`);

await knex.raw(`
ALTER TABLE files DROP CONSTRAINT files_type_not_null_constraint;
`);
};

/**
* @param {import('knex').Knex} knex
*/
export const down = async (knex) => {
await knex.raw(`
ALTER TABLE files ALTER column type DROP NOT NULL;
`);
};

export const config = { transaction: false };
5 changes: 3 additions & 2 deletions apps/backend/db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ CREATE TABLE public.files (
key character varying(255) NOT NULL,
width integer,
height integer,
type text,
type text NOT NULL,
CONSTRAINT files_type_check CHECK ((type = ANY (ARRAY['screenshot'::text, 'screenshotDiff'::text, 'playwrightTrace'::text])))
);

Expand Down Expand Up @@ -2757,4 +2757,5 @@ INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('2023102
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20231024085955_fix-github-account-type-constraint.js', 1, NOW());
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20231106114751_pw-traces.js', 1, NOW());
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20231115151718_file-type.js', 1, NOW());
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20231115160027_playwrightTraceIndex.js', 1, NOW());
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20231115160027_playwrightTraceIndex.js', 1, NOW());
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20231115210334_file-type-not-null.js', 1, NOW());
6 changes: 3 additions & 3 deletions apps/backend/src/database/models/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ export class File extends Model {
static override tableName = "files";

static override jsonSchema = mergeSchemas(timestampsSchema, {
required: ["key"],
required: ["key", "type"],
properties: {
key: { type: ["string"] },
width: { type: ["number", "null"], minimum: 0 },
height: { type: ["number", "null"], minimum: 0 },
type: {
type: ["string", "null"],
type: "string",
enum: ["screenshot", "screenshotDiff", "playwrightTrace"],
},
},
Expand All @@ -20,5 +20,5 @@ export class File extends Model {
key!: string;
width!: number | null;
height!: number | null;
type!: "screenshot" | "screenshotDiff" | "playwrightTrace" | null;
type!: "screenshot" | "screenshotDiff" | "playwrightTrace";
}

0 comments on commit 63f17b6

Please sign in to comment.