Skip to content

Commit bdf82d9

Browse files
feat: Add support for dropping temporary tables with temporary() modifier (#1615)
Co-authored-by: Igal Klebanov <igalklebanov@gmail.com>
1 parent 456e0b2 commit bdf82d9

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

src/operation-node/drop-table-node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface DropTableNode extends OperationNode {
1111
readonly table: TableNode
1212
readonly ifExists?: boolean
1313
readonly cascade?: boolean
14+
readonly temporary?: boolean
1415
}
1516

1617
/**

src/operation-node/operation-node-transformer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ export class OperationNodeTransformer {
533533
table: this.transformNode(node.table, queryId),
534534
ifExists: node.ifExists,
535535
cascade: node.cascade,
536+
temporary: node.temporary,
536537
})
537538
}
538539

src/query-compiler/default-query-compiler.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,13 @@ export class DefaultQueryCompiler
745745
}
746746

747747
protected override visitDropTable(node: DropTableNode): void {
748-
this.append('drop table ')
748+
this.append('drop ')
749+
750+
if (node.temporary) {
751+
this.append('temporary ')
752+
}
753+
754+
this.append('table ')
749755

750756
if (node.ifExists) {
751757
this.append('if exists ')

src/schema/drop-table-builder.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ export class DropTableBuilder implements OperationNodeSource, Compilable {
1313
this.#props = freeze(props)
1414
}
1515

16+
/**
17+
* Adds the "temporary" modifier.
18+
*
19+
* This is only supported by some dialects like MySQL.
20+
*/
21+
temporary(): DropTableBuilder {
22+
return new DropTableBuilder({
23+
...this.#props,
24+
node: DropTableNode.cloneWith(this.#props.node, {
25+
temporary: true,
26+
}),
27+
})
28+
}
29+
1630
ifExists(): DropTableBuilder {
1731
return new DropTableBuilder({
1832
...this.#props,

test/node/src/schema.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,22 @@ for (const dialect of DIALECTS) {
15121512
sqlite: NOT_SUPPORTED,
15131513
})
15141514
})
1515+
1516+
if (sqlSpec === 'mysql') {
1517+
it('should drop a temporary table if it exists', async () => {
1518+
const builder = ctx.db.schema.dropTable('test').temporary().ifExists()
1519+
1520+
testSql(builder, dialect, {
1521+
postgres: NOT_SUPPORTED,
1522+
mysql: {
1523+
sql: 'drop temporary table if exists `test`',
1524+
parameters: [],
1525+
},
1526+
mssql: NOT_SUPPORTED,
1527+
sqlite: NOT_SUPPORTED,
1528+
})
1529+
})
1530+
}
15151531
}
15161532
})
15171533

0 commit comments

Comments
 (0)