From: Aldrin Martoq Ahumada <aldrin.martoq@gmail.com>
To: Melvin Davidson <melvin6925@gmail.com>
Cc: Andre Oliveira Freitas <afreitas@callixbrasil.com>
Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Cc: matt.figg@internode.on.net
Subject: Re: Postgresql upgrade to 9.5.12/10.3 changes pg_dump format for cloning schemas
Date: Tue, 13 Mar 2018 12:35:29 -0300
Message-ID: <C38EE328-CF14-491E-9680-D88333C75F34@gmail.com> (raw)
In-Reply-To: <CANu8FiyWLBx7k2KV0OkYO_MV6J5efEBMLzoj_J=v+8nVRe1-+Q@mail.gmail.com>
References: <ED39C2F0-21E9-4F7C-B0CF-14A62E78AFD6@gmail.com>
<CAN6ijTAhW9f08z6GkASVKMWV-FmwFbfYALyZVmGzCL_fzD18-g@mail.gmail.com>
<1D546CA9-B443-4CE9-A43F-EE70D70DAA9E@gmail.com>
<CANu8FiyWLBx7k2KV0OkYO_MV6J5efEBMLzoj_J=v+8nVRe1-+Q@mail.gmail.com>
> On Mar 9, 2018, at 12:15 PM, Melvin Davidson <melvin6925@gmail.com> wrote:
> On Fri, Mar 9, 2018 at 10:00 AM, Aldrin Martoq Ahumada <aldrin.martoq@gmail.com <mailto:aldrin.martoq@gmail.com>> wrote:
> Yes, here is the issue: https://github.com/influitive/apartment/issues/532 <https://github.com/influitive/apartment/issues/532;
> It happens if you configured apartment with use_sql=true, which means it clones the schema from pg_dump. My first attempt was to “fix” the script generated by pg_dump, but I feel it will be a mess. We solved our issue going back to use_sql=false, which is the default (creates the schema from db/schema.rb). But there is people that have other requirements, like functions, so the easier way for them is to keep use_sql and replace strings in the script.
> >...how could be the best way to clone a schema into another?
>
> The safest way is to use pgdump -F p -n <the_schema_name> > schema.sql
> Then edit schema.sql and change all references to old_schema name to new_schema name.
> Finally, use psql < schema.sql to create the new_schema.
>
> That being said, a year ago I optimized a function originally written by Emanuel '3manuek'
> called clone_schema, which is added to the public schema. It clones all sequences, tables,
> indexes, rules, triggers, data(optional), views & functions from any existing schema to a
> new schema
> SAMPLE CALL:
> SELECT clone_schema('public', 'new_schema', TRUE);
>
> I've attached it for your convenience.
> disclaimer: I do not accept any responsibility for any unknow bugs in the function.
> Test first and use at your own risk.
Thank you Melvin, I forgot to mention I've already found your script before I asked here, but I didn’t think it was robust enough (please don't offend :-). Particularly, it didn't work well on PostgreSQL 10.
I think the solution for the long term is to add a flag to pg_dump, but in the short/mid term we’ll have to replicate some form of your script into the gem.
Cheers,
Aldrin.
PS: I’ve added initial support for PostgreSQL 10 to clone_schema here, but I have no time to test it well:
https://gist.github.com/aldrinmartoq/5df0aa03f86f3ad03982c793753c04a1 <https://gist.github.com/aldrinmartoq/5df0aa03f86f3ad03982c793753c04a1;
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: aldrin.martoq@gmail.com, melvin6925@gmail.com, afreitas@callixbrasil.com, matt.figg@internode.on.net
Subject: Re: Postgresql upgrade to 9.5.12/10.3 changes pg_dump format for cloning schemas
In-Reply-To: <C38EE328-CF14-491E-9680-D88333C75F34@gmail.com>
References: <ED39C2F0-21E9-4F7C-B0CF-14A62E78AFD6@gmail.com> <CAN6ijTAhW9f08z6GkASVKMWV-FmwFbfYALyZVmGzCL_fzD18-g@mail.gmail.com> <1D546CA9-B443-4CE9-A43F-EE70D70DAA9E@gmail.com> <CANu8FiyWLBx7k2KV0OkYO_MV6J5efEBMLzoj_J=v+8nVRe1-+Q@mail.gmail.com> <C38EE328-CF14-491E-9680-D88333C75F34@gmail.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