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

Add columns in create table like #4820 #4821

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/dialects/mssql/schema/mssql-tablecompiler.js
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions lib/dialects/mysql/schema/mysql-tablecompiler.js
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions lib/dialects/oracle/schema/oracle-tablecompiler.js
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion lib/dialects/postgres/schema/pg-tablecompiler.js
Expand Up @@ -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)})`;
Expand Down
3 changes: 3 additions & 0 deletions lib/dialects/redshift/schema/redshift-tablecompiler.js
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions lib/dialects/sqlite3/schema/sqlite-tablecompiler.js
Expand Up @@ -30,6 +30,10 @@ class TableCompiler_SQLite3 extends TableCompiler {
sql += ')';
}
this.pushQuery(sql);

if (like) {
this.addColumns(columns, this.addColumnsPrefix);
}
}

addColumns(columns, prefix, colCompilers) {
Expand Down
56 changes: 56 additions & 0 deletions test/integration2/schema/misc.spec.js
Expand Up @@ -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', () => {
Expand Down
17 changes: 17 additions & 0 deletions test/unit/schema-builder/mssql.js
Expand Up @@ -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;

Expand Down
17 changes: 17 additions & 0 deletions test/unit/schema-builder/mysql.js
Expand Up @@ -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()
Expand Down
17 changes: 17 additions & 0 deletions test/unit/schema-builder/oracledb.js
Expand Up @@ -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;

Expand Down
14 changes: 14 additions & 0 deletions test/unit/schema-builder/postgres.js
Expand Up @@ -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()
Expand Down
20 changes: 20 additions & 0 deletions test/unit/schema-builder/redshift.js
Expand Up @@ -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()
Expand Down
20 changes: 20 additions & 0 deletions test/unit/schema-builder/sqlite3.js
Expand Up @@ -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;

Expand Down