Skip to content

Commit

Permalink
feat: add build metadata
Browse files Browse the repository at this point in the history
Also add notion of valid bucket. A bucket that can be used as baseline.
  • Loading branch information
gregberge committed Jul 7, 2024
1 parent 1564a8c commit 600ff0e
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 32 deletions.
55 changes: 55 additions & 0 deletions apps/backend/db/migrations/20240707121753_build-metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @param {import('knex').Knex} knex
*/
export const up = async (knex) => {
await knex.schema.alterTable("builds", async (table) => {
table.jsonb("metadata");
});

await knex.schema.alterTable("build_shards", async (table) => {
table.jsonb("metadata");
});

await knex.schema.alterTable("screenshot_buckets", (table) => {
table.boolean("valid");
});

await knex.raw(
`UPDATE screenshot_buckets SET valid = true WHERE valid IS NULL`,
);

await knex.raw(`
ALTER TABLE screenshot_buckets ADD CONSTRAINT screenshot_buckets_valid_not_null_constraint CHECK (valid IS NOT NULL) NOT VALID;
`);

await knex.raw(`
ALTER TABLE screenshot_buckets VALIDATE CONSTRAINT screenshot_buckets_valid_not_null_constraint;
`);

await knex.raw(`
ALTER TABLE screenshot_buckets ALTER column valid SET NOT NULL;
`);

await knex.raw(`
ALTER TABLE screenshot_buckets DROP CONSTRAINT screenshot_buckets_valid_not_null_constraint;
`);
};

/**
* @param {import('knex').Knex} knex
*/
export const down = async (knex) => {
await knex.schema.alterTable("builds", async (table) => {
table.dropColumn("metadata");
});

await knex.schema.alterTable("build_shards", async (table) => {
table.dropColumn("metadata");
});

await knex.schema.alterTable("screenshot_buckets", async (table) => {
table.dropColumn("valid");
});
};

export const config = { transaction: false };
1 change: 1 addition & 0 deletions apps/backend/db/seeds/seeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export const seed = async (knex) => {
createdAt: "2016-12-08T22:59:55Z",
updatedAt: "2016-12-08T22:59:55Z",
complete: true,
valid: true,
screenshotCount: 0,
};

Expand Down
8 changes: 6 additions & 2 deletions apps/backend/db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ CREATE TABLE public.build_shards (
"createdAt" timestamp with time zone NOT NULL,
"updatedAt" timestamp with time zone NOT NULL,
"buildId" bigint NOT NULL,
index integer
index integer,
metadata jsonb
);


Expand Down Expand Up @@ -221,6 +222,7 @@ CREATE TABLE public.builds (
"runId" character varying(255),
"runAttempt" integer,
partial boolean DEFAULT false NOT NULL,
metadata jsonb,
CONSTRAINT builds_mode_check CHECK ((mode = ANY (ARRAY['ci'::text, 'monitoring'::text]))),
CONSTRAINT builds_type_check CHECK ((type = ANY (ARRAY['reference'::text, 'check'::text, 'orphan'::text])))
);
Expand Down Expand Up @@ -930,6 +932,7 @@ CREATE TABLE public.screenshot_buckets (
"projectId" bigint NOT NULL,
"screenshotCount" integer,
mode text DEFAULT 'ci'::text NOT NULL,
valid boolean NOT NULL,
CONSTRAINT chk_complete_true_screenshotcount_not_null CHECK (((complete = false) OR ("screenshotCount" IS NOT NULL))),
CONSTRAINT screenshot_buckets_mode_check CHECK ((mode = ANY (ARRAY['ci'::text, 'monitoring'::text])))
);
Expand Down Expand Up @@ -2809,4 +2812,5 @@ INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('2024060
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20240614204320_build_shards.js', 1, NOW());
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20240616142430_build_shards_indices.js', 1, NOW());
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20240630151704_screenshot-threshold.js', 1, NOW());
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20240706121810_screenshot-base-name.js', 1, NOW());
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20240706121810_screenshot-base-name.js', 1, NOW());
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20240707121753_build-metadata.js', 1, NOW());
1 change: 1 addition & 0 deletions apps/backend/src/build/strategy/strategies/ci/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function queryBaseBucket(build: Build) {
projectId: build.projectId,
name: build.name,
complete: true,
valid: true,
mode: build.mode,
});
}
6 changes: 6 additions & 0 deletions apps/backend/src/database/models/Build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { z } from "zod";

import config from "@/config/index.js";

import {
BuildMetadata,
BuildMetadataJsonSchema,
} from "../services/buildMetadata.js";
import { Model } from "../util/model.js";
import {
jobModelSchema,
Expand Down Expand Up @@ -92,6 +96,7 @@ export class Build extends Model {
runId: { type: ["string", "null"] },
runAttempt: { type: ["integer", "null"] },
partial: { type: ["boolean", "null"] },
metadata: BuildMetadataJsonSchema,
},
});

