Skip to content

Commit

Permalink
Added all like_options while creating a table. #6377
Browse files Browse the repository at this point in the history
updated test cases

updated docs
  • Loading branch information
pravesh-sharma committed Aug 4, 2023
1 parent 59c4a00 commit d7badb0
Show file tree
Hide file tree
Showing 40 changed files with 9,612 additions and 82 deletions.
Binary file modified docs/en_US/images/table_advanced.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 13 additions & 5 deletions docs/en_US/table_dialog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,21 @@ not-null constraints; after saving the new or modified table, any changes to
the original table will not be applied to the new table.

* Use the drop-down listbox next to *Relation* to select a reference table.
* Move the *With default values?* switch to the *Yes* position to copy default
* Move the switch next to *With default values?* towards the *right position* to copy default
values.
* Move the *With constraints?* switch to the *Yes* position to copy table and
* Move the switch next to *With constraints?* towards the *right position* to copy table and
column constraints.
* Move the *With indexes?* switch to the *Yes* position to copy indexes.
* Move the *With storage?* switch to the *Yes* position to copy storage settings.
* Move the *With comments?* switch to the *Yes* position to copy comments.
* Move the switch next to *With indexes?* towards the *right position* to copy indexes.
* Move the switch next to *With storage?* towards the *right position* to copy storage settings.
* Move the switch next to *With comments?* towards the *right position* to copy comments.
* Move the switch next to *With compression?* towards the *right position* to copy
compression method. This option is available only on PostgreSQL 14 and above.
* Move the switch next to *With generated?* towards the *right position* to copy
generation expressions of copied column. This option is available only on PostgreSQL 12 and above.
* Move the switch next to *With identity?* towards the *right position* to copy
any identity specifications of copied column.
* Move the switch next to *With statistics?* towards the *right position* to copy
extended statistics.

