don't error when failing to create http_sessions due to permissions
dhduvall opened this issue · 3 comments
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.
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.
Cool. I can file a pull request, if you prefer.
That would be amazing, thank you!