2014-09-01 17:18:45 +02:00
|
|
|
/*global describe, expect, it, d*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2019-05-13 12:21:36 +03:00
|
|
|
const Bluebird = require('bluebird');
|
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
module.exports = function(knex) {
|
2018-07-09 08:10:34 -04:00
|
|
|
describe('Updates', function() {
|
2013-09-13 10:24:39 -04:00
|
|
|
it('should handle updates', function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.where('id', 1)
|
|
|
|
.update({
|
|
|
|
first_name: 'User',
|
|
|
|
last_name: 'Test',
|
2018-07-09 08:10:34 -04:00
|
|
|
email: 'test100@example.com',
|
|
|
|
})
|
|
|
|
.testSql(function(tester) {
|
2013-12-27 14:44:21 -05:00
|
|
|
tester(
|
|
|
|
'mysql',
|
2017-07-26 11:36:50 +03:00
|
|
|
'update `accounts` set `first_name` = ?, `last_name` = ?, `email` = ? where `id` = ?',
|
2018-07-09 08:10:34 -04:00
|
|
|
['User', 'Test', 'test100@example.com', 1],
|
2013-12-27 14:44:21 -05:00
|
|
|
1
|
|
|
|
);
|
|
|
|
tester(
|
2018-06-29 10:47:06 +03:00
|
|
|
'pg',
|
2017-07-26 11:36:50 +03:00
|
|
|
'update "accounts" set "first_name" = ?, "last_name" = ?, "email" = ? where "id" = ?',
|
2018-07-09 08:10:34 -04:00
|
|
|
['User', 'Test', 'test100@example.com', 1],
|
2013-12-27 14:44:21 -05:00
|
|
|
1
|
|
|
|
);
|
2018-02-03 08:33:02 -05:00
|
|
|
tester(
|
|
|
|
'pg-redshift',
|
|
|
|
'update "accounts" set "first_name" = ?, "last_name" = ?, "email" = ? where "id" = ?',
|
2018-07-09 08:10:34 -04:00
|
|
|
['User', 'Test', 'test100@example.com', 1],
|
2018-02-03 08:33:02 -05:00
|
|
|
1
|
|
|
|
);
|
2013-12-27 14:44:21 -05:00
|
|
|
tester(
|
|
|
|
'sqlite3',
|
2017-07-26 11:36:50 +03:00
|
|
|
'update `accounts` set `first_name` = ?, `last_name` = ?, `email` = ? where `id` = ?',
|
2018-07-09 08:10:34 -04:00
|
|
|
['User', 'Test', 'test100@example.com', 1],
|
2013-12-27 14:44:21 -05:00
|
|
|
1
|
|
|
|
);
|
2015-12-09 17:53:53 -06:00
|
|
|
tester(
|
|
|
|
'mssql',
|
2017-07-26 11:36:50 +03:00
|
|
|
'update [accounts] set [first_name] = ?, [last_name] = ?, [email] = ? where [id] = ?;select @@rowcount',
|
2018-07-09 08:10:34 -04:00
|
|
|
['User', 'Test', 'test100@example.com', 1],
|
2015-12-09 17:53:53 -06:00
|
|
|
1
|
|
|
|
);
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-02-23 15:38:53 -05:00
|
|
|
it('should allow for null updates', function() {
|
|
|
|
return knex('accounts')
|
|
|
|
.where('id', 1000)
|
|
|
|
.update({
|
2018-07-09 08:10:34 -04:00
|
|
|
email: 'test100@example.com',
|
2014-02-23 15:38:53 -05:00
|
|
|
first_name: null,
|
2018-07-09 08:10:34 -04:00
|
|
|
last_name: 'Test',
|
|
|
|
})
|
|
|
|
.testSql(function(tester) {
|
2014-02-23 15:38:53 -05:00
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'update `accounts` set `email` = ?, `first_name` = ?, `last_name` = ? where `id` = ?',
|
|
|
|
['test100@example.com', null, 'Test', 1000],
|
|
|
|
0
|
|
|
|
);
|
2015-12-09 17:53:53 -06:00
|
|
|
tester(
|
|
|
|
'mssql',
|
|
|
|
'update [accounts] set [email] = ?, [first_name] = ?, [last_name] = ? where [id] = ?;select @@rowcount',
|
|
|
|
['test100@example.com', null, 'Test', 1000],
|
|
|
|
0
|
|
|
|
);
|
2014-02-23 15:38:53 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-13 12:21:36 +03:00
|
|
|
it('should immediately return updated value for other connections when updating row to DB returns (TODO: fix oracle fail)', function() {
|
|
|
|
if (knex.client.driverName == 'oracledb') {
|
|
|
|
// TODO: this test was added to catch strange random fails with oracle
|
|
|
|
// currently it looks like at least oracle transactions seem to return before
|
|
|
|
// they are actually committed to database...
|
|
|
|
|
|
|
|
// if those selects are changed to forUpdate() then the test seem to pass fine
|
|
|
|
|
|
|
|
this.skip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return knex('accounts').then((res) => {
|
|
|
|
function runTest() {
|
|
|
|
return Bluebird.all(
|
|
|
|
res.map((origRow) => {
|
|
|
|
return Bluebird.resolve()
|
|
|
|
.then(() => {
|
|
|
|
return knex.transaction((trx) =>
|
|
|
|
trx('accounts')
|
|
|
|
.where('id', origRow.id)
|
|
|
|
.update({ balance: 654 })
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return knex('accounts')
|
|
|
|
.where('id', origRow.id)
|
|
|
|
.then((res) => res[0]);
|
|
|
|
})
|
|
|
|
.then((updatedRow) => {
|
|
|
|
expect(updatedRow.balance).to.equal(654);
|
|
|
|
return knex.transaction((trx) =>
|
|
|
|
trx('accounts')
|
|
|
|
.where('id', origRow.id)
|
|
|
|
.update({ balance: origRow.balance })
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return knex('accounts')
|
|
|
|
.where('id', origRow.id)
|
|
|
|
.then((res) => res[0]);
|
|
|
|
})
|
|
|
|
.then((updatedRow) => {
|
|
|
|
expect(updatedRow.balance).to.equal(origRow.balance);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// run few times to try to catch the problem
|
|
|
|
return runTest()
|
|
|
|
.then(() => runTest())
|
|
|
|
.then(() => runTest())
|
|
|
|
.then(() => runTest())
|
|
|
|
.then(() => runTest())
|
|
|
|
.then(() => runTest())
|
|
|
|
.then(() => runTest())
|
|
|
|
.then(() => runTest())
|
|
|
|
.then(() => runTest())
|
|
|
|
.then(() => runTest())
|
|
|
|
.then(() => runTest())
|
|
|
|
.then(() => runTest())
|
|
|
|
.then(() => runTest())
|
|
|
|
.then(() => runTest())
|
|
|
|
.then(() => runTest())
|
|
|
|
.then(() => runTest())
|
|
|
|
.then(() => runTest())
|
|
|
|
.then(() => runTest())
|
|
|
|
.then(() => runTest());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-11-20 09:17:08 -05:00
|
|
|
it('should increment a value', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.select('logins')
|
|
|
|
.where('id', 1)
|
|
|
|
.then(function(accounts) {
|
|
|
|
return knex('accounts')
|
|
|
|
.where('id', 1)
|
|
|
|
.increment('logins')
|
|
|
|
.then(function(rowsAffected) {
|
|
|
|
expect(rowsAffected).to.equal(1);
|
|
|
|
return knex('accounts')
|
|
|
|
.select('logins')
|
|
|
|
.where('id', 1);
|
|
|
|
})
|
|
|
|
.then(function(accounts2) {
|
|
|
|
expect(accounts[0].logins + 1).to.equal(accounts2[0].logins);
|
|
|
|
});
|
2013-11-20 09:17:08 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-03-20 13:01:42 +01:00
|
|
|
it('should increment a negative value', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.select('logins')
|
|
|
|
.where('id', 1)
|
|
|
|
.then(function(accounts) {
|
|
|
|
return knex('accounts')
|
|
|
|
.where('id', 1)
|
|
|
|
.increment('logins', -2)
|
|
|
|
.then(function(rowsAffected) {
|
|
|
|
expect(rowsAffected).to.equal(1);
|
|
|
|
return knex('accounts')
|
|
|
|
.select('logins')
|
|
|
|
.where('id', 1);
|
|
|
|
})
|
|
|
|
.then(function(accounts2) {
|
|
|
|
expect(accounts[0].logins - 2).to.equal(accounts2[0].logins);
|
|
|
|
});
|
2014-03-20 13:01:42 +01:00
|
|
|
});
|
|
|
|
});
|
2013-11-20 09:17:08 -05:00
|
|
|
|
2018-05-19 01:49:00 -07:00
|
|
|
it('should increment a float value', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.select('balance')
|
|
|
|
.where('id', 1)
|
|
|
|
.then(function(accounts) {
|
|
|
|
return knex('accounts')
|
|
|
|
.where('id', 1)
|
|
|
|
.increment('balance', 22.53)
|
|
|
|
.then(function(rowsAffected) {
|
|
|
|
expect(rowsAffected).to.equal(1);
|
|
|
|
return knex('accounts')
|
|
|
|
.select('balance')
|
|
|
|
.where('id', 1);
|
|
|
|
})
|
|
|
|
.then(function(accounts2) {
|
|
|
|
expect(accounts[0].balance + 22.53).to.be.closeTo(
|
|
|
|
accounts2[0].balance,
|
|
|
|
0.001
|
|
|
|
);
|
|
|
|
});
|
2018-05-19 01:49:00 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-11-20 09:17:08 -05:00
|
|
|
it('should decrement a value', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.select('logins')
|
|
|
|
.where('id', 1)
|
|
|
|
.then(function(accounts) {
|
|
|
|
return knex('accounts')
|
|
|
|
.where('id', 1)
|
|
|
|
.decrement('logins')
|
|
|
|
.then(function(rowsAffected) {
|
|
|
|
expect(rowsAffected).to.equal(1);
|
|
|
|
return knex('accounts')
|
|
|
|
.select('logins')
|
|
|
|
.where('id', 1);
|
|
|
|
})
|
|
|
|
.then(function(accounts2) {
|
|
|
|
expect(accounts[0].logins - 1).to.equal(accounts2[0].logins);
|
|
|
|
});
|
2013-11-20 09:17:08 -05:00
|
|
|
});
|
2014-03-20 13:01:42 +01:00
|
|
|
});
|
2013-11-20 09:17:08 -05:00
|
|
|
|
2014-03-20 13:01:42 +01:00
|
|
|
it('should decrement a negative value', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.select('logins')
|
|
|
|
.where('id', 1)
|
|
|
|
.then(function(accounts) {
|
|
|
|
return knex('accounts')
|
|
|
|
.where('id', 1)
|
|
|
|
.decrement('logins', -2)
|
|
|
|
.then(function(rowsAffected) {
|
|
|
|
expect(rowsAffected).to.equal(1);
|
|
|
|
return knex('accounts')
|
|
|
|
.select('logins')
|
|
|
|
.where('id', 1);
|
|
|
|
})
|
|
|
|
.then(function(accounts2) {
|
|
|
|
expect(accounts[0].logins + 2).to.equal(accounts2[0].logins);
|
|
|
|
});
|
2014-03-20 13:01:42 +01:00
|
|
|
});
|
2013-11-20 09:17:08 -05:00
|
|
|
});
|
|
|
|
|
2018-05-19 01:49:00 -07:00
|
|
|
it('should decrement a float value', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.select('balance')
|
|
|
|
.where('id', 1)
|
|
|
|
.then(function(accounts) {
|
|
|
|
return knex('accounts')
|
|
|
|
.where('id', 1)
|
|
|
|
.decrement('balance', 10.29)
|
|
|
|
.then(function(rowsAffected) {
|
|
|
|
expect(rowsAffected).to.equal(1);
|
|
|
|
return knex('accounts')
|
|
|
|
.select('balance')
|
|
|
|
.where('id', 1);
|
|
|
|
})
|
|
|
|
.then(function(accounts2) {
|
|
|
|
expect(accounts[0].balance - 10.29).to.be.closeTo(
|
|
|
|
accounts2[0].balance,
|
|
|
|
0.001
|
|
|
|
);
|
|
|
|
});
|
2018-05-19 01:49:00 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-03-24 17:48:14 -07:00
|
|
|
it('should allow returning for updates in postgresql', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.where('id', 1)
|
|
|
|
.update(
|
|
|
|
{
|
2013-12-27 14:44:21 -05:00
|
|
|
email: 'test100@example.com',
|
2015-12-09 17:53:53 -06:00
|
|
|
first_name: 'UpdatedUser',
|
|
|
|
last_name: 'UpdatedTest',
|
2018-07-09 08:10:34 -04:00
|
|
|
},
|
|
|
|
'*'
|
|
|
|
)
|
|
|
|
.testSql(function(tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'update `accounts` set `email` = ?, `first_name` = ?, `last_name` = ? where `id` = ?',
|
|
|
|
['test100@example.com', 'UpdatedUser', 'UpdatedTest', 1],
|
|
|
|
1
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'pg',
|
|
|
|
'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ? returning *',
|
|
|
|
['test100@example.com', 'UpdatedUser', 'UpdatedTest', 1],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
id: '1',
|
|
|
|
first_name: 'UpdatedUser',
|
|
|
|
last_name: 'UpdatedTest',
|
|
|
|
email: 'test100@example.com',
|
|
|
|
logins: 1,
|
|
|
|
balance: 12.24,
|
|
|
|
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
|
|
|
created_at: d,
|
|
|
|
updated_at: d,
|
|
|
|
phone: null,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'pg-redshift',
|
|
|
|
'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ?',
|
|
|
|
['test100@example.com', 'UpdatedUser', 'UpdatedTest', 1],
|
|
|
|
1
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'sqlite3',
|
|
|
|
'update `accounts` set `email` = ?, `first_name` = ?, `last_name` = ? where `id` = ?',
|
|
|
|
['test100@example.com', 'UpdatedUser', 'UpdatedTest', 1],
|
|
|
|
1
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'oracledb',
|
|
|
|
'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ? returning "ROWID" into ?',
|
|
|
|
[
|
|
|
|
'test100@example.com',
|
|
|
|
'UpdatedUser',
|
|
|
|
'UpdatedTest',
|
|
|
|
1,
|
|
|
|
(v) => v.toString() === '[object ReturningHelper:ROWID]',
|
|
|
|
],
|
|
|
|
1
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'mssql',
|
|
|
|
'update [accounts] set [email] = ?, [first_name] = ?, [last_name] = ? output inserted.* where [id] = ?',
|
|
|
|
['test100@example.com', 'UpdatedUser', 'UpdatedTest', 1],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
id: '1',
|
|
|
|
first_name: 'UpdatedUser',
|
|
|
|
last_name: 'UpdatedTest',
|
|
|
|
email: 'test100@example.com',
|
|
|
|
logins: 1,
|
|
|
|
balance: 12.240000000000002,
|
|
|
|
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
|
|
|
created_at: d,
|
|
|
|
updated_at: d,
|
|
|
|
phone: null,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
});
|
2013-12-10 14:54:51 -05:00
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
2014-03-20 13:01:42 +01:00
|
|
|
};
|