With PostgreSQL 10 forward, the *Partition* tab will be visible.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ export class LikeSchema extends BaseUISchema {
like_indexes: false,
like_storage: false,
like_comments: false,
like_compression: false,
like_generated: false,
like_identity: false,
like_statistics: false
};
}
}
Expand All @@ -256,22 +260,46 @@ export class LikeSchema extends BaseUISchema {
id: 'like_default_value', label: gettext('With default values?'),
type: 'switch', mode: ['create'], deps: ['like_relation'],
disabled: this.isRelationDisable, depChange: (...args)=>obj.resetVals(...args),
inlineNext: true,
},{
id: 'like_constraints', label: gettext('With constraints?'),
type: 'switch', mode: ['create'], deps: ['like_relation'],
disabled: this.isRelationDisable, depChange: (...args)=>obj.resetVals(...args),
inlineNext: true,
},{
id: 'like_indexes', label: gettext('With indexes?'),
type: 'switch', mode: ['create'], deps: ['like_relation'],
disabled: this.isRelationDisable, depChange: (...args)=>obj.resetVals(...args),
inlineNext: true,
},{
id: 'like_storage', label: gettext('With storage?'),
type: 'switch', mode: ['create'], deps: ['like_relation'],
disabled: this.isRelationDisable, depChange: (...args)=>obj.resetVals(...args),
inlineNext: true,
},{
id: 'like_comments', label: gettext('With comments?'),
type: 'switch', mode: ['create'], deps: ['like_relation'],
disabled: this.isRelationDisable, depChange: (...args)=>obj.resetVals(...args),
inlineNext: true,
},{
id: 'like_compression', label: gettext('With compression?'),
type: 'switch', mode: ['create'], deps: ['like_relation'],
disabled: this.isRelationDisable, depChange: (...args)=>obj.resetVals(...args),
min_version: 140000, inlineNext: true,
},{
id: 'like_generated', label: gettext('With generated?'),
type: 'switch', mode: ['create'], deps: ['like_relation'],
disabled: this.isRelationDisable, depChange: (...args)=>obj.resetVals(...args),
min_version: 120000, inlineNext: true,
},{
id: 'like_identity', label: gettext('With identity?'),
type: 'switch', mode: ['create'], deps: ['like_relation'],
disabled: this.isRelationDisable, depChange: (...args)=>obj.resetVals(...args),
inlineNext: true,
},{
id: 'like_statistics', label: gettext('With statistics?'),
type: 'switch', mode: ['create'], deps: ['like_relation'],
disabled: this.isRelationDisable, depChange: (...args)=>obj.resetVals(...args),
}
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ CREATE {% if data.relpersistence %}UNLOGGED {% endif %}TABLE{% if add_not_exists

INCLUDING STORAGE{% endif %}{% if data.like_comments %}

INCLUDING COMMENTS{% endif %}{% if data.columns|length > 0 %},
INCLUDING COMMENTS{% endif %}{% if data.like_identity %}

INCLUDING IDENTITY{% endif %}{% if data.like_statistics %}

INCLUDING STATISTICS{% endif %}{% if data.columns|length > 0 %},
{% endif %}

{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ CREATE {% if data.relpersistence %}UNLOGGED {% endif %}TABLE{% if add_not_exists

INCLUDING STORAGE{% endif %}{% if data.like_comments %}

INCLUDING COMMENTS{% endif %}{% if data.columns|length > 0 %},
INCLUDING COMMENTS{% endif %}{% if data.like_compression %}

INCLUDING GENERATED{% endif %}{% if data.like_identity %}

INCLUDING IDENTITY{% endif %}{% if data.like_statistics %}

INCLUDING STATISTICS{% endif %}{% if data.columns|length > 0 %},
{% endif %}

{% endif %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
{% import 'macros/schemas/security.macros' as SECLABEL %}
{% import 'macros/schemas/privilege.macros' as PRIVILEGE %}
{% import 'macros/variable.macros' as VARIABLE %}
{% import 'columns/macros/security.macros' as COLUMN_SECLABEL %}
{% import 'columns/macros/privilege.macros' as COLUMN_PRIVILEGE %}
{% import 'tables/sql/macros/constraints.macro' as CONSTRAINTS %}
{% import 'types/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 %}
{% set with_clause = false%}
{% if data.fillfactor or data.parallel_workers or data.toast_tuple_target or (data.autovacuum_custom and data.add_vacuum_settings_in_sql) or data.autovacuum_enabled in ('t', 'f') or (data.toast_autovacuum and data.add_vacuum_settings_in_sql) or data.toast_autovacuum_enabled in ('t', 'f') %}
{% set with_clause = true%}
{% endif %}
CREATE {% if data.relpersistence %}UNLOGGED {% endif %}TABLE{% if add_not_exists_clause %} IF NOT EXISTS{% endif %} {{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.like_compression %}

INCLUDING COMPRESSION{% endif %}{% if data.like_generated %}

INCLUDING GENERATED{% endif %}{% if data.like_identity %}

INCLUDING IDENTITY{% endif %}{% if data.like_statistics %}

INCLUDING STATISTICS{% 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 %}
{% if c.inheritedfromtable %}-- Inherited from table {{c.inheritedfromtable}}: {% elif c.inheritedfromtype %}-- Inherited from type {{c.inheritedfromtype}}: {% endif %}{{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 is defined and c.defval is not none and c.defval != '' and c.colconstype != 'g' %} DEFAULT {{c.defval}}{% endif %}
{% if c.colconstype == 'i' and c.attidentity and c.attidentity != '' %}
{% if c.attidentity == 'a' %} GENERATED ALWAYS AS IDENTITY{% elif c.attidentity == 'd' %} GENERATED BY DEFAULT AS IDENTITY{% endif %}
{% if c.seqincrement or c.seqcycle or c.seqincrement or c.seqstart or c.seqmin or c.seqmax or c.seqcache %} ( {% endif %}
{% if c.seqcycle is defined and c.seqcycle %}
CYCLE {% endif %}{% if c.seqincrement is defined and c.seqincrement|int(-1) > -1 %}
INCREMENT {{c.seqincrement|int}} {% endif %}{% if c.seqstart is defined and c.seqstart|int(-1) > -1%}
START {{c.seqstart|int}} {% endif %}{% if c.seqmin is defined and c.seqmin|int(-1) > -1%}
MINVALUE {{c.seqmin|int}} {% endif %}{% if c.seqmax is defined and c.seqmax|int(-1) > -1%}
MAXVALUE {{c.seqmax|int}} {% endif %}{% if c.seqcache is defined and c.seqcache|int(-1) > -1%}
CACHE {{c.seqcache|int}} {% endif %}
{% if c.seqincrement or c.seqcycle or c.seqincrement or c.seqstart or c.seqmin or c.seqmax or c.seqcache %}){% endif %}
{% endif %}
{% if c.colconstype == 'g' and c.genexpr and c.genexpr != '' %} GENERATED ALWAYS AS ({{c.genexpr}}) STORED{% 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 not data.coll_inherits and not data.spcname and not with_clause %};{% 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 %}){% if not data.spcname and not with_clause %};{% endif %}
{% endif %}

{% if with_clause %}
{% set ns = namespace(add_comma=false) %}
WITH (
{% if data.fillfactor %}{% set ns.add_comma = true%}
FILLFACTOR = {{ data.fillfactor }}{% endif %}{% if data.parallel_workers %}
{% if ns.add_comma %},
{% endif %}
parallel_workers = {{ data.parallel_workers }}{% set ns.add_comma = true%}{% endif %}{% if data.toast_tuple_target %}
{% if ns.add_comma %},
{% endif %}
toast_tuple_target = {{ data.toast_tuple_target }}{% set ns.add_comma = true%}{% endif %}{% if data.autovacuum_enabled in ('t', 'f') %}
{% if ns.add_comma %},
{% endif %}
autovacuum_enabled = {% if data.autovacuum_enabled == 't' %}TRUE{% else %}FALSE{% endif %}{% set ns.add_comma = true%}{% endif %}{% if data.toast_autovacuum_enabled in ('t', 'f') %}
{% if ns.add_comma %},
{% endif %}
toast.autovacuum_enabled = {% if data.toast_autovacuum_enabled == 't' %}TRUE{% else %}FALSE{% endif %}{% set ns.add_comma = true%}{% endif %}{% if data.autovacuum_custom %}
{% for opt in data.vacuum_table %}{% if opt.name and opt.value is defined %}
{% if ns.add_comma %},
{% endif %}
{{opt.name}} = {{opt.value}}{% set ns.add_comma = true%}{% endif %}
{% endfor %}{% endif %}{% if data.toast_autovacuum %}
{% for opt in data.vacuum_toast %}{% if opt.name and opt.value is defined %}
{% if ns.add_comma %},
{% endif %}
toast.{{opt.name}} = {{opt.value}}{% set ns.add_comma = true%}{% endif %}
{% endfor %}{% endif %}

{% if data.spcname %}){% else %});{% endif %}

{% endif %}
{### SQL for Tablespace ###}
{% if data.spcname %}
TABLESPACE {{ conn|qtIdent(data.spcname) }};
{% endif %}
{### Alter SQL for Owner ###}
{% if data.relowner %}

ALTER TABLE IF EXISTS {{conn|qtIdent(data.schema, data.name)}}
OWNER to {{conn|qtIdent(data.relowner)}};
{% endif %}

{#####################################################}
{## Enable Row Level Security Policy on table ##}
{#####################################################}
{% if data.rlspolicy %}
ALTER TABLE IF EXISTS {{conn|qtIdent(data.schema, data.name)}}
ENABLE ROW LEVEL SECURITY;
{% endif %}

{#####################################################}
{## Force Enable Row Level Security Policy on table ##}
{#####################################################}
{% if data.forcerlspolicy %}
ALTER TABLE IF EXISTS {{conn|qtIdent(data.schema, data.name)}}
FORCE ROW LEVEL SECURITY;
{% 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.revoke_all %}
{% for priv in data.revoke_all %}
{{ PRIVILEGE.UNSETALL(conn, "TABLE", priv, data.name, data.schema)}}
{% endfor %}
{% endif %}
{% 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(conn)}};
{% 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(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 %}
{### 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)}}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ CREATE {% if data.relpersistence %}UNLOGGED {% endif %}TABLE{% if add_not_exists

INCLUDING STORAGE{% endif %}{% if data.like_comments %}

INCLUDING COMMENTS{% endif %}{% if data.columns|length > 0 %},
INCLUDING COMMENTS{% endif %}{% if data.like_identity %}

INCLUDING IDENTITY{% endif %}{% if data.like_statistics %}

INCLUDING STATISTICS{% endif %}{% if data.columns|length > 0 %},
{% endif %}

{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ COMMENT ON TABLE public."partition_table_with_collate_$%{}[]()&*^!@""'`\/#"
-- Partitions SQL

CREATE TABLE public.cust_arr_small PARTITION OF public."partition_table_with_collate_$%{}[]()&*^!@""'`\/#"
FOR VALUES FROM ('20') TO ('25');
FOR VALUES FROM ('20') TO ('25')
TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.cust_arr_small
OWNER to postgres;
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ COMMENT ON TABLE public."table_with_hash_patition_$%{}[]()&*^!@\""'`\\/#"
-- Partitions SQL

CREATE TABLE public.cust_part11 PARTITION OF public."table_with_hash_patition_$%{}[]()&*^!@\""'`\\/#"
FOR VALUES WITH (modulus 2, remainder 1);
FOR VALUES WITH (modulus 2, remainder 1)
TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.cust_part11
OWNER to postgres;
Loading

0 comments on commit d7badb0

Please sign in to comment.