Skip to content

Commit

Permalink
owned seq ++
Browse files Browse the repository at this point in the history
  • Loading branch information
grandmasterIII committed Oct 19, 2023
1 parent 13dc9ed commit 0a68c64
Show file tree
Hide file tree
Showing 10 changed files with 8 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Version 0.27
- improved `CREATE TYPE` for base types, shell types support
- improved dropping of indexes which are really constraints
- improved support for publications (columns and qualifiers)
- improved handling of owned sequences
- compatibility improvements with old postgres versions
- `comments` option to show all comments, even null ones, these are now omitted by default if null

STILL TO DO:
- `noconstraints` + `lite` options should omit constraints
- `owner` and `noowner` and `nodcl` for serial sequences
- check if `script` option works ok for sequences

Version 0.26
Expand Down
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Plus it couldn't handle dumping say only functions from a certain schema.
I have since then learned how to fix my database and pg_dump got options like --schema.

But the idea of a database being able to dump itself more autonomously persisted.
After all, if LISP can do it, why not Postgres with its awesome SQL power?
After all, if LISP can do it, why not Postgres with its awesome SQL power and verbosity?
Information is all there in the system catalogs. One just needs to decipher it.

This tool uses information schema and standard SQL as much as possible,
Expand Down
15 changes: 6 additions & 9 deletions ddlx.sql
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,6 @@ CREATE OR REPLACE FUNCTION ddlx_create_function(regproc, text[] default '{}') RE

CREATE OR REPLACE FUNCTION ddlx_alter_owner(oid, text[] default '{owner}')
RETURNS text LANGUAGE sql AS $function$
with obj as (select * from ddlx_identify($1))
select case when 'nodcl' ilike any($2) or 'noowner' ilike any($2) or 'lite' ilike any($2) then null
else case
when 'owner' ilike any($2) or obj.owner is distinct from current_role
Expand All @@ -781,7 +780,7 @@ CREATE OR REPLACE FUNCTION ddlx_alter_owner(oid, text[] default '{owner}')
' OWNER TO '||quote_ident(owner)||E';\n'
end end
end
from obj
from ddlx_identify($1) obj
$function$ strict;

