Skip to content

Commit

Permalink
Revert "fix: AlterColumn now properly applies all operations"
Browse files Browse the repository at this point in the history
This reverts commit 300a384.
  • Loading branch information
soanvig committed May 12, 2023
1 parent 3050b10 commit 6bd80a7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 118 deletions.
49 changes: 0 additions & 49 deletions src/operation-node/alter-column-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,53 +42,4 @@ export const AlterColumnNode = freeze({
...props,
})
},

// Because AlterColumnNode is aggregation for multiple "ALTER COLUMN" expressions
// We can unwind any instance into multiple instances of single expression
unwind(node: AlterColumnNode): AlterColumnNode[] {
const operations: AlterColumnNode[] = []

if (node.dataType) {
operations.push({
column: node.column,
kind: node.kind,
dataType: node.dataType,
dataTypeExpression: node.dataTypeExpression,
})
}

if (node.dropDefault) {
operations.push({
column: node.column,
kind: node.kind,
dropDefault: node.dropDefault,
})
}

if (node.dropNotNull) {
operations.push({
column: node.column,
kind: node.kind,
dropNotNull: node.dropNotNull,
})
}

if (node.setDefault) {
operations.push({
column: node.column,
kind: node.kind,
setDefault: node.setDefault,
})
}

if (node.setNotNull) {
operations.push({
column: node.column,
kind: node.kind,
setNotNull: node.setNotNull,
})
}

return operations
}
})
62 changes: 26 additions & 36 deletions src/query-compiler/default-query-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -990,46 +990,36 @@ export class DefaultQueryCompiler
}

protected override visitAlterColumn(node: AlterColumnNode): void {
const operations = AlterColumnNode.unwind(node);

if (operations.length === 0) {
return
} else if (operations.length > 1) {
return this.compileList(operations)
} else {
const operation = operations[0]
this.append('alter column ')
this.visitNode(node.column)
this.append(' ')

this.append('alter column ')
this.visitNode(node.column)
this.append(' ')
if (node.dataType) {
this.append('type ')
this.visitNode(node.dataType)

if (operation.dataType) {
this.append('type ')
this.visitNode(operation.dataType)

if (operation.dataTypeExpression) {
this.append('using ')
this.visitNode(operation.dataTypeExpression)
}
}

if (operation.setDefault) {
this.append('set default ')
this.visitNode(operation.setDefault)
}

if (operation.dropDefault) {
this.append('drop default')
}

if (operation.setNotNull) {
this.append('set not null')
}

if (operation.dropNotNull) {
this.append('drop not null')
if (node.dataTypeExpression) {
this.append('using ')
this.visitNode(node.dataTypeExpression)
}
}

if (node.setDefault) {
this.append('set default ')
this.visitNode(node.setDefault)
}

if (node.dropDefault) {
this.append('drop default')
}

if (node.setNotNull) {
this.append('set not null')
}

if (node.dropNotNull) {
this.append('drop not null')
}
}

protected override visitModifyColumn(node: ModifyColumnNode): void {
Expand Down
33 changes: 0 additions & 33 deletions test/node/src/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2023,39 +2023,6 @@ for (const dialect of DIALECTS) {

await builder.execute()
})

// Checking MySQL for that operation doesn't make sense, because it supports
// only setDefault and dropDefault in ALTER COLUMN, and probably nobody will use this combination
it('should set multiple attributes', async () => {
const builder = ctx.db.schema
.alterTable('test')
.alterColumn('varchar_col', (ac) =>
ac
.setDefault('foo')
.setDataType('varchar')
.setNotNull()
.dropDefault()
.dropNotNull()
)

testSql(builder, dialect, {
postgres: {
sql: [
`alter table "test" `,
`alter column "varchar_col" type varchar`,
`, alter column "varchar_col" drop default`,
`, alter column "varchar_col" drop not null`,
`, alter column "varchar_col" set default 'foo'`,
`, alter column "varchar_col" set not null`,
].join(''),
parameters: [],
},
mysql: NOT_SUPPORTED,
sqlite: NOT_SUPPORTED,
})

await builder.execute()
})
}

it('should alter multiple columns', async () => {
Expand Down

0 comments on commit 6bd80a7

Please sign in to comment.