pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Programmatically duplicating a schema
7+ messages / 5 participants
[nested] [flat]

* Programmatically duplicating a schema
@ 2018-03-13 03:23  matt.figg@internode.on.net
  0 siblings, 2 replies; 7+ messages in thread

From: matt.figg@internode.on.net @ 2018-03-13 03:23 UTC (permalink / raw)
  To: pgsql-general

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 ; )
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  WITH
TEMPLATE  kind of statement, we can't see any alternative more
reliable than the regex search/replace. 
The open issue on the Apartment gem for
context: https://github.com/influitive/apartment/issues/532
Thanks in advance.

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Programmatically duplicating a schema
@ 2018-03-13 04:22  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: matt.figg@internode.on.net
  1 sibling, 1 reply; 7+ messages in thread

From: Adrian Klaver @ 2018-03-13 04:22 UTC (permalink / raw)
  To: matt.figg@internode.on.net; pgsql-general

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




^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Programmatically duplicating a schema
@ 2018-03-13 06:05  matt.figg@internode.on.net
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 2 replies; 7+ messages in thread

From: matt.figg@internode.on.net @ 2018-03-13 06:05 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; matt.figg@internode.on.net; pgsql-general



	Thanks Adrian,

	  

	Really appreciate the suggestions.

	  

	The objective when trying to solve this for the Apartment library
itself is to keep it generic (works for any/all database objects -
tables, views, stored procedures, 3rd party extension objects, etc.) &
to require minimal configuration (preferably not having to tell the
library your schema).

	  

	I believe pg_dump was the only way to satisfy the second point.  The
reason we'd want to infer the create statements via pg_dump is, so we
don't need to keep database migration files in sync with a 'create new
schema' SQL script.  It adds risk that they get out of sync, causing
inconsistencies in new schemas created by the library.

	  

	Assuming there's no other way to infer the create statements from the
public schema, Ruby on Rails' structure.sql could probably be used as
a starting point for the 'create new schema' SQL file  It's similar
already, however it's also generated via pg_dump (having the same
issues as Apartment library).
http://guides.rubyonrails.org/v3.2.9/migrations.html#types-of-schema-dumps
[1]  This is outside the realms of this mail group though.

	  

	Cheers,

	Matt.

	  

----- Original Message -----
From: "Adrian Klaver" 
To:, 
Cc:
Sent:Mon, 12 Mar 2018 21:22:01 -0700
Subject:Re: Programmatically duplicating a schema

 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 ; )
 > 
 > 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 
 >  WITH TEMPLATE  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


Links:
------
[1]
http://guides.rubyonrails.org/v3.2.9/migrations.html#types-of-schema-dumps


^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Programmatically duplicating a schema
@ 2018-03-13 08:21  Alban Hertroys <haramrae@gmail.com>
  parent: matt.figg@internode.on.net
  1 sibling, 1 reply; 7+ messages in thread

From: Alban Hertroys @ 2018-03-13 08:21 UTC (permalink / raw)
  To: matt.figg@internode.on.net; +Cc: pgsql-general


> On 13 Mar 2018, at 4:23, 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.

In my opinion, it makes sense that if you have the option of dumping the contents of a specific schema, it should be possible to restore that dump into a different schema. Unfortunately, looking at pg_restore, there does not appear to be such an option (yet).

I'd even go so far to suggest that every single object type that can be dumped with pg_dump (single database, single schema, single table, single function, etc) should be restorable under a different name. I realise that this could make pg_restore options potentially more confusing.

I suppose people currently manually edit the dumps to this effect, but that risks silent corruption of data when for example a data value contains a string such as 'The building is now open to public.'. Regular expressions don't know the difference between data and identifiers in a dump file - pg_restore does.

Whether psql needs the same treatment? I'd qualify this as "advanced" use and limit it to pg_restore.

But then, I'm just a list-lurker, I currently have but the option of voicing my opinion.

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Programmatically duplicating a schema
@ 2018-03-13 13:09  Melvin Davidson <melvin6925@gmail.com>
  parent: Alban Hertroys <haramrae@gmail.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Melvin Davidson @ 2018-03-13 13:09 UTC (permalink / raw)
  To: Alban Hertroys <haramrae@gmail.com>; +Cc: matt.figg@internode.on.net; pgsql-general

