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

fix(postgres): parse enums correctly when describing a table #12409

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
2 changes: 1 addition & 1 deletion lib/dialects/postgres/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
return [];
}

matches = matches.map(m => m.replace(/",$/, '').replace(/,$/, '').replace(/(^"|"$)/, ''));
matches = matches.map(m => m.replace(/",$/, '').replace(/,$/, '').replace(/(^"|"$)/g, ''));

return matches.slice(0, -1);
}
Expand Down
40 changes: 40 additions & 0 deletions test/unit/dialects/postgres/query-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1265,5 +1265,45 @@ if (dialect.startsWith('postgres')) {
});
});
});

describe('fromArray()', () => {
beforeEach(function() {
this.queryGenerator = new QueryGenerator({
sequelize: this.sequelize,
_dialect: this.sequelize.dialect
});
});

const tests = [
{
title: 'should convert an enum with no quoted strings to an array',
arguments: '{foo,bar,foobar}',
expectation: ['foo', 'bar', 'foobar']
}, {
title: 'should convert an enum starting with a quoted string to an array',
arguments: '{"foo bar",foo,bar}',
expectation: ['foo bar', 'foo', 'bar']
}, {
title: 'should convert an enum ending with a quoted string to an array',
arguments: '{foo,bar,"foo bar"}',
expectation: ['foo', 'bar', 'foo bar']
}, {
title: 'should convert an enum with a quoted string in the middle to an array',
arguments: '{foo,"foo bar",bar}',
expectation: ['foo', 'foo bar', 'bar']
}, {
title: 'should convert an enum full of quoted strings to an array',
arguments: '{"foo bar","foo bar","foo bar"}',
expectation: ['foo bar', 'foo bar', 'foo bar']
}
];

_.each(tests, test => {
it(test.title, function() {
const convertedText = this.queryGenerator.fromArray(test.arguments);
expect(convertedText).to.deep.equal(test.expectation);
});
});
});
});
}