Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1evd4v-0003rt-JN for pgsql-general@arkaria.postgresql.org; Tue, 13 Mar 2018 06:06:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1evd4s-0000PR-5p for pgsql-general@arkaria.postgresql.org; Tue, 13 Mar 2018 06:06:26 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1evd4r-0000PH-Qe for pgsql-general@lists.postgresql.org; Tue, 13 Mar 2018 06:06:25 +0000 Received: from ipmail07.adl2.internode.on.net ([150.101.137.131]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1evd4h-0008ED-Dk for pgsql-general@postgresql.org; Tue, 13 Mar 2018 06:06:23 +0000 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A2BrBwDh299Z/42KZZZeHAEBBAEBCgEBgmqBV24ngy9LgSuJaJA0kUiGeSiETk8ChRYBAgEBAQEBAmsohR0BAQICASNiDAMRBAEBASoCUQgGARIIE4l7BxGjRogcgieEcYZLAQEBAQEBAQEBAQEBAQEBAQEBAQEBHYMtiGyDMoEzAQGDMYJhBZhkiGCHXo99kB+WVFAPSFwygQOCCoV5LTYBAQECiQ4PGASCGQEBAQ Received: from webmail10.internode.on.net (HELO localhost) ([150.101.138.141]) by ipmail07.adl2.internode.on.net with ESMTP; 13 Mar 2018 16:35:53 +1030 Message-Id: From: matt.figg@internode.on.net Reply-To: matt.figg@internode.on.net To: "Adrian Klaver" , matt.figg@internode.on.net, pgsql-general@postgresql.org X-Mailer: Atmail 6.20.12 in-reply-to: <4a502571-92cd-8073-5e3f-6287027c393a@aklaver.com> Subject: Re: Programmatically duplicating a schema Date: Tue, 13 Mar 2018 16:35:51 +1030 Content-Type: multipart/alternative; boundary="=_4be2ffcf59542ccd49c9ed4120920a81" MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --=_4be2ffcf59542ccd49c9ed4120920a81 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable =0A=0A=09Thanks Adrian,=0A=0A=09=C2=A0 =0A=0A=09Really appreciate the su= ggestions.=0A=0A=09=C2=A0 =0A=0A=09The objective when trying to solve th= is for the Apartment library=0Aitself is to keep it generic (works for a= ny/all database objects -=0Atables, views, stored procedures, 3rd party= extension objects, etc.) &=0Ato require minimal configuration (preferab= ly not having to tell the=0Alibrary your schema).=0A=0A=09=C2=A0 =0A=0A= =09I believe pg_dump was the only way to satisfy the second point.=C2=A0= The=0Areason we'd want to infer the create statements via pg_dump is, s= o we=0Adon't need to keep database migration files in sync with a 'creat= e new=0Aschema' SQL script.=C2=A0 It adds risk that they get out of sync= , causing=0Ainconsistencies in new schemas created by the library.=0A=0A= =09=C2=A0 =0A=0A=09Assuming there's no other way to infer the create sta= tements from the=0Apublic schema, Ruby on Rails' structure.sql could pro= bably be used as=0Aa starting point for the 'create new schema' SQL file= =C2=A0 It's similar=0Aalready, however it's also generated via pg_dump= (having the same=0Aissues as Apartment library).=0Ahttp://guides.rubyon= rails.org/v3.2.9/migrations.html#types-of-schema-dumps=0A[1]=C2=A0 This= is outside the realms of this mail group though.=0A=0A=09=C2=A0 =0A=0A= =09Cheers,=0A=0A=09Matt.=0A=0A=09 =C2=A0=0A=0A----- Original Message ---= --=0AFrom: "Adrian Klaver" =0ATo:, =0ACc:=0ASent:Mon, 12 Mar 2018 21:22:= 01 -0700=0ASubject:Re: Programmatically duplicating a schema=0A=0A On 03= /12/2018 08:23 PM, matt.figg@internode.on.net wrote:=0A > Hi all,=0A >= =0A > What is a reliable way to programmatically & generically populate= =0Aan =0A > empty schema with all the objects in the public schema as a= =0Atemplate?=0A > =0A > We are using the multi tenancy ruby gem Apartmen= t ( =0A > https://github.com/influitive/apartment ), which was recently= =0Abroken by =0A > the changes made to pg_dump to address=C2=A0CVE-2018-= 1058 =0A > https://nvd.nist.gov/vuln/detail/CVE-2018-1058=0A > =0A > Apa= rtment attempts to duplicate the public schema whenever creating=0Aa =0A= > new schema by running:=0A > =0A > pg_dump -s -x -0 -n public=0A > =0A= > to get the SQL statements needed to recreate the public schema &=0Ath= en =0A > executes the pg_dump's sql output after creating & switching to= the=0Anew =0A > schema ( via set search_path to ; )=0A > =0A > After th= e fix to CVE-2018-1058, all table references in pg_dump's=0Aoutput =0A >= (including within SQL of stored procedures) are prefixed by the=0Apubli= c. =0A > schema, which means you cannot just reuse this output in a=0Adi= fferent =0A > schema context without first manually changing the sql.=0A= > As a temporary fix so we can handle new customers in production, we= =0Aare =0A > using a regex search/replace for public. in the pg_dump out= put, but=0A=0A > clearly this is not a reliable solution for a generic g= em such as=0AApartment.=0A > =0A > Is there a different way this gem cou= ld now be achieving this?=0A > Without a flag on pg_dump to force the ol= d format, or a CREATE=0ASCHEMA =0A > WITH TEMPLATE kind of statement,= we can't =0A > see any alternative more reliable than the regex search/= replace.=0A=0A Wild idea:=0A=0A 1) Roll back to:=0A https://git.postgres= ql.org/gitweb/?p=3Dpostgresql.git;a=3Dtree;h=3Dc987089c48afba99df0c3c2dc= 45fd69238b52705;hb=3D2840d201c6a62beb86d9671a66eeec56183d261b=0A=0A 2) U= se that to build pre-10.3 version of pg_dump and use that to dump=0A=0A= your schema. Basically long way to to force old format. Is fragile =0A= though as that version will lose contact with changes.=0A=0A Less wild= idea and previously suggested upstream.=0A=0A 1) Check your schema obje= cts into scripts that are checked into=0Aversion =0A control w/o schema= qualifications.=0A=0A 2) Use scripts to populate new schema.=0A=0A >= =0A > The open issue on the Apartment gem for =0A > context:=C2=A0https= ://github.com/influitive/apartment/issues/532=0A > =0A > Thanks in advan= ce.=0A=0A -- =0A Adrian Klaver=0A adrian.klaver@aklaver.com=0A=0A=0ALink= s:=0A------=0A[1]=0Ahttp://guides.rubyonrails.org/v3.2.9/migrations.html= #types-of-schema-dumps=0A --=_4be2ffcf59542ccd49c9ed4120920a81 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

Thanks Adrian,

=0A=0A

=C2=A0

=0A=0A

Really appreciate= the suggestions.

=0A=0A

=C2=A0

=0A=0A

The objective when t= rying to solve this=0Afor the Apartment library itself is to keep it gen= eric (works for any/all database=0Aobjects - tables, views, stored proce= dures, 3rd party extension objects, etc.)=0A& to require minimal con= figuration (preferably not having to tell the=0Alibrary your schema).

=0A=0A

=C2=A0

=0A=0A

I believe pg_dump was the only way to=0A= satisfy the second point.=C2=A0 The reason we'd=0Awant to infer the crea= te statements via pg_dump is, so we don't need to keep database=0Amigrat= ion files in sync with a 'create new schema' SQL script.=C2=A0 It adds r= isk that they get out of sync,=0Acausing inconsistencies in new schemas= created by the library.

=0A=0A

=C2=A0

=0A=0A

Assuming ther= e's no other way to infer=0Athe create statements from the public schema= , Ruby on Rails' structure.sql=0Acould probably be used as a starting po= int for the 'create new schema' SQL=0Afile.=C2=A0 It's similar already,= however it's=0Aalso generated via pg_dump (having the same issues as Ap= artment library). http://guides.rubyonrails.org/v3.2.9/mi= grations.html#types-of-schema-dumps=C2=A0 This is outside the realms= of this mail group=0Athough.

=0A=0A

=C2=A0

=0A=0ACheers,<= /span>

=0A=0A

Matt.

=0A=0A=C2=A0



----- Original Message -----
From:<= /div> "Adrian Klaver" <adrian.klaver@aklaver.com>

To:
<matt.figg@internode.on.net>= , <pgsql-general@postgresql.org>
Cc:

Sent:
Mon, 12= Mar 2018 21:22:01 -0700
Subject:<= /div>Re: Programmatically duplicating a schema


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

=0AWild idea:

=0A1) Roll back to:
= =0Ahttps://git.postgresql.org/gitweb/?p=3Dpostgresql.git;a=3Dtree;h=3Dc9= 87089c48afba99df0c3c2dc45fd69238b52705;hb=3D2840d201c6a62beb86d9671a66ee= ec56183d261b

=0A2) Use that to build pre-10.3 version of pg_d= ump and use that to dump
=0Ayour schema. Basically long way to to= force old format. Is fragile
=0Athough as that version will lose= contact with changes.


=0ALess wild idea and previously= suggested upstream.

=0A1) Check your schema objects into scr= ipts that are checked into version
=0Acontrol w/o schema qualifica= tions.

=0A2) Use scripts to populate new schema.

= =0A>
=0A> The open issue on the Apartment gem for
=0A&= gt; context:=C2=A0https://github.com/influitive/apartment/issues/532=0A>
=0A> Thanks in advance.


=0A--
=0AAdrian Klaver
=0Aadrian.klaver@aklaver.com
--=_4be2ffcf59542ccd49c9ed4120920a81--