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

Commit f1ba2808 by Raphaël Ricard Committed by GitHub

feat(postgres): minify include aliases over limit (#11940)

1 parent b7396137
......@@ -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
......@@ -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),
......
......@@ -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
......
......@@ -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
}
});
});
});
});
});
});
});
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!