Skip to content

Commit

Permalink
Update pgroll schema for alter_column (#1343)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexis Rico <sferadev@gmail.com>
  • Loading branch information
SferaDev committed Feb 7, 2024
1 parent a50e902 commit c7964f1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
6 changes: 5 additions & 1 deletion packages/pgroll/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Definition =
| {
type: 'object';
properties: Record<string, Definition>;
oneOf?: unknown[];
required?: string[];
description?: string;
additionalProperties?: boolean;
Expand All @@ -30,6 +31,8 @@ const DefinitionSchema: z.ZodSchema<Definition> = 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()
Expand Down Expand Up @@ -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
Expand Down
43 changes: 41 additions & 2 deletions packages/pgroll/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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: {
Expand All @@ -150,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: {
Expand Down Expand Up @@ -190,6 +225,10 @@ export const schema = {
name: {
description: 'Name of the table',
type: 'string'
},
comment: {
description: 'Postgres comment for the table',
type: 'string'
}
},
required: ['columns', 'name'],
Expand Down
14 changes: 8 additions & 6 deletions packages/pgroll/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof OpAddColumnDefinition>;
Expand All @@ -45,14 +46,14 @@ export type OpAlterColumn = z.infer<typeof OpAlterColumnDefinition>;
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<typeof OpCreateIndexDefinition>;
Expand All @@ -67,7 +68,8 @@ export type OpCreateTable = z.infer<typeof OpCreateTableDefinition>;

export const OpCreateTableDefinition = z.object({
columns: z.array(ColumnDefinition),
name: z.string()
name: z.string(),
comment: z.string().optional()
});

export type OpDropColumn = z.infer<typeof OpDropColumnDefinition>;
Expand Down

0 comments on commit c7964f1

Please sign in to comment.