pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: matt.figg@internode.on.net
To: pgsql-general@postgresql.org
Subject: Re: Programmatically duplicating a schema
Date: Mon, 12 Mar 2018 21:22:01 -0700
Message-ID: <4a502571-92cd-8073-5e3f-6287027c393a@aklaver.com> (raw)
In-Reply-To: <bf04e73a8f29eee80703d3ec7e4507e95a8a7af2@webmail.internode.on.net>
References: <bf04e73a8f29eee80703d3ec7e4507e95a8a7af2@webmail.internode.on.net>

On 03/12/2018 08:23 PM, matt.figg@internode.on.net wrote:
> Hi all,
> 
> What is a reliable way to programmatically & generically populate an 
> empty schema with all the objects in the public schema as a template?
> 
> We are using the multi tenancy ruby gem Apartment ( 
> https://github.com/influitive/apartment ), which was recently broken by 
> the changes made to pg_dump to address CVE-2018-1058 
> https://nvd.nist.gov/vuln/detail/CVE-2018-1058
> 
> Apartment attempts to duplicate the public schema whenever creating a 
> new schema by running:
> 
> pg_dump -s -x -0 -n public
> 
> to get the SQL statements needed to recreate the public schema & then 
> executes the pg_dump's sql output after creating & switching to the new 
> schema ( via set search_path to <new schema>; )
> 
> After the fix to CVE-2018-1058, all table references in pg_dump's output 
> (including within SQL of stored procedures) are prefixed by the public. 
> schema, which means you cannot just reuse this output in a different 
> schema context without first manually changing the sql.
> As a temporary fix so we can handle new customers in production, we are 
> using a regex search/replace for public. in the pg_dump output, but 
> clearly this is not a reliable solution for a generic gem such as Apartment.
> 
> Is there a different way this gem could now be achieving this?
> Without a flag on pg_dump to force the old format, or a CREATE SCHEMA 
> <schema_name> WITH TEMPLATE <other_schema> kind of statement, we can't 
> see any alternative more reliable than the regex search/replace.

Wild idea:

1) Roll back to:
https://git.postgresql.org/gitweb/?p=postgresql.git;a=tree;h=c987089c48afba99df0c3c2dc45fd69238b5270...

2) Use that to build pre-10.3 version of pg_dump and use that to dump 
your schema. Basically long way to to force old format. Is fragile 
though as that version will lose contact with changes.


Less wild idea and previously suggested upstream.

1) Check your schema objects into scripts that are checked into version 
control w/o schema qualifications.

2) Use scripts to populate new schema.

> 
> The open issue on the Apartment gem for 
> context: https://github.com/influitive/apartment/issues/532
> 
> Thanks in advance.


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (7+ messages)  latest in thread

Message-ID: <4a502571-92cd-8073-5e3f-6287027c393a@aklaver.com>
Permalink:  ../4a502571-92cd-8073-5e3f-6287027c393a@aklaver.com/
Also on:    postgresql.org/message-id/4a502571-92cd-8073-5e3f-6287027c393a@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, matt.figg@internode.on.net
  Subject: Re: Programmatically duplicating a schema
  In-Reply-To: <4a502571-92cd-8073-5e3f-6287027c393a@aklaver.com>
  References: <bf04e73a8f29eee80703d3ec7e4507e95a8a7af2@webmail.internode.on.net> <4a502571-92cd-8073-5e3f-6287027c393a@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox