diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/static/js/column.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/static/js/column.js index c4f19184..61059598 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/static/js/column.js +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/static/js/column.js @@ -185,6 +185,7 @@ define('pgadmin.node.column', [ attnotnull: false, attlen: null, attprecision: null, + attidentity: undefined, }, schema: [{ id: 'name', label: gettext('Name'), cell: 'string', @@ -473,6 +474,17 @@ define('pgadmin.node.column', [ || type == 'smallserial'; } }, + },{ + id: 'attidentity', label: gettext('Identity'), control: 'select2', + cell: 'select2', select2: { placeholder: 'Select identity', + allowClear: true, + width: '100%', + }, + min_version: 100000, group: gettext('Definition'), + 'options': [ + {label: gettext('ALWAYS'), value: 'a'}, + {label: gettext('BY DEFAULT'), value: 'd'}, + ], },{ id: 'attnotnull', label: gettext('Not NULL?'), cell: 'switch', type: 'switch', disabled: 'inSchemaWithColumnCheck', cellHeaderClasses:'width_percent_20', diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/10_plus/update.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/10_plus/update.sql new file mode 100644 index 00000000..78cc96d3 --- /dev/null +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/10_plus/update.sql @@ -0,0 +1,168 @@ +{% import 'column/macros/security.macros' as SECLABEL %} +{% import 'column/macros/privilege.macros' as PRIVILEGE %} +{% import 'macros/variable.macros' as VARIABLE %} +{% import 'type/macros/get_full_type_sql_format.macros' as GET_TYPE %} +{### Rename column name ###} +{% if data.name and data.name != o_data.name %} +ALTER TABLE {{conn|qtIdent(data.schema, data.table)}} + RENAME {{conn|qtIdent(o_data.name)}} TO {{conn|qtIdent(data.name)}}; + +{% endif %} +{### Alter column type and collation ###} +{% if (data.cltype and data.cltype != o_data.cltype) or (data.attlen is defined and data.attlen is not none and data.attlen != o_data.attlen) or (data.attprecision is defined and data.attprecision != o_data.attprecision) or (data.collspcname and data.collspcname != o_data.collspcname)%} +ALTER TABLE {{conn|qtIdent(data.schema, data.table)}} + ALTER COLUMN {% if data.name %}{{conn|qtTypeIdent(data.name)}}{% else %}{{conn|qtTypeIdent(o_data.name)}}{% endif %} TYPE {{ GET_TYPE.UPDATE_TYPE_SQL(conn, data, o_data) }}{% if data.collspcname and data.collspcname != o_data.collspcname %} + COLLATE {{data.collspcname}}{% elif o_data.collspcname %} COLLATE {{o_data.collspcname}}{% endif %}; +{% endif %} +{### Alter column default value ###} +{% if data.defval is defined and data.defval is not none and data.defval != '' and data.defval != o_data.defval %} +ALTER TABLE {{conn|qtIdent(data.schema, data.table)}} + ALTER COLUMN {% if data.name %}{{conn|qtTypeIdent(data.name)}}{% else %}{{conn|qtTypeIdent(o_data.name)}}{% endif %} SET DEFAULT {{data.defval}}; + +{% endif %} +{### Drop column default value ###} +{% if data.defval is defined and data.defval == '' and data.defval != o_data.defval %} +ALTER TABLE {{conn|qtIdent(data.schema, data.table)}} + ALTER COLUMN {% if data.name %}{{conn|qtTypeIdent(data.name)}}{% else %}{{conn|qtTypeIdent(o_data.name)}}{% endif %} DROP DEFAULT; + +{% endif %} +{### Alter column not null value ###} +{% if 'attnotnull' in data and data.attnotnull != o_data.attnotnull %} +ALTER TABLE {{conn|qtIdent(data.schema, data.table)}} + ALTER COLUMN {% if data.name %}{{conn|qtTypeIdent(data.name)}}{% else %}{{conn|qtTypeIdent(o_data.name)}}{% endif %} {% if data.attnotnull %}SET{% else %}DROP{% endif %} NOT NULL; + +{% endif %} +{### Alter column - add identity ###} +{% if 'attidentity' in data and o_data.attidentity == '' and data.attidentity != o_data.attidentity %} +ALTER TABLE {{conn|qtIdent(data.schema, data.table)}} + ALTER COLUMN {% if data.name %}{{conn|qtTypeIdent(data.name)}}{% else %}{{conn|qtTypeIdent(o_data.name)}}{% endif %} ADD GENERATED {% if data.attidentity == 'a' %}ALWAYS{% else%}BY DEFAULT{% endif %} AS IDENTITY; + +{% endif %} +{### Alter column - change identity ###} +{% if 'attidentity' in data and data.attidentity != '' and o_data.attidentity != '' and data.attidentity != o_data.attidentity %} +ALTER TABLE {{conn|qtIdent(data.schema, data.table)}} + ALTER COLUMN {% if data.name %}{{conn|qtTypeIdent(data.name)}}{% else %}{{conn|qtTypeIdent(o_data.name)}}{% endif %} SET GENERATED {% if data.attidentity == 'a' %}ALWAYS{% else%}BY DEFAULT{% endif %}; + +{% endif %} +{### Alter column - drop identity ###} +{% if 'attidentity' in data and data.attidentity == '' and o_data.attidentity != '' and data.attidentity != o_data.attidentity %} +ALTER TABLE {{conn|qtIdent(data.schema, data.table)}} + ALTER COLUMN {% if data.name %}{{conn|qtTypeIdent(data.name)}}{% else %}{{conn|qtTypeIdent(o_data.name)}}{% endif %} DROP IDENTITY; + +{% endif %} +{### Alter column statistics value ###} +{% if data.attstattarget and data.attstattarget != o_data.attstattarget %} +ALTER TABLE {{conn|qtIdent(data.schema, data.table)}} + ALTER COLUMN {% if data.name %}{{conn|qtTypeIdent(data.name)}}{% else %}{{conn|qtTypeIdent(o_data.name)}}{% endif %} SET STATISTICS {{data.attstattarget}}; + +{% endif %} +{### Alter column storage value ###} +{% if data.attstorage and data.attstorage != o_data.attstorage %} +ALTER TABLE {{conn|qtIdent(data.schema, data.table)}} + ALTER COLUMN {% if data.name %}{{conn|qtTypeIdent(data.name)}}{% else %}{{conn|qtTypeIdent(o_data.name)}}{% endif %} SET STORAGE {%if data.attstorage == 'p' %} +PLAIN{% elif data.attstorage == 'm'%}MAIN{% elif data.attstorage == 'e'%} +EXTERNAL{% elif data.attstorage == 'x'%}EXTENDED{% endif %}; + +{% endif %} +{% if data.description is defined %} +{% if data.name %} +COMMENT ON COLUMN {{conn|qtIdent(data.schema, data.table, data.name)}} +{% else %} +COMMENT ON COLUMN {{conn|qtIdent(data.schema, data.table, o_data.name)}} +{% endif %} + IS {{data.description|qtLiteral}}; + +{% endif %} +{### Update column variables ###} +{% if 'attoptions' in data and data.attoptions|length > 0 %} +{% set variables = data.attoptions %} +{% if 'deleted' in variables and variables.deleted|length > 0 %} +ALTER TABLE {{conn|qtIdent(data.schema, data.table)}} +{% if data.name %} + {{ VARIABLE.UNSET(conn, 'COLUMN', data.name, variables.deleted) }} +{% else %} + {{ VARIABLE.UNSET(conn, 'COLUMN', o_data.name, variables.deleted) }} +{% endif %} +{% endif %} +{% if 'added' in variables and variables.added|length > 0 %} +ALTER TABLE {{conn|qtIdent(data.schema, data.table)}} +{% if data.name %} + {{ VARIABLE.SET(conn, 'COLUMN', data.name, variables.added) }} +{% else %} + {{ VARIABLE.SET(conn, 'COLUMN', o_data.name, variables.added) }} +{% endif %} +{% endif %} +{% if 'changed' in variables and variables.changed|length > 0 %} +ALTER TABLE {{conn|qtIdent(data.schema, data.table)}} +{% if data.name %} + {{ VARIABLE.SET(conn, 'COLUMN', data.name, variables.changed) }} +{% else %} + {{ VARIABLE.SET(conn, 'COLUMN', o_data.name, variables.changed) }} +{% endif %} +{% endif %} +{% endif %} +{### Update column privileges ###} +{# Change the privileges #} +{% if data.attacl %} +{% if 'deleted' in data.attacl %} +{% for priv in data.attacl.deleted %} +{% if data.name %} + {{ PRIVILEGE.RESETALL(conn, data.schema, data.table, data.name, priv.grantee) }} +{% else %} + {{ PRIVILEGE.RESETALL(conn, data.schema, data.table, o_data.name, priv.grantee) }} +{% endif %} +{% endfor %} +{% endif %} +{% if 'changed' in data.attacl %} +{% for priv in data.attacl.changed %} +{% if data.name %} + {{ PRIVILEGE.RESETALL(conn, data.schema, data.table, data.name, priv.grantee) }} + {{ PRIVILEGE.APPLY(conn, data.schema, data.table, data.name, priv.grantee, priv.without_grant, priv.with_grant) }} +{% else %} + {{ PRIVILEGE.RESETALL(conn, data.schema, data.table, o_data.name, priv.grantee) }} + {{ PRIVILEGE.APPLY(conn, data.schema, data.table, o_data.name, priv.grantee, priv.without_grant, priv.with_grant) }} +{% endif %} +{% endfor %} +{% endif %} +{% if 'added' in data.attacl %} +{% for priv in data.attacl.added %} +{% if data.name %} + {{ PRIVILEGE.APPLY(conn, data.schema, data.table, data.name, priv.grantee, priv.without_grant, priv.with_grant) }} +{% else %} + {{ PRIVILEGE.APPLY(conn, data.schema, data.table, o_data.name, priv.grantee, priv.without_grant, priv.with_grant) }} +{% endif %} +{% endfor %} +{% endif %} +{% endif %} +{### Uppdate tablespace securitylabel ###} +{# The SQL generated below will change Security Label #} +{% if data.seclabels and data.seclabels|length > 0 %} +{% set seclabels = data.seclabels %} +{% if 'deleted' in seclabels and seclabels.deleted|length > 0 %} +{% for r in seclabels.deleted %} +{% if data.name %} +{{ SECLABEL.DROP(conn, 'COLUMN', data.schema, data.table, data.name, r.provider) }} +{% else %} +{{ SECLABEL.DROP(conn, 'COLUMN', data.schema, data.table, o_data.name, r.provider) }} +{% endif %} +{% endfor %} +{% endif %} +{% if 'added' in seclabels and seclabels.added|length > 0 %} +{% for r in seclabels.added %} +{% if data.name %} +{{ SECLABEL.APPLY(conn, 'COLUMN',data.schema, data.table, data.name, r.provider, r.label) }} +{% else %} +{{ SECLABEL.APPLY(conn, 'COLUMN',data.schema, data.table, o_data.name, r.provider, r.label) }} +{% endif %} +{% endfor %} +{% endif %} +{% if 'changed' in seclabels and seclabels.changed|length > 0 %} +{% for r in seclabels.changed %} +{% if data.name %} +{{ SECLABEL.APPLY(conn, 'COLUMN',data.schema, data.table, data.name, r.provider, r.label) }} +{% else %} +{{ SECLABEL.APPLY(conn, 'COLUMN',data.schema, data.table, o_data.name, r.provider, r.label) }} +{% endif %} +{% endfor %} +{% endif %} +{% endif %} diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/10_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/10_plus/create.sql new file mode 100644 index 00000000..b3f87749 --- /dev/null +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/10_plus/create.sql @@ -0,0 +1,158 @@ +{% import 'macros/schemas/security.macros' as SECLABEL %} +{% import 'macros/schemas/privilege.macros' as PRIVILEGE %} +{% import 'macros/variable.macros' as VARIABLE %} +{% import 'column/macros/security.macros' as COLUMN_SECLABEL %} +{% import 'column/macros/privilege.macros' as COLUMN_PRIVILEGE %} +{% import 'table/sql/macros/constraints.macro' as CONSTRAINTS %} +{% import 'type/macros/get_full_type_sql_format.macros' as GET_TYPE %} +{#===========================================#} +{#====== MAIN TABLE TEMPLATE STARTS HERE ======#} +{#===========================================#} +{# + If user has not provided any details but only name then + add empty bracket with table name +#} +{% set empty_bracket = ""%} +{% if data.coll_inherits|length == 0 and data.columns|length == 0 and not data.typname and not data.like_relation and data.primary_key|length == 0 and data.unique_constraint|length == 0 and data.foreign_key|length == 0 and data.check_constraint|length == 0 and data.exclude_constraint|length == 0 %} +{% set empty_bracket = "\n(\n)"%} +{% endif %} +CREATE {% if data.relpersistence %}UNLOGGED {% endif %}TABLE {{conn|qtIdent(data.schema, data.name)}}{{empty_bracket}} +{% if data.typname %} + OF {{ data.typname }} +{% endif %} +{% if data.like_relation or data.coll_inherits or data.columns|length > 0 or data.primary_key|length > 0 or data.unique_constraint|length > 0 or data.foreign_key|length > 0 or data.check_constraint|length > 0 or data.exclude_constraint|length > 0 %} +( +{% endif %} +{% if data.like_relation %} + LIKE {{ data.like_relation }}{% if data.like_default_value %} + + INCLUDING DEFAULTS{% endif %}{% if data.like_constraints %} + + INCLUDING CONSTRAINTS{% endif %}{% if data.like_indexes %} + + INCLUDING INDEXES{% endif %}{% if data.like_storage %} + + INCLUDING STORAGE{% endif %}{% if data.like_comments %} + + INCLUDING COMMENTS{% endif %}{% if data.columns|length > 0 %}, +{% endif %} + +{% endif %} +{### Add columns ###} +{% if data.columns and data.columns|length > 0 %} +{% for c in data.columns %} +{% if c.name and c.cltype and not c.inheritedfrom %} + {{conn|qtIdent(c.name)}} {% if is_sql %}{{c.displaytypname}}{% else %}{{ GET_TYPE.CREATE_TYPE_SQL(conn, c.cltype, c.attlen, c.attprecision, c.hasSqrBracket) }}{% endif %}{% if c.collspcname %} COLLATE {{c.collspcname}}{% endif %}{% if c.attnotnull %} NOT NULL{% endif %}{% if c.defval %} DEFAULT {{c.defval}}{% endif %}{% if c.attidentity and c.attidentity == 'a' %} GENERATED ALWAYS AS IDENTITY {% elif c.attidentity and c.attidentity == 'd' %} GENERATED BY DEFAULT AS IDENTITY{% endif %} +{% if not loop.last %}, +{% endif %} +{% endif %} +{% endfor %} +{% endif %} +{# Macro to render for constraints #} +{% if data.primary_key|length > 0 %}{% if data.columns|length > 0 %},{% endif %} +{{CONSTRAINTS.PRIMARY_KEY(conn, data.primary_key[0])}}{% endif %}{% if data.unique_constraint|length > 0 %}{% if data.columns|length > 0 or data.primary_key|length > 0 %},{% endif %} +{{CONSTRAINTS.UNIQUE(conn, data.unique_constraint)}}{% endif %}{% if data.foreign_key|length > 0 %}{% if data.columns|length > 0 or data.primary_key|length > 0 or data.unique_constraint|length > 0 %},{% endif %} +{{CONSTRAINTS.FOREIGN_KEY(conn, data.foreign_key)}}{% endif %}{% if data.check_constraint|length > 0 %}{% if data.columns|length > 0 or data.primary_key|length > 0 or data.unique_constraint|length > 0 or data.foreign_key|length > 0 %},{% endif %} +{{CONSTRAINTS.CHECK(conn, data.check_constraint)}}{% endif %}{% if data.exclude_constraint|length > 0 %}{% if data.columns|length > 0 or data.primary_key|length > 0 or data.unique_constraint|length > 0 or data.foreign_key|length > 0 or data.check_constraint|length > 0 %},{% endif %} +{{CONSTRAINTS.EXCLUDE(conn, data.exclude_constraint)}}{% endif %} +{% if data.like_relation or data.coll_inherits or data.columns|length > 0 or data.primary_key|length > 0 or data.unique_constraint|length > 0 or data.foreign_key|length > 0 or data.check_constraint|length > 0 or data.exclude_constraint|length > 0 %} + +){% endif %}{% if data.relkind is defined and data.relkind == 'p' %} PARTITION BY {{ data.partition_scheme }} {% endif %} + +{### If we are inheriting it from another table(s) ###} +{% if data.coll_inherits %} + INHERITS ({% for val in data.coll_inherits %}{% if loop.index != 1 %}, {% endif %}{{val}}{% endfor %}) +{% endif %} +WITH ( + OIDS = {% if data.relhasoids %}TRUE{% else %}FALSE{% endif %}{% if data.fillfactor %}, + FILLFACTOR = {{ data.fillfactor }}{% endif %}{% if data.autovacuum_custom %}, + autovacuum_enabled = {% if data.autovacuum_enabled %}TRUE{% else %}FALSE{% endif %}{% endif %}{% if data.toast_autovacuum %}, + toast.autovacuum_enabled = {% if data.toast_autovacuum_enabled %}TRUE{% else %}FALSE{% endif %} +{% endif %}{% if data.autovacuum_enabled and data.vacuum_table|length > 0 %} +{% for opt in data.vacuum_table %}{% if opt.name and opt.value %} +, + {{opt.name}} = {{opt.value}}{% endif %} +{% endfor %}{% endif %}{% if data.toast_autovacuum_enabled and data.vacuum_toast|length > 0 %} +{% for opt in data.vacuum_toast %}{% if opt.name and opt.value %} +, + toast.{{opt.name}} = {{opt.value}}{% endif %} +{% endfor %}{% endif %} + +{### SQL for Tablespace ###} +{% if data.spcname %} +) +TABLESPACE {{ conn|qtIdent(data.spcname) }}; +{% else %} +); +{% endif %} +{### Alter SQL for Owner ###} +{% if data.relowner %} + +ALTER TABLE {{conn|qtIdent(data.schema, data.name)}} + OWNER to {{conn|qtIdent(data.relowner)}}; +{% endif %} +{### Security Labels on Table ###} +{% if data.seclabels and data.seclabels|length > 0 %} + +{% for r in data.seclabels %} +{{ SECLABEL.SET(conn, 'TABLE', data.name, r.provider, r.label, data.schema) }} +{% endfor %} +{% endif %} +{### ACL on Table ###} +{% if data.relacl %} + +{% for priv in data.relacl %} +{{ PRIVILEGE.SET(conn, 'TABLE', priv.grantee, data.name, priv.without_grant, priv.with_grant, data.schema) }} +{% endfor %} +{% endif %} +{### SQL for COMMENT ###} +{% if data.description %} +COMMENT ON TABLE {{conn|qtIdent(data.schema, data.name)}} + IS {{data.description|qtLiteral}}; +{% endif %} +{#===========================================#} +{#====== MAIN TABLE TEMPLATE ENDS HERE ======#} +{#===========================================#} +{#===========================================#} +{# 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}}; +{% endif %} +{### Add variables to column ###} +{% if c.attoptions and c.attoptions|length > 0 %} + +ALTER TABLE {{conn|qtIdent(data.schema, data.name)}} + {{ VARIABLE.SET(conn, 'COLUMN', c.name, c.attoptions) }} +{% endif %} +{### ACL ###} +{% if c.attacl and c.attacl|length > 0 %} + +{% for priv in c.attacl %} + {{ COLUMN_PRIVILEGE.APPLY(conn, data.schema, data.name, c.name, priv.grantee, priv.without_grant, priv.with_grant) }} +{% endfor %} +{% endif %} +{### Security Lables ###} +{% if c.seclabels and c.seclabels|length > 0 %} + +{% for r in c.seclabels %} +{{ COLUMN_SECLABEL.APPLY(conn, 'COLUMN',data.schema, data.name, c.name, r.provider, r.label) }} +{% endfor %} +{% endif %} +{% endfor %} +{% endif %} +{#===========================================#} +{# COLUMN SPECIFIC TEMPLATES ENDS HERE #} +{#===========================================#} +{#======================================#} +{# CONSTRAINTS SPECIFIC TEMPLATES #} +{#======================================#} +{{CONSTRAINTS.CONSTRAINT_COMMENTS(conn, data.schema, data.name, data.primary_key)}} +{{CONSTRAINTS.CONSTRAINT_COMMENTS(conn, data.schema, data.name, data.unique_constraint)}} +{{CONSTRAINTS.CONSTRAINT_COMMENTS(conn, data.schema, data.name, data.foreign_key)}} +{{CONSTRAINTS.CONSTRAINT_COMMENTS(conn, data.schema, data.name, data.check_constraint)}} +{{CONSTRAINTS.CONSTRAINT_COMMENTS(conn, data.schema, data.name, data.exclude_constraint)}} diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/10_plus/get_columns_for_table.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/10_plus/get_columns_for_table.sql new file mode 100644 index 00000000..f5ae9942 --- /dev/null +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/table/sql/10_plus/get_columns_for_table.sql @@ -0,0 +1,16 @@ +SELECT + a.attname AS name, format_type(a.atttypid, NULL) AS cltype, a.attidentity as clidentity, + quote_ident(n.nspname)||'.'||quote_ident(c.relname) as inheritedfrom, + c.oid as inheritedid +FROM + pg_class c +JOIN + pg_namespace n ON c.relnamespace=n.oid +JOIN + pg_attribute a ON a.attrelid = c.oid AND NOT a.attisdropped AND a.attnum > 0 +WHERE +{% if tid %} + c.oid = {{tid}}::OID +{% else %} + c.relname = {{tname|qtLiteral}} +{% endif %}