Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/lacanoid/pgddl
Browse files Browse the repository at this point in the history
  • Loading branch information
lacanoid committed May 15, 2021
2 parents afe93ab + 7eb047f commit ab9f1c1
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 41 deletions.
29 changes: 29 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
dist: bionic
language: c
addons:
apt:
sources:
- sourceline: 'deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main'
key_url: 'https://www.postgresql.org/media/keys/ACCC4CF8.asc'
update: true
packages:
- liburiparser-dev
env:
- PGVERSION=9.1
- PGVERSION=9.2
- PGVERSION=9.3
- PGVERSION=9.4
- PGVERSION=9.5
- PGVERSION=9.6
- PGVERSION=10
- PGVERSION=11
- PGVERSION=12
- PGVERSION=13
script: |
set -e
PATH=/usr/lib/postgresql/$PGVERSION/bin:$PATH
sudo apt-get install postgresql-$PGVERSION postgresql-server-dev-$PGVERSION
sudo pg_createcluster --start $PGVERSION test -p 55435 -- -A trust
make all
sudo make install PG_CONFIG=/usr/lib/postgresql/$PGVERSION/bin/pg_config
PGPORT=55435 make installcheck PGUSER=postgres
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Version 0.18
------------
- bug fix in create_event_trigger()
- ddlx_identify() now more correctly identifies regtype vs regclass objects
- exclude objects from extensions unless 'ext' option is specified
- added .travis.yml

Version 0.17
------------
- improved support for Postgres 13 (added missing test files)
Expand Down
6 changes: 3 additions & 3 deletions META.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
Me{
"name": "ddlx",
"abstract": "DDL eXtractor Functions",
"description": "A set of SQL only functions for recreating DDL for objects in a database",
"version": "0.17.0",
"version": "0.18.0",
"maintainer": [
"Ziga Kranjec <[email protected]>"
],
Expand All @@ -24,7 +24,7 @@
"pgddl": {
"file": "ddlx.sql",
"docfile": "README.md",
"version": "0.17.0",
"version": "0.18.0",
"abstract": "DDL eXtractor Functions"
}
},
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ PG_CONFIG = pg_config
PKG_CONFIG = pkg-config

EXTENSION = ddlx
EXT_VERSION = 0.17
EXT_VERSION = 0.18
VTESTS = $(shell bin/tests ${VERSION})

DATA_built = ddlx--$(EXT_VERSION).sql

REGRESS = init manifest role type class fdw tsearch policy misc script ${VTESTS}
#REGRESS = init manifest role type class fdw tsearch policy misc script ${VTESTS}
REGRESS = init role type class fdw tsearch policy misc script ${VTESTS}
#REGRESS = ($shell bin/tests)
REGRESS_OPTS = --inputdir=test

Expand All @@ -17,3 +18,4 @@ include $(PGXS)
$(DATA_built): ddlx.sql
@echo "Building extension version" $(EXT_VERSION) "for Postgres version" $(VERSION)
VERSION=${VERSION} ./bin/pgsqlpp $^ >$@

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Advantages over using other tools like `psql` or `pg_dump` include:
just use SELECT and hairy SQL instead!
- It is entrely made out of **plain SQL functions** so you don't have to install any extra
languages, not even PL/PgSQL! It runs on plain vanilla Postgres.

Some disadvantages:

- Not all Postgres objects and all options are supported yet. Postgres is huge.
Expand Down
28 changes: 13 additions & 15 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ Support for other postgres objects:
- extensions

Support for other missing options:
- PG 10+: partitions (subpartitions)
- PG 10+: generated
- comments on all objects
- ownership of all objects
- grants on all objects
Expand All @@ -40,6 +38,8 @@ Other:
- figure out how to elegantly separate pre-data, post-data, create, alter and dcl
- use ONLY when appropriate
- move not nulls to constraints section
- move storage setting to pre-data section
- group column alters together by column name
- improve support for non superusers (more testing, etc)
- improve dumping of comments (be quiet on NULL comments)
Currently NULL comments are included, to encourage commenting.
Expand All @@ -56,23 +56,21 @@ Options

Some options as to what and how to dump stuff might be required:

