Skip to content

Commit

Permalink
Update pgroll spec (#1455)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexis Rico <[email protected]>
Co-authored-by: SferaDev <[email protected]>
Co-authored-by: Alexis Rico <[email protected]>
  • Loading branch information
3 people authored Apr 29, 2024
1 parent 41970ab commit a223b03
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
30 changes: 16 additions & 14 deletions packages/pgroll/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { z } from 'zod';
import { PGROLL_JSON_SCHEMA_URL } from '../src';
import prettier from 'prettier';

type DefintionType = 'string' | 'boolean' | 'number' | 'null';

type Definition =
| { type: 'string' | 'boolean' | 'number'; description?: string }
| { type: DefintionType | DefintionType[]; description?: string }
| { $ref: string; description?: string }
| {
type: 'object';
Expand All @@ -18,10 +20,12 @@ type Definition =
| { type: 'array'; items: Definition | Definition[]; description?: string }
| { anyOf: Definition[] };

const DefinitionTypeSchema = z.enum(['string', 'boolean', 'number', 'null']);

const DefinitionSchema: z.ZodSchema<Definition> = z.lazy(() =>
z.union([
z.object({
type: z.enum(['string', 'boolean', 'number']),
type: z.union([DefinitionTypeSchema, z.array(DefinitionTypeSchema)]),
description: z.string().optional()
}),
z.object({
Expand Down Expand Up @@ -83,19 +87,17 @@ function buildZodSchema(definition: Definition): string {
return `z.object({ ${properties.join(', ')} })`;
}

if (definition.type === 'string') {
return 'z.string()';
}

if (definition.type === 'boolean') {
return 'z.boolean()';
}

if (definition.type === 'number') {
return 'z.number()';
}
const types = typeof definition.type === 'string' ? [definition.type] : definition.type;
const zodTypes = types.map((type) => {
if (type === 'string') return 'z.string()';
if (type === 'boolean') return 'z.boolean()';
if (type === 'number') return 'z.number()';
if (type === 'null') return 'z.null()';
throw new Error(`Unknown type: ${type}`);
});

throw new Error(`Unknown type: ${definition.type}`);
if (zodTypes.length === 1) return zodTypes[0];
return `z.union([${zodTypes.join(', ')}])`;
}

function getDependencies(definition: Definition): string[] {
Expand Down
21 changes: 21 additions & 0 deletions packages/pgroll/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ export const schema = {
$ref: '#/$defs/UniqueConstraint',
description: 'Add unique constraint to the column'
},
comment: {
description: 'New comment on the column',
type: ['string', 'null'],
goJSONSchema: {
imports: ['github.com/oapi-codegen/nullable'],
nillable: true,
type: 'nullable.Nullable[string]'
}
},
up: {
default: '',
description: 'SQL expression for up migration',
Expand All @@ -180,6 +189,12 @@ export const schema = {
{
required: ['nullable']
},
{
required: ['default']
},
{
required: ['comment']
},
{
required: ['unique']
},
Expand All @@ -202,6 +217,12 @@ export const schema = {
{
required: ['nullable']
},
{
required: ['default']
},
{
required: ['comment']
},
{
required: ['unique']
},
Expand Down
1 change: 1 addition & 0 deletions packages/pgroll/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const OpAlterColumnDefinition = z.object({
table: z.string(),
type: z.string().optional(),
unique: UniqueConstraintDefinition.optional(),
comment: z.union([z.string(), z.null()]).optional(),
up: z.string().optional()
});

Expand Down

0 comments on commit a223b03

Please sign in to comment.