Showing consitent reversed engineered SQL for the database node.

This commit is contained in:
Ashesh Vashi 2016-03-01 10:42:04 +05:30
parent a6466c091c
commit 1cb2a054ff
25 changed files with 294 additions and 270 deletions

View File

@ -1,35 +1,35 @@
SELECT
'datacl' AS deftype, COALESCE(gt.rolname, 'public') AS grantee,
g.rolname AS grantor, array_agg(privilege_type) AS privileges,
array_agg(is_grantable) AS grantable
'datacl' AS deftype, COALESCE(gt.rolname, 'public') AS grantee,
g.rolname AS grantor, array_agg(privilege_type) AS privileges,
array_agg(is_grantable) AS grantable
FROM
(SELECT
d.grantee, d.grantor, d.is_grantable,
CASE d.privilege_type
WHEN 'CONNECT' THEN 'c'
WHEN 'CREATE' THEN 'C'
WHEN 'DELETE' THEN 'd'
WHEN 'EXECUTE' THEN 'X'
WHEN 'INSERT' THEN 'a'
WHEN 'REFERENCES' THEN 'x'
WHEN 'SELECT' THEN 'r'
WHEN 'TEMPORARY' THEN 'T'
WHEN 'TRIGGER' THEN 't'
WHEN 'TRUNCATE' THEN 'D'
WHEN 'UPDATE' THEN 'w'
WHEN 'USAGE' THEN 'U'
ELSE 'UNKNOWN'
END AS privilege_type
FROM
(SELECT
(d).grantee AS grantee, (d).grantor AS grantor,
(d).is_grantable AS is_grantable,
(d).privilege_type AS privilege_type
FROM
(SELECT aclexplode(db.datacl) AS d FROM pg_database db
WHERE db.oid = {{ did|qtLiteral }}::OID) a
) d
) d
LEFT JOIN pg_catalog.pg_roles g ON (d.grantor = g.oid)
LEFT JOIN pg_catalog.pg_roles gt ON (d.grantee = gt.oid)
(SELECT
d.grantee, d.grantor, d.is_grantable,
CASE d.privilege_type
WHEN 'CONNECT' THEN 'c'
WHEN 'CREATE' THEN 'C'
WHEN 'DELETE' THEN 'd'
WHEN 'EXECUTE' THEN 'X'
WHEN 'INSERT' THEN 'a'
WHEN 'REFERENCES' THEN 'x'
WHEN 'SELECT' THEN 'r'
WHEN 'TEMPORARY' THEN 'T'
WHEN 'TRIGGER' THEN 't'
WHEN 'TRUNCATE' THEN 'D'
WHEN 'UPDATE' THEN 'w'
WHEN 'USAGE' THEN 'U'
ELSE 'UNKNOWN'
END AS privilege_type
FROM
(SELECT
(d).grantee AS grantee, (d).grantor AS grantor,
(d).is_grantable AS is_grantable,
(d).privilege_type AS privilege_type
FROM
(SELECT aclexplode(db.datacl) AS d FROM pg_database db
WHERE db.oid = {{ did|qtLiteral }}::OID) a
) d
) d
LEFT JOIN pg_catalog.pg_roles g ON (d.grantor = g.oid)
LEFT JOIN pg_catalog.pg_roles gt ON (d.grantee = gt.oid)
GROUP BY g.rolname, gt.rolname;

View File

