Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix alterTable table name parsing #471

Merged
merged 2 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 35 additions & 0 deletions test/node/src/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2466,6 +2466,41 @@ for (const dialect of DIALECTS) {
})
}

if (dialect !== 'sqlite') {
describe('parse schema name', () => {
beforeEach(cleanup)
afterEach(cleanup)

it('should parse the schema from table name', async () => {
await ctx.db.schema.createSchema('test_schema').ifNotExists().execute()
await ctx.db.schema.createTable('test_schema.test').addColumn('id', 'serial').execute();

const builder = ctx.db.schema
.alterTable('test_schema.test')
.addColumn('second_column', 'text')

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

await builder.execute()
})

async function cleanup() {
await ctx.db.schema.dropTable('test_schema.test').ifExists().execute()
await ctx.db.schema.dropSchema('test_schema').ifExists().execute()
}
})
}

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