1033 lines
28 KiB
JavaScript
Raw Normal View History

2015-12-08 11:37:31 -06:00
'use strict';
2019-10-06 20:21:32 +02:00
const expect = require('chai').expect;
const sinon = require('sinon');
2019-07-10 22:48:43 +01:00
const MSSQL_Client = require('../../../lib/dialects/mssql');
const client = new MSSQL_Client({ client: 'mssql' });
2015-12-08 11:37:31 -06:00
2020-04-19 00:40:23 +02:00
describe('MSSQL SchemaBuilder', function () {
let tableSql;
const equal = require('assert').equal;
2015-12-08 11:37:31 -06:00
2020-04-19 00:40:23 +02:00
it('throws when charset and collate are specified', function () {
expect(() => {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.createTable('users', function (table) {
table.increments('id');
table.string('email');
table.charset('utf8');
table.collate('utf8_unicode_ci');
})
.toSQL();
}).to.throw('Knex only supports charset statement with mysql');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('basic create table', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.increments('id');
this.string('email');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [id] int identity(1,1) not null primary key, [email] nvarchar(255)'
);
2015-12-08 11:37:31 -06:00
});
2021-02-03 13:47:32 +01:00
it('test basic create table with incrementing without primary key', function () {
tableSql = client.schemaBuilder().createTable('users', function (table) {
table.increments('id', { primaryKey: false });
});
equal(1, tableSql.toSQL().length);
expect(tableSql.toSQL()[0].sql).to.equal(
'CREATE TABLE [users] ([id] int identity(1,1) not null)'
);
expect(tableSql.toQuery()).to.equal(
'CREATE TABLE [users] ([id] int identity(1,1) not null)'
);
});
2020-04-19 00:40:23 +02:00
it('test drop table', function () {
tableSql = client.schemaBuilder().dropTable('users').toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('DROP TABLE [users]');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test drop table if exists', function () {
tableSql = client.schemaBuilder().dropTableIfExists('users').toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
"if object_id('[users]', 'U') is not null DROP TABLE [users]"
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test drop column', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.dropColumn('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(2, tableSql.length);
expect(tableSql[0].sql).to.includes(
`IF @constraint IS NOT NULL EXEC('ALTER TABLE users DROP CONSTRAINT ' + @constraint)`
);
expect(tableSql[1].sql).to.equal('ALTER TABLE [users] DROP COLUMN [foo]');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('drops multiple columns with an array', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.dropColumn(['foo', 'bar']);
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(3, tableSql.length);
expect(tableSql[0].sql).to.includes(
`IF @constraint IS NOT NULL EXEC('ALTER TABLE users DROP CONSTRAINT ' + @constraint)`
);
expect(tableSql[1].sql).to.includes(
`IF @constraint IS NOT NULL EXEC('ALTER TABLE users DROP CONSTRAINT ' + @constraint)`
);
expect(tableSql[2].sql).to.equal(
'ALTER TABLE [users] DROP COLUMN [foo], [bar]'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('drops multiple columns as multiple arguments', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.dropColumn('foo', 'bar');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(3, tableSql.length);
expect(tableSql[0].sql).to.includes(
`IF @constraint IS NOT NULL EXEC('ALTER TABLE users DROP CONSTRAINT ' + @constraint)`
);
expect(tableSql[1].sql).to.includes(
`IF @constraint IS NOT NULL EXEC('ALTER TABLE users DROP CONSTRAINT ' + @constraint)`
);
expect(tableSql[2].sql).to.equal(
'ALTER TABLE [users] DROP COLUMN [foo], [bar]'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test drop primary', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.dropPrimary('testconstraintname');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] DROP CONSTRAINT [testconstraintname]'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test drop unique', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.dropUnique('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'DROP INDEX [users_foo_unique] ON [users]'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('should alter columns with the alter flag', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.string('foo').alter();
this.string('bar');
})
.toSQL();
equal(2, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [bar] nvarchar(255)'
);
expect(tableSql[1].sql).to.equal(
'ALTER TABLE [users] alter column [foo] nvarchar(255)'
);
});
2020-04-19 00:40:23 +02:00
it('test drop unique, custom', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.dropUnique(null, 'foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('DROP INDEX [foo] ON [users]');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test drop index', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.dropIndex('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
2015-07-15 23:16:30 -03:00
expect(tableSql[0].sql).to.equal('DROP INDEX [users_foo_index] ON [users]');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test drop index, custom', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.dropIndex(null, 'foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
2015-07-15 23:16:30 -03:00
expect(tableSql[0].sql).to.equal('DROP INDEX [foo] ON [users]');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test drop foreign', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.dropForeign('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] DROP CONSTRAINT [users_foo_foreign]'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test drop foreign, custom', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.dropForeign(null, 'foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] DROP CONSTRAINT [foo]'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test drop timestamps', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.dropTimestamps();
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(3, tableSql.length);
expect(tableSql[0].sql).to.includes(
`IF @constraint IS NOT NULL EXEC('ALTER TABLE users DROP CONSTRAINT ' + @constraint)`
);
expect(tableSql[1].sql).to.includes(
`IF @constraint IS NOT NULL EXEC('ALTER TABLE users DROP CONSTRAINT ' + @constraint)`
);
expect(tableSql[2].sql).to.equal(
'ALTER TABLE [users] DROP COLUMN [created_at], [updated_at]'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test rename table', function () {
tableSql = client.schemaBuilder().renameTable('users', 'foo').toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('exec sp_rename ?, ?');
expect(tableSql[0].bindings[0]).to.equal('users');
expect(tableSql[0].bindings[1]).to.equal('foo');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test has table', function () {
tableSql = client.schemaBuilder().hasTable('users').toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'select object_id from sys.tables where object_id = object_id(?)'
);
expect(tableSql[0].bindings[0]).to.equal('[users]');
});
2020-04-19 00:40:23 +02:00
it('test has table with schema', function () {
tableSql = client
.schemaBuilder()
.withSchema('schema')
.hasTable('users')
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'select object_id from sys.tables where object_id = object_id(?)'
);
expect(tableSql[0].bindings[0]).to.equal('[schema].[users]');
});
2020-04-19 00:40:23 +02:00
it('test rename table with schema', function () {
tableSql = client
.schemaBuilder()
.withSchema('schema')
.renameTable('users', 'foo')
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('exec sp_rename ?, ?');
expect(tableSql[0].bindings[0]).to.equal('schema.users');
expect(tableSql[0].bindings[1]).to.equal('foo');
});
2020-04-19 00:40:23 +02:00
it('test has column', function () {
tableSql = client.schemaBuilder().hasColumn('users', 'foo').toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'select object_id from sys.columns where name = ? and object_id = object_id(?)'
);
expect(tableSql[0].bindings[0]).to.equal('foo');
expect(tableSql[0].bindings[1]).to.equal('[users]');
});
2020-04-19 00:40:23 +02:00
it('test has column with schema', function () {
tableSql = client
.schemaBuilder()
.withSchema('schema')
.hasColumn('users', 'foo')
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'select object_id from sys.columns where name = ? and object_id = object_id(?)'
);
expect(tableSql[0].bindings[0]).to.equal('foo');
expect(tableSql[0].bindings[1]).to.equal('[schema].[users]');
});
2015-12-08 11:37:31 -06:00
2020-04-19 00:40:23 +02:00
it('test adding primary key', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.primary('foo', 'bar');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD CONSTRAINT [bar] PRIMARY KEY ([foo])'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding unique key', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.unique('foo', 'bar');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'CREATE UNIQUE INDEX [bar] ON [users] ([foo]) WHERE [foo] IS NOT NULL'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding index', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.index(['foo', 'bar'], 'baz');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'CREATE INDEX [baz] ON [users] ([foo], [bar])'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding foreign key', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.foreign('foo_id').references('id').on('orders');
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD CONSTRAINT [users_foo_id_foreign] FOREIGN KEY ([foo_id]) REFERENCES [orders] ([id])'
);
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.integer('foo_id').references('id').on('orders');
})
.toSQL();
equal(2, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo_id] int');
expect(tableSql[1].sql).to.equal(
'ALTER TABLE [users] ADD CONSTRAINT [users_foo_id_foreign] FOREIGN KEY ([foo_id]) REFERENCES [orders] ([id])'
);
});
2020-04-19 00:40:23 +02:00
it('adding foreign key with specific identifier', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.foreign('foo_id', 'fk_foo').references('id').on('orders');
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD CONSTRAINT [fk_foo] FOREIGN KEY ([foo_id]) REFERENCES [orders] ([id])'
);
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.integer('foo_id')
.references('id')
.on('orders')
.withKeyName('fk_foo');
})
.toSQL();
equal(2, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo_id] int');
expect(tableSql[1].sql).to.equal(
'ALTER TABLE [users] ADD CONSTRAINT [fk_foo] FOREIGN KEY ([foo_id]) REFERENCES [orders] ([id])'
);
});
2020-04-19 00:40:23 +02:00
it('adds foreign key with onUpdate and onDelete', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.createTable('person', function (table) {
table
.integer('user_id')
.notNull()
.references('users.id')
.onDelete('SET NULL');
table
.integer('account_id')
.notNull()
.references('id')
.inTable('accounts')
.onUpdate('cascade');
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'CREATE TABLE [person] ([user_id] int not null, [account_id] int not null, CONSTRAINT [person_user_id_foreign] FOREIGN KEY ([user_id]) REFERENCES [users] ([id]) ON DELETE SET NULL, CONSTRAINT [person_account_id_foreign] FOREIGN KEY ([account_id]) REFERENCES [accounts] ([id]) ON UPDATE cascade)'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding incrementing id', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.increments('id');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [id] int identity(1,1) not null primary key'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding big incrementing id', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.bigIncrements('id');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [id] bigint identity(1,1) not null primary key'
);
2015-12-08 11:37:31 -06:00
});
2021-02-03 13:47:32 +01:00
it('test adding big incrementing id without primary key', function () {
tableSql = client
.schemaBuilder()
.table('users', function () {
this.bigIncrements('id', { primaryKey: false });
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [id] bigint identity(1,1) not null'
);
});
2020-04-19 00:40:23 +02:00
it('test adding column after another column', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.string('name').after('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [name] nvarchar(255)'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding column on the first place', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.string('first_name').first();
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [first_name] nvarchar(255)'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding string', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.string('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [foo] nvarchar(255)'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('uses the varchar column constraint', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.string('foo', 100);
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [foo] nvarchar(100)'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('chains notNull and defaultTo', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.string('foo', 100).notNull().defaultTo('bar');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
"ALTER TABLE [users] ADD [foo] nvarchar(100) not null default 'bar'"
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('allows for raw values in the default field', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.string('foo', 100)
.nullable()
.defaultTo(client.raw('CURRENT TIMESTAMP'));
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [foo] nvarchar(100) null default CURRENT TIMESTAMP'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding text', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.text('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [foo] nvarchar(max)'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding big integer', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.bigInteger('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] bigint');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding integer', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.integer('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] int');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding medium integer', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.mediumint('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] int');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding small integer', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.smallint('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] smallint');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding tiny integer', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.tinyint('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] tinyint');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding float', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.float('foo', 5, 2);
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] float');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding double', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.double('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] float');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding double specifying precision', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.double('foo', 15, 8);
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] float');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding decimal', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.decimal('foo', 5, 2);
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [foo] decimal(5, 2)'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding decimal, no precision', function () {
expect(() => {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.decimal('foo', null);
})
.toSQL();
}).to.throw('Specifying no precision on decimal columns is not supported');
});
2020-04-19 00:40:23 +02:00
it('set comment to undefined', function () {
expect(function () {
client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('user', function (t) {
t.comment();
})
.toSQL();
}).to.throw(TypeError);
});
2020-04-19 00:40:23 +02:00
it('set comment to null', function () {
expect(function () {
client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('user', function (t) {
t.comment(null);
})
.toSQL();
}).to.throw(TypeError);
});
2020-04-19 00:40:23 +02:00
it('test adding boolean', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.boolean('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] bit');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding enum', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.enum('foo', ['bar', 'baz']);
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [foo] nvarchar(100)'
);
});
2015-12-08 11:37:31 -06:00
2020-04-19 00:40:23 +02:00
it('test adding date', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.date('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] date');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding date time', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.dateTime('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] datetime2');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding time', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.time('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
2015-12-14 09:56:53 -06:00
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] time');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding time stamp', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.timestamp('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] datetime2');
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding time stamp with timezone', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.timestamp('foo', {
useTz: true,
});
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [foo] datetimeoffset'
);
});
2020-04-19 00:40:23 +02:00
it('test adding time stamps', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.timestamps();
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [created_at] datetime2, [updated_at] datetime2'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding binary', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.binary('foo');
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [foo] varbinary(max)'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding decimal', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.decimal('foo', 2, 6);
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [foo] decimal(2, 6)'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('test adding multiple columns, #1348', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function () {
this.integer('foo');
this.integer('baa');
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD [foo] int, [baa] int'
);
});
2020-04-19 00:40:23 +02:00
it('is possible to set raw statements in defaultTo, #146', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.createTable('default_raw_test', function (t) {
t.timestamp('created_at').defaultTo(client.raw('GETDATE()'));
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'CREATE TABLE [default_raw_test] ([created_at] datetime2 default GETDATE())'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('allows dropping a unique compound index', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('composite_key_test', function (t) {
t.dropUnique(['column_a', 'column_b']);
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'DROP INDEX [composite_key_test_column_a_column_b_unique] ON [composite_key_test]'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('allows default as alias for defaultTo', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.createTable('default_raw_test', function (t) {
t.timestamp('created_at').default(client.raw('GETDATE()'));
})
.toSQL();
2015-12-08 11:37:31 -06:00
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'CREATE TABLE [default_raw_test] ([created_at] datetime2 default GETDATE())'
);
2015-12-08 11:37:31 -06:00
});
2020-04-19 00:40:23 +02:00
it('#1430 - .primary & .dropPrimary takes columns and constraintName', function () {
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.table('users', function (t) {
t.primary(['test1', 'test2'], 'testconstraintname');
})
.toSQL();
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ADD CONSTRAINT [testconstraintname] PRIMARY KEY ([test1], [test2])'
);
tableSql = client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.createTable('users', function (t) {
t.string('test').primary('testconstraintname');
})
.toSQL();
expect(tableSql[0].sql).to.equal(
'CREATE TABLE [users] ([test] nvarchar(255), CONSTRAINT [testconstraintname] PRIMARY KEY ([test]))'
);
});
2020-04-19 00:40:23 +02:00
describe('queryContext', function () {
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
let spy;
let originalWrapIdentifier;
2020-04-19 00:40:23 +02:00
before(function () {
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
spy = sinon.spy();
originalWrapIdentifier = client.config.wrapIdentifier;
2020-04-19 00:40:23 +02:00
client.config.wrapIdentifier = function (value, wrap, queryContext) {
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
spy(value, queryContext);
return wrap(value);
};
});
2020-04-19 00:40:23 +02:00
beforeEach(function () {
spy.resetHistory();
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
});
2020-04-19 00:40:23 +02:00
after(function () {
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
client.config.wrapIdentifier = originalWrapIdentifier;
});
2020-04-19 00:40:23 +02:00
it('SchemaCompiler passes queryContext to wrapIdentifier via TableCompiler', function () {
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
client
.schemaBuilder()
.queryContext('table context')
2020-04-19 00:40:23 +02:00
.createTable('users', function (table) {
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
table.increments('id');
table.string('email');
})
.toSQL();
expect(spy.callCount).to.equal(3);
expect(spy.firstCall.args).to.deep.equal(['id', 'table context']);
expect(spy.secondCall.args).to.deep.equal(['email', 'table context']);
expect(spy.thirdCall.args).to.deep.equal(['users', 'table context']);
});
2020-04-19 00:40:23 +02:00
it('TableCompiler passes queryContext to wrapIdentifier', function () {
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
client
.schemaBuilder()
2020-04-19 00:40:23 +02:00
.createTable('users', function (table) {
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
table.increments('id').queryContext('id context');
table.string('email').queryContext('email context');
})
.toSQL();
expect(spy.callCount).to.equal(3);
expect(spy.firstCall.args).to.deep.equal(['id', 'id context']);
expect(spy.secondCall.args).to.deep.equal(['email', 'email context']);
expect(spy.thirdCall.args).to.deep.equal(['users', undefined]);
});
2020-04-19 00:40:23 +02:00
it('TableCompiler allows overwriting queryContext from SchemaCompiler', function () {
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
client
.schemaBuilder()
.queryContext('schema context')
2020-04-19 00:40:23 +02:00
.createTable('users', function (table) {
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
table.queryContext('table context');
table.increments('id');
table.string('email');
})
.toSQL();
expect(spy.callCount).to.equal(3);
expect(spy.firstCall.args).to.deep.equal(['id', 'table context']);
expect(spy.secondCall.args).to.deep.equal(['email', 'table context']);
expect(spy.thirdCall.args).to.deep.equal(['users', 'table context']);
});
2020-04-19 00:40:23 +02:00
it('ColumnCompiler allows overwriting queryContext from TableCompiler', function () {
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
client
.schemaBuilder()
.queryContext('schema context')
2020-04-19 00:40:23 +02:00
.createTable('users', function (table) {
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
table.queryContext('table context');
table.increments('id').queryContext('id context');
table.string('email').queryContext('email context');
})
.toSQL();
expect(spy.callCount).to.equal(3);
expect(spy.firstCall.args).to.deep.equal(['id', 'id context']);
expect(spy.secondCall.args).to.deep.equal(['email', 'email context']);
expect(spy.thirdCall.args).to.deep.equal(['users', 'table context']);
});
});
2015-12-08 11:37:31 -06:00
});