Skip to content

Remove assumption about public schema

Andreas Brandl requested to merge ab/dont-assume-public into master

What does this MR do?

Summary

This change removes references to the public schema from db/structure.sql. Just like in the application, we default to using whatever is configured as the search_path. Typically this is public, but it can be different (see below).

This is likely to conflict with other in-flight changes (having database migrations). An example of this is a build failure in CI like this (assuming testing is a table you added):

-CREATE TABLE public.testing (
+CREATE TABLE testing (
     id integer NOT NULL,
     foobar text
 );

If you encounter this situation, simply re-run rake db:structure:dump on your branch to reflect changes. Alternatively, reset the schema and re-run any added migrations.

This should result in a change to db/structure.sql where the public. suffixes are being removed from database objects your migrations added.

Background

AFAIS we never assume anything about the default schema name being public. db/structure.sql is the exception pg_dump produces fully qualified object names when working with multiple schemas.

Now as noted in #228724 (closed), this limits new installations (and only those) in a sense that they have to use public as the default schema. In the case brought up, we have an external DBA managed postgres where the customer wanted to use gitlab as the default schema (and not public).

Note that existing installations never use db/structure.sql, as database migrations are being used instead.

So this brings up the question: Do we need to have public specifically encoded in the schema?

Without it (this is what this change does), objects not fully qualified with a schema default to whatever the current search_path setting is. This in fact matches what the application does (we never fully-qualify tables with public.).

By default, search_path is public. Note this can even be configured on a per-user (database user) basis, allowing customers to use a different default schema transparently.

I'm open to challenge. We might as well just start nailing that assumption down and only work with public.

At the moment though, I don't see a strong reason to do that and we can always decide to do that later, too.

Verifying

  1. On this branch, run rake db:reset
  2. On master, run rake db:structure:dump
  3. Check that there are no local changes to db/structure.sql coming from that

Merging

This is likely to produce merge conflicts. We might want to lock db/structure.sql while we refresh the change and merge it.

Edited by Andreas Brandl

Merge request reports