Skip to content

Commit

Permalink
Fix alterTable table name parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesj committed May 10, 2023
1 parent 9ecf9bf commit a2cce49
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/operation-node/alter-table-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ export const AlterTableNode = freeze({
return node.kind === 'AlterTableNode'
},

create(table: string): AlterTableNode {
create(table: TableNode): AlterTableNode {
return freeze({
kind: 'AlterTableNode',
table: TableNode.create(table),
table,
})
},

Expand Down
2 changes: 1 addition & 1 deletion src/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export class SchemaModule {
return new AlterTableBuilder({
queryId: createQueryId(),
executor: this.#executor,
node: AlterTableNode.create(table),
node: AlterTableNode.create(parseTable(table)),
})
}

Expand Down
29 changes: 29 additions & 0 deletions test/node/src/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2446,6 +2446,35 @@ for (const dialect of DIALECTS) {
})
}

if (dialect !== 'sqlite') {
describe('parse schema name', () => {
it('should parse the schema from table name', async () => {
await ctx.db.schema.createSchema('pets').ifNotExists().execute()
await ctx.db.schema.createTable('pets.name').addColumn('id', 'serial').execute();

const builder = ctx.db.schema.alterTable('pets.name').addColumn('nickname', 'text')

testSql(builder, dialect, {
postgres: {
sql: `alter table "pets"."name" add column "nickname" text`,
parameters: [],
},
mysql: {
sql: "alter table `pets`.`name` add column `nickname` text",
parameters: [],
},
sqlite: NOT_SUPPORTED,
})

await builder.execute()
})

async function cleanup() {
await ctx.db.schema.dropSchema('pets').cascade().ifExists().execute()
}
})
}

it('should alter a table calling query builder functions', async () => {
const builder = ctx.db.schema
.alterTable('test')
Expand Down

0 comments on commit a2cce49

Please sign in to comment.