From c770a98d8a804f4ae73976d59ab0a1ea1090592f Mon Sep 17 00:00:00 2001 From: Alexis Rico Date: Thu, 1 Feb 2024 09:04:04 +0100 Subject: [PATCH 1/2] Add comments Signed-off-by: Alexis Rico --- packages/pgroll/src/schema.ts | 10 +++++++++- packages/pgroll/src/types.ts | 6 ++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/pgroll/src/schema.ts b/packages/pgroll/src/schema.ts index 11dbd4039..8f048982e 100644 --- a/packages/pgroll/src/schema.ts +++ b/packages/pgroll/src/schema.ts @@ -60,6 +60,10 @@ export const schema = { unique: { description: 'Indicates if the column values must be unique', type: 'boolean' + }, + comment: { + description: 'Postgres comment for the column', + type: 'string' } }, required: ['name', 'nullable', 'pk', 'type', 'unique'], @@ -126,7 +130,7 @@ export const schema = { type: 'string' }, nullable: { - description: 'Indicates if the column is nullable (for add not null constraint operation)', + description: 'Indicates if the column is nullable (for add/remove not null constraint operation)', type: 'boolean' }, references: { @@ -190,6 +194,10 @@ export const schema = { name: { description: 'Name of the table', type: 'string' + }, + comment: { + description: 'Postgres comment for the table', + type: 'string' } }, required: ['columns', 'name'], diff --git a/packages/pgroll/src/types.ts b/packages/pgroll/src/types.ts index 462fb30e8..bba6cb4e0 100644 --- a/packages/pgroll/src/types.ts +++ b/packages/pgroll/src/types.ts @@ -25,7 +25,8 @@ export const ColumnDefinition = z.object({ pk: z.boolean(), references: ForeignKeyReferenceDefinition.optional(), type: z.string(), - unique: z.boolean() + unique: z.boolean(), + comment: z.string().optional() }); export type OpAddColumn = z.infer; @@ -67,7 +68,8 @@ export type OpCreateTable = z.infer; export const OpCreateTableDefinition = z.object({ columns: z.array(ColumnDefinition), - name: z.string() + name: z.string(), + comment: z.string().optional() }); export type OpDropColumn = z.infer; From 979d4d102d4216ee4ddb5aace50dada31b381458 Mon Sep 17 00:00:00 2001 From: Alexis Rico Date: Thu, 1 Feb 2024 10:02:10 +0100 Subject: [PATCH 2/2] Update `alter_column` operation Signed-off-by: Alexis Rico --- packages/pgroll/scripts/build.ts | 6 +++++- packages/pgroll/src/schema.ts | 33 +++++++++++++++++++++++++++++++- packages/pgroll/src/types.ts | 8 ++++---- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/packages/pgroll/scripts/build.ts b/packages/pgroll/scripts/build.ts index 8ad9195d4..ce5cfa0f7 100644 --- a/packages/pgroll/scripts/build.ts +++ b/packages/pgroll/scripts/build.ts @@ -10,6 +10,7 @@ type Definition = | { type: 'object'; properties: Record; + oneOf?: unknown[]; required?: string[]; description?: string; additionalProperties?: boolean; @@ -30,6 +31,8 @@ const DefinitionSchema: z.ZodSchema = z.lazy(() => z.object({ type: z.literal('object'), properties: z.record(DefinitionSchema), + // TODO: Add full support for oneOf + oneOf: z.array(z.any()).optional(), required: z.array(z.string()).optional(), description: z.string().optional(), additionalProperties: z.boolean().optional() @@ -146,7 +149,8 @@ function topologicalSort(nodes: [string, Definition][]): [string, Definition][] } async function main() { - const response = await fetch(PGROLL_JSON_SCHEMA_URL).then((response) => response.json()); + const url = process.env.PGROLL_JSON_SCHEMA_URL ?? PGROLL_JSON_SCHEMA_URL; + const response = await fetch(url).then((response) => response.json()); const schema = JSONSchema.parse(response); // Create a TypeScript project diff --git a/packages/pgroll/src/schema.ts b/packages/pgroll/src/schema.ts index 8f048982e..7dc6b65f1 100644 --- a/packages/pgroll/src/schema.ts +++ b/packages/pgroll/src/schema.ts @@ -154,7 +154,38 @@ export const schema = { type: 'string' } }, - required: ['column', 'down', 'name', 'table', 'type', 'up'], + required: ['table', 'column'], + oneOf: [ + { + required: ['name'], + not: { + required: ['up', 'down'] + } + }, + { + required: ['up', 'down'], + oneOf: [ + { + required: ['check'] + }, + { + required: ['type'] + }, + { + required: ['nullable'] + }, + { + required: ['unique'] + }, + { + required: ['references'] + } + ], + not: { + required: ['name'] + } + } + ], type: 'object' }, OpCreateIndex: { diff --git a/packages/pgroll/src/types.ts b/packages/pgroll/src/types.ts index bba6cb4e0..3c7a0c36d 100644 --- a/packages/pgroll/src/types.ts +++ b/packages/pgroll/src/types.ts @@ -46,14 +46,14 @@ export type OpAlterColumn = z.infer; export const OpAlterColumnDefinition = z.object({ check: CheckConstraintDefinition.optional(), column: z.string(), - down: z.string(), - name: z.string(), + down: z.string().optional(), + name: z.string().optional(), nullable: z.boolean().optional(), references: ForeignKeyReferenceDefinition.optional(), table: z.string(), - type: z.string(), + type: z.string().optional(), unique: UniqueConstraintDefinition.optional(), - up: z.string() + up: z.string().optional() }); export type OpCreateIndex = z.infer;