diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/__init__.py index 1bff4b2e29c..e058a9c2cc9 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/__init__.py @@ -1189,7 +1189,8 @@ def _check_for_column_delete(self, columns, data, column_sql): c['schema'] = data['schema'] c['table'] = data['name'] # Sql for drop column - if 'inheritedfrom' not in c: + if 'inheritedfrom' not in c or \ + ('inheritedfrom' in c and c['inheritedfrom'] is None): column_sql += render_template("/".join( [self.foreign_table_column_template_path, self._DELETE_SQL]), diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/templates/foreign_table_columns/sql/default/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/templates/foreign_table_columns/sql/default/create.sql index 20e67798dcc..cf55c6672d4 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/templates/foreign_table_columns/sql/default/create.sql +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/templates/foreign_table_columns/sql/default/create.sql @@ -11,27 +11,27 @@ ALTER FOREIGN TABLE IF EXISTS {{conn|qtIdent(data.schema, data.table)}} NOT NULL{% endif %}{% if data.defval is defined and data.defval is not none and data.defval != '' and data.colconstype != 'g' %} DEFAULT {{data.defval}}{% endif %}{% if data.colconstype == 'g' and data.genexpr and data.genexpr != '' %} GENERATED ALWAYS AS ({{data.genexpr}}) STORED{% endif %}{% endif %}; - {### Add comments ###} {% if data and data.description and data.description != None %} + COMMENT ON COLUMN {{conn|qtIdent(data.schema, data.table, data.name)}} IS {{data.description|qtLiteral(conn)}}; {% endif %} - {### Add variables to column ###} {% if data.attoptions %} + ALTER FOREIGN TABLE IF EXISTS {{conn|qtIdent(data.schema, data.table)}} {{ VARIABLE.SET(conn, 'COLUMN', data.name, data.attoptions) }} {% endif %} - {### Alter column statistics value ###} {% if data.attstattarget is defined and data.attstattarget > -1 %} + ALTER FOREIGN TABLE IF EXISTS {{conn|qtIdent(data.schema, data.table)}} ALTER COLUMN {{conn|qtTypeIdent(data.name)}} SET STATISTICS {{data.attstattarget}}; {% endif %} - {### Alter column storage value ###} {% if data.attstorage is defined and data.attstorage != data.defaultstorage %} + ALTER FOREIGN TABLE IF EXISTS {{conn|qtIdent(data.schema, data.table)}} ALTER COLUMN {{conn|qtTypeIdent(data.name)}} SET STORAGE {%if data.attstorage == 'p' %} PLAIN{% elif data.attstorage == 'm'%}MAIN{% elif data.attstorage == 'e'%} diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/templates/foreign_tables/sql/default/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/templates/foreign_tables/sql/default/create.sql index fae839f595c..691ff57603c 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/templates/foreign_tables/sql/default/create.sql +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/templates/foreign_tables/sql/default/create.sql @@ -1,5 +1,6 @@ {% import 'macros/schemas/security.macros' as SECLABEL %} {% import 'macros/schemas/privilege.macros' as PRIVILEGE %} +{% import 'macros/variable.macros' as VARIABLE %} {% set is_columns = [] %} {% if data %} CREATE FOREIGN TABLE{% if add_not_exists_clause %} IF NOT EXISTS{% endif %} {{ conn|qtIdent(data.basensp, data.name) }}( @@ -12,7 +13,9 @@ CREATE FOREIGN TABLE{% if add_not_exists_clause %} IF NOT EXISTS{% endif %} {{ c {% if loop.first %} OPTIONS ({% endif %}{% if not loop.first %}, {% endif %}{{o.option}} {{o.value|qtLiteral(conn)}}{% if loop.last %}){% endif %}{% endif %} {% endfor %}{% endif %} {% if c.attnotnull %} NOT NULL{% endif %} -{% if c.defval is defined and c.defval is not none %} DEFAULT {{c.defval}}{% endif %} +{% if c.defval is defined and c.defval is not none and c.defval != '' and c.colconstype != 'g' %} DEFAULT {{c.defval}}{% endif %} +{% if c.colconstype == 'g' and c.genexpr and c.genexpr != '' %} + GENERATED ALWAYS AS {{c.genexpr}} STORED{% endif %} {% if c.collname %} COLLATE {{c.collname}}{% endif %} {% if not loop.last %}, {% endif %} @@ -71,3 +74,37 @@ COMMENT ON COLUMN {{conn|qtIdent(data.basensp, data.name, c.name)}} {% endfor %} {% endif %} {% endif %} + +{#===========================================#} +{# COLUMN SPECIFIC TEMPLATES STARTS HERE #} +{#===========================================#} +{% if data.columns and data.columns|length > 0 %} +{% for c in data.columns %} +{% if c.description %} + +COMMENT ON COLUMN {{conn|qtIdent(data.schema, data.name, c.name)}} + IS {{c.description|qtLiteral(conn)}}; +{% endif %} +{### Add variables to column ###} +{% if c.attoptions and c.attoptions|length > 0 %} + +ALTER TABLE IF EXISTS {{conn|qtIdent(data.schema, data.name)}} + {{ VARIABLE.SET(conn, 'COLUMN', c.name, c.attoptions) }} + +{% endif %} +{### Alter column statistics value ###} +{% if c.attstattarget is defined and c.attstattarget > -1 %} +ALTER TABLE IF EXISTS {{conn|qtIdent(data.schema, data.name)}} + ALTER COLUMN {{conn|qtTypeIdent(c.name)}} SET STATISTICS {{c.attstattarget}}; + +{% endif %} +{### Alter column storage value ###} +{% if c.attstorage is defined and c.attstorage != c.defaultstorage %} +ALTER TABLE IF EXISTS {{conn|qtIdent(data.schema, data.name)}} + ALTER COLUMN {{conn|qtTypeIdent(c.name)}} SET STORAGE {%if c.attstorage == 'p' %} +PLAIN{% elif c.attstorage == 'm'%}MAIN{% elif c.attstorage == 'e'%} +EXTERNAL{% elif c.attstorage == 'x'%}EXTENDED{% endif %}; + +{% endif %} +{% endfor %} +{% endif %} diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/templates/foreign_tables/sql/default/get_columns.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/templates/foreign_tables/sql/default/get_columns.sql index 960c5bfdefb..24f290a201c 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/templates/foreign_tables/sql/default/get_columns.sql +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/templates/foreign_tables/sql/default/get_columns.sql @@ -16,6 +16,7 @@ WITH INH_TABLES AS SELECT INH.inheritedfrom, INH.inheritedid, att.attoptions, att.atttypid, attfdwoptions, att.attname as name, att.attndims, att.atttypmod, pg_catalog.format_type(t.oid,NULL) AS cltype, att.attnotnull, att.attstorage, att.attstattarget, att.attnum, pg_catalog.format_type(t.oid, att.atttypmod) AS fulltype, + t.typstorage AS defaultstorage, CASE WHEN t.typelem > 0 THEN t.typelem ELSE t.oid END as elemoid, (CASE WHEN (att.attidentity in ('a', 'd')) THEN 'i' WHEN (att.attgenerated in ('s')) THEN 'g' ELSE 'n' END) AS colconstype, (CASE WHEN (att.attgenerated in ('s')) THEN pg_catalog.pg_get_expr(def.adbin, def.adrelid) END) AS genexpr, diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/templates/foreign_tables/sql/default/properties.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/templates/foreign_tables/sql/default/properties.sql index c560899f86d..1ae79122a3a 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/templates/foreign_tables/sql/default/properties.sql +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/templates/foreign_tables/sql/default/properties.sql @@ -2,6 +2,7 @@ SELECT c.oid, c.relname AS name, pg_catalog.pg_get_userbyid(relowner) AS owner, pg_catalog.array_to_string(c.relacl::text[], ', ') as acl, ftoptions, srvname AS ftsrvname, description, nspname AS basensp, + (SELECT count(*) FROM pg_catalog.pg_trigger WHERE tgrelid=ftrelid AND tgisinternal = FALSE) AS triggercount, (SELECT pg_catalog.array_agg(provider || '=' || label) FROM diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/pg/default/alter_col_opts.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/pg/default/alter_col_opts.sql index 0b6df464a22..30c4c3e4df1 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/pg/default/alter_col_opts.sql +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/pg/default/alter_col_opts.sql @@ -20,3 +20,7 @@ COMMENT ON FOREIGN TABLE public."FT1_$%{}[]()&*^!@""'`\/#" GRANT SELECT ON TABLE public."FT1_$%{}[]()&*^!@""'`\/#" TO PUBLIC; GRANT ALL ON TABLE public."FT1_$%{}[]()&*^!@""'`\/#" TO postgres; + + +ALTER TABLE IF EXISTS "FT1_$%{}[]()&*^!@""'`\/#" + ALTER COLUMN col1 SET STATISTICS 10; diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/pg/default/alter_remove_opts_priv_cons.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/pg/default/alter_remove_opts_priv_cons.sql index f0e932f3fb6..25051edb085 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/pg/default/alter_remove_opts_priv_cons.sql +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/pg/default/alter_remove_opts_priv_cons.sql @@ -15,3 +15,7 @@ COMMENT ON FOREIGN TABLE public."FT1_$%{}[]()&*^!@""'`\/#" IS 'Test Comment'; GRANT ALL ON TABLE public."FT1_$%{}[]()&*^!@""'`\/#" TO postgres; + + +ALTER TABLE IF EXISTS "FT1_$%{}[]()&*^!@""'`\/#" + ALTER COLUMN col1 SET STATISTICS 10; diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/ppas/default/alter_col_opts.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/ppas/default/alter_col_opts.sql index 7368467e571..35136f2ddff 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/ppas/default/alter_col_opts.sql +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/ppas/default/alter_col_opts.sql @@ -20,3 +20,7 @@ COMMENT ON FOREIGN TABLE public."FT1_$%{}[]()&*^!@""'`\/#" GRANT SELECT ON TABLE public."FT1_$%{}[]()&*^!@""'`\/#" TO PUBLIC; GRANT ALL ON TABLE public."FT1_$%{}[]()&*^!@""'`\/#" TO enterprisedb; + + +ALTER TABLE IF EXISTS "FT1_$%{}[]()&*^!@""'`\/#" + ALTER COLUMN col1 SET STATISTICS 10; diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/ppas/default/alter_remove_opts_priv_cons.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/ppas/default/alter_remove_opts_priv_cons.sql index 69f3ff66944..128d70c839c 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/ppas/default/alter_remove_opts_priv_cons.sql +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/ppas/default/alter_remove_opts_priv_cons.sql @@ -15,3 +15,7 @@ COMMENT ON FOREIGN TABLE public."FT1_$%{}[]()&*^!@""'`\/#" IS 'Test Comment'; GRANT ALL ON TABLE public."FT1_$%{}[]()&*^!@""'`\/#" TO enterprisedb; + + +ALTER TABLE IF EXISTS "FT1_$%{}[]()&*^!@""'`\/#" + ALTER COLUMN col1 SET STATISTICS 10;