Field identifiers were not quoted properly in Sqlite3 dialect (#2087)

* field wrapping changed from quotes to backticks in sqlite3 dialect; unit tests adjusted accordingly

* addColumn was accidently removed
This commit is contained in:
irsl 2017-05-28 22:48:18 +02:00 committed by Mikael Lepistö
parent 17bf763cb1
commit a47a077330
11 changed files with 154 additions and 148 deletions

View File

@ -57,6 +57,10 @@ assign(Client_SQLite3.prototype, {
return new SQLite3_DDL(this, compiler, pragma, connection)
},
wrapIdentifier(value) {
return (value !== '*' ? `\`${value.replace(/`/g, '``')}\`` : '*')
},
// Get a raw connection from the database, returning a promise with the connection object.
acquireRawConnection() {
return new Promise((resolve, reject) => {

View File

@ -57,7 +57,7 @@ module.exports = function(knex) {
.testSql(function(tester) {
tester('mysql', 'truncate `test_table_two`');
tester('postgresql', 'truncate "test_table_two" restart identity');
tester('sqlite3', "delete from \"test_table_two\"");
tester('sqlite3', "delete from `test_table_two`");
tester('oracle', "truncate table \"test_table_two\"");
tester('mssql', 'truncate table [test_table_two]');
})
@ -244,7 +244,7 @@ module.exports = function(knex) {
}).testSql(function(tester) {
tester('mysql', ["show fields from `accounts` where field = ?"]);
tester('postgresql', ["alter table \"accounts\" rename \"about\" to \"about_col\""]);
tester('sqlite3', ["PRAGMA table_info(\"accounts\")"]);
tester('sqlite3', ["PRAGMA table_info(`accounts`)"]);
tester('oracle', ["alter table \"accounts\" rename column \"about\" to \"about_col\""]);
tester('mssql', ["exec sp_rename ?, ?, 'COLUMN'"]);
});
@ -281,7 +281,7 @@ module.exports = function(knex) {
}).testSql(function(tester) {
tester('mysql', ["alter table `accounts` drop `first_name`"]);
tester('postgresql', ['alter table "accounts" drop column "first_name"']);
tester('sqlite3', ["PRAGMA table_info(\"accounts\")"]);
tester('sqlite3', ["PRAGMA table_info(`accounts`)"]);
tester('oracle', ['alter table "accounts" drop ("first_name")']);
//tester('oracledb', ['alter table "accounts" drop ("first_name")']);
tester('mssql', ["ALTER TABLE [accounts] DROP COLUMN [first_name]"]);

View File

@ -35,10 +35,10 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select sum("logins") from "accounts"',
'select sum(`logins`) from `accounts`',
[],
[{
'sum("logins")': 10
'sum(`logins`)': 10
}]
);
tester(
@ -74,7 +74,7 @@ module.exports = function(knex) {
// mysql: 1.6667
tester('mysql', 'select avg(`logins`) from `accounts`', [], checkResRange.bind(null, 'avg(`logins`)'));
// sqlite: 1.6666666666666667
tester('sqlite3', 'select avg("logins") from "accounts"', [], checkResRange.bind(null, 'avg("logins")'));
tester('sqlite3', 'select avg(`logins`) from `accounts`', [], checkResRange.bind(null, 'avg(`logins`)'));
// postgres: '1.6666666666666667'
tester('postgresql', 'select avg("logins") from "accounts"', [], checkResRange.bind(null, 'avg'));
// oracle: 1.66666666666667
@ -106,10 +106,10 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select count("id") from "accounts"',
'select count(`id`) from `accounts`',
[],
[{
'count("id")': 6
'count(`id`)': 6
}]
);
tester(
@ -157,12 +157,12 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select count("id"), max("logins"), min("logins") from "accounts"',
'select count(`id`), max(`logins`), min(`logins`) from `accounts`',
[],
[{
'count("id")': 6,
'max("logins")': 2,
'min("logins")': 1
'count(`id`)': 6,
'max(`logins`)': 2,
'min(`logins`)': 1
}]
);
tester(
@ -212,12 +212,12 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select count(distinct "id"), sum(distinct "logins"), avg(distinct "logins") from "accounts"',
'select count(distinct `id`), sum(distinct `logins`), avg(distinct `logins`) from `accounts`',
[],
[{
'count(distinct "id")': 6,
'sum(distinct "logins")': 3,
'avg(distinct "logins")': 1.5
'count(distinct `id`)': 6,
'sum(distinct `logins`)': 3,
'avg(distinct `logins`)': 1.5
}]
);
tester(
@ -267,12 +267,12 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select count("id") from "accounts" group by "logins"',
'select count(`id`) from `accounts` group by `logins`',
[],
[{
'count("id")': 2
'count(`id`)': 2
},{
'count("id")': 4
'count(`id`)': 4
}]
);
tester(
@ -316,10 +316,10 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select count("id") from "accounts" group by "first_name"',
'select count(`id`) from `accounts` group by `first_name`',
[],
[{
'count("id")': 6
'count(`id`)': 6
}]
);
tester(

View File

@ -25,7 +25,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'delete from "accounts" where "id" = ?',
'delete from `accounts` where `id` = ?',
[1],
1
);
@ -73,7 +73,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'delete from "accounts" where "id" = ?',
'delete from `accounts` where `id` = ?',
[2],
1
);

View File

@ -35,7 +35,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?)',
'insert into `accounts` (`about`, `created_at`, `email`, `first_name`, `last_name`, `logins`, `updated_at`) values (?, ?, ?, ?, ?, ?, ?)',
['Lorem ipsum Dolore labore incididunt enim.', d,'test@example.com','Test','User', 1, d],
[1]
);
@ -88,7 +88,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") select ? as "about", ? as "created_at", ? as "email", ? as "first_name", ? as "last_name", ? as "logins", ? as "updated_at" union all select ? as "about", ? as "created_at", ? as "email", ? as "first_name", ? as "last_name", ? as "logins", ? as "updated_at"',
'insert into `accounts` (`about`, `created_at`, `email`, `first_name`, `last_name`, `logins`, `updated_at`) select ? as `about`, ? as `created_at`, ? as `email`, ? as `first_name`, ? as `last_name`, ? as `logins`, ? as `updated_at` union all select ? as `about`, ? as `created_at`, ? as `email`, ? as `first_name`, ? as `last_name`, ? as `logins`, ? as `updated_at`',
['Lorem ipsum Dolore labore incididunt enim.', d,'test2@example.com','Test','User',1, d,'Lorem ipsum Dolore labore incididunt enim.', d,'test3@example.com','Test','User',2, d],
[3]
);
@ -184,7 +184,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") select ? as "about", ? as "created_at", ? as "email", ? as "first_name", ? as "last_name", ? as "logins", ? as "updated_at" union all select ? as "about", ? as "created_at", ? as "email", ? as "first_name", ? as "last_name", ? as "logins", ? as "updated_at"',
'insert into `accounts` (`about`, `created_at`, `email`, `first_name`, `last_name`, `logins`, `updated_at`) select ? as `about`, ? as `created_at`, ? as `email`, ? as `first_name`, ? as `last_name`, ? as `logins`, ? as `updated_at` union all select ? as `about`, ? as `created_at`, ? as `email`, ? as `first_name`, ? as `last_name`, ? as `logins`, ? as `updated_at`',
['Lorem ipsum Dolore labore incididunt enim.', d,'test4@example.com','Test','User',2, d,'Lorem ipsum Dolore labore incididunt enim.', d,'test5@example.com','Test','User',2, d],
[5]
);
@ -248,7 +248,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?)',
'insert into `accounts` (`about`, `created_at`, `email`, `first_name`, `last_name`, `logins`, `updated_at`) values (?, ?, ?, ?, ?, ?, ?)',
['Lorem ipsum Dolore labore incididunt enim.', d, 'test5@example.com','Test','User', 2, d]
);
tester(
@ -296,7 +296,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?)',
'insert into `accounts` (`about`, `created_at`, `email`, `first_name`, `last_name`, `logins`, `updated_at`) values (?, ?, ?, ?, ?, ?, ?)',
['Lorem ipsum Dolore labore incididunt enim.', d, 'test6@example.com','Test','User',2, d],
[6]
);
@ -333,7 +333,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'insert into "datatype_test" ("enum_value") values (?)',
'insert into `datatype_test` (`enum_value`) values (?)',
['d'],
[1]
);
@ -413,7 +413,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'insert into "test_default_table" default values',
'insert into `test_default_table` default values',
[],
[1]
);
@ -458,7 +458,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'insert into "test_default_table2" default values',
'insert into `test_default_table2` default values',
[],
[1]
);
@ -549,7 +549,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'insert into "test_table_two" ("account_id", "details", "status") values (?, ?, ?)',
'insert into `test_table_two` (`account_id`, `details`, `status`) values (?, ?, ?)',
[10,'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.',0],
[4]
);

