From 92968c49ddd5232d7fbbcd242425535632a8f60b Mon Sep 17 00:00:00 2001 From: wangsijie Date: Tue, 17 Jan 2023 18:04:49 +0800 Subject: [PATCH] fix(schemas): set scope description to required (#2954) --- .../src/tests/api/resource.scope.test.ts | 3 ++- ...xt-1673940577-scope-description-not-null.ts | 18 ++++++++++++++++++ packages/schemas/tables/scopes.sql | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 packages/schemas/alterations/next-1673940577-scope-description-not-null.ts diff --git a/packages/integration-tests/src/tests/api/resource.scope.test.ts b/packages/integration-tests/src/tests/api/resource.scope.test.ts index 59c743fdb..79b0a2c50 100644 --- a/packages/integration-tests/src/tests/api/resource.scope.test.ts +++ b/packages/integration-tests/src/tests/api/resource.scope.test.ts @@ -40,7 +40,7 @@ describe('scopes', () => { expect(scope).toBeTruthy(); const newScopeName = `new_${scope.name}`; - const newScopeDescription = `new_${scope.description ?? ''}`; + const newScopeDescription = `new_${scope.description}`; const updatesScope = await updateScope(resource.id, scope.id, { name: newScopeName, @@ -58,6 +58,7 @@ describe('scopes', () => { const createdScope2 = await createScope(resource.id); const response = await updateScope(resource.id, createdScope2.id, { name: createdScope.name, + description: '', }).catch((error: unknown) => error); expect(response instanceof HTTPError && response.response.statusCode === 422).toBe(true); }); diff --git a/packages/schemas/alterations/next-1673940577-scope-description-not-null.ts b/packages/schemas/alterations/next-1673940577-scope-description-not-null.ts new file mode 100644 index 000000000..4d1490f29 --- /dev/null +++ b/packages/schemas/alterations/next-1673940577-scope-description-not-null.ts @@ -0,0 +1,18 @@ +import { sql } from 'slonik'; + +import type { AlterationScript } from '../lib/types/alteration.js'; + +const alteration: AlterationScript = { + up: async (pool) => { + await pool.query(sql` + ALTER TABLE scopes ALTER COLUMN description SET NOT NULL; + `); + }, + down: async (pool) => { + await pool.query(sql` + ALTER TABLE scopes ALTER COLUMN description DROP NOT NULL; + `); + }, +}; + +export default alteration; diff --git a/packages/schemas/tables/scopes.sql b/packages/schemas/tables/scopes.sql index f36420c58..f8f68a586 100644 --- a/packages/schemas/tables/scopes.sql +++ b/packages/schemas/tables/scopes.sql @@ -2,7 +2,7 @@ create table scopes ( id varchar(21) not null, resource_id varchar(21) not null references resources (id) on update cascade on delete cascade, name varchar(256) not null, - description text, + description text not null, created_at timestamptz not null default(now()), primary key (id) );