CREATE TYPE pg_ddl_options AS (
ddldrop boolean, -- generate DROP statements
ddlalter boolean, -- prefer ALTER to CREATE
ddldcl boolean, -- include DCL (GRANTS)
ddlcor boolean, -- CREATE OR REPLACE
ddline boolean, -- IF NOT EXISTS
ddlie boolean, -- IF EXISTS
ddlwrap boolean, -- wrap in BEGIN / END
ddldep boolean, -- output objects which depend on this object too
ddldata boolean -- add statements preserve / copy table data
);

Also, consider pre and post data DDL.
* `DROP` - generate DROP statements
* `ALTER` - prefer ALTER to CREATE, implies 'INE' and 'IE'
* `DCL` - include DCL (GRANTS)
* `COR` - use CREATE OR REPLACE where possible
* `INE` - use IF NOT EXISTS where possible
* `IE` - use IF EXISTS where possible
* `WRAP` - wrap in BEGIN / END
* `DEP` - output objects which depend on this object too
* `DATA` - add statements preserve / copy table data

These might be passed as optional second arg to extractor functions
Perhaps as a text array? JSON?

Also, consider how to choose pre and post data DDL.

Perhaps there are other ways to implement some of this?

Other DDL dumping tools
Expand Down
2 changes: 1 addition & 1 deletion ddlx.control
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
comment = 'DDL eXtractor functions'
default_version = 0.17
default_version = 0.18
relocatable = true


33 changes: 20 additions & 13 deletions ddlx.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--
-- DDL eXtractor functions
-- version 0.17 [email protected]
-- version 0.18 [email protected]
--
---------------------------------------------------

Expand Down Expand Up @@ -43,14 +43,16 @@ AS $function$
('p','PSEUDO','TYPE'),
('r','RANGE','TYPE')
)
SELECT c.oid,'pg_class'::regclass,
c.relname AS name,
n.nspname AS namespace,
SELECT coalesce(t.oid,c.oid),
case when t.oid is not null then 'pg_type'::regclass
else 'pg_class'::regclass end,
c.relname AS name, n.nspname AS namespace,
pg_get_userbyid(c.relowner) AS owner,
coalesce(cc.v,c.relkind::text) AS sql_kind,
cast($1::regclass AS text) AS sql_identifier,
relacl as acl
FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace
LEFT JOIN pg_type t ON t.typrelid=c.oid AND t.typtype='c' AND c.relkind='c'
LEFT JOIN rel_kind AS cc on cc.k = c.relkind
WHERE c.oid = $1
UNION
Expand All @@ -75,7 +77,9 @@ AS $function$
FROM pg_proc p JOIN pg_namespace n ON n.oid=p.pronamespace
WHERE p.oid = $1
UNION
SELECT t.oid,'pg_type'::regclass,
SELECT coalesce(c.oid,t.oid),
case when c.oid is not null then 'pg_class'::regclass
else 'pg_type'::regclass end,
t.typname AS name, n.nspname AS namespace, pg_get_userbyid(t.typowner) AS owner,
coalesce(cc.v,tt.v2,t.typtype::text) AS sql_kind,
format_type($1,null) AS sql_identifier,
Expand All @@ -85,8 +89,8 @@ AS $function$
null as acl
#end
FROM pg_type t JOIN pg_namespace n ON n.oid=t.typnamespace
LEFT JOIN typ_type AS tt ON tt.k = t.typtype
LEFT JOIN pg_class AS c ON c.oid = t.typrelid
LEFT JOIN typ_type AS tt ON tt.k = t.typtype
LEFT JOIN pg_class AS c ON c.oid = t.typrelid AND t.typtype='c' AND c.relkind<>'c'
LEFT JOIN rel_kind AS cc ON cc.k = c.relkind
WHERE t.oid = $1
UNION
Expand Down Expand Up @@ -1611,7 +1615,7 @@ AS $function$
with obj as (select * from pg_event_trigger where oid = $1)
select
'CREATE EVENT TRIGGER ' || quote_ident(obj.evtname) ||
' ON ' || obj.evtname || E'\n' ||
' ON ' || obj.evtevent || E'\n' ||
case
when obj.evttags is not null
then ' WHEN tag IN ' ||
Expand Down Expand Up @@ -2890,16 +2894,19 @@ AS $function$
with
ddl as (
select row_number() over() as n,
ddlx_drop(objid),
ddlx_create(objid,$2),
objid
from ddlx_get_dependants($1)
ddlx_drop(gd.objid),
ddlx_create(gd.objid,$2),
gd.objid
from ddlx_get_dependants($1) gd
left join pg_depend de
on de.objid=gd.objid and de.refclassid='pg_extension'::regclass
where 'ext' ilike any($2) or de.refclassid is null
)
select ddlx_create($1,$2) as ddl_create,
ddlx_drop($1) as ddl_drop,
string_agg(ddlx_create,E'\n' order by n) as ddl_create_deps,
string_agg(ddlx_drop,'' order by n desc) as ddl_drop_deps
from ddl
from ddl
$function$ strict;