View File

@ -88,7 +88,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select "accounts".*, "test_table_two"."details" from "accounts" inner join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" order by "accounts"."id" asc', [], [{
'select `accounts`.*, `test_table_two`.`details` from `accounts` inner join `test_table_two` on `accounts`.`id` = `test_table_two`.`account_id` order by `accounts`.`id` asc', [], [{
id: 1,
first_name: 'Test',
last_name: 'User',
@ -349,7 +349,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select "accounts".*, "test_table_two"."details" from "accounts" left join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" order by "accounts"."id" asc', [], [{
'select `accounts`.*, `test_table_two`.`details` from `accounts` left join `test_table_two` on `accounts`.`id` = `test_table_two`.`account_id` order by `accounts`.`id` asc', [], [{
id: 1,
first_name: 'Test',
last_name: 'User',
@ -746,7 +746,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select * from "accounts" left join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" or "accounts"."email" = "test_table_two"."details" order by "accounts"."id" asc', [], [{
'select * from `accounts` left join `test_table_two` on `accounts`.`id` = `test_table_two`.`account_id` or `accounts`.`email` = `test_table_two`.`details` order by `accounts`.`id` asc', [], [{
id: 1,
first_name: 'Test',
last_name: 'User',
@ -976,7 +976,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select "accounts"."email" as "e1", "a2"."email" as "e2" from "accounts" inner join "accounts" as "a2" on "a2"."email" <> "accounts"."email" where "a2"."email" = ? order by "e1" asc limit ?',
'select `accounts`.`email` as `e1`, `a2`.`email` as `e2` from `accounts` inner join `accounts` as `a2` on `a2`.`email` <> `accounts`.`email` where `a2`.`email` = ? order by `e1` asc limit ?',
['test2@example.com', 5],
[{
e1: 'test3@example.com',
@ -1096,7 +1096,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select "accounts"."email" as "e1", "a2"."email" as "e2" from "accounts" inner join "accounts" as "a2" on "accounts"."email" <> "a2"."email" or "accounts"."id" = 2 where "a2"."email" = ? order by "e1" asc limit ?',
'select `accounts`.`email` as `e1`, `a2`.`email` as `e2` from `accounts` inner join `accounts` as `a2` on `accounts`.`email` <> `a2`.`email` or `accounts`.`id` = 2 where `a2`.`email` = ? order by `e1` asc limit ?',
['test2@example.com', 5],
[{
e1: 'test2@example.com',
@ -1189,7 +1189,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select "account_id" from "accounts" cross join "test_table_two" order by "account_id" asc',
'select `account_id` from `accounts` cross join `test_table_two` order by `account_id` asc',
[],
function (res) {
return res.length === 30;
@ -1258,7 +1258,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select "a1"."email", "a2"."email" from "accounts" as "a1" left join "accounts" as "a2" on "a1"."email" <> "a2"."email" where a1.id = 1 limit ?',
'select `a1`.`email`, `a2`.`email` from `accounts` as `a1` left join `accounts` as `a2` on `a1`.`email` <> `a2`.`email` where a1.id = 1 limit ?',
[2],
[{
email: 'test2@example.com'

View File

@ -31,7 +31,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select "id" from "accounts" order by "id" asc',
'select `id` from `accounts` order by `id` asc',
[],
[1, 2, 3, 4, 5, 6]
);
@ -67,7 +67,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select "accounts"."id" from "accounts" order by "accounts"."id" asc',
'select `accounts`.`id` from `accounts` order by `accounts`.`id` asc',
[],
[1, 2, 3, 4, 5, 6]
);
@ -103,7 +103,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select "id" from "accounts" order by "id" asc limit ? offset ?',
'select `id` from `accounts` order by `id` asc limit ? offset ?',
[-1, 2],
[3, 4, 5, 6]
);
@ -139,7 +139,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select "id", "first_name" from "accounts" order by "id" asc limit ?',
'select `id`, `first_name` from `accounts` order by `id` asc limit ?',
[1],
{ id: 1, first_name: 'Test' }
);
@ -256,7 +256,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select "first_name", "last_name" from "accounts" where "id" = ?',
'select `first_name`, `last_name` from `accounts` where `id` = ?',
[1],
[{
first_name: 'Test',
@ -310,7 +310,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select "first_name", "last_name" from "accounts" where "id" = ?',
'select `first_name`, `last_name` from `accounts` where `id` = ?',
[1],
[{
first_name: 'Test',
@ -356,7 +356,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select "email", "logins" from "accounts" where "id" > ?',
'select `email`, `logins` from `accounts` where `id` > ?',
[1]
);
tester(
@ -412,7 +412,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select * from "accounts" where "id" = ?',
'select * from `accounts` where `id` = ?',
[1],
[{
id: 1,
@ -481,7 +481,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select "first_name", "email" from "accounts" where "id" is null',
'select `first_name`, `email` from `accounts` where `id` is null',
[],
[]
);
@ -521,7 +521,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'select * from "accounts" where "id" = ?',
'select * from `accounts` where `id` = ?',
[0],
[]
);

View File

@ -28,7 +28,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ?',
'update `accounts` set `email` = ?, `first_name` = ?, `last_name` = ? where `id` = ?',
['test100@example.com','User','Test',1],
1
);
@ -139,7 +139,7 @@ module.exports = function(knex) {
);
tester(
'sqlite3',
'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ?',
'update `accounts` set `email` = ?, `first_name` = ?, `last_name` = ? where `id` = ?',
['test100@example.com','UpdatedUser','UpdatedTest',1],
1
);

View File

@ -13,8 +13,8 @@ module.exports = function(knex) {
it('has a dropTableIfExists method', function() {
return Promise.all([
knex.schema.dropTableIfExists('test_foreign_table_two').testSql(function(tester) {
tester(['sqlite3', 'pg'], ['drop table if exists "test_foreign_table_two"']);
tester('mysql', ['drop table if exists `test_foreign_table_two`']);
tester(['pg'], ['drop table if exists "test_foreign_table_two"']);
tester(['sqlite3', 'mysql'], ['drop table if exists `test_foreign_table_two`']);
tester('oracle', [
"begin execute immediate 'drop table \"test_foreign_table_two\"'; exception when others then if sqlcode != -942 then raise; end if; end;",
"begin execute immediate 'drop sequence \"test_foreign_table_two_seq\"'; exception when others then if sqlcode != -2289 then raise; end if; end;"
@ -90,7 +90,7 @@ module.exports = function(knex) {
}).testSql(function(tester) {
tester('mysql', ['create table `test_table_one` (`id` bigint unsigned not null auto_increment primary key, `first_name` varchar(255), `last_name` varchar(255), `email` varchar(255) null, `logins` int default \'1\', `about` text comment \'A comment.\', `created_at` datetime, `updated_at` datetime) default character set utf8 engine = InnoDB comment = \'A table comment.\'','alter table `test_table_one` add index `test_table_one_first_name_index`(`first_name`)','alter table `test_table_one` add unique `test_table_one_email_unique`(`email`)','alter table `test_table_one` add index `test_table_one_logins_index`(`logins`)']);
tester('pg', ['create table "test_table_one" ("id" bigserial primary key, "first_name" varchar(255), "last_name" varchar(255), "email" varchar(255) null, "logins" integer default \'1\', "about" text, "created_at" timestamptz, "updated_at" timestamptz)','comment on table "test_table_one" is \'A table comment.\'',"comment on column \"test_table_one\".\"logins\" is NULL",'comment on column "test_table_one"."about" is \'A comment.\'','create index "test_table_one_first_name_index" on "test_table_one" ("first_name")','alter table "test_table_one" add constraint "test_table_one_email_unique" unique ("email")','create index "test_table_one_logins_index" on "test_table_one" ("logins")']);
tester('sqlite3', ['create table "test_table_one" ("id" integer not null primary key autoincrement, "first_name" varchar(255), "last_name" varchar(255), "email" varchar(255) null, "logins" integer default \'1\', "about" text, "created_at" datetime, "updated_at" datetime)','create index "test_table_one_first_name_index" on "test_table_one" ("first_name")','create unique index "test_table_one_email_unique" on "test_table_one" ("email")','create index "test_table_one_logins_index" on "test_table_one" ("logins")']);
tester('sqlite3', ['create table `test_table_one` (`id` integer not null primary key autoincrement, `first_name` varchar(255), `last_name` varchar(255), `email` varchar(255) null, `logins` integer default \'1\', `about` text, `created_at` datetime, `updated_at` datetime)','create index `test_table_one_first_name_index` on `test_table_one` (`first_name`)','create unique index `test_table_one_email_unique` on `test_table_one` (`email`)','create index `test_table_one_logins_index` on `test_table_one` (`logins`)']);
tester('oracle', [
'create table "test_table_one" ("id" number(20, 0) not null primary key, "first_name" varchar2(255), "last_name" varchar2(255), "email" varchar2(255) null, "logins" integer default \'1\', "about" varchar2(4000), "created_at" timestamp with time zone, "updated_at" timestamp with time zone)',
'comment on table "test_table_one" is \'A table comment.\'',
@ -136,7 +136,7 @@ module.exports = function(knex) {
}).testSql(function(tester) {
tester('mysql', ['create table `test_table_three` (`main` int not null, `paragraph` text) default character set utf8 engine = InnoDB','alter table `test_table_three` add primary key `test_table_three_pkey`(`main`)']);
tester('pg', ['create table "test_table_three" ("main" integer not null, "paragraph" text default \'Lorem ipsum Qui quis qui in.\')','alter table "test_table_three" add constraint "test_table_three_pkey" primary key ("main")']);
tester('sqlite3', ['create table "test_table_three" ("main" integer not null, "paragraph" text default \'Lorem ipsum Qui quis qui in.\', primary key ("main"))']);
tester('sqlite3', ['create table `test_table_three` (`main` integer not null, `paragraph` text default \'Lorem ipsum Qui quis qui in.\', primary key (`main`))']);
tester('oracle', ['create table "test_table_three" ("main" integer not null, "paragraph" clob default \'Lorem ipsum Qui quis qui in.\')','alter table "test_table_three" add constraint "test_table_three_pkey" primary key ("main")']);
tester('mssql', ['CREATE TABLE [test_table_three] ([main] int not null, [paragraph] nvarchar(max), CONSTRAINT [test_table_three_pkey] PRIMARY KEY ([main]))']);
});
@ -150,7 +150,7 @@ module.exports = function(knex) {
}).testSql(function(tester) {
tester('mysql', ['create table `datatype_test` (`enum_value` enum(\'a\', \'b\', \'c\'), `uuid` char(36) not null) default character set utf8']);
tester('pg', ['create table "datatype_test" ("enum_value" text check ("enum_value" in (\'a\', \'b\', \'c\')), "uuid" uuid not null)']);
tester('sqlite3', ['create table "datatype_test" ("enum_value" text check ("enum_value" in (\'a\', \'b\', \'c\')), "uuid" char(36) not null)']);
tester('sqlite3', ['create table `datatype_test` (`enum_value` text check (`enum_value` in (\'a\', \'b\', \'c\')), `uuid` char(36) not null)']);
tester('oracle', ['create table "datatype_test" ("enum_value" varchar2(1) check ("enum_value" in (\'a\', \'b\', \'c\')), "uuid" char(36) not null)']);
tester('mssql', ['CREATE TABLE [datatype_test] ([enum_value] nvarchar(100), [uuid] uniqueidentifier not null)']);
});
@ -185,10 +185,10 @@ module.exports = function(knex) {
'alter table "test_foreign_table_two" add constraint "fk_fkey_four" foreign key ("fkey_four") references "test_table_two" ("id")'
]);
tester('sqlite3', [
'create table "test_foreign_table_two" ("id" integer not null primary key autoincrement, "fkey_two" integer, "fkey_three" integer, "fkey_four" integer, ' +
'foreign key("fkey_two") references "test_table_two"("id"), ' +
'foreign key("fkey_three") references "test_table_two"("id"), ' +
'foreign key("fkey_four") references "test_table_two"("id"))'
'create table `test_foreign_table_two` (`id` integer not null primary key autoincrement, `fkey_two` integer, `fkey_three` integer, `fkey_four` integer, ' +
'foreign key(`fkey_two`) references `test_table_two`(`id`), ' +
'foreign key(`fkey_three`) references `test_table_two`(`id`), ' +
'foreign key(`fkey_four`) references `test_table_two`(`id`))'
]);
tester('oracle', [
'create table "test_foreign_table_two" ("id" integer not null primary key, "fkey_two" integer, "fkey_three" integer, "fkey_four" integer)',
@ -232,7 +232,7 @@ module.exports = function(knex) {
}).testSql(function(tester) {
tester('mysql', ['create table `composite_key_test` (`column_a` int, `column_b` int, `details` text, `status` tinyint) default character set utf8','alter table `composite_key_test` add unique `composite_key_test_column_a_column_b_unique`(`column_a`, `column_b`)']);
tester('pg', ['create table "composite_key_test" ("column_a" integer, "column_b" integer, "details" text, "status" smallint)','alter table "composite_key_test" add constraint "composite_key_test_column_a_column_b_unique" unique ("column_a", "column_b")']);
tester('sqlite3', ['create table "composite_key_test" ("column_a" integer, "column_b" integer, "details" text, "status" tinyint)','create unique index "composite_key_test_column_a_column_b_unique" on "composite_key_test" ("column_a", "column_b")']);
tester('sqlite3', ['create table `composite_key_test` (`column_a` integer, `column_b` integer, `details` text, `status` tinyint)','create unique index `composite_key_test_column_a_column_b_unique` on `composite_key_test` (`column_a`, `column_b`)']);
tester('oracle', ['create table "composite_key_test" ("column_a" integer, "column_b" integer, "details" clob, "status" smallint)','alter table "composite_key_test" add constraint "zYmMt0VQwlLZ20XnrMicXZ0ufZk" unique ("column_a", "column_b")']);
tester('mssql', ['CREATE TABLE [composite_key_test] ([column_a] int, [column_b] int, [details] nvarchar(max), [status] tinyint, CONSTRAINT [composite_key_test_column_a_column_b_unique] UNIQUE ([column_a], [column_b]))']);
}).then(function() {
@ -268,7 +268,7 @@ module.exports = function(knex) {
}).testSql(function(tester) {
tester('mysql', ['create table `charset_collate_test` (`id` int unsigned not null auto_increment primary key, `account_id` int, `details` text, `status` tinyint) default character set latin1 collate latin1_general_ci engine = InnoDB']);
tester('pg', ['create table "charset_collate_test" ("id" serial primary key, "account_id" integer, "details" text, "status" smallint)']);
tester('sqlite3', ['create table "charset_collate_test" ("id" integer not null primary key autoincrement, "account_id" integer, "details" text, "status" tinyint)']);
tester('sqlite3', ['create table `charset_collate_test` (`id` integer not null primary key autoincrement, `account_id` integer, `details` text, `status` tinyint)']);
tester('oracle', [
"create table \"charset_collate_test\" (\"id\" integer not null primary key, \"account_id\" integer, \"details\" clob, \"status\" smallint)",
"begin execute immediate 'create sequence \"charset_collate_test_seq\"'; exception when others then if sqlcode != -955 then raise; end if; end;",
@ -289,7 +289,7 @@ module.exports = function(knex) {
}).testSql(function(tester) {
tester('mysql', ['create table `bool_test` (`one` boolean, `two` boolean default \'0\', `three` boolean default \'1\', `four` boolean default \'1\', `five` boolean default \'0\') default character set utf8']);
tester('pg', ['create table "bool_test" ("one" boolean, "two" boolean default \'0\', "three" boolean default \'1\', "four" boolean default \'1\', "five" boolean default \'0\')']);
tester('sqlite3', ['create table "bool_test" ("one" boolean, "two" boolean default \'0\', "three" boolean default \'1\', "four" boolean default \'1\', "five" boolean default \'0\')']);
tester('sqlite3', ['create table `bool_test` (`one` boolean, `two` boolean default \'0\', `three` boolean default \'1\', `four` boolean default \'1\', `five` boolean default \'0\')']);
tester('oracle', ['create table "bool_test" ("one" number(1, 0) check ("one" in (\'0\', \'1\')), "two" number(1, 0) default \'0\' check ("two" in (\'0\', \'1\')), "three" number(1, 0) default \'1\' check ("three" in (\'0\', \'1\')), "four" number(1, 0) default \'1\' check ("four" in (\'0\', \'1\')), "five" number(1, 0) default \'0\' check ("five" in (\'0\', \'1\')))']);
tester('mssql', ['CREATE TABLE [bool_test] ([one] bit, [two] bit default \'0\', [three] bit default \'1\', [four] bit default \'1\', [five] bit default \'0\')']);
}).then(function() {
@ -320,10 +320,10 @@ module.exports = function(knex) {
'create index "10_test_table_logins_index" on "10_test_table" ("logins")'
]);
tester('sqlite3', [
'create table "10_test_table" ("id" integer not null primary key autoincrement, "first_name" varchar(255), "last_name" varchar(255), "email" varchar(255) null, "logins" integer default \'1\')',
'create index "10_test_table_first_name_index" on "10_test_table" ("first_name")',
'create unique index "10_test_table_email_unique" on "10_test_table" ("email")',
'create index "10_test_table_logins_index" on "10_test_table" ("logins")'
'create table `10_test_table` (`id` integer not null primary key autoincrement, `first_name` varchar(255), `last_name` varchar(255), `email` varchar(255) null, `logins` integer default \'1\')',
'create index `10_test_table_first_name_index` on `10_test_table` (`first_name`)',
'create unique index `10_test_table_email_unique` on `10_test_table` (`email`)',
'create index `10_test_table_logins_index` on `10_test_table` (`logins`)'
]);
tester('oracle', [
'create table "10_test_table" ("id" number(20, 0) not null primary key, "first_name" varchar2(255), "last_name" varchar2(255), "email" varchar2(255) null, "logins" integer default \'1\')',

View File

@ -28,12 +28,14 @@ var clientsWithNullAsDefault = {
mssql: new MSSQL_Client(useNullAsDefaultConfig),
}
// note: as a workaround, we are using postgres here, since that's using the default " field wrapping
// otherwise subquery cloning would need to be fixed. See: https://github.com/tgriesser/knex/pull/2063
function qb() {
return clients.sqlite3.queryBuilder()
return clients.postgres.queryBuilder()
}
function raw(sql, bindings) {
return clients.sqlite3.raw(sql, bindings)
return clients.postgres.raw(sql, bindings)
}
function verifySqlResult(dialect, expectedObj, sqlObj) {
@ -341,7 +343,7 @@ describe("QueryBuilder", function() {
it('where bool', function() {
testquery(qb().select('*').from('users').where(true), {
mysql: 'select * from `users` where 1 = 1',
sqlite3: 'select * from "users" where 1 = 1',
sqlite3: 'select * from `users` where 1 = 1',
mssql: 'select * from [users] where 1 = 1',
postgres: 'select * from "users" where 1 = 1'
});
@ -677,7 +679,7 @@ describe("QueryBuilder", function() {
bindings: [0]
},
sqlite3: {
sql: 'select * from "users" where 1 = ?',
sql: 'select * from `users` where 1 = ?',
bindings: [0]
},
mssql: {
@ -698,7 +700,7 @@ describe("QueryBuilder", function() {
bindings: [1]
},
sqlite3: {
sql: 'select * from "users" where 1 = ?',
sql: 'select * from `users` where 1 = ?',
bindings: [1]
},
mssql: {
@ -1647,7 +1649,7 @@ describe("QueryBuilder", function() {
bindings: [5]
},
sqlite3: {
sql: 'select * from "users" limit ? offset ?',
sql: 'select * from `users` limit ? offset ?',
bindings: [-1, 5]
},
postgres: {
@ -2368,7 +2370,7 @@ describe("QueryBuilder", function() {
bindings: ['foo', 'taylor', 'bar', 'dayle']
},
sqlite3: {
sql: 'insert into "users" ("email", "name") select ? as "email", ? as "name" union all select ? as "email", ? as "name"',
sql: 'insert into `users` (`email`, `name`) select ? as `email`, ? as `name` union all select ? as `email`, ? as `name`',
bindings: ['foo', 'taylor', 'bar', 'dayle']
},
oracle: {
@ -2393,7 +2395,7 @@ describe("QueryBuilder", function() {
it("multiple inserts with partly undefined keys client with configuration nullAsDefault: true", function() {
testquery(qb().from('users').insert([{email: 'foo', name: 'taylor'}, {name: 'dayle'}]), {
mysql: "insert into `users` (`email`, `name`) values ('foo', 'taylor'), (NULL, 'dayle')",
sqlite3: 'insert into "users" ("email", "name") select \'foo\' as "email", \'taylor\' as "name" union all select NULL as "email", \'dayle\' as "name"',
sqlite3: 'insert into `users` (`email`, `name`) select \'foo\' as `email`, \'taylor\' as `name` union all select NULL as `email`, \'dayle\' as `name`',
oracle: 'begin execute immediate \'insert into "users" ("email", "name") values (:1, :2)\' using \'foo\', \'taylor\'; execute immediate \'insert into "users" ("email", "name") values (:1, :2)\' using NULL, \'dayle\';end;',
mssql: "insert into [users] ([email], [name]) values ('foo', 'taylor'), (NULL, 'dayle')",
oracledb: 'begin execute immediate \'insert into "users" ("email", "name") values (:1, :2)\' using \'foo\', \'taylor\'; execute immediate \'insert into "users" ("email", "name") values (:1, :2)\' using NULL, \'dayle\';end;',
@ -2428,7 +2430,7 @@ describe("QueryBuilder", function() {
bindings: ['foo', 'taylor', 'bar', 'dayle']
},
sqlite3: {
sql: "insert into \"users\" (\"email\", \"name\") select ? as \"email\", ? as \"name\" union all select ? as \"email\", ? as \"name\"",
sql: "insert into `users` (`email`, `name`) select ? as `email`, ? as `name` union all select ? as `email`, ? as `name`",
},
postgres: {
sql: "insert into \"users\" (\"email\", \"name\") values (?, ?), (?, ?) returning \"id\"",
@ -2472,7 +2474,7 @@ describe("QueryBuilder", function() {
bindings: ['foo', 'taylor', 'bar', 'dayle']
},
sqlite3: {
sql: "insert into \"users\" (\"email\", \"name\") select ? as \"email\", ? as \"name\" union all select ? as \"email\", ? as \"name\"",
sql: "insert into `users` (`email`, `name`) select ? as `email`, ? as `name` union all select ? as `email`, ? as `name`",
bindings: ['foo', 'taylor', 'bar', 'dayle']
},
postgres: {
@ -2544,7 +2546,7 @@ describe("QueryBuilder", function() {
bindings: [1, 2, 2, 3]
},
sqlite3: {
sql: 'insert into "table" ("a", "b", "c") select ? as "a", ? as "b", ? as "c" union all select ? as "a", ? as "b", ? as "c" union all select ? as "a", ? as "b", ? as "c"',
sql: 'insert into `table` (`a`, `b`, `c`) select ? as `a`, ? as `b`, ? as `c` union all select ? as `a`, ? as `b`, ? as `c` union all select ? as `a`, ? as `b`, ? as `c`',
bindings: [1, undefined, undefined, undefined, 2, undefined, 2, undefined, 3]
},
oracle: {
@ -2624,7 +2626,7 @@ describe("QueryBuilder", function() {
bindings: []
},
sqlite3: {
sql: 'insert into "users" default values',
sql: 'insert into `users` default values',
bindings: []
},
postgres: {
@ -2916,7 +2918,7 @@ describe("QueryBuilder", function() {
bindings: []
},
sqlite3: {
sql: 'delete from "users"',
sql: 'delete from `users`',
bindings: [],
output: function (output) {
expect(typeof output).to.equal('function');
@ -3885,7 +3887,7 @@ describe("QueryBuilder", function() {
expect(mysqlQb.sql).to.equal('select * from `users` where `users`.`name` = ? or `users`.`name` = ?');
expect(mysqlQb.bindings).to.deep.equal(['Bob', 'Jay']);
expect(sqliteQb.sql).to.equal('select * from "users" where "users"."name" = ? or "users"."name" = ?');
expect(sqliteQb.sql).to.equal('select * from `users` where `users`.`name` = ? or `users`.`name` = ?');
expect(sqliteQb.bindings).to.deep.equal(['Bob', 'Jay']);
});
@ -4055,7 +4057,7 @@ describe("QueryBuilder", function() {
this.select('foo').from('users');
}).select('*').from('withClause'), {
mssql: 'with [withClause] as (select [foo] from [users]) select * from [withClause]',
sqlite3: 'with "withClause" as (select "foo" from "users") select * from "withClause"',
sqlite3: 'with `withClause` as (select `foo` from `users`) select * from `withClause`',
postgres: 'with "withClause" as (select "foo" from "users") select * from "withClause"',
oracledb: 'with "withClause" as (select "foo" from "users") select * from "withClause"',
oracle: 'with "withClause" as (select "foo" from "users") select * from "withClause"'
@ -4067,7 +4069,7 @@ describe("QueryBuilder", function() {
this.select('foo').from('users');
}).insert(raw('select * from "withClause"')).into('users'), {
mssql: 'with [withClause] as (select [foo] from [users]) insert into [users] select * from "withClause"',
sqlite3: 'with "withClause" as (select "foo" from "users") insert into "users" select * from "withClause"',
sqlite3: 'with `withClause` as (select `foo` from `users`) insert into `users` select * from "withClause"',
postgres: 'with "withClause" as (select "foo" from "users") insert into "users" select * from "withClause"'
});
});
@ -4081,7 +4083,7 @@ describe("QueryBuilder", function() {
bindings: ['bob', 'thisMail', 'sam', 'thatMail', 'jack']
},
sqlite3: {
sql: 'with "withClause" as (select "foo" from "users" where "name" = ?) insert into "users" ("email", "name") select ? as "email", ? as "name" union all select ? as "email", ? as "name"',
sql: 'with `withClause` as (select `foo` from `users` where `name` = ?) insert into `users` (`email`, `name`) select ? as `email`, ? as `name` union all select ? as `email`, ? as `name`',
bindings: ['bob', 'thisMail', 'sam', 'thatMail', 'jack']
},
postgres: {
@ -4096,7 +4098,7 @@ describe("QueryBuilder", function() {
this.select('foo').from('users');
}).update({foo: 'updatedFoo'}).where('email', '=', 'foo').from('users'), {
mssql: 'with [withClause] as (select [foo] from [users]) update [users] set [foo] = ? where [email] = ?;select @@rowcount',
sqlite3: 'with "withClause" as (select "foo" from "users") update "users" set "foo" = ? where "email" = ?',
sqlite3: 'with `withClause` as (select `foo` from `users`) update `users` set `foo` = ? where `email` = ?',
postgres: 'with "withClause" as (select "foo" from "users") update "users" set "foo" = ? where "email" = ?'
});
});
@ -4106,7 +4108,7 @@ describe("QueryBuilder", function() {
this.select('email').from('users');
}).del().where('foo', '=', 'updatedFoo').from('users'), {
mssql: 'with [withClause] as (select [email] from [users]) delete from [users] where [foo] = ?;select @@rowcount',
sqlite3: 'with "withClause" as (select "email" from "users") delete from "users" where "foo" = ?',
sqlite3: 'with `withClause` as (select `email` from `users`) delete from `users` where `foo` = ?',
postgres: 'with "withClause" as (select "email" from "users") delete from "users" where "foo" = ?'
});
});
@ -4114,7 +4116,7 @@ describe("QueryBuilder", function() {
it("raw 'with' clause", function() {
testsql(qb().with('withRawClause', raw('select "foo" as "baz" from "users"')).select('*').from('withRawClause'), {
mssql: 'with [withRawClause] as (select "foo" as "baz" from "users") select * from [withRawClause]',
sqlite3: 'with "withRawClause" as (select "foo" as "baz" from "users") select * from "withRawClause"',
sqlite3: 'with `withRawClause` as (select "foo" as "baz" from "users") select * from `withRawClause`',
postgres: 'with "withRawClause" as (select "foo" as "baz" from "users") select * from "withRawClause"',
oracledb: 'with "withRawClause" as (select "foo" as "baz" from "users") select * from "withRawClause"',
oracle: 'with "withRawClause" as (select "foo" as "baz" from "users") select * from "withRawClause"'
@ -4128,7 +4130,7 @@ describe("QueryBuilder", function() {
this.select('bar').from('users');
}).select('*').from('secondWithClause'), {
mssql: 'with [firstWithClause] as (select [foo] from [users]), [secondWithClause] as (select [bar] from [users]) select * from [secondWithClause]',
sqlite3: 'with "firstWithClause" as (select "foo" from "users"), "secondWithClause" as (select "bar" from "users") select * from "secondWithClause"',
sqlite3: 'with `firstWithClause` as (select `foo` from `users`), `secondWithClause` as (select `bar` from `users`) select * from `secondWithClause`',
postgres: 'with "firstWithClause" as (select "foo" from "users"), "secondWithClause" as (select "bar" from "users") select * from "secondWithClause"',
oracledb: 'with "firstWithClause" as (select "foo" from "users"), "secondWithClause" as (select "bar" from "users") select * from "secondWithClause"',
oracle: 'with "firstWithClause" as (select "foo" from "users"), "secondWithClause" as (select "bar" from "users") select * from "secondWithClause"'
@ -4142,7 +4144,7 @@ describe("QueryBuilder", function() {
}).select('*').from('withSubClause');
}).select('*').from('withClause'), {
mssql: 'with [withClause] as (with [withSubClause] as ((select [foo] from [users]) as [baz]) select * from [withSubClause]) select * from [withClause]',
sqlite3: 'with "withClause" as (with "withSubClause" as ((select "foo" from "users") as "baz") select * from "withSubClause") select * from "withClause"',
sqlite3: 'with `withClause` as (with `withSubClause` as ((select `foo` from `users`) as `baz`) select * from `withSubClause`) select * from `withClause`',
postgres: 'with "withClause" as (with "withSubClause" as ((select "foo" from "users") as "baz") select * from "withSubClause") select * from "withClause"',
oracledb: 'with "withClause" as (with "withSubClause" as ((select "foo" from "users") "baz") select * from "withSubClause") select * from "withClause"',
oracle: 'with "withClause" as (with "withSubClause" as ((select "foo" from "users") "baz") select * from "withSubClause") select * from "withClause"'
@ -4159,7 +4161,7 @@ describe("QueryBuilder", function() {
bindings: [1, 20, 10]
},
sqlite3: {
sql: 'with "withClause" as (with "withSubClause" as (select "foo" as "baz" from "users" where "baz" > ? and "baz" < ?) select * from "withSubClause") select * from "withClause" where "id" = ?',
sql: 'with `withClause` as (with `withSubClause` as (select "foo" as "baz" from "users" where "baz" > ? and "baz" < ?) select * from `withSubClause`) select * from `withClause` where `id` = ?',
bindings: [1, 20, 10]
},
postgres: {
@ -4188,7 +4190,7 @@ describe("QueryBuilder", function() {
}).select('*').from('secondWithSubClause');
}).select('*').from('secondWithClause'), {
mssql: 'with [firstWithClause] as (with [firstWithSubClause] as ((select [foo] from [users]) as [foz]) select * from [firstWithSubClause]), [secondWithClause] as (with [secondWithSubClause] as ((select [bar] from [users]) as [baz]) select * from [secondWithSubClause]) select * from [secondWithClause]',
sqlite3: 'with "firstWithClause" as (with "firstWithSubClause" as ((select "foo" from "users") as "foz") select * from "firstWithSubClause"), "secondWithClause" as (with "secondWithSubClause" as ((select "bar" from "users") as "baz") select * from "secondWithSubClause") select * from "secondWithClause"',
sqlite3: 'with `firstWithClause` as (with `firstWithSubClause` as ((select `foo` from `users`) as `foz`) select * from `firstWithSubClause`), `secondWithClause` as (with `secondWithSubClause` as ((select `bar` from `users`) as `baz`) select * from `secondWithSubClause`) select * from `secondWithClause`',
postgres: 'with "firstWithClause" as (with "firstWithSubClause" as ((select "foo" from "users") as "foz") select * from "firstWithSubClause"), "secondWithClause" as (with "secondWithSubClause" as ((select "bar" from "users") as "baz") select * from "secondWithSubClause") select * from "secondWithClause"',
oracledb: 'with "firstWithClause" as (with "firstWithSubClause" as ((select "foo" from "users") "foz") select * from "firstWithSubClause"), "secondWithClause" as (with "secondWithSubClause" as ((select "bar" from "users") "baz") select * from "secondWithSubClause") select * from "secondWithClause"',
oracle: 'with "firstWithClause" as (with "firstWithSubClause" as ((select "foo" from "users") "foz") select * from "firstWithSubClause"), "secondWithClause" as (with "secondWithSubClause" as ((select "bar" from "users") "baz") select * from "secondWithSubClause") select * from "secondWithClause"'

View File

@ -21,7 +21,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'create table "users" ("id" integer not null primary key autoincrement, "email" varchar(255))');
equal(tableSql[0].sql, 'create table `users` (`id` integer not null primary key autoincrement, `email` varchar(255))');
});
it("basic alter table", function() {
@ -32,8 +32,8 @@ describe("SQLite SchemaBuilder", function() {
equal(2, tableSql.length);
var expected = [
'alter table "users" add column "id" integer not null primary key autoincrement',
'alter table "users" add column "email" varchar(255)',
'alter table `users` add column `id` integer not null primary key autoincrement',
'alter table `users` add column `email` varchar(255)',
];
expect(expected).to.eql(_.map(tableSql, 'sql'));
});
@ -52,14 +52,14 @@ describe("SQLite SchemaBuilder", function() {
it("drop table", function() {
tableSql = client.schemaBuilder().dropTable('users').toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'drop table "users"');
equal(tableSql[0].sql, 'drop table `users`');
});
it("drop table if exists", function() {
tableSql = client.schemaBuilder().dropTableIfExists('users').toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'drop table if exists "users"');
equal(tableSql[0].sql, 'drop table if exists `users`');
});
it("drop unique", function() {
@ -68,7 +68,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'drop index "users_foo_unique"');
equal(tableSql[0].sql, 'drop index `users_foo_unique`');
});
it("drop unique, custom", function() {
@ -77,7 +77,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'drop index "foo"');
equal(tableSql[0].sql, 'drop index `foo`');
});
it("drop index", function() {
@ -86,7 +86,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'drop index "users_foo_index"');
equal(tableSql[0].sql, 'drop index `users_foo_index`');
});
it("drop index, custom", function() {
@ -95,14 +95,14 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'drop index "foo"');
equal(tableSql[0].sql, 'drop index `foo`');
});
it("rename table", function() {
tableSql = client.schemaBuilder().renameTable('users', 'foo').toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" rename to "foo"');
equal(tableSql[0].sql, 'alter table `users` rename to `foo`');
});
it("adding primary key", function() {
@ -112,7 +112,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'create table "users" ("foo" varchar(255), primary key ("foo"))');
equal(tableSql[0].sql, 'create table `users` (`foo` varchar(255), primary key (`foo`))');
});
it("adding composite primary key", function() {
@ -123,7 +123,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'create table "users" ("foo" varchar(255), "order_id" varchar(255), primary key ("foo", "order_id"))');
equal(tableSql[0].sql, 'create table `users` (`foo` varchar(255), `order_id` varchar(255), primary key (`foo`, `order_id`))');
tableSql = client.schemaBuilder().createTable('users', function(table) {
table.string('foo');
@ -132,7 +132,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'create table "users" ("foo" varchar(255), "order_id" varchar(255), primary key ("foo", "order_id"))');
equal(tableSql[0].sql, 'create table `users` (`foo` varchar(255), `order_id` varchar(255), primary key (`foo`, `order_id`))');
});
it("adding primary key fluently", function() {
@ -141,7 +141,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'create table "users" ("foo" varchar(255), primary key ("foo"))');
equal(tableSql[0].sql, 'create table `users` (`foo` varchar(255), primary key (`foo`))');
});
it("adding foreign key", function() {
@ -152,7 +152,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
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"))');
equal(tableSql[0].sql, 'create table `users` (`foo` varchar(255), `order_id` varchar(255), foreign key(`order_id`) references `orders`(`id`), primary key (`foo`))');
});
// SQLite3 doesn't support named foreign keys
@ -165,7 +165,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
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"))');
equal(tableSql[0].sql, 'create table `users` (`foo` varchar(255), `order_id` varchar(255), foreign key(`order_id`) references `orders`(`id`), primary key (`foo`))');
});
it("adds a unique key with autogenerated name", function() {
@ -174,7 +174,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'create unique index "users_foo_unique" on "users" ("foo")');
equal(tableSql[0].sql, 'create unique index `users_foo_unique` on `users` (`foo`)');
});
it("adding unique key with specific name", function() {
@ -183,7 +183,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'create unique index "bar" on "users" ("foo")');
equal(tableSql[0].sql, 'create unique index `bar` on `users` (`foo`)');
});
it("adding index", function() {
@ -192,7 +192,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'create index "baz" on "users" ("foo", "bar")');
equal(tableSql[0].sql, 'create index `baz` on `users` (`foo`, `bar`)');
});
it("adding incrementing id", function() {
@ -201,7 +201,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "id" integer not null primary key autoincrement');
equal(tableSql[0].sql, 'alter table `users` add column `id` integer not null primary key autoincrement');
});
it("adding big incrementing id", function() {
@ -210,7 +210,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "id" integer not null primary key autoincrement');
equal(tableSql[0].sql, 'alter table `users` add column `id` integer not null primary key autoincrement');
});
it("adding string", function() {
@ -219,7 +219,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" varchar(255)');
equal(tableSql[0].sql, 'alter table `users` add column `foo` varchar(255)');
});
it("allows setting a value in the string length, although unused by sqlite3", function() {
@ -228,7 +228,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" varchar(100)');
equal(tableSql[0].sql, 'alter table `users` add column `foo` varchar(100)');
});
it("correctly interprets defaultTo(null)", function() {
@ -236,7 +236,7 @@ describe("SQLite SchemaBuilder", function() {
table.string('foo').defaultTo(null);
}).toSQL();
equal(tableSql[0].sql, 'alter table "users" add column "foo" varchar(255) default null');
equal(tableSql[0].sql, 'alter table `users` add column `foo` varchar(255) default null');
});
it("chains notNull and defaultTo", function() {
@ -245,7 +245,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" varchar(100) not null default \'bar\'');
equal(tableSql[0].sql, 'alter table `users` add column `foo` varchar(100) not null default \'bar\'');
});
it("adding text", function() {
@ -254,7 +254,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" text');
equal(tableSql[0].sql, 'alter table `users` add column `foo` text');
});
it("adding big integer", function() {
@ -263,7 +263,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" bigint');
equal(tableSql[0].sql, 'alter table `users` add column `foo` bigint');
});
it("bigincrements works the same as increments for sqlite3", function() {
@ -272,7 +272,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" integer not null primary key autoincrement');
equal(tableSql[0].sql, 'alter table `users` add column `foo` integer not null primary key autoincrement');
});
it("adding integer", function() {
@ -281,7 +281,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" integer');
equal(tableSql[0].sql, 'alter table `users` add column `foo` integer');
});
it("adding autoincrements", function() {
@ -290,7 +290,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" integer not null primary key autoincrement');
equal(tableSql[0].sql, 'alter table `users` add column `foo` integer not null primary key autoincrement');
});
it("adding medium integer", function() {
@ -299,7 +299,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" integer');
equal(tableSql[0].sql, 'alter table `users` add column `foo` integer');
});
it("adding tiny integer", function() {
@ -308,7 +308,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" tinyint');
equal(tableSql[0].sql, 'alter table `users` add column `foo` tinyint');
});
it("adding small integer", function() {
@ -317,7 +317,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" integer');
equal(tableSql[0].sql, 'alter table `users` add column `foo` integer');
});
it("adding float", function() {
@ -326,7 +326,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" float');
equal(tableSql[0].sql, 'alter table `users` add column `foo` float');
});
it("adding double", function() {
@ -335,7 +335,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" float');
equal(tableSql[0].sql, 'alter table `users` add column `foo` float');
});
it("adding decimal", function() {
@ -344,7 +344,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" float');
equal(tableSql[0].sql, 'alter table `users` add column `foo` float');
});
it("adding boolean", function() {
@ -353,7 +353,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" boolean');
equal(tableSql[0].sql, 'alter table `users` add column `foo` boolean');
});
it("adding enum", function() {
@ -362,7 +362,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" text check ("foo" in (\'bar\', \'baz\'))');
equal(tableSql[0].sql, 'alter table `users` add column `foo` text check (`foo` in (\'bar\', \'baz\'))');
});
it("adding date", function() {
@ -371,7 +371,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" date');
equal(tableSql[0].sql, 'alter table `users` add column `foo` date');
});
it("adding date time", function() {
@ -380,7 +380,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" datetime');
equal(tableSql[0].sql, 'alter table `users` add column `foo` datetime');
});
it("adding time", function() {
@ -389,7 +389,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" time');
equal(tableSql[0].sql, 'alter table `users` add column `foo` time');
});
it("adding time stamp", function() {
@ -398,7 +398,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" datetime');
equal(tableSql[0].sql, 'alter table `users` add column `foo` datetime');
});
it("adding time stamps", function() {
@ -408,8 +408,8 @@ describe("SQLite SchemaBuilder", function() {
equal(2, tableSql.length);
var expected = [
'alter table "users" add column "created_at" datetime',
'alter table "users" add column "updated_at" datetime'
'alter table `users` add column `created_at` datetime',
'alter table `users` add column `updated_at` datetime'
];
deepEqual(expected, _.map(tableSql, 'sql'));
});
@ -420,7 +420,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
equal(1, tableSql.length);
equal(tableSql[0].sql, 'alter table "users" add column "foo" blob');
equal(tableSql[0].sql, 'alter table `users` add column `foo` blob');
});
it('allows for on delete cascade with foreign keys, #166', function() {
@ -433,7 +433,7 @@ describe("SQLite SchemaBuilder", function() {
}).toSQL();
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)');
equal(tableSql[0].sql, 'create table `users` (`user_id` varchar(36), foreign key(`user_id`) references `user`(`id`) on delete CASCADE)');
});
describe('SQLite3_DDL.prototype._doReplace', function () {
@ -442,20 +442,20 @@ describe("SQLite SchemaBuilder", function() {
var doReplace = SQLite3_DDL.prototype._doReplace;
var sql1 = 'CREATE TABLE "foo" ("id" integer not null primary key autoincrement, '+
'"parent_id_test" integer, foreign key("parent_id") references "foo"("id"))';
var sql2 = 'CREATE TABLE "foo" ("id" integer not null primary key autoincrement, '+
'"parent_id_test" integer, foreign key("parent_id") references "bar"("id"))';
var sql1 = 'CREATE TABLE `foo` (`id` integer not null primary key autoincrement, '+
'"parent_id_test" integer, foreign key("parent_id") references `foo`(`id`))';
var sql2 = 'CREATE TABLE `foo` (`id` integer not null primary key autoincrement, '+
'"parent_id_test" integer, foreign key("parent_id") references `bar`(`id`))';
var sql1b = 'CREATE TABLE "foo" ("id_foo" integer not null primary key autoincrement, '+
'"parent_id_test" integer, foreign key("parent_id") references "foo"("id_foo"))';
var sql2b = 'CREATE TABLE "foo" ("id_foo" integer not null primary key autoincrement, '+
'"parent_id_test" integer, foreign key("parent_id") references "bar"("id"))';
var sql1b = 'CREATE TABLE `foo` ("id_foo" integer not null primary key autoincrement, '+
'"parent_id_test" integer, foreign key("parent_id") references `foo`("id_foo"))';
var 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);
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();
});
});