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

Make view columns optional in create view like #4827 #4829

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
16 changes: 15 additions & 1 deletion lib/dialects/mssql/schema/mssql-viewcompiler.js
@@ -1,6 +1,9 @@
/* eslint max-len: 0 */

const ViewCompiler = require('../../../schema/viewcompiler.js');
const {
columnize: columnize_,
} = require('../../../formatter/wrappingFormatter');

class ViewCompiler_MSSQL extends ViewCompiler {
constructor(client, viewCompiler) {
Expand All @@ -11,7 +14,18 @@ class ViewCompiler_MSSQL extends ViewCompiler {
const createStatement = 'CREATE ' + (replace ? 'OR ALTER ' : '') + 'VIEW ';
let sql = createStatement + this.viewName();

sql += ' (' + columns.join(', ') + ')';
const columnList = columns
? ' (' +
columnize_(
columns,
this.viewBuilder,
this.client,
this.bindingsHolder
) +
')'
: '';

sql += columnList;
sql += ' AS ';
sql += selectQuery.toString();
this.pushQuery({
Expand Down
19 changes: 11 additions & 8 deletions lib/schema/viewcompiler.js
Expand Up @@ -54,14 +54,17 @@ class ViewCompiler {
(materialized ? 'materialized ' : '') +
'view ' +
(replace ? 'or replace ' : '');
const formatColumns = [];
for (const c of columns) {
formatColumns.push(
columnize_(c, this.viewBuilder, this.client, this.bindingsHolder)
);
}
let sql =
createStatement + this.viewName() + ' (' + formatColumns.join(', ') + ')';
const columnList = columns
? ' (' +
columnize_(
columns,
this.viewBuilder,
this.client,
this.bindingsHolder
) +
')'
: '';
let sql = createStatement + this.viewName() + columnList;
sql += ' as ';
sql += selectQuery.toString();
switch (this.single.checkOption) {
Expand Down
41 changes: 39 additions & 2 deletions test/integration2/schema/views.spec.js
Expand Up @@ -69,11 +69,48 @@ describe('Views', () => {
]
);
tester('mssql', [
"CREATE VIEW [view_test] (a, b) AS select [a], [b] from [table_view] where [b] > '10'",
"CREATE VIEW [view_test] ([a], [b]) AS select [a], [b] from [table_view] where [b] > '10'",
]);
});

// We test if the select on the view work and if results are good
// We test if the select on the view works and if results are good
await knex
.select(['a', 'b'])
.from('view_test')
.then(function (results) {
assertNumber(knex, results[0].b, 12);
assertNumber(knex, results[1].b, 45);
expect(results[0].a).to.be.equal('test2');
expect(results[1].a).to.be.equal('test3');
});
});

it('create view without columns', async () => {
await knex.schema
.createView('view_test', function (view) {
view.as(
knex('table_view').select('a', 'b').where('b', '>', '10')
);
})
.testSql((tester) => {
tester(
['pg', 'pg-redshift', 'cockroachdb', 'oracledb'],
[
'create view "view_test" as select "a", "b" from "table_view" where "b" > \'10\'',
]
);
tester(
['sqlite3', 'mysql'],
[
"create view `view_test` as select `a`, `b` from `table_view` where `b` > '10'",
]
);
tester('mssql', [
"CREATE VIEW [view_test] AS select [a], [b] from [table_view] where [b] > '10'",
]);
});

// We test if the select on the view works and if results are good
await knex
.select(['a', 'b'])
.from('view_test')
Expand Down
17 changes: 15 additions & 2 deletions test/unit/schema-builder/mssql.js
Expand Up @@ -87,7 +87,20 @@ describe('MSSQL SchemaBuilder', function () {
.toSQL();
equal(1, viewSql.length);
expect(viewSql[0].sql).to.equal(
"CREATE VIEW [adults] (name) AS select [name] from [users] where [age] > '18'"
"CREATE VIEW [adults] ([name]) AS select [name] from [users] where [age] > '18'"
);
});

it('basic create view without columns', async function () {
const viewSql = client
.schemaBuilder()
.createView('adults', function (view) {
view.as(knexMssql('users').select('name').where('age', '>', '18'));
})
.toSQL();
equal(1, viewSql.length);
expect(viewSql[0].sql).to.equal(
"CREATE VIEW [adults] AS select [name] from [users] where [age] > '18'"
);
});

Expand All @@ -101,7 +114,7 @@ describe('MSSQL SchemaBuilder', function () {
.toSQL();
equal(1, viewSql.length);
expect(viewSql[0].sql).to.equal(
"CREATE OR ALTER VIEW [adults] (name) AS select [name] from [users] where [age] > '18'"
"CREATE OR ALTER VIEW [adults] ([name]) AS select [name] from [users] where [age] > '18'"
);
});

Expand Down
13 changes: 13 additions & 0 deletions test/unit/schema-builder/mysql.js
Expand Up @@ -134,6 +134,19 @@ module.exports = function (dialect) {
);
});

it('basic create view without columns', async function () {
const viewSql = client
.schemaBuilder()
.createView('adults', function (view) {
view.as(knexMysql('users').select('name').where('age', '>', '18'));
})
.toSQL();
equal(1, viewSql.length);
expect(viewSql[0].sql).to.equal(
"create view `adults` as select `name` from `users` where `age` > '18'"
);
});

it('create view or replace', async function () {
const viewSql = client
.schemaBuilder()
Expand Down
13 changes: 13 additions & 0 deletions test/unit/schema-builder/oracledb.js
Expand Up @@ -76,6 +76,19 @@ describe('OracleDb SchemaBuilder', function () {
);
});

it('basic create view without columns', async function () {
const viewSql = client
.schemaBuilder()
.createView('adults', function (view) {
view.as(knexOracleDb('users').select('name').where('age', '>', '18'));
})
.toSQL();
equal(1, viewSql.length);
expect(viewSql[0].sql).to.equal(
'create view "adults" as select "name" from "users" where "age" > \'18\''
);
});

it('create view or replace', async function () {
const viewSql = client
.schemaBuilder()
Expand Down
13 changes: 13 additions & 0 deletions test/unit/schema-builder/postgres.js
Expand Up @@ -234,6 +234,19 @@ describe('PostgreSQL SchemaBuilder', function () {
);
});

it('basic create view without columns', async function () {
const viewSql = client
.schemaBuilder()
.createView('adults', function (view) {
view.as(knexPg('users').select('name').where('age', '>', '18'));
})
.toSQL();
equal(1, viewSql.length);
expect(viewSql[0].sql).to.equal(
'create view "adults" as select "name" from "users" where "age" > \'18\''
);
});

it('create view or replace', async function () {
const viewSql = client
.schemaBuilder()
Expand Down
13 changes: 13 additions & 0 deletions test/unit/schema-builder/redshift.js
Expand Up @@ -99,6 +99,19 @@ describe('Redshift SchemaBuilder', function () {
);
});

it('basic create view without columns', async function () {
const viewSql = client
.schemaBuilder()
.createView('adults', function (view) {
view.as(knexRedShift('users').select('name').where('age', '>', '18'));
})
.toSQL();
equal(1, viewSql.length);
expect(viewSql[0].sql).to.equal(
'create view "adults" as select "name" from "users" where "age" > \'18\''
);
});

it('create view or replace', async function () {
const viewSql = client
.schemaBuilder()
Expand Down
13 changes: 13 additions & 0 deletions test/unit/schema-builder/sqlite3.js
Expand Up @@ -96,6 +96,19 @@ describe('SQLite SchemaBuilder', function () {
);
});

it('basic create view without columns', async function () {
const viewSql = client
.schemaBuilder()
.createView('adults', function (view) {
view.as(knexSqlite3('users').select('name').where('age', '>', '18'));
})
.toSQL();
equal(1, viewSql.length);
expect(viewSql[0].sql).to.equal(
"create view `adults` as select `name` from `users` where `age` > '18'"
);
});

it('create view or replace', async function () {
expect(() => {
tableSql = client
Expand Down