Skip to content

Commit

Permalink
fix(sync): throw when no models defined (sequelize#10175)
Browse files Browse the repository at this point in the history
  • Loading branch information
sushantdhiman committed Nov 18, 2018
1 parent 6b1ff3b commit 946fb72
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 44 deletions.
3 changes: 3 additions & 0 deletions lib/sequelize.js
Expand Up @@ -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));
}).then(() => {
if (options.hooks) {
Expand Down
97 changes: 53 additions & 44 deletions test/integration/sequelize.test.js
Expand Up @@ -965,25 +965,35 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => {
});

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() {
this.sequelizeWithInvalidCredentials = new Sequelize('omg', 'bar', null, _.omit(this.sequelize.options, ['host']));

const User2 = this.sequelizeWithInvalidCredentials.define('User', { name: DataTypes.STRING, bio: DataTypes.TEXT });

return User2.sync().catch(err => {
if (dialect === 'postgres' || dialect === 'postgres-native') {
assert([
'fe_sendauth: no password supplied',
'role "bar" does not exist',
'FATAL: role "bar" does not exist',
'password authentication failed for user "bar"'
].includes(err.message.trim()));
} else if (dialect === 'mssql') {
expect(err.message).to.equal('Login failed for user \'bar\'.');
} else {
expect(err.message.toString()).to.match(/.*Access\ denied.*/);
}
});
return User2.sync()
.then(() => { expect.fail(); })
.catch(err => {
if (dialect === 'postgres' || dialect === 'postgres-native') {
assert([
'fe_sendauth: no password supplied',
'role "bar" does not exist',
'FATAL: role "bar" does not exist',
'password authentication failed for user "bar"'
].includes(err.message.trim()));
} else if (dialect === 'mssql') {
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() {
Expand All @@ -994,9 +1004,7 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => {
sequelize.define('Project', {title: Sequelize.STRING});
sequelize.define('Task', {title: Sequelize.STRING});

return sequelize.sync({force: true}).catch(err => {
expect(err).to.be.ok;
});
return expect(sequelize.sync({force: true})).to.be.rejected;
});

it('fails with incorrect database credentials (3)', function() {
Expand All @@ -1008,9 +1016,7 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => {
sequelize.define('Project', {title: Sequelize.STRING});
sequelize.define('Task', {title: Sequelize.STRING});

return sequelize.sync({force: true}).catch(err => {
expect(err).to.be.ok;
});
return expect(sequelize.sync({force: true})).to.be.rejected;
});

it('fails with incorrect database credentials (4)', function() {
Expand All @@ -1023,19 +1029,22 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => {
sequelize.define('Project', {title: Sequelize.STRING});
sequelize.define('Task', {title: Sequelize.STRING});

return sequelize.sync({force: true}).catch(err => {
expect(err).to.be.ok;
});
return expect(sequelize.sync({force: true})).to.be.rejected;
});

it('returns an error correctly if unable to sync a foreign key referenced model', function() {
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 => {
assert.ok(error);
});
return expect(this.sequelize.sync()).to.be.rejected;
});

it('handles this dependant foreign key constraints', function() {
Expand Down Expand Up @@ -1065,28 +1074,28 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => {

return this.sequelize.sync();
});
}

it('return the sequelize instance after syncing', function() {
return this.sequelize.sync().then(sequelize => {
expect(sequelize).to.deep.equal(this.sequelize);
});
it('return the sequelize instance after syncing', function() {
return this.sequelize.sync().then(sequelize => {
expect(sequelize).to.deep.equal(this.sequelize);
});
});

it('return the single dao after syncing', function() {
const block = this.sequelize.define('block', {
id: { type: DataTypes.INTEGER, primaryKey: true },
name: DataTypes.STRING
}, {
tableName: 'block',
timestamps: false,
paranoid: false
});
it('return the single dao after syncing', function() {
const block = this.sequelize.define('block', {
id: { type: DataTypes.INTEGER, primaryKey: true },
name: DataTypes.STRING
}, {
tableName: 'block',
timestamps: false,
paranoid: false
});

return block.sync().then(result => {
expect(result).to.deep.equal(block);
});
return block.sync().then(result => {
expect(result).to.deep.equal(block);
});
}
});

describe("doesn't emit logging when explicitly saying not to", () => {
afterEach(function() {
Expand Down

0 comments on commit 946fb72

Please sign in to comment.