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

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

Merged
Merged
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
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
}
});
});
});
});
});
});
});
}