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

Initial modifications to get enums working with postgres schemas #4796

Merged
merged 3 commits into from Nov 13, 2015
Merged

Initial modifications to get enums working with postgres schemas #4796

merged 3 commits into from Nov 13, 2015

Conversation

faceleg
Copy link
Contributor

@faceleg faceleg commented Nov 3, 2015

First off, thanks for all the hard work, I'm loving Sequelize.

I am using Postgres and want to use a non-standard schema for some models. I use ENUM for my model status fields, which has lead to problems for me when combined with the non-standard schema.

This PR aims to make Sequelize work perfectly with non standard Postgres schemas.

Please can you glance through the commit and let me know if you're happy with the direction I'm heading. Once I have it working I'll look at extracting the duplication in to a helper function or two.

var schema = tableName.schema || options.schema || 'public';
var delimiter = tableName.delimiter || options.delimiter || '.';

var sql = 'CREATE TYPE ' + this.quoteIdentifier(schema) + delimiter + enumName + ' AS ' + values + ';';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change - the name creation is intentionally moved to a function, which handles schema? Isn't the end output the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was causing this portion (L746) to fail:

var query = 'SELECT t.typname enum_name, array_agg(e.enumlabel) enum_value FROM pg_type t ' +
       'JOIN pg_enum e ON t.oid = e.enumtypid ' +
       'JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace ' +
       "WHERE n.nspname = '" + schema + "'" + enumName + ' GROUP BY 1';

Example:

WHERE n.nspname = "public" "public"."enumNameHere" GROUP BY 1;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then the change should be the other way I think - so that enumName is generated manually here. pgEnumName is used name places, otherwise we'd have to duplicate the logic for adding schema in front of enum name to all those functiond

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just typing the same thing 💃

Too many late late nights in a row

@faceleg faceleg closed this Nov 3, 2015
@faceleg faceleg reopened this Nov 3, 2015
@faceleg
Copy link
Contributor Author

faceleg commented Nov 3, 2015

@janmeier another go

@faceleg
Copy link
Contributor Author

faceleg commented Nov 3, 2015

@janmeier ready for your scrutiny

@faceleg
Copy link
Contributor Author

faceleg commented Nov 5, 2015

@janmeier bump

@janmeier
Copy link
Member

janmeier commented Nov 5, 2015

One failing test, otherwise LGTM.

If you could fix the test (just add a check for options), add a changelog entry and squash to a single descriptive commit this is good to go

@janmeier janmeier self-assigned this Nov 5, 2015
@faceleg
Copy link
Contributor Author

faceleg commented Nov 5, 2015

Is it expected that pgListEnums would get called with no tableName?

@janmeier
Copy link
Member

janmeier commented Nov 5, 2015

Yes, it can also be used to list all existing enums, not just those in a single table

@faceleg
Copy link
Contributor Author

faceleg commented Nov 5, 2015

Please let me know if this is suitable

@faceleg
Copy link
Contributor Author

faceleg commented Nov 6, 2015

Bump

@faceleg
Copy link
Contributor Author

faceleg commented Nov 8, 2015

Bumping again 💃

@faceleg
Copy link
Contributor Author

faceleg commented Nov 8, 2015

@mickhansen if @janmeier is super busy, would you please be able to approve this?

enumName = '"' + tableName.schema + '"' + tableName.delimiter + enumName;
if (options.schema !== false && (tableName.schema || options.schema)) {
var schema = tableName.schema || options.schema || 'public';
var delimiter = tableName.delimiter || options.delimiter || '.';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use pre-comma per the general style,

var schema = ...
  , delimiter = ...

Goes for everywhere else aswell :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code handles having schema/delimter be part of the table name and be part of the options.
I imagine we'll need this multiple places since a lot of queryInterface (migration) calls should support both, ideally we could provide some helper that takes a tableName and an options and returns schema, delimiter and tableName.

@mickhansen
Copy link
Contributor

Linter errors!

@faceleg
Copy link
Contributor Author

faceleg commented Nov 11, 2015

Never ending pull request

@mickhansen
Copy link
Contributor

Well a make jshint woulda helped you on that one ;)

@faceleg
Copy link
Contributor Author

faceleg commented Nov 11, 2015

Yep my bad :D ill have to deal with it in 12 hours it's sleep time

@faceleg
Copy link
Contributor Author

faceleg commented Nov 11, 2015

@mickhansen updated

@mickhansen
Copy link
Contributor

@faceleg
Copy link
Contributor Author

faceleg commented Nov 12, 2015

😄 🌴

@faceleg
Copy link
Contributor Author

faceleg commented Nov 12, 2015

Huh, they pass on my machine. Looking into it

@faceleg
Copy link
Contributor Author

faceleg commented Nov 12, 2015

Turns out I was running dramatic pause the wrong tests

@mickhansen
Copy link
Contributor

https://travis-ci.org/sequelize/sequelize/jobs/90688045
Just a single expectation that's off.

@faceleg
Copy link
Contributor Author

faceleg commented Nov 12, 2015

Oh look, I did fix it! (went to bed after last commit)

@faceleg
Copy link
Contributor Author

faceleg commented Nov 12, 2015

Ahem, I mean, it's ready to merge.

mickhansen added a commit that referenced this pull request Nov 13, 2015
Initial modifications to get enums working with postgres schemas
@mickhansen mickhansen merged commit 9775f36 into sequelize:master Nov 13, 2015
@mickhansen
Copy link
Contributor

Thank you for the good work and the patience @faceleg :)

@faceleg
Copy link
Contributor Author

faceleg commented Nov 13, 2015

Pretty sure you were the patient one haha.

Thanks for bearing with me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants