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

model: Add UUID to Application model #491

Merged
1 commit merged into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/balena.sbvr
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,10 @@ Fact type: application has application type
Necessity: each application has exactly one application type.
Fact type: application is host
Fact type: application is archived

Fact type: application has uuid
Necessity: each application has exactly one uuid.
Necessity: each uuid is of exactly one application.
Necessity: each application has a uuid that has a Length (Type) that is equal to 32.

-- service instance

Expand Down
25 changes: 25 additions & 0 deletions src/features/applications/hooks/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { hooks, errors } from '@balena/pinejs';
import * as uuid from 'uuid';
import { DefaultApplicationType } from '../../application-types/application-types';

// reconstitute the value into a properly formatted UUID...
const toUuid = (strippedUuid: string): string => {
if (strippedUuid.length !== 32) {
return '';
}

const parts: string[] = [];
parts.push(strippedUuid.substr(0, 8));
parts.push(strippedUuid.substr(8, 4));
parts.push(strippedUuid.substr(12, 4));
parts.push(strippedUuid.substr(16, 4));
parts.push(strippedUuid.substr(20, 12));
return parts.join('-');
};

hooks.addPureHook('POST', 'resin', 'application', {
POSTPARSE: async (args) => {
const { request } = args;
Expand All @@ -14,6 +30,15 @@ hooks.addPureHook('POST', 'resin', 'application', {
);
}

request.values.uuid = request.values.uuid ?? uuid.v4().replace(/-/g, '');

const appUuid = toUuid(request.values.uuid);
if (!uuid.validate(appUuid) || uuid.version(appUuid) !== 4) {
throw new errors.BadRequestError(
'Application UUID must be a 32 character long lower case UUID version 4.',
);
}

request.values.should_track_latest_release = true;
request.values.slug ??= appName.toLowerCase();
},
Expand Down
11 changes: 11 additions & 0 deletions src/migrations/00033-add-application-uuid.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- load the UUID functions...
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- migrate to add a new UUID value to the application model...
ALTER TABLE "application"
ADD COLUMN IF NOT EXISTS "uuid" TEXT NOT NULL DEFAULT REPLACE(CAST(UUID_GENERATE_V4() AS TEXT), '-', ''),
ADD CONSTRAINT "application_uuid_key" UNIQUE ("uuid");

-- remove the default
ALTER TABLE "application"
ALTER COLUMN "uuid" DROP DEFAULT;