knex/test/integration/query/deletes.js

160 lines
5.1 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
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-04-19 00:40:23 +02:00
module.exports = function (knex) {
describe('Deletes', function () {
it('should handle deletes', function () {
return knex('accounts')
.where('id', 1)
2014-04-16 02:50:19 -04:00
.del()
2020-04-19 00:40:23 +02:00
.testSql(function (tester) {
tester('mysql', 'delete from `accounts` where `id` = ?', [1], 1);
tester('pg', 'delete from "accounts" where "id" = ?', [1], 1);
Add redshift support without changing cli or package.json (#2233) * Add a Redshift dialect that inherits from Postgres. * Turn .index() and .dropIndex() into no-ops with warnings in the Redshift dialect. * Update the Redshift dialect to be compatible with master. * Update package.json * Disable liftoff cli * Remove the CLI * Add lib to the repo * Allow the escaping of named bindings. * Update dist * Update the Redshift dialect’s instantiation of the query and column compilers. * Update the distribution * Fix a merge conflict * Take lib back out * Trying to bring back in line with tgreisser/knex * Add npm 5 package-lock * Bring cli.js back in line * Bring cli.js back in line * Progress commit on redshift integration tests * Revert "Progress commit on redshift integration tests" This reverts commit 207e31635c638853dec54ce0580d34559ba5a54c. * Progress commit * Working not null on primary columns in createTable * Working redshift unit tests * Working unit and integration tests, still need to fix migration tests * Brought datatypes more in line with what redshift actually supports * Added query compiler unit tests * Add a hacky returning clause for redshift ugh * Working migration integration tests * Working insert integration tests * Allow multiple insert returning values * Working select integration tests * Working join integration tests * Working aggregate integration tests * All integration suite tests working * Put docker index for reconnect tests back * Redshift does not support insert...returning, there does not seem to be a way around that, therefore accept it and test accordingly * Leave redshift integration tests in place, but do not run them by default * Fix mysql order by test * Fix more tests * Change test db name to knex_test for consistency * Address PR comments
2018-02-03 08:33:02 -05:00
tester(
'pg-redshift',
'delete from "accounts" where "id" = ?',
[1],
1
);
tester('sqlite3', 'delete from `accounts` where `id` = ?', [1], 1);
tester('oracledb', 'delete from "accounts" where "id" = ?', [1], 1);
tester(
'mssql',
'delete from [accounts] where [id] = ?;select @@rowcount',
[1],
1
);
2014-04-16 02:50:19 -04:00
});
});
2020-04-19 00:40:23 +02:00
it('should allow returning for deletes in postgresql and mssql', function () {
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) {
tester('mysql', 'delete from `accounts` where `id` = ?', [2], 1);
2014-04-16 02:59:27 -04:00
tester(
'pg',
2014-04-16 02:59:27 -04:00
'delete from "accounts" where "id" = ? returning *',
[2],
[
{
id: '2',
first_name: 'Test',
last_name: 'User',
email: 'test2@example.com',
logins: 1,
balance: 0,
about: 'Lorem ipsum Dolore labore incididunt enim.',
created_at: TEST_TIMESTAMP,
updated_at: TEST_TIMESTAMP,
phone: null,
},
]
2014-04-16 02:59:27 -04:00
);
Add redshift support without changing cli or package.json (#2233) * Add a Redshift dialect that inherits from Postgres. * Turn .index() and .dropIndex() into no-ops with warnings in the Redshift dialect. * Update the Redshift dialect to be compatible with master. * Update package.json * Disable liftoff cli * Remove the CLI * Add lib to the repo * Allow the escaping of named bindings. * Update dist * Update the Redshift dialect’s instantiation of the query and column compilers. * Update the distribution * Fix a merge conflict * Take lib back out * Trying to bring back in line with tgreisser/knex * Add npm 5 package-lock * Bring cli.js back in line * Bring cli.js back in line * Progress commit on redshift integration tests * Revert "Progress commit on redshift integration tests" This reverts commit 207e31635c638853dec54ce0580d34559ba5a54c. * Progress commit * Working not null on primary columns in createTable * Working redshift unit tests * Working unit and integration tests, still need to fix migration tests * Brought datatypes more in line with what redshift actually supports * Added query compiler unit tests * Add a hacky returning clause for redshift ugh * Working migration integration tests * Working insert integration tests * Allow multiple insert returning values * Working select integration tests * Working join integration tests * Working aggregate integration tests * All integration suite tests working * Put docker index for reconnect tests back * Redshift does not support insert...returning, there does not seem to be a way around that, therefore accept it and test accordingly * Leave redshift integration tests in place, but do not run them by default * Fix mysql order by test * Fix more tests * Change test db name to knex_test for consistency * Address PR comments
2018-02-03 08:33:02 -05:00
tester(
'pg-redshift',
'delete from "accounts" where "id" = ?',
[2],
1
);
tester('sqlite3', 'delete from `accounts` where `id` = ?', [2], 1);
tester('oracledb', 'delete from "accounts" where "id" = ?', [2], 1);
tester(
'mssql',
'delete from [accounts] output deleted.* where [id] = ?',
[2],
[
{
id: '2',
first_name: 'Test',
last_name: 'User',
email: 'test2@example.com',
logins: 1,
balance: 0,
about: 'Lorem ipsum Dolore labore incididunt enim.',
created_at: TEST_TIMESTAMP,
updated_at: TEST_TIMESTAMP,
phone: null,
},
]
);
2014-04-16 02:50:19 -04: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)
) {
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)
) {
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'],
[{ id: 9, account_id: 4, details: '', status: 1 }]
);
});
});
});
});
};