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

SQLStore: Fix PostgreSQL failure to create organisation for first time #21648

Merged
merged 5 commits into from
Jan 22, 2020

Conversation

papagian
Copy link
Contributor

What this PR does / why we need it:
Grafana explicitly sets the org id when creates the main organisation. However, this does not work nicely with PostgresSQL because it seems that the primary key sequence is only evaluated when the id field is not set. This fix adds a hook for syncing the sequence for the org table after inserts.

Which issue(s) this PR fixes:

Fixes #18628

Special notes for your reviewer:

@papagian papagian requested review from a team and bergquist and removed request for a team January 21, 2020 16:42

// sync primary key sequence of org table
if _, err := sess.Exec("SELECT setval('org_id_seq', (SELECT max(id)+1 FROM org));"); err != nil {
return fmt.Errorf("Failed to sync primary key for org table")
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to follow the error pattern in the file, but I wonder:

  1. Why not return the error if an error?
  2. Regardless of what is done with the error, should the contents of sess.Exec's err be wrapped into the message so the reason is logged?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good points, I will fix it!

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, wrapping the error sounds like the thing to do for me as well :)

Copy link
Contributor

@aknuds1 aknuds1 left a comment

Choose a reason for hiding this comment

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

Looks good AFAICT, apart from the error that should get wrapped.


// sync primary key sequence of org table
if _, err := sess.Exec("SELECT setval('org_id_seq', (SELECT max(id)+1 FROM org));"); err != nil {
return fmt.Errorf("Failed to sync primary key for org table")
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, wrapping the error sounds like the thing to do for me as well :)

papagian and others added 2 commits January 22, 2020 11:25
Co-Authored-By: Arve Knudsen <arve.knudsen@gmail.com>
Copy link
Contributor

@aknuds1 aknuds1 left a comment

Choose a reason for hiding this comment

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

LGTM!

@papagian papagian merged commit 2283cee into master Jan 22, 2020
@papagian papagian deleted the 18628_fix branch January 22, 2020 15:56
@papagian papagian modified the milestones: 6.7, 6.6.0-stable Jan 22, 2020
xlson pushed a commit that referenced this pull request Jan 27, 2020
#21648)

* Fix PostgreSQL failure to create organisation for first time

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
(cherry picked from commit 2283cee)
xlson pushed a commit that referenced this pull request Jan 27, 2020
#21648)

* Fix PostgreSQL failure to create organisation for first time

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
(cherry picked from commit 2283cee)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

First attempt of creating an organization fails with PostgreSQL database
4 participants