diff --git a/docs/en_US/menu_bar.rst b/docs/en_US/menu_bar.rst index 44a0d267897..bc9e7a20375 100644 --- a/docs/en_US/menu_bar.rst +++ b/docs/en_US/menu_bar.rst @@ -81,9 +81,9 @@ following options (in alphabetical order): +-----------------------------+--------------------------------------------------------------------------------------------------------------------------+ | *Trigger(s)* | Click to *Disable* or *Enable* trigger(s) for the currently selected table. Options are displayed on the flyout menu. | +-----------------------------+--------------------------------------------------------------------------------------------------------------------------+ -| *Truncate* | Click to remove all rows from a table (*Truncate*), to remove all rows from a table and its child tables | -| | (*Truncate Cascade*) or to remove all rows from a table and automatically restart sequences owned by columns | -| | (*Truncate Restart Identity*). Options are displayed on the flyout menu. | +| *Truncate* | Click to remove all rows from a table/foreign tables (*Truncate*), to remove all rows from a table/foreign tables and | +| | its child tables (*Truncate Cascade*) or to remove all rows from a table/foreign tables and automatically restart | +| | sequences owned by columns (*Truncate Restart Identity*). Options are displayed on the flyout menu. | +-----------------------------+--------------------------------------------------------------------------------------------------------------------------+ | *View Data* | Click to access a context menu that provides several options for viewing data (see below). | +-----------------------------+--------------------------------------------------------------------------------------------------------------------------+ 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 3cfed8f1087..a520fd32b65 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 @@ -37,6 +37,8 @@ columns import utils as column_utils from pgadmin.browser.server_groups.servers.databases.schemas.tables.\ triggers import utils as trigger_utils +from pgadmin.browser.server_groups.servers.databases.schemas.tables.\ + utils import BaseTableView class ForeignTableModule(SchemaChildModule): @@ -102,7 +104,7 @@ def register(self, app, options): blueprint = ForeignTableModule(__name__) -class ForeignTableView(PGChildNodeView, DataTypeReader, +class ForeignTableView(BaseTableView, DataTypeReader, SchemaDiffObjectCompare): """ class ForeignTableView(PGChildNodeView) @@ -187,6 +189,9 @@ class ForeignTableView(PGChildNodeView) * compare(**kwargs): - This function will compare the foreign table nodes from two different schemas. + + * truncate(gid, sid, scid, tid): + - This function will truncate foreign table object """ node_type = blueprint.node_type @@ -211,6 +216,7 @@ class ForeignTableView(PGChildNodeView) {'get': 'list', 'post': 'create', 'delete': 'delete'} ], 'delete': [{'delete': 'delete'}, {'delete': 'delete'}], + 'truncate': [{'put': 'truncate'}], 'children': [{'get': 'children'}], 'nodes': [{'get': 'node'}, {'get': 'nodes'}], 'sql': [{'get': 'sql'}], @@ -409,6 +415,9 @@ def wrap(*args, **kwargs): self.template_path = \ self.BASE_TEMPLATE_PATH.format(self.manager.version) + self.table_template_path = compile_template_path( + 'tables/sql', self.manager.version) + self.foreign_table_column_template_path = compile_template_path( 'foreign_table_columns/sql', self.manager.version) @@ -884,6 +893,39 @@ def update(self, gid, sid, did, scid, foid): except Exception as e: return internal_server_error(errormsg=str(e)) + @check_precondition + def truncate(self, gid, sid, did, scid, foid): + """ + This function will truncate the foreign table. + + Args: + gid: Server Group ID + sid: Server ID + did: Database ID + scid: Schema ID + foid: Foreign Table ID + """ + + try: + SQL = render_template( + "/".join([self.template_path, self._PROPERTIES_SQL]), + did=did, scid=scid, foid=foid, + datlastsysoid=self._DATABASE_LAST_SYSTEM_OID + ) + status, res = self.conn.execute_dict(SQL) + if not status: + return internal_server_error(errormsg=res) + + if len(res['rows']) == 0: + return gone(gettext(self.not_found_error_msg())) + + return super().truncate( + gid, sid, did, scid, foid, res + ) + + except Exception as e: + return internal_server_error(errormsg=str(e)) + @check_precondition def sql(self, gid, sid, did, scid, foid=None, **kwargs): """ diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/js/foreign_table.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/js/foreign_table.js index f95e69d9185..2a3cdfcc38d 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/js/foreign_table.js +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/js/foreign_table.js @@ -12,6 +12,7 @@ import { getNodePrivilegeRoleSchema } from '../../../../../static/js/privilege.u import ForeignTableSchema from './foreign_table.ui'; import _ from 'lodash'; import Notify from '../../../../../../../../static/js/helpers/Notifier'; +import getApiInstance from '../../../../../../../../static/js/api_instance'; /* Create and Register Foreign Table Collection and Node. */ define('pgadmin.node.foreign_table', ['pgadmin.tables.js/enable_disable_triggers', @@ -72,6 +73,21 @@ define('pgadmin.node.foreign_table', ['pgadmin.tables.js/enable_disable_triggers applies: ['object', 'context'], callback: 'show_obj_properties', category: 'create', priority: 4, label: gettext('Foreign Table...'), data: {action: 'create', check: false}, enable: 'canCreate', + },{ + name: 'truncate_foreign_table', node: 'foreign_table', module: this, + applies: ['object', 'context'], callback: 'truncate_foreign_table', + category: gettext('Truncate'), priority: 3, label: gettext('Truncate'), + enable : 'canCreate', + },{ + name: 'truncate_foreign_table_cascade', node: 'foreign_table', module: this, + applies: ['object', 'context'], callback: 'truncate_foreign_table_cascade', + category: gettext('Truncate'), priority: 3, label: gettext('Truncate Cascade'), + enable : 'canCreate', + },{ + name: 'truncate_foreign_table_identity', node: 'foreign_table', module: this, + applies: ['object', 'context'], callback: 'truncate_foreign_table_identity', + category: gettext('Truncate'), priority: 3, label: gettext('Truncate Restart Identity'), + enable : 'canCreate', },{ // To enable/disable all triggers for the table name: 'enable_all_triggers', node: 'foreign_table', module: this, @@ -112,6 +128,56 @@ define('pgadmin.node.foreign_table', ['pgadmin.tables.js/enable_disable_triggers args ); }, + /* Truncate foreign table */ + truncate_foreign_table: function(args) { + let params = {'cascade': false }; + this.callbacks.truncate.apply(this, [args, params]); + }, + /* Truncate foreign table with cascade */ + truncate_foreign_table_cascade: function(args) { + let params = {'cascade': true }; + this.callbacks.truncate.apply(this, [args, params]); + }, + /* Truncate foreign table with identity */ + truncate_foreign_table_identity: function(args) { + let params = {'identity': true }; + this.callbacks.truncate.apply(this, [args, params]); + }, + truncate: function(args, params) { + let input = args || {}, + obj = this, + t = pgBrowser.tree, + i = input.item || t.selected(), + d = i ? t.itemData(i) : undefined; + + if (!d) + return false; + + pgBrowser.notifier.confirm( + gettext('Truncate Foreign Table'), + gettext('Are you sure you want to truncate foreign table %s?', d.label), + function () { + let data = d; + getApiInstance().put(obj.generate_url(i, 'truncate' , d, true), params) + .then(({data: res})=>{ + if (res.success == 1) { + pgBrowser.notifier.success(res.info); + t.removeIcon(i); + data.icon = data.is_partitioned ? 'icon-partition': 'icon-table'; + t.addIcon(i, {icon: data.icon}); + t.updateAndReselectNode(i, data); + } + if (res.success == 2) { + pgBrowser.notifier.error(res.info); + } + }) + .catch((error)=>{ + pgBrowser.notifier.pgRespErrorNotify(error); + t.refresh(i); + }); + }, function() {/*This is intentional (SonarQube)*/} + ); + }, }, // Check to whether table has disable trigger(s) canCreate_with_trigger_enable: function(itemData) {