不要怂,就是干,撸起袖子干!

Commit 946fb729 by Sushant Committed by GitHub

fix(sync): throw when no models defined (#10175)

1 parent 6b1ff3b1
...@@ -733,6 +733,9 @@ class Sequelize { ...@@ -733,6 +733,9 @@ class Sequelize {
} }
}); });
// no models defined, just authenticate
if (!models.length) return this.authenticate(options);
return Promise.each(models, model => model.sync(options)); return Promise.each(models, model => model.sync(options));
}).then(() => { }).then(() => {
if (options.hooks) { if (options.hooks) {
......
...@@ -965,25 +965,35 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => { ...@@ -965,25 +965,35 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => {
}); });
if (dialect !== 'sqlite') { if (dialect !== 'sqlite') {
it('fails for incorrect connection even when no models are defined', function() {
const sequelize = new Sequelize('cyber_bird', 'user', 'pass', {
dialect: this.sequelize.options.dialect
});
return expect(sequelize.sync({force: true})).to.be.rejected;
});
it('fails with incorrect database credentials (1)', function() { it('fails with incorrect database credentials (1)', function() {
this.sequelizeWithInvalidCredentials = new Sequelize('omg', 'bar', null, _.omit(this.sequelize.options, ['host'])); this.sequelizeWithInvalidCredentials = new Sequelize('omg', 'bar', null, _.omit(this.sequelize.options, ['host']));
const User2 = this.sequelizeWithInvalidCredentials.define('User', { name: DataTypes.STRING, bio: DataTypes.TEXT }); const User2 = this.sequelizeWithInvalidCredentials.define('User', { name: DataTypes.STRING, bio: DataTypes.TEXT });
return User2.sync().catch(err => { return User2.sync()
if (dialect === 'postgres' || dialect === 'postgres-native') { .then(() => { expect.fail(); })
assert([ .catch(err => {
'fe_sendauth: no password supplied', if (dialect === 'postgres' || dialect === 'postgres-native') {
'role "bar" does not exist', assert([
'FATAL: role "bar" does not exist', 'fe_sendauth: no password supplied',
'password authentication failed for user "bar"' 'role "bar" does not exist',
].includes(err.message.trim())); 'FATAL: role "bar" does not exist',
} else if (dialect === 'mssql') { 'password authentication failed for user "bar"'
expect(err.message).to.equal('Login failed for user \'bar\'.'); ].includes(err.message.trim()));
} else { } else if (dialect === 'mssql') {
expect(err.message.toString()).to.match(/.*Access\ denied.*/); expect(err.message).to.equal('Login failed for user \'bar\'.');
} } else {
}); expect(err.message.toString()).to.match(/.*Access\ denied.*/);
}
});
}); });
it('fails with incorrect database credentials (2)', function() { it('fails with incorrect database credentials (2)', function() {
...@@ -994,9 +1004,7 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => { ...@@ -994,9 +1004,7 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => {
sequelize.define('Project', {title: Sequelize.STRING}); sequelize.define('Project', {title: Sequelize.STRING});
sequelize.define('Task', {title: Sequelize.STRING}); sequelize.define('Task', {title: Sequelize.STRING});
return sequelize.sync({force: true}).catch(err => { return expect(sequelize.sync({force: true})).to.be.rejected;
expect(err).to.be.ok;
});
}); });
it('fails with incorrect database credentials (3)', function() { it('fails with incorrect database credentials (3)', function() {
...@@ -1008,9 +1016,7 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => { ...@@ -1008,9 +1016,7 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => {
sequelize.define('Project', {title: Sequelize.STRING}); sequelize.define('Project', {title: Sequelize.STRING});
sequelize.define('Task', {title: Sequelize.STRING}); sequelize.define('Task', {title: Sequelize.STRING});
return sequelize.sync({force: true}).catch(err => { return expect(sequelize.sync({force: true})).to.be.rejected;
expect(err).to.be.ok;
});
}); });
it('fails with incorrect database credentials (4)', function() { it('fails with incorrect database credentials (4)', function() {
...@@ -1023,19 +1029,22 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => { ...@@ -1023,19 +1029,22 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => {
sequelize.define('Project', {title: Sequelize.STRING}); sequelize.define('Project', {title: Sequelize.STRING});
sequelize.define('Task', {title: Sequelize.STRING}); sequelize.define('Task', {title: Sequelize.STRING});
return sequelize.sync({force: true}).catch(err => { return expect(sequelize.sync({force: true})).to.be.rejected;
expect(err).to.be.ok;
});
}); });
it('returns an error correctly if unable to sync a foreign key referenced model', function() { it('returns an error correctly if unable to sync a foreign key referenced model', function() {
this.sequelize.define('Application', { this.sequelize.define('Application', {
authorID: { type: Sequelize.BIGINT, allowNull: false, references: { model: 'User', key: 'id' } } authorID: {
type: Sequelize.BIGINT,
allowNull: false,
references: {
model: 'User',
key: 'id'
}
}
}); });
return this.sequelize.sync().catch(error => { return expect(this.sequelize.sync()).to.be.rejected;
assert.ok(error);
});
}); });
it('handles this dependant foreign key constraints', function() { it('handles this dependant foreign key constraints', function() {
...@@ -1065,28 +1074,28 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => { ...@@ -1065,28 +1074,28 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => {
return this.sequelize.sync(); return this.sequelize.sync();
}); });
}
it('return the sequelize instance after syncing', function() { it('return the sequelize instance after syncing', function() {
return this.sequelize.sync().then(sequelize => { return this.sequelize.sync().then(sequelize => {
expect(sequelize).to.deep.equal(this.sequelize); expect(sequelize).to.deep.equal(this.sequelize);
});
}); });
});
it('return the single dao after syncing', function() { it('return the single dao after syncing', function() {
const block = this.sequelize.define('block', { const block = this.sequelize.define('block', {
id: { type: DataTypes.INTEGER, primaryKey: true }, id: { type: DataTypes.INTEGER, primaryKey: true },
name: DataTypes.STRING name: DataTypes.STRING
}, { }, {
tableName: 'block', tableName: 'block',
timestamps: false, timestamps: false,
paranoid: false paranoid: false
}); });
return block.sync().then(result => { return block.sync().then(result => {
expect(result).to.deep.equal(block); expect(result).to.deep.equal(block);
});
}); });
} });
describe("doesn't emit logging when explicitly saying not to", () => { describe("doesn't emit logging when explicitly saying not to", () => {
afterEach(function() { afterEach(function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!