Skip to content

Commit

Permalink
feat(postgres): minify include aliases over limit (#11940)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrrinot committed Mar 16, 2020
1 parent b739613 commit f1ba280
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/dialects/abstract/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,7 @@ class QueryGenerator {
if (this.options.minifyAliases && !options.aliasesMapping) {
options.aliasesMapping = new Map();
options.aliasesByTable = {};
options.includeAliases = new Map();
}

// resolve table name options
Expand Down Expand Up @@ -1721,6 +1722,12 @@ class QueryGenerator {
}
}

if (this.options.minifyAliases && asRight.length > 63) {
const alias = `%${topLevelInfo.options.includeAliases.size}`;

topLevelInfo.options.includeAliases.set(alias, asRight);
}

return {
join: include.required ? 'INNER JOIN' : include.right && this._dialect.supports['RIGHT JOIN'] ? 'RIGHT OUTER JOIN' : 'LEFT OUTER JOIN',
body: this.quoteTable(tableRight, asRight),
Expand Down
12 changes: 12 additions & 0 deletions lib/dialects/postgres/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ class Query extends AbstractQuery {
if (!_.isEmpty(this.options.searchPath)) {
sql = this.sequelize.getQueryInterface().QueryGenerator.setSearchPath(this.options.searchPath) + sql;
}

if (this.sequelize.options.minifyAliases && this.options.includeAliases) {
_.toPairs(this.options.includeAliases)
// Sorting to replace the longest aliases first to prevent alias collision
.sort((a, b) => b[1].length - a[1].length)
.forEach(([alias, original]) => {
const reg = new RegExp(_.escapeRegExp(original), 'g');

sql = sql.replace(reg, alias);
});
}

this.sql = sql;

const query = parameters && parameters.length
Expand Down
47 changes: 47 additions & 0 deletions test/integration/dialects/postgres/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,52 @@ if (dialect.match(/^postgres/)) {
expect(res[taskAlias].title).to.be.equal('SuperTask');
});
});

it('should throw due to table name being truncated', () => {
const sequelize = Support.createSequelizeInstance({ minifyAliases: true });

const User = sequelize.define('user_model_name_that_is_long_for_demo_but_also_surpasses_the_character_limit',
{
name: DataTypes.STRING,
email: DataTypes.STRING
},
{
tableName: 'user'
}
);
const Project = sequelize.define('project_model_name_that_is_long_for_demo_but_also_surpasses_the_character_limit',
{
name: DataTypes.STRING
},
{
tableName: 'project'
}
);
const Company = sequelize.define('company_model_name_that_is_long_for_demo_but_also_surpasses_the_character_limit',
{
name: DataTypes.STRING
},
{
tableName: 'company'
}
);
User.hasMany(Project, { foreignKey: 'userId' });
Project.belongsTo(Company, { foreignKey: 'companyId' });

return sequelize.sync({ force: true }).then(() => {
return Company.create({ name: 'Sequelize' }).then(comp => {
return User.create({ name: 'standard user' }).then(user => {
return Project.create({ name: 'Manhattan', companyId: comp.id, userId: user.id }).then(() => {
return User.findAll({
include: {
model: Project,
include: Company
}
});
});
});
});
});
});
});
}

0 comments on commit f1ba280

Please sign in to comment.