Expand All @@ -116,6 +121,7 @@ export class Build extends Model {
runId!: string | null;
runAttempt!: number | null;
partial!: boolean | null;
metadata!: BuildMetadata | null;

static override get relationMappings(): RelationMappings {
return {
Expand Down
6 changes: 6 additions & 0 deletions apps/backend/src/database/models/BuildShard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { RelationMappings } from "objection";

import {
BuildMetadata,
BuildMetadataJsonSchema,
} from "../services/buildMetadata.js";
import { Model } from "../util/model.js";
import { mergeSchemas, timestampsSchema } from "../util/schemas.js";
import { Build } from "./Build.js";
Expand All @@ -13,11 +17,13 @@ export class BuildShard extends Model {
properties: {
buildId: { type: "string" },
index: { type: ["integer", "null"] },
metadata: BuildMetadataJsonSchema,
},
});

buildId!: string;
index!: number | null;
metadata!: BuildMetadata | null;

static override get relationMappings(): RelationMappings {
return {
Expand Down
26 changes: 15 additions & 11 deletions apps/backend/src/database/models/Screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ export type ScreenshotMetadata = {

export const ScreenshotMetadataJsonSchema = {
type: ["object", "null"],
required: ["sdk", "automationLibrary"],
additionalProperties: false,
properties: {
url: {
type: "string",
},
viewport: {
type: "object",
required: ["width", "height"],
additionalProperties: false,
properties: {
width: {
type: "integer",
Expand All @@ -60,7 +64,6 @@ export const ScreenshotMetadataJsonSchema = {
minimum: 0,
},
},
required: ["width", "height"],
},
colorScheme: {
type: "string",
Expand All @@ -74,6 +77,8 @@ export const ScreenshotMetadataJsonSchema = {
oneOf: [
{
type: "object",
required: ["title", "titlePath"],
additionalProperties: false,
properties: {
id: {
type: "string",
Expand Down Expand Up @@ -101,6 +106,8 @@ export const ScreenshotMetadataJsonSchema = {
},
location: {
type: "object",
required: ["file", "line", "column"],
additionalProperties: false,
properties: {
file: {
type: "string",
Expand All @@ -114,18 +121,16 @@ export const ScreenshotMetadataJsonSchema = {
minimum: 0,
},
},
required: ["file", "line", "column"],
},
},
required: ["title", "titlePath"],
},
{
type: "null",
},
{ type: "null" },
],
},
browser: {
type: "object",
required: ["name", "version"],
additionalProperties: false,
properties: {
name: {
type: "string",
Expand All @@ -134,10 +139,11 @@ export const ScreenshotMetadataJsonSchema = {
type: "string",
},
},
required: ["name", "version"],
},
automationLibrary: {
type: "object",
required: ["name", "version"],
additionalProperties: false,
properties: {
name: {
type: "string",
Expand All @@ -146,10 +152,11 @@ export const ScreenshotMetadataJsonSchema = {
type: "string",
},
},
required: ["name", "version"],
},
sdk: {
type: "object",
required: ["name", "version"],
additionalProperties: false,
properties: {
name: {
type: "string",
Expand All @@ -158,11 +165,8 @@ export const ScreenshotMetadataJsonSchema = {
type: "string",
},
},
required: ["name", "version"],
},
},
required: ["sdk", "automationLibrary"],
additionalProperties: false,
};

export class Screenshot extends Model {
Expand Down
2 changes: 2 additions & 0 deletions apps/backend/src/database/models/ScreenshotBucket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const baseData = {
name: "878",
branch: "BUGS-130",
commit: "ff4474843ccab36e72814e321cbf6ab6a6303385",
valid: true,
complete: true,
};

describe("ScreenshotBucket", () => {
Expand Down
29 changes: 28 additions & 1 deletion apps/backend/src/database/models/ScreenshotBucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class ScreenshotBucket extends Model {
static override tableName = "screenshot_buckets";

static override jsonSchema = mergeSchemas(timestampsSchema, {
required: ["name", "commit", "branch"],
required: ["name", "commit", "branch", "valid"],
properties: {
name: { type: "string" },
complete: { type: "boolean" },
Expand All @@ -24,16 +24,43 @@ export class ScreenshotBucket extends Model {
projectId: { type: "string" },
screenshotCount: { type: "integer" },
mode: { type: "string", enum: ["ci", "monitoring"] },
valid: { type: "boolean" },
},
});

/**
* The name of the screenshot bucket, identic to the build name.
*/
name!: string;
/**
* True if screenshots from all shards have been uploaded.
*/
complete!: boolean;
/**
* The commit hash of the build.
*/
commit!: string;
/**
* The branch of the build.
*/
branch!: string;
/**
* The project ID of the build.
*/
projectId!: string;
/**
* The number of screenshots in the bucket.
*/
screenshotCount!: number;
/**
* The mode of the build.
*/
mode!: BuildMode;
/**
* True if the bucket is valid.
* A bucket is considered valid if it's created from a "passed" test suite.
*/
valid!: boolean;

static override get relationMappings(): RelationMappings {
return {
Expand Down
Loading

0 comments on commit 600ff0e

Please sign in to comment.