Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unify returning queries #17157

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2531,7 +2531,7 @@ ${associationOwner._getAssociationDebugList()}`);
!instance ||
(key === model.primaryKeyAttribute &&
instance.get(model.primaryKeyAttribute) &&
['mysql', 'mariadb', 'sqlite3'].includes(dialect))
['mysql', 'mariadb'].includes(dialect))
) {
// The query.js for these DBs is blind, it autoincrements the
// primarykey value, even if it was set manually. Also, it can
Expand Down
2 changes: 2 additions & 0 deletions packages/core/test/integration/model/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ describe('Model.update', () => {

expectsql(sqlQuery, {
default: `UPDATE [users1] SET [secretValue]=$sequelize_1,[updatedAt]=$sequelize_2 WHERE [id] = $sequelize_3`,
sqlite3:
'UPDATE `users1` SET `secretValue`=$sequelize_1,`updatedAt`=$sequelize_2 WHERE `id` = $sequelize_3 RETURNING *',
postgres: `UPDATE "users1" SET "secretValue"=$1,"updatedAt"=$2 WHERE "id" = $3 RETURNING *`,
mysql: 'UPDATE `users1` SET `secretValue`=?,`updatedAt`=? WHERE `id` = ?',
mariadb: 'UPDATE `users1` SET `secretValue`=?,`updatedAt`=? WHERE `id` = ?',
Expand Down
105 changes: 29 additions & 76 deletions packages/core/test/integration/model/upsert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,16 @@ describe('Model', () => {
describe('upsert', () => {
it('works with upsert on id', async function () {
const [, created0] = await this.User.upsert({ id: 42, username: 'john' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.true;
}

this.clock.tick(1000);
const [, created] = await this.User.upsert({ id: 42, username: 'doe' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand All @@ -89,20 +85,16 @@ describe('Model', () => {

it('works with upsert on a composite key', async function () {
const [, created0] = await this.User.upsert({ foo: 'baz', bar: 19, username: 'john' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.true;
}

this.clock.tick(1000);
const [, created] = await this.User.upsert({ foo: 'baz', bar: 19, username: 'doe' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand Down Expand Up @@ -152,12 +144,9 @@ describe('Model', () => {
User.upsert({ a: 'a', b: 'a', username: 'curt' }),
]);

if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created1[1]).to.be.null;
expect(created2[1]).to.be.null;
} else if (dialectName === 'db2') {
expect(created1[1]).to.be.undefined;
expect(created2[1]).to.be.undefined;
} else {
expect(created1[1]).to.be.true;
expect(created2[1]).to.be.true;
Expand All @@ -166,10 +155,8 @@ describe('Model', () => {
this.clock.tick(1000);
// Update the first one
const [, created] = await User.upsert({ a: 'a', b: 'b', username: 'doe' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand Down Expand Up @@ -215,10 +202,8 @@ describe('Model', () => {

await User.sync({ force: true });
const [, created] = await User.upsert({ id: 1, email: 'notanemail' }, options);
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.true;
}
Expand All @@ -230,10 +215,8 @@ describe('Model', () => {
username: 'john',
blob: Buffer.from('kaj'),
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.ok;
}
Expand All @@ -244,10 +227,8 @@ describe('Model', () => {
username: 'doe',
blob: Buffer.from('andrea'),
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand All @@ -261,19 +242,15 @@ describe('Model', () => {

it('works with .field', async function () {
const [, created0] = await this.User.upsert({ id: 42, baz: 'foo' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.ok;
}

const [, created] = await this.User.upsert({ id: 42, baz: 'oof' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand All @@ -284,20 +261,16 @@ describe('Model', () => {

it('works with primary key using .field', async function () {
const [, created0] = await this.ModelWithFieldPK.upsert({ userId: 42, foo: 'first' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.ok;
}

this.clock.tick(1000);
const [, created] = await this.ModelWithFieldPK.upsert({ userId: 42, foo: 'second' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand All @@ -312,10 +285,8 @@ describe('Model', () => {
username: 'john',
foo: this.sequelize.fn('upper', 'mixedCase1'),
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.ok;
}
Expand All @@ -326,10 +297,8 @@ describe('Model', () => {
username: 'doe',
foo: this.sequelize.fn('upper', 'mixedCase2'),
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand Down Expand Up @@ -411,10 +380,8 @@ describe('Model', () => {
await this.User.create({ id: 42, username: 'john' });
const user = await this.User.findByPk(42);
const [, created] = await this.User.upsert({ id: user.id, username: user.username });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
// After set node-mysql flags = '-FOUND_ROWS' / foundRows=false
// result from upsert should be false when upsert a row to its current value
Expand Down Expand Up @@ -443,10 +410,8 @@ describe('Model', () => {
email: 'user1@domain.ext',
city: 'City',
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.ok;
}
Expand All @@ -456,10 +421,8 @@ describe('Model', () => {
email: 'user1@domain.ext',
city: 'New City',
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand Down Expand Up @@ -499,10 +462,8 @@ describe('Model', () => {
email: 'user1@domain.ext',
city: 'City',
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.ok;
}
Expand All @@ -512,10 +473,8 @@ describe('Model', () => {
email: 'user1@domain.ext',
city: 'New City',
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand Down Expand Up @@ -547,10 +506,8 @@ describe('Model', () => {

await User.sync({ force: true });
const [, created0] = await User.upsert({ name: 'user1', address: 'address', city: 'City' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.ok;
}
Expand All @@ -560,10 +517,8 @@ describe('Model', () => {
address: 'address',
city: 'New City',
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).not.to.be.ok;
}
Expand Down Expand Up @@ -663,7 +618,7 @@ describe('Model', () => {
);
expect(user0.get('id')).to.equal(42);
expect(user0.get('username')).to.equal('john');
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else {
expect(created0).to.be.true;
Expand All @@ -675,7 +630,7 @@ describe('Model', () => {
);
expect(user.get('id')).to.equal(42);
expect(user.get('username')).to.equal('doe');
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else {
expect(created).to.be.false;
Expand All @@ -702,7 +657,7 @@ describe('Model', () => {
);
expect(user0.get('id')).to.equal(42);
expect(user0.get('username')).to.equal('john');
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else {
expect(created0).to.be.true;
Expand All @@ -714,7 +669,7 @@ describe('Model', () => {
);
expect(user.get('id')).to.equal(42);
expect(user.get('username')).to.equal('doe');
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else {
expect(created).to.be.false;
Expand All @@ -740,7 +695,7 @@ describe('Model', () => {
);
expect(user0.get('id')).to.equal('surya');
expect(user0.get('username')).to.equal('john');
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else {
expect(created0).to.be.true;
Expand All @@ -752,7 +707,7 @@ describe('Model', () => {
);
expect(user.get('id')).to.equal('surya');
expect(user.get('username')).to.equal('doe');
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else {
expect(created).to.be.false;
Expand All @@ -775,10 +730,8 @@ describe('Model', () => {
expect(user.name).to.equal('Test default value');
expect(user.code).to.equal(2020);

if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite3', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.true;
}
Expand Down