@ -2,20 +2,20 @@
{% import 'macros/privilege.macros' as PRIVILEGE %}
{% import 'macros/default_privilege.macros' as DEFAULT_PRIVILEGE %}
{% if data %}
{# To change owner #}
{# Change the owner #}
{% if data.datowner %}
ALTER DATABASE {{ conn|qtIdent(data.name) }} OWNER TO {{ conn|qtIdent(data.datowner) }};
{% endif %}
{# To change comments #}
{# Change the comments/description #}
{% if data.comments %}
COMMENT ON DATABASE {{ conn|qtIdent(data.name) }}
IS {{ data.comments|qtLiteral }};
IS {{ data.comments|qtLiteral }};
{% endif %}
{# To change conn limit #}
{# Change the connection limit #}
{% if data.datconnlimit %}
ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.datconnlimit }};
{% endif %}
{# To change Variables #}
{# Change the variables/options #}
{% if data.variables and data.variables|length > 0 %}
{% set variables = data.variables %}
{% if 'deleted' in variables and variables.deleted|length > 0 %}
@ -47,7 +47,7 @@ ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.dat
{% endif %}
{% endif %}
{# To change priviledges #}
{# Change the priviledges/ACLs #}
{% if data.datacl %}
{% if 'deleted' in data.datacl %}
{% for priv in data.datacl.deleted %}
@ -67,7 +67,7 @@ ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.dat
{% endif %}
{% endif %}
{# To change default priviledges #}
{# Change the default priviledges for the tables #}
{% if data.deftblacl %}
{% if 'deleted' in data.deftblacl %}
{% for priv in data.deftblacl.deleted %}
@ -86,7 +86,8 @@ ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.dat
{% endif %}
{% endif %}
{# To change default priviledges #}
{# Change the default priviledges for the sequences #}
{% if data.deftblacl %}
{% if data.defseqacl %}
{% if 'deleted' in data.defseqacl %}
{% for priv in data.defseqacl.deleted %}
@ -106,7 +107,7 @@ ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.dat
{% endif %}
{% endif %}
{# To change default priviledges #}
{# Change the default priviledges for the functions #}
{% if data.deffuncacl %}
{% if 'deleted' in data.deffuncacl %}
{% for priv in data.deffuncacl.deleted %}
@ -126,4 +127,4 @@ ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.dat
{% endif %}
{% endif %}
{% endif %}
{% endif %}

View File

@ -1,19 +1,19 @@
{% if data %}
CREATE DATABASE {{ conn|qtIdent(data.name) }}
{% if data.datowner %}
WITH {% endif %}{% if data.datowner %}
WITH {% endif %}{% if data.datowner %}
OWNER = {{ conn|qtIdent(data.datowner) }}{% endif %}{% if data.template %}
OWNER = {{ conn|qtIdent(data.datowner) }}{% endif %}{% if data.template %}
TEMPLATE = {{ conn|qtIdent(data.template) }}{% endif %}{% if data.encoding %}
TEMPLATE = {{ conn|qtIdent(data.template) }}{% endif %}{% if data.encoding %}
ENCODING = {{ data.encoding|qtLiteral }}{% endif %}{% if data.datcollate %}
ENCODING = {{ data.encoding|qtLiteral }}{% endif %}{% if data.datcollate %}
LC_COLLATE = {{ data.datcollate|qtLiteral }}{% endif %}{% if data.datctype %}
LC_COLLATE = {{ data.datcollate|qtLiteral }}{% endif %}{% if data.datctype %}
LC_CTYPE = {{ data.datctype|qtLiteral }}{% endif %}{% if data.spcname %}
LC_CTYPE = {{ data.datctype|qtLiteral }}{% endif %}{% if data.spcname %}
TABLESPACE = {{ conn|qtIdent(data.spcname) }}{% endif %}{% if data.datconnlimit %}
TABLESPACE = {{ conn|qtIdent(data.spcname) }}{% endif %}{% if data.datconnlimit %}
CONNECTION LIMIT = {{ data.datconnlimit }}{% endif %};
{% endif %}
CONNECTION LIMIT = {{ data.datconnlimit }}{% endif %};
{% endif %}

View File

@ -1,34 +1,34 @@
SELECT
CASE (a.deftype)
WHEN 'r' THEN 'deftblacl'
WHEN 'S' THEN 'defseqacl'
WHEN 'f' THEN 'deffuncacl'
WHEN 'T' THEN 'deftypeacl'
END AS deftype,
COALESCE(gt.rolname, 'public') grantee, g.rolname grantor, array_agg(a.privilege_type) as privileges, array_agg(a.is_grantable) as grantable
CASE (a.deftype)
WHEN 'r' THEN 'deftblacl'
WHEN 'S' THEN 'defseqacl'
WHEN 'f' THEN 'deffuncacl'
WHEN 'T' THEN 'deftypeacl'
END AS deftype,
COALESCE(gt.rolname, 'public') grantee, g.rolname grantor, array_agg(a.privilege_type) as privileges, array_agg(a.is_grantable) as grantable
FROM
(SELECT
(acl).grantee as grantee, (acl).grantor AS grantor, (acl).is_grantable AS is_grantable,
CASE (acl).privilege_type
WHEN 'CONNECT' THEN 'c'
WHEN 'CREATE' THEN 'C'
WHEN 'DELETE' THEN 'd'
WHEN 'EXECUTE' THEN 'X'
WHEN 'INSERT' THEN 'a'
WHEN 'REFERENCES' THEN 'x'
WHEN 'SELECT' THEN 'r'
WHEN 'TEMPORARY' THEN 'T'
WHEN 'TRIGGER' THEN 't'
WHEN 'TRUNCATE' THEN 'D'
WHEN 'UPDATE' THEN 'w'
WHEN 'USAGE' THEN 'U'
ELSE 'UNKNOWN'
END AS privilege_type,
defaclobjtype as deftype
FROM
(SELECT defaclobjtype, aclexplode(defaclacl) as acl FROM pg_catalog.pg_default_acl dacl
(SELECT
(acl).grantee as grantee, (acl).grantor AS grantor, (acl).is_grantable AS is_grantable,
CASE (acl).privilege_type
WHEN 'CONNECT' THEN 'c'
WHEN 'CREATE' THEN 'C'
WHEN 'DELETE' THEN 'd'
WHEN 'EXECUTE' THEN 'X'
WHEN 'INSERT' THEN 'a'
WHEN 'REFERENCES' THEN 'x'
WHEN 'SELECT' THEN 'r'
WHEN 'TEMPORARY' THEN 'T'
WHEN 'TRIGGER' THEN 't'
WHEN 'TRUNCATE' THEN 'D'
WHEN 'UPDATE' THEN 'w'
WHEN 'USAGE' THEN 'U'
ELSE 'UNKNOWN'
END AS privilege_type,
defaclobjtype as deftype
FROM
(SELECT defaclobjtype, aclexplode(defaclacl) as acl FROM pg_catalog.pg_default_acl dacl
WHERE dacl.defaclnamespace = 0::OID) d) a
LEFT JOIN pg_catalog.pg_roles g ON (a.grantor = g.oid)
LEFT JOIN pg_catalog.pg_roles gt ON (a.grantee = gt.oid)
LEFT JOIN pg_catalog.pg_roles g ON (a.grantor = g.oid)
LEFT JOIN pg_catalog.pg_roles gt ON (a.grantee = gt.oid)
GROUP BY g.rolname, gt.rolname, a.deftype
ORDER BY a.deftype
ORDER BY a.deftype

View File

@ -4,7 +4,7 @@
#}
SELECT * FROM
(SELECT pg_encoding_to_char(s.i) AS encoding
FROM (SELECT generate_series(0, 100, 1) as i) s) a
FROM (SELECT generate_series(0, 100, 1) as i) s) a
WHERE encoding != '';
{#
@ -14,4 +14,4 @@ WHERE encoding != '';
-- as encoding
-- FROM (SELECT generate_series(0, 100, 1) as i) s) a
--WHERE encoding != ''
#}
#}

View File

@ -8,7 +8,7 @@
{% import 'macros/default_privilege.macros' as DEFAULT_PRIVILEGE %}
{% if data.comments %}
COMMENT ON DATABASE {{ conn|qtIdent(data.name) }}
IS {{ data.comments|qtLiteral }};
IS {{ data.comments|qtLiteral }};
{% endif %}
{# TO generate Variable SQL using macro #}
@ -42,4 +42,4 @@ COMMENT ON DATABASE {{ conn|qtIdent(data.name) }}
{% for priv in data.deffuncacl %}
{{ DEFAULT_PRIVILEGE.APPLY(conn, 'FUNCTIONS', priv.grantee, priv.without_grant, priv.with_grant) }}
{% endfor %}
{% endif %}
{% endif %}

View File

@ -1,2 +1,2 @@
SELECT name, vartype, min_val, max_val, enumvals
FROM pg_settings WHERE context in ('user', 'superuser')
FROM pg_settings WHERE context in ('user', 'superuser')

View File

@ -1,35 +1,35 @@
SELECT
'datacl' AS deftype, COALESCE(gt.rolname, 'public') AS grantee,
g.rolname grantor, array_agg(privilege_type) AS privileges,
array_agg(is_grantable) AS grantable
'datacl' AS deftype, COALESCE(gt.rolname, 'public') AS grantee,
g.rolname grantor, array_agg(privilege_type) AS privileges,
array_agg(is_grantable) AS grantable
FROM
(SELECT
d.grantee, d.grantor, d.is_grantable,
CASE d.privilege_type
WHEN 'CONNECT' THEN 'c'
WHEN 'CREATE' THEN 'C'
WHEN 'DELETE' THEN 'd'
WHEN 'EXECUTE' THEN 'X'
WHEN 'INSERT' THEN 'a'
WHEN 'REFERENCES' THEN 'x'
WHEN 'SELECT' THEN 'r'
WHEN 'TEMPORARY' THEN 'T'
WHEN 'TRIGGER' THEN 't'
WHEN 'TRUNCATE' THEN 'D'
WHEN 'UPDATE' THEN 'w'
WHEN 'USAGE' THEN 'U'
ELSE 'UNKNOWN'
END AS privilege_type
FROM
(SELECT
(d).grantee AS grantee, (d).grantor AS grantor,
(d).is_grantable AS is_grantable,
(d).privilege_type AS privilege_type
FROM
(SELECT aclexplode(db.datacl) AS d FROM pg_database db
WHERE db.oid = {{ did|qtLiteral }}::OID) a
) d
) d
LEFT JOIN pg_catalog.pg_roles g ON (d.grantor = g.oid)
LEFT JOIN pg_catalog.pg_roles gt ON (d.grantee = gt.oid)
(SELECT
d.grantee, d.grantor, d.is_grantable,
CASE d.privilege_type
WHEN 'CONNECT' THEN 'c'
WHEN 'CREATE' THEN 'C'
WHEN 'DELETE' THEN 'd'
WHEN 'EXECUTE' THEN 'X'
WHEN 'INSERT' THEN 'a'
WHEN 'REFERENCES' THEN 'x'
WHEN 'SELECT' THEN 'r'
WHEN 'TEMPORARY' THEN 'T'
WHEN 'TRIGGER' THEN 't'
WHEN 'TRUNCATE' THEN 'D'
WHEN 'UPDATE' THEN 'w'
WHEN 'USAGE' THEN 'U'
ELSE 'UNKNOWN'
END AS privilege_type
FROM
(SELECT
(d).grantee AS grantee, (d).grantor AS grantor,
(d).is_grantable AS is_grantable,
(d).privilege_type AS privilege_type
FROM
(SELECT aclexplode(db.datacl) AS d FROM pg_database db
WHERE db.oid = {{ did|qtLiteral }}::OID) a
) d
) d
LEFT JOIN pg_catalog.pg_roles g ON (d.grantor = g.oid)
LEFT JOIN pg_catalog.pg_roles gt ON (d.grantee = gt.oid)
GROUP BY g.rolname, gt.rolname;

View File

@ -3,20 +3,20 @@
{% import 'macros/privilege.macros' as PRIVILEGE %}
{% import 'macros/default_privilege.macros' as DEFAULT_PRIVILEGE %}
{% if data %}
{# To change owner #}
{# Change the owner #}
{% if data.datowner %}
ALTER DATABASE {{ conn|qtIdent(data.name) }} OWNER TO {{ conn|qtIdent(data.datowner) }};
{% endif %}
{# TO change comments #}
{# Change the comments/description #}
{% if data.comments %}
COMMENT ON DATABASE {{ conn|qtIdent(data.name) }}
IS {{ data.comments|qtLiteral }};
IS {{ data.comments|qtLiteral }};
{% endif %}
{# TO change conn limit #}
{# Change the connection limit #}
{% if data.datconnlimit %}
ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.datconnlimit }};
{% endif %}
{# To change Security Label #}
{# Change the security labels #}
{% if data.seclabels and data.seclabels|length > 0 %}
{% set seclabels = data.seclabels %}
{% if 'deleted' in seclabels and seclabels.deleted|length > 0 %}
@ -35,7 +35,7 @@ ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.dat
{% endfor %}
{% endif %}
{% endif %}
{# To change Variables #}
{# Change the variables/options #}
{% if data.variables and data.variables|length > 0 %}
{% set variables = data.variables %}
{% if 'deleted' in variables and variables.deleted|length > 0 %}
@ -67,7 +67,7 @@ ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.dat
{% endif %}
{% endif %}
{# To change priviledges #}
{# Change the priviledges/ACLs #}
{% if data.datacl %}
{% if 'deleted' in data.datacl %}
{% for priv in data.datacl.deleted %}
@ -87,7 +87,7 @@ ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.dat
{% endif %}
{% endif %}
{# To change default priviledges #}
{# Change the default priviledges/ACLs for the tables #}
{% if data.deftblacl %}
{% if 'deleted' in data.deftblacl %}
{% for priv in data.deftblacl.deleted %}
@ -107,7 +107,7 @@ ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.dat
{% endif %}
{% endif %}
{# To change default priviledges #}
{# Change the default priviledges/ACLs for the sequences #}
{% if data.defseqacl %}
{% if 'deleted' in data.defseqacl %}
{% for priv in data.defseqacl.deleted %}
@ -127,7 +127,7 @@ ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.dat
{% endif %}
{% endif %}
{# To change default priviledges #}
{# Change the default priviledges/ACLs for the functions #}
{% if data.deffuncacl %}
{% if 'deleted' in data.deffuncacl %}
{% for priv in data.deffuncacl.deleted %}
@ -147,7 +147,7 @@ ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.dat
{% endif %}
{% endif %}
{# To change default priviledges #}
{# Change the default priviledges/ACLs for the types #}
{% if data.deftypeacl %}
{% if 'deleted' in data.deftypeacl %}
{% for priv in data.deftypeacl.deleted %}

View File

@ -1,19 +1,19 @@
{% if data %}
CREATE DATABASE {{ conn|qtIdent(data.name) }}
{% if data.datowner %}
WITH {% endif %}{% if data.datowner %}
WITH {% endif %}{% if data.datowner %}
OWNER = {{ conn|qtIdent(data.datowner) }}{% endif %}{% if data.template %}
OWNER = {{ conn|qtIdent(data.datowner) }}{% endif %}{% if data.template %}
TEMPLATE = {{ conn|qtIdent(data.template) }}{% endif %}{% if data.encoding %}
TEMPLATE = {{ conn|qtIdent(data.template) }}{% endif %}{% if data.encoding %}
ENCODING = {{ data.encoding|qtLiteral }}{% endif %}{% if data.datcollate %}
ENCODING = {{ data.encoding|qtLiteral }}{% endif %}{% if data.datcollate %}
LC_COLLATE = {{ data.datcollate|qtLiteral }}{% endif %}{% if data.datctype %}
LC_COLLATE = {{ data.datcollate|qtLiteral }}{% endif %}{% if data.datctype %}
LC_CTYPE = {{ data.datctype|qtLiteral }}{% endif %}{% if data.spcname %}
LC_CTYPE = {{ data.datctype|qtLiteral }}{% endif %}{% if data.spcname %}
TABLESPACE = {{ conn|qtIdent(data.spcname) }}{% endif %}{% if data.datconnlimit %}
TABLESPACE = {{ conn|qtIdent(data.spcname) }}{% endif %}{% if data.datconnlimit %}
CONNECTION LIMIT = {{ data.datconnlimit }}{% endif %};
{% endif %}
CONNECTION LIMIT = {{ data.datconnlimit }}{% endif %};
{% endif %}

View File

@ -1,34 +1,34 @@
SELECT
CASE (a.deftype)
WHEN 'r' THEN 'deftblacl'
WHEN 'S' THEN 'defseqacl'
WHEN 'f' THEN 'deffuncacl'
WHEN 'T' THEN 'deftypeacl'
END AS deftype,
COALESCE(gt.rolname, 'public') grantee, g.rolname grantor, array_agg(a.privilege_type) as privileges, array_agg(a.is_grantable) as grantable
CASE (a.deftype)
WHEN 'r' THEN 'deftblacl'
WHEN 'S' THEN 'defseqacl'
WHEN 'f' THEN 'deffuncacl'
WHEN 'T' THEN 'deftypeacl'
END AS deftype,
COALESCE(gt.rolname, 'public') grantee, g.rolname grantor, array_agg(a.privilege_type) as privileges, array_agg(a.is_grantable) as grantable
FROM
(SELECT
(acl).grantee as grantee, (acl).grantor AS grantor, (acl).is_grantable AS is_grantable,
CASE (acl).privilege_type
WHEN 'CONNECT' THEN 'c'
WHEN 'CREATE' THEN 'C'
WHEN 'DELETE' THEN 'd'
WHEN 'EXECUTE' THEN 'X'
WHEN 'INSERT' THEN 'a'
WHEN 'REFERENCES' THEN 'x'
WHEN 'SELECT' THEN 'r'
WHEN 'TEMPORARY' THEN 'T'
WHEN 'TRIGGER' THEN 't'
WHEN 'TRUNCATE' THEN 'D'
WHEN 'UPDATE' THEN 'w'
WHEN 'USAGE' THEN 'U'
ELSE 'UNKNOWN'
END AS privilege_type,
defaclobjtype as deftype
FROM
(SELECT defaclobjtype, aclexplode(defaclacl) as acl FROM pg_catalog.pg_default_acl dacl
(SELECT
(acl).grantee as grantee, (acl).grantor AS grantor, (acl).is_grantable AS is_grantable,
CASE (acl).privilege_type
WHEN 'CONNECT' THEN 'c'
WHEN 'CREATE' THEN 'C'
WHEN 'DELETE' THEN 'd'
WHEN 'EXECUTE' THEN 'X'
WHEN 'INSERT' THEN 'a'
WHEN 'REFERENCES' THEN 'x'
WHEN 'SELECT' THEN 'r'
WHEN 'TEMPORARY' THEN 'T'
WHEN 'TRIGGER' THEN 't'
WHEN 'TRUNCATE' THEN 'D'
WHEN 'UPDATE' THEN 'w'
WHEN 'USAGE' THEN 'U'
ELSE 'UNKNOWN'
END AS privilege_type,
defaclobjtype as deftype
FROM
(SELECT defaclobjtype, aclexplode(defaclacl) as acl FROM pg_catalog.pg_default_acl dacl
WHERE dacl.defaclnamespace = 0::OID) d) a
LEFT JOIN pg_catalog.pg_roles g ON (a.grantor = g.oid)
LEFT JOIN pg_catalog.pg_roles gt ON (a.grantee = gt.oid)
LEFT JOIN pg_catalog.pg_roles g ON (a.grantor = g.oid)
LEFT JOIN pg_catalog.pg_roles gt ON (a.grantee = gt.oid)
GROUP BY g.rolname, gt.rolname, a.deftype
ORDER BY a.deftype
ORDER BY a.deftype

View File

@ -4,5 +4,5 @@ SELECT db.datname as name FROM pg_database as db WHERE db.oid = {{did}}
{% endif %}
{# Using name from above query we will drop the database #}
{% if datname %}
DROP DATABASE {{ conn|qtIdent(datname) }};
DROP DATABASE {{ conn|qtIdent(datname) }};
{% endif %}

View File

@ -2,9 +2,10 @@
-- Given SQL is workaround
-- We need to implement a mechanism to check for valid supported server encoding
#}
SELECT * FROM
(SELECT pg_encoding_to_char(s.i) AS encoding
FROM (SELECT generate_series(0, 100, 1) as i) s) a
SELECT *
FROM
(SELECT pg_encoding_to_char(s.i) AS encoding
FROM (SELECT generate_series(0, 100, 1) as i) s) a
WHERE encoding != '';
{#
@ -14,4 +15,4 @@ WHERE encoding != '';
-- as encoding
-- FROM (SELECT generate_series(0, 100, 1) as i) s) a
--WHERE encoding != ''
#}
#}

View File

@ -1,5 +1,7 @@
SELECT rl.*, r.rolname AS user_name, db.datname as db_name
FROM pg_db_role_setting AS rl
LEFT JOIN pg_roles AS r ON rl.setrole = r.oid
LEFT JOIN pg_database AS db ON rl.setdatabase = db.oid
WHERE setdatabase = {{did}}
SELECT
rl.*, r.rolname AS user_name, db.datname as db_name
FROM
pg_db_role_setting AS rl
LEFT JOIN pg_roles AS r ON rl.setrole = r.oid
LEFT JOIN pg_database AS db ON rl.setdatabase = db.oid
WHERE setdatabase = {{did}};

View File

@ -9,16 +9,16 @@
{% import 'macros/default_privilege.macros' as DEFAULT_PRIVILEGE %}
{% if data.comments %}
COMMENT ON DATABASE {{ conn|qtIdent(data.name) }}
IS {{ data.comments|qtLiteral }};
IS {{ data.comments|qtLiteral }};
{% endif %}
{# To generate Security Label SQL using macro #}
{# Generate the security labels #}
{% if data.securities %}
{% for r in data.securities %}
{{ SECLABEL.APPLY(conn, 'DATABASE', data.name, r.provider, r.securitylabel) }}
{% endfor %}
{% endif %}
{# To generate Variable SQL using macro #}
{# Generate the variable/options #}
{% if data.variables %}
{% for var in data.variables %}
{% if var.value == True %}
@ -30,6 +30,7 @@ COMMENT ON DATABASE {{ conn|qtIdent(data.name) }}
{% endif %}
{% endfor %}
{% endif %}
{# Generate the privileges/ACLs #}
{% if data.datacl %}
{% for priv in data.datacl %}
{{ PRIVILEGE.APPLY(conn, 'DATABASE', priv.grantee, data.name, priv.without_grant, priv.with_grant) }}

View File

@ -1 +1,4 @@
SELECT name, vartype, min_val, max_val, enumvals FROM pg_settings WHERE context in ('user', 'superuser')
SELECT
name, vartype, min_val, max_val, enumvals
FROM pg_settings
WHERE context in ('user', 'superuser');

View File

@ -1,35 +1,35 @@
SELECT
'datacl' AS deftype, COALESCE(gt.rolname, 'public') AS grantee,
g.rolname AS grantor, array_agg(privilege_type) AS privileges,
array_agg(is_grantable) AS grantable
'datacl' AS deftype, COALESCE(gt.rolname, 'public') AS grantee,
g.rolname AS grantor, array_agg(privilege_type) AS privileges,
array_agg(is_grantable) AS grantable
FROM
(SELECT
d.grantee, d.grantor, d.is_grantable,
CASE d.privilege_type
WHEN 'CONNECT' THEN 'c'
WHEN 'CREATE' THEN 'C'
WHEN 'DELETE' THEN 'd'
WHEN 'EXECUTE' THEN 'X'
WHEN 'INSERT' THEN 'a'
WHEN 'REFERENCES' THEN 'x'
WHEN 'SELECT' THEN 'r'
WHEN 'TEMPORARY' THEN 'T'
WHEN 'TRIGGER' THEN 't'
WHEN 'TRUNCATE' THEN 'D'
WHEN 'UPDATE' THEN 'w'
WHEN 'USAGE' THEN 'U'
ELSE 'UNKNOWN'
END AS privilege_type
FROM
(SELECT
(d).grantee AS grantee, (d).grantor AS grantor,
(d).is_grantable AS is_grantable,
(d).privilege_type AS privilege_type
FROM
(SELECT aclexplode(db.datacl) AS d FROM pg_database db
WHERE db.oid = {{ did|qtLiteral }}::OID) a
) d
) d
LEFT JOIN pg_catalog.pg_roles g ON (d.grantor = g.oid)
LEFT JOIN pg_catalog.pg_roles gt ON (d.grantee = gt.oid)
(SELECT
d.grantee, d.grantor, d.is_grantable,
CASE d.privilege_type
WHEN 'CONNECT' THEN 'c'
WHEN 'CREATE' THEN 'C'
WHEN 'DELETE' THEN 'd'
WHEN 'EXECUTE' THEN 'X'
WHEN 'INSERT' THEN 'a'
WHEN 'REFERENCES' THEN 'x'
WHEN 'SELECT' THEN 'r'
WHEN 'TEMPORARY' THEN 'T'
WHEN 'TRIGGER' THEN 't'
WHEN 'TRUNCATE' THEN 'D'
WHEN 'UPDATE' THEN 'w'
WHEN 'USAGE' THEN 'U'
ELSE 'UNKNOWN'
END AS privilege_type
FROM
(SELECT
(d).grantee AS grantee, (d).grantor AS grantor,
(d).is_grantable AS is_grantable,
(d).privilege_type AS privilege_type
FROM
(SELECT aclexplode(db.datacl) AS d FROM pg_database db
WHERE db.oid = {{ did|qtLiteral }}::OID) a
) d
) d
LEFT JOIN pg_catalog.pg_roles g ON (d.grantor = g.oid)
LEFT JOIN pg_catalog.pg_roles gt ON (d.grantee = gt.oid)
GROUP BY g.rolname, gt.rolname;

View File

@ -3,20 +3,20 @@
{% import 'macros/privilege.macros' as PRIVILEGE %}
{% import 'macros/default_privilege.macros' as DEFAULT_PRIVILEGE %}
{% if data %}
{# The SQL generated below will change owner #}
{# Change the owner #}
{% if data.datowner %}
ALTER DATABASE {{ conn|qtIdent(data.name) }} OWNER TO {{ conn|qtIdent(data.datowner) }};
{% endif %}
{# The SQL generated below will change comments #}
{# Change the comments/description #}
{% if data.comments %}
COMMENT ON DATABASE {{ conn|qtIdent(data.name) }}
IS {{ data.comments|qtLiteral }};
{% endif %}
{# The SQL generated below will change conn limit #}
{# Change the connection limit #}
{% if data.datconnlimit %}
ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.datconnlimit }};
{% endif %}
{# The SQL generated below will change Security Label #}
{# Change the security labels #}
{% if data.seclabels and data.seclabels|length > 0 %}
{% set seclabels = data.seclabels %}
{% if 'deleted' in seclabels and seclabels.deleted|length > 0 %}
@ -35,7 +35,7 @@ ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.dat
{% endfor %}
{% endif %}
{% endif %}
{# The SQL generated below will change Variables #}
{# Change the variables/options #}
{% if data.variables and data.variables|length > 0 %}
{% set variables = data.variables %}
{% if 'deleted' in variables and variables.deleted|length > 0 %}
@ -67,7 +67,7 @@ ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.dat
{% endif %}
{% endif %}
{# The SQL generated below will change priviledges #}
{# Change the priviledges/ACLs #}
{% if data.datacl %}
{% if 'deleted' in data.datacl %}
{% for priv in data.datacl.deleted %}
@ -87,6 +87,7 @@ ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.dat
{% endif %}
{% endif %}
{# Change the default priviledges/ACLs for tables #}
{% if data.deftblacl %}
{% if 'deleted' in data.deftblacl %}
{% for priv in data.deftblacl.deleted %}
@ -106,6 +107,7 @@ ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.dat
{% endif %}
{% endif %}
{# Change the default priviledges/ACLs for sequences #}
{% if data.defseqacl %}
{% if 'deleted' in data.defseqacl %}
{% for priv in data.defseqacl.deleted %}
@ -125,6 +127,7 @@ ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.dat
{% endif %}
{% endif %}
{# Change the default priviledges/ACLs for functions #}
{% if data.deffuncacl %}
{% if 'deleted' in data.deffuncacl %}
{% for priv in data.deffuncacl.deleted %}
@ -144,6 +147,7 @@ ALTER DATABASE {{ conn|qtIdent(data.name) }} WITH CONNECTION LIMIT = {{ data.dat
{% endif %}
{% endif %}
{# Change the default priviledges/ACLs for types #}
{% if data.deftypeacl %}
{% if 'deleted' in data.deftypeacl %}
{% for priv in data.deftypeacl.deleted %}

View File

@ -1,19 +1,19 @@
{% if data %}
CREATE DATABASE {{ conn|qtIdent(data.name) }}
{% if data.datowner %}
WITH {% endif %}{% if data.datowner %}
WITH {% endif %}{% if data.datowner %}
OWNER = {{ conn|qtIdent(data.datowner) }}{% endif %}{% if data.template %}
OWNER = {{ conn|qtIdent(data.datowner) }}{% endif %}{% if data.template %}
TEMPLATE = {{ conn|qtIdent(data.template) }}{% endif %}{% if data.encoding %}
TEMPLATE = {{ conn|qtIdent(data.template) }}{% endif %}{% if data.encoding %}
ENCODING = {{ data.encoding|qtLiteral }}{% endif %}{% if data.datcollate %}
ENCODING = {{ data.encoding|qtLiteral }}{% endif %}{% if data.datcollate %}
LC_COLLATE = {{ data.datcollate|qtLiteral }}{% endif %}{% if data.datctype %}
LC_COLLATE = {{ data.datcollate|qtLiteral }}{% endif %}{% if data.datctype %}
LC_CTYPE = {{ data.datctype|qtLiteral }}{% endif %}{% if data.spcname %}
LC_CTYPE = {{ data.datctype|qtLiteral }}{% endif %}{% if data.spcname %}
TABLESPACE = {{ conn|qtIdent(data.spcname) }}{% endif %}{% if data.datconnlimit %}
TABLESPACE = {{ conn|qtIdent(data.spcname) }}{% endif %}{% if data.datconnlimit %}
CONNECTION LIMIT = {{ data.datconnlimit }}{% endif %};
{% endif %}
CONNECTION LIMIT = {{ data.datconnlimit }}{% endif %};
{% endif %}

View File

@ -1,34 +1,34 @@
SELECT
CASE (a.deftype)
WHEN 'r' THEN 'deftblacl'
WHEN 'S' THEN 'defseqacl'
WHEN 'f' THEN 'deffuncacl'
WHEN 'T' THEN 'deftypeacl'
END AS deftype,
COALESCE(gt.rolname, 'public') AS grantee, g.rolname AS grantor, array_agg(a.privilege_type) as privileges, array_agg(a.is_grantable) as grantable
CASE (a.deftype)
WHEN 'r' THEN 'deftblacl'
WHEN 'S' THEN 'defseqacl'
WHEN 'f' THEN 'deffuncacl'
WHEN 'T' THEN 'deftypeacl'
END AS deftype,
COALESCE(gt.rolname, 'public') AS grantee, g.rolname AS grantor, array_agg(a.privilege_type) as privileges, array_agg(a.is_grantable) as grantable
FROM
(SELECT
(acl).grantee as grantee, (acl).grantor AS grantor, (acl).is_grantable AS is_grantable,
CASE (acl).privilege_type
WHEN 'CONNECT' THEN 'c'
WHEN 'CREATE' THEN 'C'
WHEN 'DELETE' THEN 'd'
WHEN 'EXECUTE' THEN 'X'
WHEN 'INSERT' THEN 'a'
WHEN 'REFERENCES' THEN 'x'
WHEN 'SELECT' THEN 'r'
WHEN 'TEMPORARY' THEN 'T'
WHEN 'TRIGGER' THEN 't'
WHEN 'TRUNCATE' THEN 'D'
WHEN 'UPDATE' THEN 'w'
WHEN 'USAGE' THEN 'U'
ELSE 'UNKNOWN'
END AS privilege_type,
defaclobjtype as deftype
FROM
(SELECT defaclobjtype, aclexplode(defaclacl) as acl FROM pg_catalog.pg_default_acl dacl
(SELECT
(acl).grantee as grantee, (acl).grantor AS grantor, (acl).is_grantable AS is_grantable,
CASE (acl).privilege_type
WHEN 'CONNECT' THEN 'c'
WHEN 'CREATE' THEN 'C'
WHEN 'DELETE' THEN 'd'
WHEN 'EXECUTE' THEN 'X'
WHEN 'INSERT' THEN 'a'
WHEN 'REFERENCES' THEN 'x'
WHEN 'SELECT' THEN 'r'
WHEN 'TEMPORARY' THEN 'T'
WHEN 'TRIGGER' THEN 't'
WHEN 'TRUNCATE' THEN 'D'
WHEN 'UPDATE' THEN 'w'
WHEN 'USAGE' THEN 'U'
ELSE 'UNKNOWN'
END AS privilege_type,
defaclobjtype as deftype
FROM
(SELECT defaclobjtype, aclexplode(defaclacl) as acl FROM pg_catalog.pg_default_acl dacl
WHERE dacl.defaclnamespace = 0::OID) d) a
LEFT JOIN pg_catalog.pg_roles g ON (a.grantor = g.oid)
LEFT JOIN pg_catalog.pg_roles gt ON (a.grantee = gt.oid)
LEFT JOIN pg_catalog.pg_roles g ON (a.grantor = g.oid)
LEFT JOIN pg_catalog.pg_roles gt ON (a.grantee = gt.oid)
GROUP BY g.rolname, gt.rolname, a.deftype
ORDER BY a.deftype

View File

@ -4,5 +4,5 @@ SELECT db.datname as name FROM pg_database as db WHERE db.oid = {{did}}
{% endif %}
{# Using name from above query we will drop the database #}
{% if datname %}
DROP DATABASE {{ conn|qtIdent(datname) }};
DROP DATABASE {{ conn|qtIdent(datname) }};
{% endif %}

View File

@ -2,9 +2,10 @@
-- Given SQL is workaround
-- We need to implement a mechanism to check for valid supported server encoding
#}
SELECT * FROM
(SELECT pg_encoding_to_char(s.i) AS encoding
FROM (SELECT generate_series(0, 100, 1) as i) s) a
SELECT *
FROM
(SELECT pg_encoding_to_char(s.i) AS encoding
FROM (SELECT generate_series(0, 100, 1) as i) s) a
WHERE encoding != '';
{#
@ -14,4 +15,4 @@ WHERE encoding != '';
-- as encoding
-- FROM (SELECT generate_series(0, 100, 1) as i) s) a
--WHERE encoding != ''
#}
#}

View File

@ -1,5 +1,6 @@
SELECT rl.*, r.rolname AS user_name, db.datname as db_name
SELECT
rl.*, r.rolname AS user_name, db.datname as db_name
FROM pg_db_role_setting AS rl
LEFT JOIN pg_roles AS r ON rl.setrole = r.oid
LEFT JOIN pg_database AS db ON rl.setdatabase = db.oid
LEFT JOIN pg_roles AS r ON rl.setrole = r.oid
LEFT JOIN pg_database AS db ON rl.setdatabase = db.oid
WHERE setdatabase = {{did}}

View File

@ -9,16 +9,16 @@
{% import 'macros/default_privilege.macros' as DEFAULT_PRIVILEGE %}
{% if data.comments %}
COMMENT ON DATABASE {{ conn|qtIdent(data.name) }}
IS {{ data.comments|qtLiteral }};
IS {{ data.comments|qtLiteral }};
{% endif %}
{# We will generate Security Label SQL's using macro #}
{# Change the security labels #}
{% if data.securities %}
{% for r in data.securities %}
{{ SECLABEL.APPLY(conn, 'DATABASE', data.name, r.provider, r.securitylabel) }}
{% endfor %}
{% endif %}
{# We will generate Variable SQL's using macro #}
{# Variables/options #}
{% if data.variables %}
{% for var in data.variables %}
{% if var.value == True %}
@ -30,26 +30,33 @@ COMMENT ON DATABASE {{ conn|qtIdent(data.name) }}
{% endif %}
{% endfor %}
{% endif %}
{# Privileges/ACLs #}
{% if data.datacl %}
{% for priv in data.datacl %}
{{ PRIVILEGE.APPLY(conn, 'DATABASE', priv.grantee, data.name, priv.without_grant, priv.with_grant) }}
{% endfor %}
{% endif %}
{# Default privileges/ACLs for tables #}
{% if data.deftblacl %}
{% for priv in data.deftblacl %}
{{ DEFAULT_PRIVILEGE.APPLY(conn, 'TABLES', priv.grantee, priv.without_grant, priv.with_grant) }}
{% endfor %}
{% endif %}
{# Default privileges/ACLs for sequences #}
{% if data.defseqacl %}
{% for priv in data.defseqacl %}
{{ DEFAULT_PRIVILEGE.APPLY(conn, 'SEQUENCES', priv.grantee, priv.without_grant, priv.with_grant) }}
{% endfor %}
{% endif %}
{# Default privileges/ACLs for functions #}
{% if data.deffuncacl %}
{% for priv in data.deffuncacl %}
{{ DEFAULT_PRIVILEGE.APPLY(conn, 'FUNCTIONS', priv.grantee, priv.without_grant, priv.with_grant) }}
{% endfor %}
{% endif %}
{# Default privileges/ACLs for types #}
{% if data.deftypeacl %}
{% for priv in data.deftypeacl %}
{{ DEFAULT_PRIVILEGE.APPLY(conn, 'TYPES', priv.grantee, priv.without_grant, priv.with_grant) }}

View File

@ -1 +1,4 @@
SELECT name, vartype, min_val, max_val, enumvals FROM pg_settings WHERE context in ('user', 'superuser')
SELECT
name, vartype, min_val, max_val, enumvals
FROM pg_settings
WHERE context in ('user', 'superuser');