Skip to content

Commit 0aa8e64

Browse files
committed
fix: throw error when no options are provided in createTable
1 parent bd49ad9 commit 0aa8e64

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/operations/tables/createTable.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ export function createTable(mOptions: MigrationOptions): CreateTable {
6666
...(like ? [parseLike(like, mOptions.literal)] : []),
6767
];
6868

69+
if (tableDefinition.length === 0) {
70+
throw new Error(
71+
'No columns, constraints, or LIKE clause provided for createTable'
72+
);
73+
}
74+
6975
if (temporary && unlogged) {
7076
throw new Error('TEMPORARY and UNLOGGED cannot be used together.');
7177
}

test/operations/tables/createTable.spec.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ describe('operations', () => {
1212
expect(createTableFn).toBeTypeOf('function');
1313
});
1414

15-
// TODO @Shinigami92 2024-03-12: This should throw an error when columns are empty
16-
it('should return sql statement', () => {
17-
const statement = createTableFn('films', {});
18-
19-
expect(statement).toBeTypeOf('string');
20-
expect(statement).toBe('CREATE TABLE "films" (\n \n);');
15+
it('should throw error when no columns, constraints, or LIKE clause are provided', () => {
16+
expect(() => createTableFn('films', {})).toThrow(
17+
new Error(
18+
'No columns, constraints, or LIKE clause provided for createTable'
19+
)
20+
);
2121
});
2222

2323
it('should return sql statement with tableOptions', () => {
@@ -54,18 +54,20 @@ describe('operations', () => {
5454
);
5555
});
5656

57-
// TODO @Shinigami92 2024-03-12: This should throw an error when columns are empty
58-
it('should return sql statement with schema', () => {
59-
const statement = createTableFn(
60-
{
61-
name: 'films',
62-
schema: 'myschema',
63-
},
64-
{}
57+
it('should throw error when no columns, constraints, or LIKE clause are provided (with schema)', () => {
58+
expect(() =>
59+
createTableFn(
60+
{
61+
name: 'films',
62+
schema: 'myschema',
63+
},
64+
{}
65+
)
66+
).toThrow(
67+
new Error(
68+
'No columns, constraints, or LIKE clause provided for createTable'
69+
)
6570
);
66-
67-
expect(statement).toBeTypeOf('string');
68-
expect(statement).toBe('CREATE TABLE "myschema"."films" (\n \n);');
6971
});
7072

7173
it.each([

0 commit comments

Comments
 (0)