Add columns in create table like #4821

This commit is contained in:
Olivier Cavadenti 2021-11-10 21:24:34 +01:00 committed by GitHub
parent 74083060d9
commit 27ade6f881
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 182 additions and 1 deletions

View File

@ -36,6 +36,9 @@ class TableCompiler_MSSQL extends TableCompiler {
if (this.single.comment) {
this.comment(this.single.comment);
}
if (like) {
this.addColumns(columns, this.addColumnsPrefix);
}
}
comment(/** @type {string} */ comment) {

View File

@ -50,6 +50,9 @@ class TableCompiler_MySQL extends TableCompiler {
}
this.pushQuery(sql);
if (like) {
this.addColumns(columns, this.addColumnsPrefix);
}
}
// Compiles the comment on the table.

View File

@ -66,6 +66,9 @@ class TableCompiler_Oracle extends TableCompiler {
bindings: columns.bindings,
});
if (this.single.comment) this.comment(this.single.comment);
if (like) {
this.addColumns(columns, this.addColumnsPrefix);
}
}
// Compiles the comment on the table.

View File

@ -49,7 +49,11 @@ class TableCompiler_PG extends TableCompiler {
createStatement +
this.tableName() +
(like && this.tableNameLike()
? ' (like ' + this.tableNameLike() + ' including all)'
? ' (like ' +
this.tableNameLike() +
' including all' +
(columns.sql.length ? ', ' + columns.sql.join(', ') : '') +
')'
: columnsSql);
if (this.single.inherits)
sql += ` inherits (${this.formatter.wrap(this.single.inherits)})`;

View File

@ -45,6 +45,9 @@ class TableCompiler_Redshift extends TableCompiler_PG {
});
const hasComment = has(this.single, 'comment');
if (hasComment) this.comment(this.single.comment);
if (like) {
this.addColumns(columns, this.addColumnsPrefix);
}
}
primary(columns, constraintName) {

View File

@ -30,6 +30,10 @@ class TableCompiler_SQLite3 extends TableCompiler {
sql += ')';
}
this.pushQuery(sql);
if (like) {
this.addColumns(columns, this.addColumnsPrefix);
}
}
addColumns(columns, prefix, colCompilers) {

View File

@ -321,6 +321,62 @@ describe('Schema (misc)', () => {
expect(Object.keys(res)).to.have.all.members(['id', 'data']);
});
});
it('copy table with additionnal column', async () => {
await knex.schema.dropTableIfExists('table_copied');
await knex.schema
.createTableLike(
'table_copied',
'table_to_copy',
function (table) {
table.text('add_col');
table.integer('add_num_col');
}
)
.testSql((tester) => {
tester('mysql', [
'create table `table_copied` like `table_to_copy`',
'alter table `table_copied` add `add_col` text, add `add_num_col` int',
]);
tester(
['pg', 'cockroachdb'],
[
'create table "table_copied" (like "table_to_copy" including all, "add_col" text, "add_num_col" integer)',
]
);
tester('pg-redshift', [
'create table "table_copied" (like "table_to_copy")',
'alter table "table_copied" add column "add_col" varchar(max)',
'alter table "table_copied" add column "add_num_col" integer',
]);
tester('sqlite3', [
'create table `table_copied` as select * from `table_to_copy` where 0=1',
'alter table `table_copied` add column `add_col` text',
'alter table `table_copied` add column `add_num_col` integer',
]);
tester('oracledb', [
'create table "table_copied" as (select * from "table_to_copy" where 0=1)',
'alter table "table_copied" add ("add_col" clob, "add_num_col" integer)',
]);
tester('mssql', [
'SELECT * INTO [table_copied] FROM [table_to_copy] WHERE 0=1',
'ALTER TABLE [table_copied] ADD [add_col] nvarchar(max), [add_num_col] int',
]);
});
expect(await knex.schema.hasTable('table_copied')).to.equal(true);
await knex('table_copied')
.columnInfo()
.then((res) => {
expect(Object.keys(res)).to.have.all.members([
'id',
'data',
'add_col',
'add_num_col',
]);
});
});
});
describe('increments types - postgres', () => {

View File

@ -50,6 +50,23 @@ describe('MSSQL SchemaBuilder', function () {
);
});
it('create table like another with additionnal columns', function () {
tableSql = client
.schemaBuilder()
.createTableLike('users_like', 'users', function (table) {
table.text('add_col');
table.integer('numeric_col');
})
.toSQL();
expect(tableSql.length).to.equal(2);
expect(tableSql[0].sql).to.equal(
'SELECT * INTO [users_like] FROM [users] WHERE 0=1'
);
expect(tableSql[1].sql).to.equal(
'ALTER TABLE [users_like] ADD [add_col] nvarchar(max), [numeric_col] int'
);
});
describe('views', function () {
let knexMssql;

View File

@ -46,6 +46,23 @@ module.exports = function (dialect) {
);
});
it('create table like another with additionnal columns', function () {
tableSql = client
.schemaBuilder()
.createTableLike('users_like', 'users', function (table) {
table.text('add_col');
table.integer('numeric_col');
})
.toSQL();
expect(tableSql.length).to.equal(2);
expect(tableSql[0].sql).to.equal(
'create table `users_like` like `users`'
);
expect(tableSql[1].sql).to.equal(
'alter table `users_like` add `add_col` text, add `numeric_col` int'
);
});
it('test basic create table with incrementing without primary key', function () {
tableSql = client
.schemaBuilder()

View File

@ -35,6 +35,23 @@ describe('OracleDb SchemaBuilder', function () {
);
});
it('test create table like with additionnal columns', function () {
tableSql = client
.schemaBuilder()
.createTableLike('users_like', 'users', function (table) {
table.text('add_col');
table.integer('add_num_col');
});
expect(tableSql.toSQL().length).to.equal(2);
expect(tableSql.toSQL()[0].sql).to.equal(
'create table "users_like" as (select * from "users" where 0=1)'
);
expect(tableSql.toSQL()[1].sql).to.equal(
'alter table "users_like" add ("add_col" clob, "add_num_col" integer)'
);
});
describe('views', function () {
let knexOracleDb;

View File

@ -123,6 +123,20 @@ describe('PostgreSQL SchemaBuilder', function () {
);
});
it('create table like another with additionnal columns', function () {
tableSql = client
.schemaBuilder()
.createTableLike('users_like', 'users', function (table) {
table.text('add_col');
table.integer('numeric_col');
})
.toSQL();
expect(tableSql.length).to.equal(1);
expect(tableSql[0].sql).to.equal(
'create table "users_like" (like "users" including all, "add_col" text, "numeric_col" integer)'
);
});
it('basic alter table', function () {
tableSql = client
.schemaBuilder()

View File

@ -38,6 +38,26 @@ describe('Redshift SchemaBuilder', function () {
);
});
it('create table like another with additional columns', function () {
tableSql = client
.schemaBuilder()
.createTableLike('users_like', 'users', function (table) {
table.text('add_col');
table.integer('add_num_col');
})
.toSQL();
expect(tableSql.length).to.equal(3);
expect(tableSql[0].sql).to.equal(
'create table "users_like" (like "users")'
);
expect(tableSql[1].sql).to.equal(
'alter table "users_like" add column "add_col" varchar(max)'
);
expect(tableSql[2].sql).to.equal(
'alter table "users_like" add column "add_num_col" integer'
);
});
it('basic alter table', function () {
tableSql = client
.schemaBuilder()

View File

@ -50,6 +50,26 @@ describe('SQLite SchemaBuilder', function () {
);
});
it('test create table like with additionnal columns', function () {
tableSql = client
.schemaBuilder()
.createTableLike('users_like', 'users', function (table) {
table.text('add_col');
table.integer('add_num_col');
});
expect(tableSql.toSQL().length).to.equal(3);
expect(tableSql.toSQL()[0].sql).to.equal(
'create table `users_like` as select * from `users` where 0=1'
);
expect(tableSql.toSQL()[1].sql).to.equal(
'alter table `users_like` add column `add_col` text'
);
expect(tableSql.toSQL()[2].sql).to.equal(
'alter table `users_like` add column `add_num_col` integer'
);
});
describe('views', function () {
let knexSqlite3;