* > What is a reliable way to programmatically & generically populate an
empty schema with all the objects in the public schema as a template? The
simplest way is just to load the attached clone_schema function. It was
originally created by Emanuel '3manuek', which I enhanced. Itnow copies all
sequences, tables, indexes, rules, triggers, data(optional), views &
functions from any existing schema to a new schema.Then just call the
function.SAMPLE CALL:SELECT clone_schema('public', 'new_schema', TRUE);Use
FALSE if you do not want to copy data.*
-- 
*Melvin Davidson*
*Maj. Database & Exploration Specialist*
*Universe Exploration Command – UXC*
Employment by invitation only!

Attachments:

clone_schema.sql (text/plain, 13.7K) download -- Function: public.clone_schema(text, text, boolean) -- DROP FUNCTION public.clone_schema(text, text, boolean); CREATE OR REPLACE FUNCTION public.clone_schema( source_schema text, dest_schema text, include_recs boolean) RETURNS void AS $BODY$ -- Initial code by Emanuel '3manuek' -- Last revision 2017-04-17 by Melvin Davidson -- Added SELECT REPLACE for schema views -- -- This function will clone 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); DECLARE src_oid oid; tbl_oid oid; func_oid oid; con_oid oid; v_path text; v_func text; v_args text; v_conname text; v_rule text; v_trig text; object text; buffer text; srctbl text; default_ text; v_column text; qry text; dest_qry text; v_def text; v_stat integer; seqval bigint; sq_last_value bigint; sq_max_value bigint; sq_start_value bigint; sq_increment_by bigint; sq_min_value bigint; sq_cache_value bigint; sq_log_cnt bigint; sq_is_called boolean; sq_is_cycled boolean; sq_cycled char(10); BEGIN -- Check that source_schema exists SELECT oid INTO src_oid FROM pg_namespace WHERE nspname = quote_ident(source_schema); IF NOT FOUND THEN RAISE NOTICE 'source schema % does not exist!', source_schema; RETURN ; END IF; -- Check that dest_schema does not yet exist PERFORM nspname FROM pg_namespace WHERE nspname = quote_ident(dest_schema); IF FOUND THEN RAISE NOTICE 'dest schema % already exists!', dest_schema; RETURN ; END IF; EXECUTE 'CREATE SCHEMA ' || quote_ident(dest_schema) ; -- Add schema comment SELECT description INTO v_def FROM pg_description WHERE objoid = src_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON SCHEMA ' || quote_ident(dest_schema) || ' IS ' || quote_literal(v_def); END IF; -- Create sequences -- TODO: Find a way to make this sequence's owner is the correct table. FOR object IN SELECT sequence_name::text FROM information_schema.sequences WHERE sequence_schema = quote_ident(source_schema) LOOP EXECUTE 'CREATE SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object); srctbl := quote_ident(source_schema) || '.' || quote_ident(object); EXECUTE 'SELECT last_value, max_value, start_value, increment_by, min_value, cache_value, log_cnt, is_cycled, is_called FROM ' || quote_ident(source_schema) || '.' || quote_ident(object) || ';' INTO sq_last_value, sq_max_value, sq_start_value, sq_increment_by, sq_min_value, sq_cache_value, sq_log_cnt, sq_is_cycled, sq_is_called ; IF sq_is_cycled THEN sq_cycled := 'CYCLE'; ELSE sq_cycled := 'NO CYCLE'; END IF; EXECUTE 'ALTER SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' INCREMENT BY ' || sq_increment_by || ' MINVALUE ' || sq_min_value || ' MAXVALUE ' || sq_max_value || ' START WITH ' || sq_start_value || ' RESTART ' || sq_min_value || ' CACHE ' || sq_cache_value || sq_cycled || ' ;' ; buffer := quote_ident(dest_schema) || '.' || quote_ident(object); IF include_recs THEN EXECUTE 'SELECT setval( ''' || buffer || ''', ' || sq_last_value || ', ' || sq_is_called || ');' ; ELSE EXECUTE 'SELECT setval( ''' || buffer || ''', ' || sq_start_value || ', ' || sq_is_called || ');' ; END IF; -- add sequence comments SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'S' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; END LOOP; -- Create tables FOR object IN SELECT TABLE_NAME::text FROM information_schema.tables WHERE table_schema = quote_ident(source_schema) AND table_type = 'BASE TABLE' LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(object); EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' || quote_ident(source_schema) || '.' || quote_ident(object) || ' INCLUDING ALL)'; -- Add table comment SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON TABLE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; IF include_recs THEN -- Insert records from source table EXECUTE 'INSERT INTO ' || buffer || ' SELECT * FROM ' || quote_ident(source_schema) || '.' || quote_ident(object) || ';'; END IF; FOR v_column, default_ IN SELECT column_name::text, REPLACE(column_default::text, quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.' ) FROM information_schema.COLUMNS WHERE table_schema = dest_schema AND TABLE_NAME = object AND column_default LIKE 'nextval(%' || quote_ident(source_schema) || '%::regclass)' LOOP EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || v_column || ' SET DEFAULT ' || default_; END LOOP; END LOOP; -- set column statistics FOR tbl_oid, srctbl IN SELECT oid, relname FROM pg_class WHERE relnamespace = src_oid AND relkind = 'r' LOOP FOR v_column, v_stat IN SELECT attname, attstattarget FROM pg_attribute WHERE attrelid = tbl_oid AND attnum > 0 LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(srctbl); -- RAISE EXCEPTION 'ALTER TABLE % ALTER COLUMN % SET STATISTICS %', buffer, v_column, v_stat::text; EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || quote_ident(v_column) || ' SET STATISTICS ' || v_stat || ';'; END LOOP; END LOOP; -- add FK constraint FOR qry IN SELECT 'ALTER TABLE ' || quote_ident(dest_schema) || '.' || quote_ident(rn.relname) || ' ADD CONSTRAINT ' || quote_ident(ct.conname) || ' ' || replace(pg_get_constraintdef(ct.oid), quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.') || ';' FROM pg_constraint ct JOIN pg_class rn ON rn.oid = ct.conrelid WHERE connamespace = src_oid AND rn.relkind = 'r' AND ct.contype = 'f' LOOP EXECUTE qry; END LOOP; -- Add constraint comment FOR con_oid IN SELECT oid FROM pg_constraint WHERE conrelid = tbl_oid LOOP SELECT conname INTO v_conname FROM pg_constraint WHERE oid = con_oid; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid; IF FOUND THEN EXECUTE 'COMMENT ON CONSTRAINT ' || v_conname || ' ON ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; END LOOP; -- Create views FOR object IN SELECT table_name::text, view_definition FROM information_schema.views WHERE table_schema = quote_ident(source_schema) LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(object); SELECT view_definition INTO v_def FROM information_schema.views WHERE table_schema = quote_ident(source_schema) AND table_name = quote_ident(object); SELECT REPLACE(v_def, source_schema, dest_schema) INTO v_def; -- RAISE NOTICE 'view def, % , source % , dest % ', v_def, source_schema, dest_schema; EXECUTE 'CREATE OR REPLACE VIEW ' || buffer || ' AS ' || v_def || ';' ; -- Add comment SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'v' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON VIEW ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ' || quote_literal(v_def); END IF; END LOOP; -- Create functions FOR func_oid IN SELECT oid, proargnames FROM pg_proc WHERE pronamespace = src_oid LOOP SELECT pg_get_functiondef(func_oid) INTO qry; SELECT proname, oidvectortypes(proargtypes) INTO v_func, v_args FROM pg_proc WHERE oid = func_oid; SELECT replace(qry, quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.') INTO dest_qry; EXECUTE dest_qry; -- Add function comment SELECT description INTO v_def FROM pg_description WHERE objoid = func_oid AND objsubid = 0; IF FOUND THEN -- RAISE NOTICE 'func_oid %, object %, v_args %', func_oid::text, quote_ident(object), v_args; EXECUTE 'COMMENT ON FUNCTION ' || quote_ident(dest_schema) || '.' || quote_ident(v_func) || '(' || v_args || ')' || ' IS ' || quote_literal(v_def) ||';' ; END IF; END LOOP; -- add Rules FOR v_def IN SELECT definition FROM pg_rules WHERE schemaname = quote_ident(source_schema) LOOP IF v_def IS NOT NULL THEN SELECT replace(v_def, 'TO ', 'TO ' || quote_ident(dest_schema) || '.') INTO v_def; EXECUTE ' ' || v_def; END IF; END LOOP; -- add triggers FOR v_def IN SELECT pg_get_triggerdef(oid) FROM pg_trigger WHERE tgname NOT LIKE 'RI_%' AND tgrelid IN (SELECT oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid) LOOP SELECT replace(v_def, ' ON ', ' ON ' || quote_ident(dest_schema) || '.') INTO dest_qry; EXECUTE dest_qry; END LOOP; -- Disable inactive triggers -- D = disabled FOR tbl_oid IN SELECT oid FROM pg_trigger WHERE tgenabled = 'D' AND tgname NOT LIKE 'RI_%' AND tgrelid IN (SELECT oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid) LOOP SELECT t.tgname, c.relname INTO object, srctbl FROM pg_trigger t JOIN pg_class c ON c.oid = t.tgrelid WHERE t.oid = tbl_oid; IF FOUND THEN EXECUTE 'ALTER TABLE ' || dest_schema || '.' || srctbl || ' DISABLE TRIGGER ' || object || ';'; END IF; END LOOP; -- Add index comment FOR tbl_oid IN SELECT oid FROM pg_class WHERE relkind = 'i' AND relnamespace = src_oid LOOP SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid; SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON INDEX ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; END LOOP; -- add rule comments FOR con_oid IN SELECT oid, * FROM pg_rewrite WHERE rulename <> '_RETURN'::name LOOP SELECT rulename, ev_class INTO v_rule, tbl_oid FROM pg_rewrite WHERE oid = con_oid; SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid AND relkind = 'r'; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON RULE ' || v_rule || ' ON ' || quote_ident(dest_schema) || '.' || object || ' IS ' || quote_literal(v_def); END IF; END LOOP; -- add trigger comments FOR con_oid IN SELECT oid, * FROM pg_trigger WHERE tgname NOT LIKE 'RI_%' LOOP SELECT tgname, tgrelid INTO v_trig, tbl_oid FROM pg_trigger WHERE oid = con_oid; SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid AND relkind = 'r'; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON TRIGGER ' || v_trig || ' ON ' || quote_ident(dest_schema) || '.' || object || ' IS ' || quote_literal(v_def); END IF; END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION public.clone_schema(text, text, boolean) OWNER TO postgres; COMMENT ON FUNCTION public.clone_schema(text, text, boolean) IS 'Duplicates sequences, tables, indexes, rules, triggers, data(optional), views & functions from the source schema to the destination schema';
^ permalink raw reply [nested|flat] 7+ messages in thread

* Re: Programmatically duplicating a schema
@ 2018-03-13 13:57  David G. Johnston <david.g.johnston@gmail.com>
  parent: matt.figg@internode.on.net
  1 sibling, 0 replies; 7+ messages in thread

From: David G. Johnston @ 2018-03-13 13:57 UTC (permalink / raw)
  To: matt.figg@internode.on.net; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general

On Mon, Mar 12, 2018 at 11:05 PM, <matt.figg@internode.on.net> wrote:

> The reason we'd want to infer the create statements via pg_dump is, so we
> don't need to keep database migration files in sync with a 'create new
> schema' SQL script.  It adds risk that they get out of sync, causing
> inconsistencies in new schemas created by the library.
>
​Then don't have a "create new schema" script (aside from the very first
one) run your migrations even when creating new installations.

​David J.

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Programmatically duplicating a schema
@ 2018-03-13 15:06  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: matt.figg@internode.on.net
  1 sibling, 0 replies; 7+ messages in thread

From: Adrian Klaver @ 2018-03-13 15:06 UTC (permalink / raw)
  To: matt.figg@internode.on.net; pgsql-general

On 03/12/2018 11:05 PM, matt.figg@internode.on.net wrote:
> Thanks Adrian,
> 
> Really appreciate the suggestions.
> 
> The objective when trying to solve this for the Apartment library itself 
> is to keep it generic (works for any/all database objects - tables, 
> views, stored procedures, 3rd party extension objects, etc.) & to 
> require minimal configuration (preferably not having to tell the library 
> your schema).
> 
> I believe pg_dump was the only way to satisfy the second point.  The 
> reason we'd want to infer the create statements via pg_dump is, so we 
> don't need to keep database migration files in sync with a 'create new 
> schema' SQL script.  It adds risk that they get out of sync, causing 
> inconsistencies in new schemas created by the library.

Seems to me your best hope is to make a plea on --hackers for a flag 
that turns off schema qualification of object names.

> 
> Assuming there's no other way to infer the create statements from the 
> public schema, Ruby on Rails' structure.sql could probably be used as a 
> starting point for the 'create new schema' SQL file.  It's similar 
> already, however it's also generated via pg_dump (having the same issues 
> as Apartment library). 
> http://guides.rubyonrails.org/v3.2.9/migrations.html#types-of-schema-dumps  
> This is outside the realms of this mail group though.
> 
> Cheers,
> 
> Matt.
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2018-03-13 15:06 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-03-13 03:23 Programmatically duplicating a schema matt.figg@internode.on.net
2018-03-13 04:22 ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-03-13 06:05   ` matt.figg@internode.on.net
2018-03-13 13:57     ` David G. Johnston <david.g.johnston@gmail.com>
2018-03-13 15:06     ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-03-13 08:21 ` Alban Hertroys <haramrae@gmail.com>
2018-03-13 13:09   ` Melvin Davidson <melvin6925@gmail.com>

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