2016-03-02 17:07:05 +01:00
|
|
|
'use strict';
|
|
|
|
|
2020-03-08 19:48:23 -04:00
|
|
|
const { expect } = require('chai');
|
|
|
|
|
2018-09-26 08:15:31 +02:00
|
|
|
let tableSql;
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-09-26 08:15:31 +02:00
|
|
|
const sinon = require('sinon');
|
2019-07-10 22:48:43 +01:00
|
|
|
const SQLite3_Client = require('../../../lib/dialects/sqlite3');
|
2018-09-26 08:15:31 +02:00
|
|
|
const client = new SQLite3_Client({ client: 'sqlite3' });
|
2019-07-10 22:48:43 +01:00
|
|
|
const SQLite3_DDL = require('../../../lib/dialects/sqlite3/schema/ddl');
|
2021-02-05 15:35:30 +01:00
|
|
|
const {
|
|
|
|
parseCreateIndex,
|
|
|
|
} = require('../../../lib/dialects/sqlite3/schema/internal/parser');
|
|
|
|
const {
|
|
|
|
compileCreateIndex,
|
|
|
|
} = require('../../../lib/dialects/sqlite3/schema/internal/compiler');
|
2018-07-09 08:10:34 -04:00
|
|
|
|
2018-09-26 08:15:31 +02:00
|
|
|
const _ = require('lodash');
|
|
|
|
const { equal, deepEqual } = require('assert');
|
2018-07-09 08:10:34 -04:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
describe('SQLite SchemaBuilder', function () {
|
|
|
|
it('basic create table', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.createTable('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
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))'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('create json table', function () {
|
2018-09-26 08:15:31 +02:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.createTable('user', function (table) {
|
2018-09-26 08:15:31 +02:00
|
|
|
table.json('preferences');
|
|
|
|
})
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('user', function (t) {})
|
2018-09-26 08:15:31 +02:00
|
|
|
.toSQL();
|
|
|
|
expect(tableSql[0].sql).to.equal(
|
|
|
|
'create table `user` (`preferences` json)'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-02-03 13:47:32 +01:00
|
|
|
it('basic create table without primary key', function () {
|
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
|
|
|
.createTable('users', function (table) {
|
|
|
|
table.increments('id');
|
|
|
|
table.increments('other_id', { primaryKey: false });
|
|
|
|
})
|
|
|
|
.toSQL();
|
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
|
|
|
equal(
|
|
|
|
tableSql[0].sql,
|
|
|
|
'create table `users` (`id` integer not null primary key autoincrement, `other_id` integer not null autoincrement)'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('basic alter table', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.alterTable('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.increments('id');
|
|
|
|
table.string('email');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(2, tableSql.length);
|
2018-09-26 08:15:31 +02:00
|
|
|
const expected = [
|
2017-05-28 22:48:18 +02:00
|
|
|
'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'));
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('alter column not supported', function () {
|
2017-02-16 20:34:59 +02:00
|
|
|
try {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.alterTable('users', function (table) {
|
|
|
|
table.string('email').notNull().alter();
|
2018-07-09 08:10:34 -04:00
|
|
|
})
|
|
|
|
.toSQL();
|
|
|
|
expect(false).to.eql('Should have thrown an error');
|
2017-02-16 20:34:59 +02:00
|
|
|
} catch (err) {
|
2018-07-09 08:10:34 -04:00
|
|
|
expect(err.message).to.eql('Sqlite does not support alter column.');
|
2017-02-16 20:34:59 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('drop table', function () {
|
|
|
|
tableSql = client.schemaBuilder().dropTable('users').toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'drop table `users`');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02: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);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'drop table if exists `users`');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('drop unique', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.dropUnique('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'drop index `users_foo_unique`');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('drop unique, custom', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.dropUnique(null, 'foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'drop index `foo`');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('drop index', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.dropIndex('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'drop index `users_foo_index`');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('drop index, custom', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.dropIndex(null, 'foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'drop index `foo`');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('rename table', function () {
|
|
|
|
tableSql = client.schemaBuilder().renameTable('users', 'foo').toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` rename to `foo`');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding primary key', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.createTable('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.string('foo');
|
|
|
|
table.primary('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2018-07-09 08:10:34 -04:00
|
|
|
equal(
|
|
|
|
tableSql[0].sql,
|
|
|
|
'create table `users` (`foo` varchar(255), primary key (`foo`))'
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding primary key with specific identifier', function () {
|
2018-10-11 18:14:26 +02:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.createTable('users', function (table) {
|
2018-10-11 18:14:26 +02:00
|
|
|
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`))'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding composite primary key', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.createTable('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
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);
|
2018-07-09 08:10:34 -04:00
|
|
|
equal(
|
|
|
|
tableSql[0].sql,
|
|
|
|
'create table `users` (`foo` varchar(255), `order_id` varchar(255), primary key (`foo`, `order_id`))'
|
|
|
|
);
|
2018-10-11 18:14:26 +02:00
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding composite primary key with specific identifier', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.createTable('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.string('foo');
|
|
|
|
table.string('order_id');
|
2018-10-11 18:14:26 +02:00
|
|
|
table.primary(['foo', 'order_id'], 'pk-users');
|
2018-07-09 08:10:34 -04:00
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2018-07-09 08:10:34 -04:00
|
|
|
equal(
|
|
|
|
tableSql[0].sql,
|
2018-10-11 18:14:26 +02:00
|
|
|
'create table `users` (`foo` varchar(255), `order_id` varchar(255), constraint `pk-users` primary key (`foo`, `order_id`))'
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding primary key fluently', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.createTable('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.string('foo').primary();
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2018-07-09 08:10:34 -04:00
|
|
|
equal(
|
|
|
|
tableSql[0].sql,
|
|
|
|
'create table `users` (`foo` varchar(255), primary key (`foo`))'
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding primary key fluently with specific identifier', function () {
|
2018-10-11 18:14:26 +02:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.createTable('users', function (table) {
|
2018-10-11 18:14:26 +02:00
|
|
|
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`))'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding foreign key', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.createTable('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.string('foo').primary();
|
|
|
|
table.string('order_id');
|
2020-04-19 00:40:23 +02:00
|
|
|
table.foreign('order_id').references('id').on('orders');
|
2018-07-09 08:10:34 -04:00
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2018-07-09 08:10:34 -04:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding foreign key with specific identifier', function () {
|
2018-10-11 18:14:26 +02:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.createTable('users', function (table) {
|
2018-10-11 18:14:26 +02:00
|
|
|
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`))'
|
|
|
|
);
|
|
|
|
});
|
2017-02-17 00:35:43 +02:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding foreign key fluently', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.createTable('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.string('foo').primary();
|
2020-04-19 00:40:23 +02:00
|
|
|
table.string('order_id').references('id').on('orders');
|
2018-07-09 08:10:34 -04:00
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2018-07-09 08:10:34 -04:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adds a unique key with autogenerated name', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.unique('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2018-07-09 08:10:34 -04:00
|
|
|
equal(
|
|
|
|
tableSql[0].sql,
|
|
|
|
'create unique index `users_foo_unique` on `users` (`foo`)'
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding unique key with specific name', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.unique('foo', 'bar');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'create unique index `bar` on `users` (`foo`)');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding index', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.index(['foo', 'bar'], 'baz');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'create index `baz` on `users` (`foo`, `bar`)');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding incrementing id', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.increments('id');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2018-07-09 08:10:34 -04:00
|
|
|
equal(
|
|
|
|
tableSql[0].sql,
|
|
|
|
'alter table `users` add column `id` integer not null primary key autoincrement'
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding big incrementing id', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.bigIncrements('id');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2018-07-09 08:10:34 -04:00
|
|
|
equal(
|
|
|
|
tableSql[0].sql,
|
|
|
|
'alter table `users` add column `id` integer not null primary key autoincrement'
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2021-02-03 13:47:32 +01:00
|
|
|
it('adding big incrementing id without primary key', function () {
|
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
|
|
|
.table('users', function (table) {
|
|
|
|
table.bigIncrements('id', { primaryKey: false });
|
|
|
|
})
|
|
|
|
.toSQL();
|
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
|
|
|
equal(
|
|
|
|
tableSql[0].sql,
|
|
|
|
'alter table `users` add column `id` integer not null autoincrement'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding string', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.string('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` varchar(255)');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('allows setting a value in the string length, although unused by sqlite3', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.string('foo', 100);
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` varchar(100)');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('correctly interprets defaultTo(null)', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.string('foo').defaultTo(null);
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
equal(
|
|
|
|
tableSql[0].sql,
|
|
|
|
'alter table `users` add column `foo` varchar(255) default null'
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-08-08 23:18:45 +05:30
|
|
|
it('correctly escape singleQuotes passed to defaultTo()', function () {
|
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
|
|
|
.table('users', function (table) {
|
|
|
|
table.string('foo').defaultTo("single 'quoted' value");
|
|
|
|
})
|
|
|
|
.toSQL();
|
|
|
|
|
|
|
|
equal(
|
|
|
|
tableSql[0].sql,
|
|
|
|
"alter table `users` add column `foo` varchar(255) default 'single ''quoted'' value'"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('chains notNull and defaultTo', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
|
|
|
table.string('foo', 100).notNull().defaultTo('bar');
|
2018-07-09 08:10:34 -04:00
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2018-07-09 08:10:34 -04:00
|
|
|
equal(
|
|
|
|
tableSql[0].sql,
|
|
|
|
"alter table `users` add column `foo` varchar(100) not null default 'bar'"
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding text', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.text('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` text');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding big integer', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.bigInteger('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` bigint');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('bigincrements works the same as increments for sqlite3', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.bigIncrements('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2018-07-09 08:10:34 -04:00
|
|
|
equal(
|
|
|
|
tableSql[0].sql,
|
|
|
|
'alter table `users` add column `foo` integer not null primary key autoincrement'
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding integer', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.integer('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` integer');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding autoincrements', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.increments('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2018-07-09 08:10:34 -04:00
|
|
|
equal(
|
|
|
|
tableSql[0].sql,
|
|
|
|
'alter table `users` add column `foo` integer not null primary key autoincrement'
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding medium integer', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.mediumint('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` integer');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding tiny integer', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.tinyint('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` tinyint');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding small integer', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.smallint('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` integer');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding float', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.float('foo', 5, 2);
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` float');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding double', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.double('foo', 15, 8);
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` float');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding decimal', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.decimal('foo', 5, 2);
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-11-30 15:05:39 -06:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` float');
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('test adding decimal, no precision', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.decimal('foo', null);
|
|
|
|
})
|
|
|
|
.toSQL();
|
2017-11-30 15:05:39 -06:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` float');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding boolean', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.boolean('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` boolean');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding enum', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.enum('foo', ['bar', 'baz']);
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2018-07-09 08:10:34 -04:00
|
|
|
equal(
|
|
|
|
tableSql[0].sql,
|
|
|
|
"alter table `users` add column `foo` text check (`foo` in ('bar', 'baz'))"
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding date', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.date('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` date');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding date time', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.dateTime('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` datetime');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding time', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.time('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` time');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding time stamp', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.timestamp('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` datetime');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding time stamps', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.timestamps();
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(2, tableSql.length);
|
2018-09-26 08:15:31 +02:00
|
|
|
const expected = [
|
2017-05-28 22:48:18 +02:00
|
|
|
'alter table `users` add column `created_at` datetime',
|
2018-07-09 08:10:34 -04:00
|
|
|
'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
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('adding binary', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
table.binary('foo');
|
|
|
|
})
|
|
|
|
.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
equal(1, tableSql.length);
|
2017-05-28 22:48:18 +02:00
|
|
|
equal(tableSql[0].sql, 'alter table `users` add column `foo` blob');
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('allows for on delete cascade with foreign keys, #166', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
tableSql = client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.createTable('users', function (table) {
|
2018-07-09 08:10:34 -04:00
|
|
|
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);
|
2018-07-09 08:10:34 -04:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
describe('SQLite3_DDL.prototype._doReplace', function () {
|
|
|
|
it('should not change a query that has no matches', function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
return client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.table('foo', function () {
|
2018-09-26 08:15:31 +02:00
|
|
|
const doReplace = SQLite3_DDL.prototype._doReplace;
|
2018-07-09 08:10:34 -04:00
|
|
|
|
2018-09-26 08:15:31 +02:00
|
|
|
const sql1 =
|
2018-07-09 08:10:34 -04:00
|
|
|
'CREATE TABLE `foo` (`id` integer not null primary key autoincrement, ' +
|
|
|
|
'"parent_id_test" integer, foreign key("parent_id") references `foo`(`id`))';
|
2018-09-26 08:15:31 +02:00
|
|
|
const sql2 =
|
2018-07-09 08:10:34 -04:00
|
|
|
'CREATE TABLE `foo` (`id` integer not null primary key autoincrement, ' +
|
|
|
|
'"parent_id_test" integer, foreign key("parent_id") references `bar`(`id`))';
|
|
|
|
|
2018-09-26 08:15:31 +02:00
|
|
|
const sql1b =
|
2018-07-09 08:10:34 -04:00
|
|
|
'CREATE TABLE `foo` ("id_foo" integer not null primary key autoincrement, ' +
|
|
|
|
'"parent_id_test" integer, foreign key("parent_id") references `foo`("id_foo"))';
|
2018-09-26 08:15:31 +02:00
|
|
|
const sql2b =
|
2018-07-09 08:10:34 -04:00
|
|
|
'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
|
|
|
});
|
|
|
|
});
|
2018-02-01 23:41:01 +01:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
describe('queryContext', function () {
|
2018-02-01 23:41:01 +01:00
|
|
|
let spy;
|
|
|
|
let originalWrapIdentifier;
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
before(function () {
|
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) {
|
2018-02-01 23:41:01 +01:00
|
|
|
spy(value, queryContext);
|
|
|
|
return wrap(value);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
beforeEach(function () {
|
2018-07-07 09:47:51 +02:00
|
|
|
spy.resetHistory();
|
2018-02-01 23:41:01 +01:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
after(function () {
|
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 () {
|
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) {
|
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 () {
|
2018-02-01 23:41:01 +01:00
|
|
|
client
|
|
|
|
.schemaBuilder()
|
2020-04-19 00:40:23 +02:00
|
|
|
.createTable('users', function (table) {
|
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 () {
|
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) {
|
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 () {
|
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) {
|
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
|
|
|
});
|
2021-02-05 15:35:30 +01:00
|
|
|
|
|
|
|
describe('SQLite parser and compiler', function () {
|
|
|
|
const wrap = (v) => `"${v}"`;
|
|
|
|
|
|
|
|
describe('create index', function () {
|
|
|
|
it('basic', function () {
|
|
|
|
const sql = 'CREATE INDEX "users_index" on "users" ("foo")';
|
|
|
|
const ast = {
|
|
|
|
unique: false,
|
|
|
|
exists: false,
|
|
|
|
schema: null,
|
|
|
|
index: 'users_index',
|
|
|
|
table: 'users',
|
|
|
|
columns: [
|
|
|
|
{ name: 'foo', expression: false, collation: null, order: null },
|
|
|
|
],
|
|
|
|
where: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
const parsed = parseCreateIndex(sql);
|
|
|
|
const compiled = compileCreateIndex(ast, wrap);
|
|
|
|
|
|
|
|
expect(parsed).to.deep.equal(ast);
|
|
|
|
expect(compiled).to.equal(sql);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('unique', function () {
|
|
|
|
const sql = 'CREATE UNIQUE INDEX "users_index" on "users" ("foo")';
|
|
|
|
const ast = {
|
|
|
|
unique: true,
|
|
|
|
exists: false,
|
|
|
|
schema: null,
|
|
|
|
index: 'users_index',
|
|
|
|
table: 'users',
|
|
|
|
columns: [
|
|
|
|
{ name: 'foo', expression: false, collation: null, order: null },
|
|
|
|
],
|
|
|
|
where: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
const parsed = parseCreateIndex(sql);
|
|
|
|
const compiled = compileCreateIndex(ast, wrap);
|
|
|
|
|
|
|
|
expect(parsed).to.deep.equal(ast);
|
|
|
|
expect(compiled).to.equal(sql);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('if not exists', function () {
|
|
|
|
const sql = 'CREATE INDEX IF NOT EXISTS "users_index" on "users" ("foo")';
|
|
|
|
const ast = {
|
|
|
|
unique: false,
|
|
|
|
exists: true,
|
|
|
|
schema: null,
|
|
|
|
index: 'users_index',
|
|
|
|
table: 'users',
|
|
|
|
columns: [
|
|
|
|
{ name: 'foo', expression: false, collation: null, order: null },
|
|
|
|
],
|
|
|
|
where: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
const parsed = parseCreateIndex(sql);
|
|
|
|
const compiled = compileCreateIndex(ast, wrap);
|
|
|
|
|
|
|
|
expect(parsed).to.deep.equal(ast);
|
|
|
|
expect(compiled).to.equal(sql);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('schema', function () {
|
|
|
|
const sql = 'CREATE INDEX "schema"."users_index" on "users" ("foo")';
|
|
|
|
const ast = {
|
|
|
|
unique: false,
|
|
|
|
exists: false,
|
|
|
|
schema: 'schema',
|
|
|
|
index: 'users_index',
|
|
|
|
table: 'users',
|
|
|
|
columns: [
|
|
|
|
{ name: 'foo', expression: false, collation: null, order: null },
|
|
|
|
],
|
|
|
|
where: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
const parsed = parseCreateIndex(sql);
|
|
|
|
const compiled = compileCreateIndex(ast, wrap);
|
|
|
|
|
|
|
|
expect(parsed).to.deep.equal(ast);
|
|
|
|
expect(compiled).to.equal(sql);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('where', function () {
|
|
|
|
const sql =
|
|
|
|
'CREATE INDEX "users_index" on "users" ("foo") where foo IS NOT NULL AND bar=42';
|
|
|
|
const ast = {
|
|
|
|
unique: false,
|
|
|
|
exists: false,
|
|
|
|
schema: null,
|
|
|
|
index: 'users_index',
|
|
|
|
table: 'users',
|
|
|
|
columns: [
|
|
|
|
{ name: 'foo', expression: false, collation: null, order: null },
|
|
|
|
],
|
|
|
|
where: 'foo IS NOT NULL AND bar=42',
|
|
|
|
};
|
|
|
|
|
|
|
|
const parsed = parseCreateIndex(sql);
|
|
|
|
const compiled = compileCreateIndex(ast, wrap);
|
|
|
|
|
|
|
|
expect(parsed).to.deep.equal(ast);
|
|
|
|
expect(compiled).to.equal(sql);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('column collation and order', function () {
|
|
|
|
const sql =
|
|
|
|
'CREATE INDEX "users_index" on "users" ("foo", "baz" COLLATE BINARY, "bar" ASC, "lar" COLLATE NOCASE DESC)';
|
|
|
|
const ast = {
|
|
|
|
unique: false,
|
|
|
|
exists: false,
|
|
|
|
schema: null,
|
|
|
|
index: 'users_index',
|
|
|
|
table: 'users',
|
|
|
|
columns: [
|
|
|
|
{
|
|
|
|
name: 'foo',
|
|
|
|
expression: false,
|
|
|
|
collation: null,
|
|
|
|
order: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'baz',
|
|
|
|
expression: false,
|
|
|
|
collation: 'BINARY',
|
|
|
|
order: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'bar',
|
|
|
|
expression: false,
|
|
|
|
collation: null,
|
|
|
|
order: 'ASC',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'lar',
|
|
|
|
expression: false,
|
|
|
|
collation: 'NOCASE',
|
|
|
|
order: 'DESC',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
where: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
const parsed = parseCreateIndex(sql);
|
|
|
|
const compiled = compileCreateIndex(ast, wrap);
|
|
|
|
|
|
|
|
expect(parsed).to.deep.equal(ast);
|
|
|
|
expect(compiled).to.equal(sql);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('column expression', function () {
|
|
|
|
const sql =
|
|
|
|
'CREATE INDEX "users_index" on "users" (abs(foo), lower(baz) COLLATE RTRIM, "bar()" ASC)';
|
|
|
|
const ast = {
|
|
|
|
unique: false,
|
|
|
|
exists: false,
|
|
|
|
schema: null,
|
|
|
|
index: 'users_index',
|
|
|
|
table: 'users',
|
|
|
|
columns: [
|
|
|
|
{
|
|
|
|
name: 'abs(foo)',
|
|
|
|
expression: true,
|
|
|
|
collation: null,
|
|
|
|
order: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'lower(baz)',
|
|
|
|
expression: true,
|
|
|
|
collation: 'RTRIM',
|
|
|
|
order: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'bar()',
|
|
|
|
expression: false,
|
|
|
|
collation: null,
|
|
|
|
order: 'ASC',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
where: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
const parsed = parseCreateIndex(sql);
|
|
|
|
const compiled = compileCreateIndex(ast, wrap);
|
|
|
|
|
|
|
|
expect(parsed).to.deep.equal(ast);
|
|
|
|
expect(compiled).to.equal(sql);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('special character identifier', function () {
|
|
|
|
const sql =
|
|
|
|
'CREATE INDEX "$chema"."users index" on "use-rs" (" ( ", " COLLATE ", " ""foo"" " ASC, "``b a z``" DESC, "[[``""bar""``]]") where foo IS NOT NULL';
|
|
|
|
const ast = {
|
|
|
|
unique: false,
|
|
|
|
exists: false,
|
|
|
|
schema: '$chema',
|
|
|
|
index: 'users index',
|
|
|
|
table: 'use-rs',
|
|
|
|
columns: [
|
|
|
|
{
|
|
|
|
name: ' ( ',
|
|
|
|
expression: false,
|
|
|
|
collation: null,
|
|
|
|
order: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: ' COLLATE ',
|
|
|
|
expression: false,
|
|
|
|
collation: null,
|
|
|
|
order: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: ' ""foo"" ',
|
|
|
|
expression: false,
|
|
|
|
collation: null,
|
|
|
|
order: 'ASC',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: '``b a z``',
|
|
|
|
expression: false,
|
|
|
|
collation: null,
|
|
|
|
order: 'DESC',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: '[[``""bar""``]]',
|
|
|
|
expression: false,
|
|
|
|
collation: null,
|
|
|
|
order: null,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
where: 'foo IS NOT NULL',
|
|
|
|
};
|
|
|
|
|
|
|
|
const parsed = parseCreateIndex(sql);
|
|
|
|
const compiled = compileCreateIndex(ast, wrap);
|
|
|
|
|
|
|
|
expect(parsed).to.deep.equal(ast);
|
|
|
|
expect(compiled).to.equal(sql);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('no wrap', function () {
|
|
|
|
const sql =
|
|
|
|
'CREATE INDEX users_index on users (foo COLLATE BINARY ASC) where foo IS NOT NULL';
|
|
|
|
const ast = {
|
|
|
|
unique: false,
|
|
|
|
exists: false,
|
|
|
|
schema: null,
|
|
|
|
index: 'users_index',
|
|
|
|
table: 'users',
|
|
|
|
columns: [
|
|
|
|
{
|
|
|
|
name: 'foo',
|
|
|
|
expression: false,
|
|
|
|
collation: 'BINARY',
|
|
|
|
order: 'ASC',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
where: 'foo IS NOT NULL',
|
|
|
|
};
|
|
|
|
|
|
|
|
const parsed = parseCreateIndex(sql);
|
|
|
|
const compiled = compileCreateIndex(ast);
|
|
|
|
|
|
|
|
expect(parsed).to.deep.equal(ast);
|
|
|
|
expect(compiled).to.equal(sql);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('lowercase', function () {
|
|
|
|
const sql =
|
|
|
|
'create index "users_index" ON "users" ("foo" collate BINARY asc) WHERE "foo" is not null';
|
|
|
|
const newSql =
|
|
|
|
'CREATE INDEX "users_index" on "users" ("foo" COLLATE BINARY ASC) where "foo" is not null';
|
|
|
|
|
|
|
|
const parsedSql = compileCreateIndex(parseCreateIndex(sql), wrap);
|
|
|
|
|
|
|
|
expect(parsedSql).to.equal(newSql);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('whitespaces', function () {
|
|
|
|
const sql =
|
|
|
|
'CREATE INDEX "users_index"\non\t"users"("foo" COLLATE\tBINARY\nASC)\r\nwhere "foo"\nIS\tNOT\r\nNULL';
|
|
|
|
const newSql =
|
|
|
|
'CREATE INDEX "users_index" on "users" ("foo" COLLATE BINARY ASC) where "foo" IS NOT NULL';
|
|
|
|
|
|
|
|
const parsedSql = compileCreateIndex(parseCreateIndex(sql), wrap);
|
|
|
|
|
|
|
|
expect(parsedSql).to.equal(newSql);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('wrap', function () {
|
|
|
|
const sql =
|
|
|
|
'CREATE INDEX "schema".[users index] on `users` ("foo " ASC, [b a z] DESC, ` bar`)';
|
|
|
|
const newSql =
|
|
|
|
'CREATE INDEX "schema"."users index" on "users" ("foo " ASC, "b a z" DESC, " bar")';
|
|
|
|
|
|
|
|
const parsedSql = compileCreateIndex(parseCreateIndex(sql), wrap);
|
|
|
|
|
|
|
|
expect(parsedSql).to.equal(newSql);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|