From d4e5d9c4f479eec8a0f551c505a7a3b361af9e3b Mon Sep 17 00:00:00 2001 From: Murtuza Zabuawala Date: Wed, 15 Jun 2016 13:21:01 +0100 Subject: [PATCH] =?UTF-8?q?Add=20"Move=20objects..=E2=80=9D=20functionalit?= =?UTF-8?q?y=20in=20tablespace=20node.=20Fixes=20#1286?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../servers/tablespaces/__init__.py | 39 ++++++++++- .../templates/tablespaces/js/tablespaces.js | 70 ++++++++++++++++--- .../tablespaces/sql/9.1_plus/move_objects.sql | 6 +- .../tablespaces/sql/9.2_plus/move_objects.sql | 6 +- 4 files changed, 106 insertions(+), 15 deletions(-) diff --git a/web/pgadmin/browser/server_groups/servers/tablespaces/__init__.py b/web/pgadmin/browser/server_groups/servers/tablespaces/__init__.py index 31beeb448..9c087b468 100644 --- a/web/pgadmin/browser/server_groups/servers/tablespaces/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/tablespaces/__init__.py @@ -74,7 +74,8 @@ class TablespaceView(PGChildNodeView): 'dependent': [{'get': 'dependents'}], 'module.js': [{}, {}, {'get': 'module_js'}], 'vopts': [{}, {'get': 'variable_options'}], - 'move_objects': [{'put': 'move_objects'}] + 'move_objects': [{'put': 'move_objects'}], + 'move_objects_sql': [{'get': 'move_objects_sql'}], }) def module_js(self): @@ -744,4 +745,40 @@ class TablespaceView(PGChildNodeView): current_app.logger.exception(e) return internal_server_error(errormsg=str(e)) + @check_precondition + def move_objects_sql(self, gid, sid, tsid): + """ + This function returns sql for Move Objects.. dialog + + Args: + gid: Server Group ID + sid: Server ID + tsid: Tablespace ID + """ + required_args = ['old_tblspc', 'tblspc', 'obj_type'] + + data = dict() + for k, v in request.args.items(): + try: + data[k] = json.loads(v) + except ValueError as ve: + current_app.logger.exception(ve) + data[k] = v + + for arg in required_args: + if arg not in data: + return make_json_response( + data=gettext("-- Incomplete definition"), + status=200 + ) + + sql = render_template("/".join( + [self.template_path, 'move_objects.sql']), + data=data, conn=self.conn + ) + + return make_json_response( + data=sql.strip('\n'), + status=200 + ) TablespaceView.register_node_view(blueprint) diff --git a/web/pgadmin/browser/server_groups/servers/tablespaces/templates/tablespaces/js/tablespaces.js b/web/pgadmin/browser/server_groups/servers/tablespaces/templates/tablespaces/js/tablespaces.js index 1d481d812..eecdc886c 100644 --- a/web/pgadmin/browser/server_groups/servers/tablespaces/templates/tablespaces/js/tablespaces.js +++ b/web/pgadmin/browser/server_groups/servers/tablespaces/templates/tablespaces/js/tablespaces.js @@ -54,7 +54,7 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { category: 'move_tablespace', priority: 5, label: '{{ _('Move objects to...') }}', icon: 'fa fa-exchange', data: {action: 'create'}, - enable: 'can_create_tablespace' + enable: 'can_move_objects' } ]); }, @@ -64,6 +64,14 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { return server.connected && server.user.is_superuser; }, + can_move_objects: function(node, item) { + var treeData = this.getTreeNodeHierarchy(item), + server = treeData['server']; + // Only supported PG9.4 and above version + return server.connected && + server.user.is_superuser && + server.version >= 90400; + }, callbacks: { /* Move objects from one tablespace to another */ move_objects: function(args){ @@ -73,7 +81,8 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { i = input.item || t.selected(), d = i && i.length == 1 ? t.itemData(i) : undefined, node = d && pgBrowser.Nodes[d._type], - url = obj.generate_url(i, 'move_objects', d, true); + url = obj.generate_url(i, 'move_objects', d, true), + msql_url = obj.generate_url(i, 'move_objects_sql', d, true); if (!d) return false; @@ -107,6 +116,41 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { id: 'user', label: '{{ _('Object owner') }}', type: 'text', disabled: false, control: 'node-list-by-name', node: 'role', select2: {allowClear: false} + },{ + id: 'sqltab', label: '{{ _('SQL') }}', group: '{{ _('SQL') }}', + type: 'text', disabled: false, control: Backform.SqlTabControl.extend({ + initialize: function() { + // Initialize parent class + Backform.SqlTabControl.prototype.initialize.apply(this, arguments); + }, + onTabChange: function(obj) { + // Fetch the information only if the SQL tab is visible at the moment. + if (this.dialog && obj.shown == this.tabIndex) { + var self = this, + args = self.model.toJSON(); + // Add existing tablespace + args.old_tblspc = d.label; + + // Fetches modified SQL + $.ajax({ + url: msql_url, + type: 'GET', + cache: false, + data: args, + dataType: "json", + contentType: "application/json" + }).done(function(res) { + self.sqlCtrl.clearHistory(); + self.sqlCtrl.setValue(res.data); + self.sqlCtrl.refresh(); + }).fail(function() { + self.model.trigger('pgadmin-view:msql:error'); + }).always(function() { + self.model.trigger('pgadmin-view:msql:fetched'); + }); + } + } + }) }], validate: function() { return null; @@ -123,7 +167,11 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { setup:function() { return { buttons: [{ - text: '{{ _('Ok') }}', key: 27, className: 'btn btn-primary fa fa-lg fa-save pg-alertify-button' + text: '', key: 27, className: 'btn btn-default pull-left fa fa-lg fa-question', + attrs:{name:'dialog_help', type:'button', label: '{{ _('Users') }}', + url: '{{ url_for('help.static', filename='move_objects.html') }}'} + },{ + text: '{{ _('OK') }}', key: 27, className: 'btn btn-primary fa fa-lg fa-save pg-alertify-button' },{ text: '{{ _('Cancel') }}', key: 27, className: 'btn btn-danger fa fa-lg fa-times pg-alertify-button' }], @@ -132,7 +180,7 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { //disable both padding and overflow control. padding : !1, overflow: !1, - model: 0, + modal: false, resizable: true, maximizable: true, pinnable: false, @@ -153,7 +201,7 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { var self = this, $container = $("
"); //Disbale Okay button - this.__internal.buttons[0].element.disabled = true; + this.__internal.buttons[1].element.disabled = true; // Find current/selected node var t = pgBrowser.tree, i = t.selected(), @@ -188,16 +236,22 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { this.view.model.on('change', function() { if (!_.isUndefined(this.get('tblspc')) && this.get('tblspc') !== '') { this.errorModel.clear(); - self.__internal.buttons[0].element.disabled = false; + self.__internal.buttons[1].element.disabled = false; } else { - self.__internal.buttons[0].element.disabled = true; + self.__internal.buttons[1].element.disabled = true; this.errorModel.set('tblspc', '{{ _('Please select tablespace') }}') } }); }, // Callback functions when click on the buttons of the Alertify dialogs callback: function(e) { - if (e.button.text === '{{ _('Ok') }}') { + if (e.button.element.name == "dialog_help") { + e.cancel = true; + pgBrowser.showHelp(e.button.element.name, e.button.element.getAttribute('url'), + null, null, e.button.element.getAttribute('label')); + return; + } + if (e.button.text === '{{ _('OK') }}') { var self = this, args = this.view.model.toJSON(); args.old_tblspc = d.label; diff --git a/web/pgadmin/browser/server_groups/servers/tablespaces/templates/tablespaces/sql/9.1_plus/move_objects.sql b/web/pgadmin/browser/server_groups/servers/tablespaces/templates/tablespaces/sql/9.1_plus/move_objects.sql index 3eaee1146..09228514a 100644 --- a/web/pgadmin/browser/server_groups/servers/tablespaces/templates/tablespaces/sql/9.1_plus/move_objects.sql +++ b/web/pgadmin/browser/server_groups/servers/tablespaces/templates/tablespaces/sql/9.1_plus/move_objects.sql @@ -3,19 +3,19 @@ {% if data.obj_type == 'all' or data.obj_type == 'tables' %} ALTER TABLE ALL IN TABLESPACE {{ conn|qtIdent(data.old_tblspc) }} {% if data.user %} OWNED BY {{ conn|qtIdent(data.user) }}{% endif %} - SET TABLESPACE {{ conn|qtIdent(data.tblspc) }}; + SET TABLESPACE {{ conn|qtIdent(data.tblspc) }}; {% endif %} {% if data.obj_type == 'all' or data.obj_type == 'indexes' %} ALTER INDEX ALL IN TABLESPACE {{ conn|qtIdent(data.old_tblspc) }} {% if data.user %} OWNED BY {{ conn|qtIdent(data.user) }}{% endif %} - SET TABLESPACE {{ conn|qtIdent(data.tblspc) }}; + SET TABLESPACE {{ conn|qtIdent(data.tblspc) }}; {% endif %} {% if data.obj_type == 'all' or data.obj_type == 'materialized_views' %} ALTER MATERIALIZED VIEW ALL IN TABLESPACE {{ conn|qtIdent(data.old_tblspc) }} {% if data.user %} OWNED BY {{ conn|qtIdent(data.user) }}{% endif %} - SET TABLESPACE {{ conn|qtIdent(data.tblspc) }}; + SET TABLESPACE {{ conn|qtIdent(data.tblspc) }}; {% endif %} {% endif %} \ No newline at end of file diff --git a/web/pgadmin/browser/server_groups/servers/tablespaces/templates/tablespaces/sql/9.2_plus/move_objects.sql b/web/pgadmin/browser/server_groups/servers/tablespaces/templates/tablespaces/sql/9.2_plus/move_objects.sql index 3eaee1146..9ad088e87 100644 --- a/web/pgadmin/browser/server_groups/servers/tablespaces/templates/tablespaces/sql/9.2_plus/move_objects.sql +++ b/web/pgadmin/browser/server_groups/servers/tablespaces/templates/tablespaces/sql/9.2_plus/move_objects.sql @@ -3,19 +3,19 @@ {% if data.obj_type == 'all' or data.obj_type == 'tables' %} ALTER TABLE ALL IN TABLESPACE {{ conn|qtIdent(data.old_tblspc) }} {% if data.user %} OWNED BY {{ conn|qtIdent(data.user) }}{% endif %} - SET TABLESPACE {{ conn|qtIdent(data.tblspc) }}; + SET TABLESPACE {{ conn|qtIdent(data.tblspc) }}; {% endif %} {% if data.obj_type == 'all' or data.obj_type == 'indexes' %} ALTER INDEX ALL IN TABLESPACE {{ conn|qtIdent(data.old_tblspc) }} {% if data.user %} OWNED BY {{ conn|qtIdent(data.user) }}{% endif %} - SET TABLESPACE {{ conn|qtIdent(data.tblspc) }}; + SET TABLESPACE {{ conn|qtIdent(data.tblspc) }}; {% endif %} {% if data.obj_type == 'all' or data.obj_type == 'materialized_views' %} ALTER MATERIALIZED VIEW ALL IN TABLESPACE {{ conn|qtIdent(data.old_tblspc) }} {% if data.user %} OWNED BY {{ conn|qtIdent(data.user) }}{% endif %} - SET TABLESPACE {{ conn|qtIdent(data.tblspc) }}; + SET TABLESPACE {{ conn|qtIdent(data.tblspc) }}; {% endif %} {% endif %} \ No newline at end of file