knex/test/unit/schema/sqlite3.js

825 lines
22 KiB
JavaScript
Raw Normal View History

2016-03-02 17:07:05 +01:00
'use strict';
const { expect } = require('chai');
let tableSql;
2016-03-02 17:07:05 +01:00
const sinon = require('sinon');
2019-07-10 22:48:43 +01:00
const SQLite3_Client = require('../../../lib/dialects/sqlite3');
const client = new SQLite3_Client({ client: 'sqlite3' });
2019-07-10 22:48:43 +01:00
const SQLite3_DDL = require('../../../lib/dialects/sqlite3/schema/ddl');
const _ = require('lodash');
const { equal, deepEqual } = require('assert');
describe('SQLite SchemaBuilder', function() {
it('basic create table', function() {
tableSql = client
.schemaBuilder()
.createTable('users', function(table) {
table.increments('id');
table.string('email');
})
.toSQL();
equal(1, tableSql.length);
equal(
tableSql[0].sql,
'create table `users` (`id` integer not null primary key autoincrement, `email` varchar(255))'
);
});
it('create json table', function() {
tableSql = client
.schemaBuilder()
.createTable('user', function(table) {
table.json('preferences');
})
.table('user', function(t) {})
.toSQL();
expect(tableSql[0].sql).to.equal(
'create table `user` (`preferences` json)'
);
});
it('basic alter table', function() {
tableSql = client
.schemaBuilder()
.alterTable('users', function(table) {
table.increments('id');
table.string('email');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(2, tableSql.length);
const expected = [
'alter table `users` add column `id` integer not null primary key autoincrement',
'alter table `users` add column `email` varchar(255)',
2016-03-02 17:07:05 +01:00
];
expect(expected).to.eql(_.map(tableSql, 'sql'));
});
it('alter column not supported', function() {
try {
tableSql = client
.schemaBuilder()
.alterTable('users', function(table) {
table
.string('email')
.notNull()
.alter();
})
.toSQL();
expect(false).to.eql('Should have thrown an error');
} catch (err) {
expect(err.message).to.eql('Sqlite does not support alter column.');
}
});
it('drop table', function() {
tableSql = client
.schemaBuilder()
.dropTable('users')
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'drop table `users`');
2016-03-02 17:07:05 +01:00
});
it('drop table if exists', function() {
tableSql = client
.schemaBuilder()
.dropTableIfExists('users')
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'drop table if exists `users`');
2016-03-02 17:07:05 +01:00
});
it('drop unique', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.dropUnique('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'drop index `users_foo_unique`');
2016-03-02 17:07:05 +01:00
});
it('drop unique, custom', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.dropUnique(null, 'foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'drop index `foo`');
2016-03-02 17:07:05 +01:00
});
it('drop index', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.dropIndex('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'drop index `users_foo_index`');
2016-03-02 17:07:05 +01:00
});
it('drop index, custom', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.dropIndex(null, 'foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'drop index `foo`');
2016-03-02 17:07:05 +01:00
});
it('rename table', function() {
tableSql = client
.schemaBuilder()
.renameTable('users', 'foo')
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` rename to `foo`');
2016-03-02 17:07:05 +01:00
});
it('adding primary key', function() {
tableSql = client
.schemaBuilder()
.createTable('users', function(table) {
table.string('foo');
table.primary('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(
tableSql[0].sql,
'create table `users` (`foo` varchar(255), primary key (`foo`))'
);
2016-03-02 17:07:05 +01:00
});
it('adding primary key with specific identifier', function() {
tableSql = client
.schemaBuilder()
.createTable('users', function(table) {
table.string('foo');
table.primary('foo', 'pk-users');
})
.toSQL();
equal(1, tableSql.length);
equal(
tableSql[0].sql,
'create table `users` (`foo` varchar(255), constraint `pk-users` primary key (`foo`))'
);
});
it('adding composite primary key', function() {
tableSql = client
.schemaBuilder()
.createTable('users', function(table) {
table.string('foo');
table.string('order_id');
table.primary(['foo', 'order_id']);
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(
tableSql[0].sql,
'create table `users` (`foo` varchar(255), `order_id` varchar(255), primary key (`foo`, `order_id`))'
);
});
2016-03-02 17:07:05 +01:00
it('adding composite primary key with specific identifier', function() {
tableSql = client
.schemaBuilder()
.createTable('users', function(table) {
table.string('foo');
table.string('order_id');
table.primary(['foo', 'order_id'], 'pk-users');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(
tableSql[0].sql,
'create table `users` (`foo` varchar(255), `order_id` varchar(255), constraint `pk-users` primary key (`foo`, `order_id`))'
);
2016-03-02 17:07:05 +01:00
});
it('adding primary key fluently', function() {
tableSql = client
.schemaBuilder()
.createTable('users', function(table) {
table.string('foo').primary();
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(
tableSql[0].sql,
'create table `users` (`foo` varchar(255), primary key (`foo`))'
);
2016-03-02 17:07:05 +01:00
});
it('adding primary key fluently with specific identifier', function() {
tableSql = client
.schemaBuilder()
.createTable('users', function(table) {
table.string('foo').primary('pk-users');
})
.toSQL();
equal(1, tableSql.length);
equal(
tableSql[0].sql,
'create table `users` (`foo` varchar(255), constraint `pk-users` primary key (`foo`))'
);
});
it('adding foreign key', function() {
tableSql = client
.schemaBuilder()
.createTable('users', function(table) {
table.string('foo').primary();
table.string('order_id');
table
.foreign('order_id')
.references('id')
.on('orders');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(
tableSql[0].sql,
'create table `users` (`foo` varchar(255), `order_id` varchar(255), foreign key(`order_id`) references `orders`(`id`), primary key (`foo`))'
);
2016-03-02 17:07:05 +01:00
});
it('adding foreign key with specific identifier', function() {
tableSql = client
.schemaBuilder()
.createTable('users', function(table) {
table.string('foo').primary();
table.string('order_id');
table
.foreign('order_id', 'fk-users-orders')
.references('id')
.on('orders');
})
.toSQL();
equal(1, tableSql.length);
equal(
tableSql[0].sql,
'create table `users` (`foo` varchar(255), `order_id` varchar(255), constraint `fk-users-orders` foreign key(`order_id`) references `orders`(`id`), primary key (`foo`))'
);
});
it('adding foreign key fluently', function() {
tableSql = client
.schemaBuilder()
.createTable('users', function(table) {
table.string('foo').primary();
table
.string('order_id')
.references('id')
.on('orders');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(
tableSql[0].sql,
'create table `users` (`foo` varchar(255), `order_id` varchar(255), foreign key(`order_id`) references `orders`(`id`), primary key (`foo`))'
);
2016-03-02 17:07:05 +01:00
});
it('adds a unique key with autogenerated name', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.unique('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(
tableSql[0].sql,
'create unique index `users_foo_unique` on `users` (`foo`)'
);
2016-03-02 17:07:05 +01:00
});
it('adding unique key with specific name', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.unique('foo', 'bar');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'create unique index `bar` on `users` (`foo`)');
2016-03-02 17:07:05 +01:00
});
it('adding index', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.index(['foo', 'bar'], 'baz');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'create index `baz` on `users` (`foo`, `bar`)');
2016-03-02 17:07:05 +01:00
});
it('adding incrementing id', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.increments('id');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(
tableSql[0].sql,
'alter table `users` add column `id` integer not null primary key autoincrement'
);
2016-03-02 17:07:05 +01:00
});
it('adding big incrementing id', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.bigIncrements('id');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(
tableSql[0].sql,
'alter table `users` add column `id` integer not null primary key autoincrement'
);
2016-03-02 17:07:05 +01:00
});
it('adding string', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.string('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` varchar(255)');
2016-03-02 17:07:05 +01:00
});
it('allows setting a value in the string length, although unused by sqlite3', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.string('foo', 100);
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` varchar(100)');
2016-03-02 17:07:05 +01:00
});
it('correctly interprets defaultTo(null)', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.string('foo').defaultTo(null);
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(
tableSql[0].sql,
'alter table `users` add column `foo` varchar(255) default null'
);
2016-03-02 17:07:05 +01:00
});
it('chains notNull and defaultTo', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table
.string('foo', 100)
.notNull()
.defaultTo('bar');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(
tableSql[0].sql,
"alter table `users` add column `foo` varchar(100) not null default 'bar'"
);
2016-03-02 17:07:05 +01:00
});
it('adding text', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.text('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` text');
2016-03-02 17:07:05 +01:00
});
it('adding big integer', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.bigInteger('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` bigint');
2016-03-02 17:07:05 +01:00
});
it('bigincrements works the same as increments for sqlite3', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.bigIncrements('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(
tableSql[0].sql,
'alter table `users` add column `foo` integer not null primary key autoincrement'
);
2016-03-02 17:07:05 +01:00
});
it('adding integer', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.integer('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` integer');
2016-03-02 17:07:05 +01:00
});
it('adding autoincrements', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.increments('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(
tableSql[0].sql,
'alter table `users` add column `foo` integer not null primary key autoincrement'
);
2016-03-02 17:07:05 +01:00
});
it('adding medium integer', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.mediumint('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` integer');
2016-03-02 17:07:05 +01:00
});
it('adding tiny integer', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.tinyint('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` tinyint');
2016-03-02 17:07:05 +01:00
});
it('adding small integer', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.smallint('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` integer');
2016-03-02 17:07:05 +01:00
});
it('adding float', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.float('foo', 5, 2);
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` float');
2016-03-02 17:07:05 +01:00
});
it('adding double', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.double('foo', 15, 8);
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` float');
2016-03-02 17:07:05 +01:00
});
it('adding decimal', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.decimal('foo', 5, 2);
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` float');
});
it('test adding decimal, no precision', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.decimal('foo', null);
})
.toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` float');
2016-03-02 17:07:05 +01:00
});
it('adding boolean', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.boolean('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` boolean');
2016-03-02 17:07:05 +01:00
});
it('adding enum', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.enum('foo', ['bar', 'baz']);
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(
tableSql[0].sql,
"alter table `users` add column `foo` text check (`foo` in ('bar', 'baz'))"
);
2016-03-02 17:07:05 +01:00
});
it('adding date', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.date('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` date');
2016-03-02 17:07:05 +01:00
});
it('adding date time', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.dateTime('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` datetime');
2016-03-02 17:07:05 +01:00
});
it('adding time', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.time('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` time');
2016-03-02 17:07:05 +01:00
});
it('adding time stamp', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.timestamp('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` datetime');
2016-03-02 17:07:05 +01:00
});
it('adding time stamps', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.timestamps();
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(2, tableSql.length);
const expected = [
'alter table `users` add column `created_at` datetime',
'alter table `users` add column `updated_at` datetime',
2016-03-02 17:07:05 +01:00
];
2016-03-02 17:25:07 +01:00
deepEqual(expected, _.map(tableSql, 'sql'));
2016-03-02 17:07:05 +01:00
});
it('adding binary', function() {
tableSql = client
.schemaBuilder()
.table('users', function(table) {
table.binary('foo');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table `users` add column `foo` blob');
2016-03-02 17:07:05 +01:00
});
it('allows for on delete cascade with foreign keys, #166', function() {
tableSql = client
.schemaBuilder()
.createTable('users', function(table) {
table
.string('user_id', 36)
.index()
.references('id')
.inTable('user')
.onDelete('CASCADE');
})
.toSQL();
2016-03-02 17:07:05 +01:00
equal(2, tableSql.length);
equal(
tableSql[0].sql,
'create table `users` (`user_id` varchar(36), foreign key(`user_id`) references `user`(`id`) on delete CASCADE)'
);
2016-03-02 17:07:05 +01:00
});
describe('SQLite3_DDL.prototype._doReplace', function() {
it('should not change a query that has no matches', function() {
return client
.schemaBuilder()
.table('foo', function() {
const doReplace = SQLite3_DDL.prototype._doReplace;
const sql1 =
'CREATE TABLE `foo` (`id` integer not null primary key autoincrement, ' +
'"parent_id_test" integer, foreign key("parent_id") references `foo`(`id`))';
const sql2 =
'CREATE TABLE `foo` (`id` integer not null primary key autoincrement, ' +
'"parent_id_test" integer, foreign key("parent_id") references `bar`(`id`))';
const sql1b =
'CREATE TABLE `foo` ("id_foo" integer not null primary key autoincrement, ' +
'"parent_id_test" integer, foreign key("parent_id") references `foo`("id_foo"))';
const sql2b =
'CREATE TABLE `foo` ("id_foo" integer not null primary key autoincrement, ' +
'"parent_id_test" integer, foreign key("parent_id") references `bar`(`id`))';
expect(doReplace(sql1, '`bar`', '"lar"')).to.equal(sql1);
expect(doReplace(sql1, '`id`', '"id_foo"')).to.equal(sql1b);
expect(doReplace(sql2, '`id`', '"id_foo"')).to.equal(sql2b);
})
.toSQL();
2016-03-02 17:07:05 +01:00
});
});
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
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;
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;
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);
};
});
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
});
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;
});
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')
.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']);
});
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()
.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]);
});
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')
.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']);
});
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')
.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-07-15 23:16:30 -03:00
});