---------------------------------------------------
Expand Down
10 changes: 7 additions & 3 deletions test/expected/class.out
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ alter table test_class_r alter e set (n_distinct=10);
alter table test_class_r alter f set (n_distinct=100);
alter table test_class_r alter h set statistics 50;
grant all on test_class_r to public;
select sql_kind, sql_identifier from ddlx_identify('test_class_r'::regclass);
sql_kind|sql_identifier
TABLE|test_class_r
select classid::regclass, sql_kind, sql_identifier from ddlx_identify('test_class_r'::regtype);
classid|sql_kind|sql_identifier
pg_class|TABLE|test_class_r
(1 row)
select classid::regclass, sql_kind, sql_identifier from ddlx_identify('test_class_r'::regclass);
classid|sql_kind|sql_identifier
pg_class|TABLE|test_class_r
(1 row)
alter table test_class_r alter h set storage external;
create trigger aaaa before
Expand Down
2 changes: 1 addition & 1 deletion test/expected/init.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE EXTENSION ddlx;
\pset tuples_only
\dx ddlx
ddlx | 0.17 | public | DDL eXtractor functions
ddlx | 0.18 | public | DDL eXtractor functions

2 changes: 1 addition & 1 deletion test/expected/tsearch.out
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ select ddlx_create((
select oid from pg_event_trigger
where evtname = 'ddlx_test_event_trigger'));
ddlx_create
CREATE EVENT TRIGGER ddlx_test_event_trigger ON ddlx_test_event_trigger
CREATE EVENT TRIGGER ddlx_test_event_trigger ON ddl_command_start
WHEN tag IN ('CREATE TABLE')
EXECUTE PROCEDURE abort_any_command();
COMMENT ON EVENT TRIGGER ddlx_test_event_trigger IS 'Test event trigger';
Expand Down
8 changes: 8 additions & 0 deletions test/expected/type.out
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ END;
create type test_type_c as (i integer, t text, d test_type_d);
comment on type test_type_c is 'my class type';
comment on column test_type_c.i is 'my class class column i';
select classid::regclass, sql_kind, sql_identifier from ddlx_identify('test_type_c'::regtype);
classid|sql_kind|sql_identifier
pg_type|TYPE|test_type_c
(1 row)
select classid::regclass, sql_kind, sql_identifier from ddlx_identify('test_type_c'::regclass);
classid|sql_kind|sql_identifier
pg_type|TYPE|test_type_c
(1 row)
select ddlx_script('test_type_c'::regtype);
ddlx_script
BEGIN;
Expand Down
3 changes: 2 additions & 1 deletion test/sql/class.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ alter table test_class_r alter e set (n_distinct=10);
alter table test_class_r alter f set (n_distinct=100);
alter table test_class_r alter h set statistics 50;
grant all on test_class_r to public;
select sql_kind, sql_identifier from ddlx_identify('test_class_r'::regclass);
select classid::regclass, sql_kind, sql_identifier from ddlx_identify('test_class_r'::regtype);
select classid::regclass, sql_kind, sql_identifier from ddlx_identify('test_class_r'::regclass);
alter table test_class_r alter h set storage external;

create trigger aaaa before
Expand Down
2 changes: 2 additions & 0 deletions test/sql/type.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ select ddlx_script('test_type_d'::regtype);
create type test_type_c as (i integer, t text, d test_type_d);
comment on type test_type_c is 'my class type';
comment on column test_type_c.i is 'my class class column i';
select classid::regclass, sql_kind, sql_identifier from ddlx_identify('test_type_c'::regtype);
select classid::regclass, sql_kind, sql_identifier from ddlx_identify('test_type_c'::regclass);
select ddlx_script('test_type_c'::regtype);
select ddlx_script('test_type_c'::regclass);

Expand Down

0 comments on commit ab9f1c1

Please sign in to comment.