2014-09-01 17:18:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
2021-07-13 09:41:16 +03:00
|
|
|
const { expect } = require('chai');
|
2020-04-07 21:27:40 +02:00
|
|
|
const { TEST_TIMESTAMP } = require('../../util/constants');
|
2021-10-13 01:19:56 +03:00
|
|
|
const {
|
|
|
|
isSQLite,
|
|
|
|
isPostgreSQL,
|
|
|
|
isOracle,
|
|
|
|
isCockroachDB,
|
|
|
|
} = require('../../util/db-helpers');
|
2020-03-24 02:54:01 +05:45
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
module.exports = function (knex) {
|
|
|
|
describe('Deletes', function () {
|
|
|
|
it('should handle deletes', function () {
|
2013-09-11 23:36:55 -04:00
|
|
|
return knex('accounts')
|
2014-04-13 17:51:57 +10:00
|
|
|
.where('id', 1)
|
2014-04-16 02:50:19 -04:00
|
|
|
.del()
|
2020-04-19 00:40:23 +02:00
|
|
|
.testSql(function (tester) {
|
2018-07-09 08:10:34 -04:00
|
|
|
tester('mysql', 'delete from `accounts` where `id` = ?', [1], 1);
|
|
|
|
tester('pg', 'delete from "accounts" where "id" = ?', [1], 1);
|
2018-02-03 08:33:02 -05:00
|
|
|
tester(
|
|
|
|
'pg-redshift',
|
|
|
|
'delete from "accounts" where "id" = ?',
|
|
|
|
[1],
|
|
|
|
1
|
|
|
|
);
|
2018-07-09 08:10:34 -04:00
|
|
|
tester('sqlite3', 'delete from `accounts` where `id` = ?', [1], 1);
|
|
|
|
tester('oracledb', 'delete from "accounts" where "id" = ?', [1], 1);
|
2015-12-09 17:53:53 -06:00
|
|
|
tester(
|
|
|
|
'mssql',
|
|
|
|
'delete from [accounts] where [id] = ?;select @@rowcount',
|
|
|
|
[1],
|
|
|
|
1
|
|
|
|
);
|
2014-04-16 02:50:19 -04:00
|
|
|
});
|
2014-04-13 17:51:57 +10:00
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('should allow returning for deletes in postgresql and mssql', function () {
|
2014-04-13 17:51:57 +10:00
|
|
|
return knex('accounts')
|
|
|
|
.where('id', 2)
|
2014-04-16 02:50:19 -04:00
|
|
|
.del('*')
|
2020-04-19 00:40:23 +02:00
|
|
|
.testSql(function (tester) {
|
2018-07-09 08:10:34 -04:00
|
|
|
tester('mysql', 'delete from `accounts` where `id` = ?', [2], 1);
|
2014-04-16 02:59:27 -04:00
|
|
|
tester(
|
2018-06-29 10:47:06 +03:00
|
|
|
'pg',
|
2014-04-16 02:59:27 -04:00
|
|
|
'delete from "accounts" where "id" = ? returning *',
|
|
|
|
[2],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
id: '2',
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
email: 'test2@example.com',
|
|
|
|
logins: 1,
|
|
|
|
balance: 0,
|
|
|
|
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
2020-03-24 02:54:01 +05:45
|
|
|
created_at: TEST_TIMESTAMP,
|
|
|
|
updated_at: TEST_TIMESTAMP,
|
2018-07-09 08:10:34 -04:00
|
|
|
phone: null,
|
|
|
|
},
|
|
|
|
]
|
2014-04-16 02:59:27 -04:00
|
|
|
);
|
2018-02-03 08:33:02 -05:00
|
|
|
tester(
|
|
|
|
'pg-redshift',
|
|
|
|
'delete from "accounts" where "id" = ?',
|
|
|
|
[2],
|
|
|
|
1
|
|
|
|
);
|
2018-07-09 08:10:34 -04:00
|
|
|
tester('sqlite3', 'delete from `accounts` where `id` = ?', [2], 1);
|
|
|
|
tester('oracledb', 'delete from "accounts" where "id" = ?', [2], 1);
|
2015-12-09 17:53:53 -06:00
|
|
|
tester(
|
|
|
|
'mssql',
|
|
|
|
'delete from [accounts] output deleted.* where [id] = ?',
|
|
|
|
[2],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
id: '2',
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
email: 'test2@example.com',
|
|
|
|
logins: 1,
|
|
|
|
balance: 0,
|
|
|
|
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
2020-03-24 02:54:01 +05:45
|
|
|
created_at: TEST_TIMESTAMP,
|
|
|
|
updated_at: TEST_TIMESTAMP,
|
2018-07-09 08:10:34 -04:00
|
|
|
phone: null,
|
|
|
|
},
|
|
|
|
]
|
2015-12-09 17:53:53 -06:00
|
|
|
);
|
2014-04-16 02:50:19 -04:00
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
2021-07-13 09:41:16 +03:00
|
|
|
|
|
|
|
describe('Delete with join', function () {
|
|
|
|
it('should handle basic delete with join', async function () {
|
|
|
|
const query = knex('test_table_two')
|
|
|
|
.join('accounts', 'accounts.id', 'test_table_two.account_id')
|
|
|
|
.where({ 'accounts.email': 'test3@example.com' })
|
|
|
|
.del();
|
2021-10-13 01:19:56 +03:00
|
|
|
if (
|
|
|
|
isSQLite(knex) ||
|
|
|
|
isPostgreSQL(knex) ||
|
|
|
|
isCockroachDB(knex) ||
|
|
|
|
isOracle(knex)
|
|
|
|
) {
|
2021-07-13 09:41:16 +03:00
|
|
|
await expect(query).to.be.rejected;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return query.testSql(function (tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'delete `test_table_two` from `test_table_two` inner join `accounts` on `accounts`.`id` = `test_table_two`.`account_id` where `accounts`.`email` = ?',
|
|
|
|
['test3@example.com'],
|
|
|
|
1
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'mssql',
|
|
|
|
'delete [test_table_two] from [test_table_two] inner join [accounts] on [accounts].[id] = [test_table_two].[account_id] where [accounts].[email] = ?;select @@rowcount',
|
|
|
|
['test3@example.com'],
|
|
|
|
1
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should handle returning', async function () {
|
|
|
|
await knex('test_table_two').insert({
|
|
|
|
account_id: 4,
|
|
|
|
details: '',
|
|
|
|
status: 1,
|
|
|
|
});
|
|
|
|
const query = knex('test_table_two')
|
|
|
|
.join('accounts', 'accounts.id', 'test_table_two.account_id')
|
|
|
|
.where({ 'accounts.email': 'test4@example.com' })
|
|
|
|
.del('*');
|
2021-10-13 01:19:56 +03:00
|
|
|
if (
|
|
|
|
isSQLite(knex) ||
|
|
|
|
isPostgreSQL(knex) ||
|
|
|
|
isCockroachDB(knex) ||
|
|
|
|
isOracle(knex)
|
|
|
|
) {
|
2021-07-13 09:41:16 +03:00
|
|
|
await expect(query).to.be.rejected;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return query.testSql(function (tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'delete `test_table_two` from `test_table_two` inner join `accounts` on `accounts`.`id` = `test_table_two`.`account_id` where `accounts`.`email` = ?',
|
|
|
|
['test4@example.com'],
|
|
|
|
1
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'mssql',
|
|
|
|
'delete [test_table_two] output deleted.* from [test_table_two] inner join [accounts] on [accounts].[id] = [test_table_two].[account_id] where [accounts].[email] = ?',
|
|
|
|
['test4@example.com'],
|
2021-10-02 23:45:17 +03:00
|
|
|
[{ id: 9, account_id: 4, details: '', status: 1 }]
|
2021-07-13 09:41:16 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
2014-04-13 17:51:57 +10:00
|
|
|
};
|