---------------------------------------------------
Expand Down Expand Up @@ -1225,18 +1224,16 @@ seq as (
select
coalesce(
string_agg(
format(e'CREATE SEQUENCE %s%s;\n'||
e'ALTER SEQUENCE %s OWNER TO %s;\n',
format(e'CREATE SEQUENCE %s%s;\n%s%s',
#if 9.5
'IF NOT EXISTS ',
#else
'',
#end
"sequence","sequence",
pg_get_userbyid((select relowner from pg_class where oid=$1))
) ||
format(e'ALTER SEQUENCE %s OWNED BY %s;',
"sequence",sql_identifier),
"sequence",
ddlx_alter_owner("sequence",$2),
format(e'ALTER SEQUENCE %s OWNED BY %s;',"sequence",sql_identifier)
),
E'\n') || E'\n\n',
'') as ddl
from ddlx_describe($1,$2)
Expand Down
5 changes: 0 additions & 5 deletions test/expected/class.out
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ CREATE UNLOGGED TABLE test_class_r2 (


CREATE SEQUENCE IF NOT EXISTS test_class_r2_i_seq;
ALTER SEQUENCE test_class_r2_i_seq OWNER TO postgres;
ALTER SEQUENCE test_class_r2_i_seq OWNED BY test_class_r2.i;

ALTER TABLE test_class_r2 ADD CONSTRAINT blah FOREIGN KEY (a) REFERENCES test_class_r(a);
Expand Down Expand Up @@ -523,7 +522,6 @@ CREATE TABLE test_collation (


CREATE SEQUENCE IF NOT EXISTS test_collation_id_seq;
ALTER SEQUENCE test_collation_id_seq OWNER TO postgres;
ALTER SEQUENCE test_collation_id_seq OWNED BY test_collation.id;


Expand Down Expand Up @@ -749,7 +747,6 @@ CREATE TABLE test_parent (


CREATE SEQUENCE IF NOT EXISTS test_parent_i_seq;
ALTER SEQUENCE test_parent_i_seq OWNER TO postgres;
ALTER SEQUENCE test_parent_i_seq OWNED BY test_parent.i;


Expand Down Expand Up @@ -792,7 +789,6 @@ ALTER TABLE test_class_r ALTER b SET DEFAULT 'Hello, world!
'::text;

CREATE SEQUENCE IF NOT EXISTS test_class_r_a_seq;
ALTER SEQUENCE test_class_r_a_seq OWNER TO postgres;
ALTER SEQUENCE test_class_r_a_seq OWNED BY test_class_r.a;

ALTER TABLE test_class_r ALTER e SET ( n_distinct = '10' );
Expand All @@ -810,7 +806,6 @@ ALTER TABLE test_class_r ALTER b SET DEFAULT 'Hello, world!
'::text;

CREATE SEQUENCE IF NOT EXISTS test_class_r_a_seq;
ALTER SEQUENCE test_class_r_a_seq OWNER TO postgres;
ALTER SEQUENCE test_class_r_a_seq OWNED BY test_class_r.a;

ALTER TABLE test_class_r ALTER e SET ( n_distinct = '10' );
Expand Down
1 change: 0 additions & 1 deletion test/expected/pg10.out
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ CREATE TABLE tab_generated (
ALTER TABLE tab_generated ALTER d SET DEFAULT 42;

CREATE SEQUENCE IF NOT EXISTS tab_generated_e_seq;
ALTER SEQUENCE tab_generated_e_seq OWNER TO postgres;
ALTER SEQUENCE tab_generated_e_seq OWNED BY tab_generated.e;


Expand Down
1 change: 0 additions & 1 deletion test/expected/pg12.out
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ CREATE TABLE tab_generated12 (
ALTER TABLE tab_generated12 ALTER d SET DEFAULT 42;

CREATE SEQUENCE IF NOT EXISTS tab_generated12_e_seq;
ALTER SEQUENCE tab_generated12_e_seq OWNER TO postgres;
ALTER SEQUENCE tab_generated12_e_seq OWNED BY tab_generated12.e;


Expand Down
1 change: 0 additions & 1 deletion test/expected/pg13.out
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,6 @@ CREATE TABLE tab_generated12 (
ALTER TABLE tab_generated12 ALTER d SET DEFAULT 42;

CREATE SEQUENCE IF NOT EXISTS tab_generated12_e_seq;
ALTER SEQUENCE tab_generated12_e_seq OWNER TO postgres;
ALTER SEQUENCE tab_generated12_e_seq OWNED BY tab_generated12.e;


Expand Down
1 change: 0 additions & 1 deletion test/expected/pg14.out
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,6 @@ CREATE TABLE tab_generated12 (
ALTER TABLE tab_generated12 ALTER d SET DEFAULT 42;

CREATE SEQUENCE IF NOT EXISTS tab_generated12_e_seq;
ALTER SEQUENCE tab_generated12_e_seq OWNER TO postgres;
ALTER SEQUENCE tab_generated12_e_seq OWNED BY tab_generated12.e;


Expand Down
1 change: 0 additions & 1 deletion test/expected/pg15.out
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,6 @@ CREATE TABLE tab_generated12 (
ALTER TABLE tab_generated12 ALTER d SET DEFAULT 42;

CREATE SEQUENCE IF NOT EXISTS tab_generated12_e_seq;
ALTER SEQUENCE tab_generated12_e_seq OWNER TO postgres;
ALTER SEQUENCE tab_generated12_e_seq OWNED BY tab_generated12.e;


Expand Down
1 change: 0 additions & 1 deletion test/expected/pg16.out
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,6 @@ CREATE TABLE tab_generated12 (
ALTER TABLE tab_generated12 ALTER d SET DEFAULT 42;

CREATE SEQUENCE IF NOT EXISTS tab_generated12_e_seq;
ALTER SEQUENCE tab_generated12_e_seq OWNER TO postgres;
ALTER SEQUENCE tab_generated12_e_seq OWNED BY tab_generated12.e;


Expand Down

0 comments on commit 0a68c64

Please sign in to comment.