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

don't error when failing to create http_sessions due to permissions #31

Closed
dhduvall opened this issue Feb 20, 2020 · 3 comments · Fixed by #32
Closed

don't error when failing to create http_sessions due to permissions #31

dhduvall opened this issue Feb 20, 2020 · 3 comments · Fixed by #32

Comments

@dhduvall
Copy link
Contributor

CREATE TABLE IF NOT EXISTS fails with an insufficient_privilege exception when the user executing the statement doesn't have the privileges to create the table in the current schema, even if the table exists. I don't know whether this is a bug in Postgres (9.6), even if it seems like it should succeed.

I've rewritten the statement that creates http_sessions to work around that:

stmt := `DO $$
    BEGIN
    CREATE TABLE IF NOT EXISTS http_sessions (
    id BIGSERIAL PRIMARY KEY,
    key BYTEA,
    data BYTEA,
    created_on TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    modified_on TIMESTAMPTZ,
    expires_on TIMESTAMPTZ);
    EXCEPTION WHEN insufficient_privilege THEN
      IF NOT EXISTS (SELECT FROM pg_catalog.pg_tables 
          WHERE schemaname = current_schema() AND tablename = 'http_sessions') THEN
        RAISE;
      END IF;
    WHEN others THEN RAISE;
    END;
    $$;`

which catches that exception and ignores it only if the table already exists. A bit more poking suggests that to_regclass(current_schema() || '.http_sessions') IS NULL would also work to check existence, though I've no idea which one would be preferred.

@antonlindstrom
Copy link
Owner

Hey @dhduvall! Thank you so much for reporting this! I haven't had this issue myself but I'm happy to merge this to fix the issue.

@dhduvall
Copy link
Contributor Author

Cool. I can file a pull request, if you prefer.

@antonlindstrom
Copy link
Owner

That would be amazing, thank you!

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 a pull request may close this issue.

2 participants