diff --git a/web/migrations/versions/84700139beb0_.py b/web/migrations/versions/84700139beb0_.py new file mode 100644 index 0000000..455fd3d --- /dev/null +++ b/web/migrations/versions/84700139beb0_.py @@ -0,0 +1,32 @@ + +"""empty message + +Revision ID: 84700139beb0 +Revises: d39482714a2e +Create Date: 2020-06-24 15:53:56.489518 + +""" +from pgadmin.model import db + + +# revision identifiers, used by Alembic. +revision = '84700139beb0' +down_revision = 'd39482714a2e' +branch_labels = None +depends_on = None + + +def upgrade(): + db.engine.execute(""" + CREATE TABLE "database" ( + "id" INTEGER NOT NULL, + "schema_res" TEXT, + "server" INTEGER NOT NULL, + PRIMARY KEY("id","server"), + FOREIGN KEY("server") REFERENCES "server"("id") + ); + """) + + +def downgrade(): + pass diff --git a/web/pgadmin/browser/server_groups/servers/__init__.py b/web/pgadmin/browser/server_groups/servers/__init__.py index 2745e5c..ca6cb8b 100644 --- a/web/pgadmin/browser/server_groups/servers/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/__init__.py @@ -536,7 +536,7 @@ class ServerNode(PGChildNodeView): if connected: for arg in ( - 'host', 'hostaddr', 'port', 'db', 'username', 'sslmode', + 'hostaddr', 'db', 'sslmode', 'role', 'service' ): if arg in data: @@ -1016,6 +1016,7 @@ class ServerNode(PGChildNodeView): # Connect the Server manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid) + manager.update(server) conn = manager.connection() # Get enc key diff --git a/web/pgadmin/browser/server_groups/servers/databases/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/__init__.py index 9602914..c9310f5 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/__init__.py @@ -32,7 +32,7 @@ from pgadmin.utils.driver import get_driver from pgadmin.tools.sqleditor.utils.query_history import QueryHistory from pgadmin.tools.schema_diff.node_registry import SchemaDiffRegistry -from pgadmin.model import Server +from pgadmin.model import db, Server, Database class DatabaseModule(CollectionNodeModule): @@ -423,6 +423,11 @@ class DatabaseView(PGChildNodeView): ) status, res1 = self.conn.execute_dict(SQL) + database = Database.query.filter_by(id=did, server=sid).first() + + if database: + result['schema_res'] = database.schema_res.split( + ',') if database.schema_res else [] if not status: return internal_server_error(errormsg=res1) @@ -611,6 +616,11 @@ class DatabaseView(PGChildNodeView): return internal_server_error(errormsg=res) response = res['rows'][0] + # Add database entry into database table with schema_restrictions. + database = Database(id=response['did'], server=sid, + schema_res=','.join(data['schema_res'])) + db.session.add(database) + db.session.commit() return jsonify( node=self.blueprint.generate_browser_node( @@ -635,6 +645,17 @@ class DatabaseView(PGChildNodeView): request.data, encoding='utf-8' ) + database = Database.query.filter_by(id=did, server=sid).first() + + if 'schema_res' in data: + if database: + data['schema_res'] = ','.join(data['schema_res']) + setattr(database, 'schema_res', data['schema_res']) + else: + database_obj = Database(id=did, server=sid, + schema_res=','.join(data['schema_res'])) + db.session.add(database_obj) + # Generic connection for offline updates conn = self.manager.connection(conn_id='db_offline_update') status, errmsg = conn.connect() @@ -737,6 +758,15 @@ class DatabaseView(PGChildNodeView): if self.manager.db == res['name']: canDrop = canDisConn = False + try: + db.session.commit() + except Exception as e: + current_app.logger.exception(e) + return make_json_response( + success=0, + errormsg=e.message + ) + return jsonify( node=self.blueprint.generate_browser_node( did, diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/__init__.py index e3c32e3..9be9fbd 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/__init__.py @@ -24,6 +24,7 @@ from pgadmin.utils.ajax import make_json_response, internal_server_error, \ make_response as ajax_response, gone, bad_request from pgadmin.utils.driver import get_driver from pgadmin.tools.schema_diff.node_registry import SchemaDiffRegistry +from pgadmin.model import Database """ This module is responsible for generating two nodes @@ -384,10 +385,21 @@ class SchemaView(PGChildNodeView): Returns: JSON of available schema nodes """ + database = Database.query.filter_by(id=did, server=sid).first() + param = None + if database: + schema_restrictions = database.schema_res + + if schema_restrictions: + schema_res = ",".join( + ["'%s'"] * len(schema_restrictions.split(','))) + param = schema_res % (tuple(schema_restrictions.split(','))) + SQL = render_template( "/".join([self.template_path, 'sql/properties.sql']), _=gettext, - show_sysobj=self.blueprint.show_system_objects + show_sysobj=self.blueprint.show_system_objects, + schema_restrictions=param ) status, res = self.conn.execute_dict(SQL) @@ -413,11 +425,22 @@ class SchemaView(PGChildNodeView): JSON of available schema child nodes """ res = [] + database = Database.query.filter_by(id=did, server=sid).first() + param = None + if database: + schema_restrictions = database.schema_res + + if schema_restrictions: + schema_res = ",".join( + ["'%s'"] * len(schema_restrictions.split(','))) + param = schema_res % (tuple(schema_restrictions.split(','))) + SQL = render_template( "/".join([self.template_path, 'sql/nodes.sql']), show_sysobj=self.blueprint.show_system_objects, _=gettext, - scid=scid + scid=scid, + schema_restrictions=param ) status, rset = self.conn.execute_2darray(SQL) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/templates/schemas/pg/9.2_plus/sql/nodes.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/templates/schemas/pg/9.2_plus/sql/nodes.sql index d1a19d1..3cf6647 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/templates/schemas/pg/9.2_plus/sql/nodes.sql +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/templates/schemas/pg/9.2_plus/sql/nodes.sql @@ -17,4 +17,10 @@ WHERE NOT ( {{ CATALOGS.LIST('nsp') }} ) + + {% if schema_restrictions %} + AND + nsp.nspname in ({{schema_restrictions}}) + {% endif %} + ORDER BY nspname; diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/templates/schemas/pg/9.2_plus/sql/properties.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/templates/schemas/pg/9.2_plus/sql/properties.sql index e07626d..f340623 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/templates/schemas/pg/9.2_plus/sql/properties.sql +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/templates/schemas/pg/9.2_plus/sql/properties.sql @@ -50,4 +50,8 @@ WHERE NOT ( {{ CATALOGS.LIST('nsp') }} ) + {% if schema_restrictions %} + AND + nsp.nspname in ({{schema_restrictions}}) + {% endif %} ORDER BY 1, nspname; diff --git a/web/pgadmin/browser/server_groups/servers/databases/static/js/database.js b/web/pgadmin/browser/server_groups/servers/databases/static/js/database.js index affa368..3091a1a 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/static/js/database.js +++ b/web/pgadmin/browser/server_groups/servers/databases/static/js/database.js @@ -46,6 +46,7 @@ define('pgadmin.node.database', [ node_image: function() { return 'pg-icon-database'; }, + width: '700px', Init: function() { /* Avoid mulitple registration of menus */ if (this.initialized) @@ -297,6 +298,7 @@ define('pgadmin.node.database', [ defseqacl: [], is_template: false, deftypeacl: [], + schema_res:'', }, // Default values! @@ -310,7 +312,8 @@ define('pgadmin.node.database', [ pgBrowser.Node.Model.prototype.initialize.apply(this, arguments); }, - schema: [{ + schema: [ + { id: 'name', label: gettext('Database'), cell: 'string', editable: false, type: 'text', },{ @@ -453,7 +456,45 @@ define('pgadmin.node.database', [ mode: ['edit', 'create'], min_version: 90200, }, ], + },{ + type: 'collection', group: gettext('Advanced'), }, + { + id: 'schema_res', label: gettext('Schema restriction'), + type: 'select2', group: gettext('Advanced'), + mode: ['properties', 'edit', 'create'], + select2: { + multiple: true, allowClear: false, tags: true, + tokenSeparators: [','], first_empty: false, + selectOnClose: true, emptyOptions: true + }, + control: Backform.Select2Control.extend({ + onChange: function() { + Backform.Select2Control.prototype.onChange.apply(this, arguments); + if (!this.model || !( + this.model.changed && + this.model.get('oid') !== undefined + )) { + this.model.inform_text = undefined; + return; + } + + if(this.model.origSessAttrs.schema_res != this.model.changed.schema_res) + { + this.model.inform_text = gettext( + 'Please refresh the Schemas node to make changes to the schema restriction take effect.' + ) + } else { + this.model.inform_text = undefined; + } + } + }) + }, + { + id: 'note', label: gettext('Note: Changes to the schema restriction will require the Schemas node in the browser to be refreshed before they will be shown.'), + group: gettext('Advanced'), type: 'help', + mode: ['edit'], + } ], validate: function() { var name = this.get('name'); diff --git a/web/pgadmin/browser/server_groups/servers/databases/tests/test_db_put.py b/web/pgadmin/browser/server_groups/servers/databases/tests/test_db_put.py index ae861b1..0a40d79 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/tests/test_db_put.py +++ b/web/pgadmin/browser/server_groups/servers/databases/tests/test_db_put.py @@ -41,7 +41,8 @@ class DatabasesUpdateTestCase(BaseTestGenerator): try: data = { "comments": "This is db update comment", - "id": self.db_id + "id": self.db_id, + "schema_res": ["public"] } response = self.tester.put( self.url + str(utils.SERVER_GROUP) + '/' + str( diff --git a/web/pgadmin/browser/server_groups/servers/databases/tests/utils.py b/web/pgadmin/browser/server_groups/servers/databases/tests/utils.py index 8eeedf7..f44a81d 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/tests/utils.py +++ b/web/pgadmin/browser/server_groups/servers/databases/tests/utils.py @@ -73,7 +73,8 @@ def get_db_data(db_owner): "name": "db_add_%s" % str(uuid.uuid4())[1: 8], "privileges": [], "securities": [], - "variables": [] + "variables": [], + "schema_res": ["public", "sample"] } return data diff --git a/web/pgadmin/browser/server_groups/servers/static/js/server.js b/web/pgadmin/browser/server_groups/servers/static/js/server.js index 240154c..d55c13d 100644 --- a/web/pgadmin/browser/server_groups/servers/static/js/server.js +++ b/web/pgadmin/browser/server_groups/servers/static/js/server.js @@ -785,16 +785,70 @@ define('pgadmin.node.server', [ mode: ['properties', 'edit', 'create'], },{ id: 'host', label: gettext('Host name/address'), type: 'text', group: gettext('Connection'), - mode: ['properties', 'edit', 'create'], readonly: 'isConnected', + mode: ['properties', 'edit', 'create'], + control: Backform.InputControl.extend({ + onChange: function() { + Backform.InputControl.prototype.onChange.apply(this, arguments); + if (!this.model || !this.model.changed) { + this.model.inform_text = undefined; + return; + } + + if(this.model.origSessAttrs.host != this.model.changed.host && !this.model.isNew()) + { + this.model.inform_text = gettext( + 'To apply changes to the connection configuration, please disconnect from the server and then reconnect.' + ) + } else { + this.model.inform_text = undefined; + } + } + }) },{ id: 'port', label: gettext('Port'), type: 'int', group: gettext('Connection'), - mode: ['properties', 'edit', 'create'], readonly: 'isConnected', min: 1, max: 65535, + mode: ['properties', 'edit', 'create'], min: 1, max: 65535, + control: Backform.InputControl.extend({ + onChange: function() { + Backform.InputControl.prototype.onChange.apply(this, arguments); + if (!this.model || !this.model.changed) { + this.model.inform_text = undefined; + return; + } + + if(this.model.origSessAttrs.port != this.model.changed.port && !this.model.isNew()) + { + this.model.inform_text = gettext( + 'To apply changes to the connection configuration, please disconnect from the server and then reconnect.' + ) + } else { + this.model.inform_text = undefined; + } + } + }) },{ id: 'db', label: gettext('Maintenance database'), type: 'text', group: gettext('Connection'), - mode: ['properties', 'edit', 'create'], readonly: 'isConnected', + mode: ['properties', 'edit', 'create'], readonly: 'isConnected' },{ id: 'username', label: gettext('Username'), type: 'text', group: gettext('Connection'), - mode: ['properties', 'edit', 'create'], readonly: 'isConnected', + mode: ['properties', 'edit', 'create'], + control: Backform.InputControl.extend({ + onChange: function() { + Backform.InputControl.prototype.onChange.apply(this, arguments); + if (!this.model || !this.model.changed) { + this.model.inform_text = undefined; + return; + } + + if(this.model.origSessAttrs.username != this.model.changed.username && !this.model.isNew()) + { + this.model.inform_text = gettext( + 'To apply changes to the connection configuration, please disconnect from the server and then reconnect.' + ) + } else { + this.model.inform_text = undefined; + } + } + }) },{ id: 'password', label: gettext('Password'), type: 'password', maxlength: '2000', group: gettext('Connection'), control: 'input', mode: ['create'], deps: ['connect_now'], diff --git a/web/pgadmin/browser/static/js/node.js b/web/pgadmin/browser/static/js/node.js index 7828b30..2776490 100644 --- a/web/pgadmin/browser/static/js/node.js +++ b/web/pgadmin/browser/static/js/node.js @@ -1339,6 +1339,22 @@ define('pgadmin.browser.node', [ } }.bind(panel), + informBeforeAttributeChange = function(ok_callback) { + var j = this.$container.find('.obj_properties').first(), + view = j && j.data('obj-view'), + self = this; + + if (view && view.model && !_.isUndefined(view.model.inform_text) && !_.isNull(view.model.inform_text)) { + Alertify.alert( + gettext('Warning'), + gettext(view.model.inform_text) + ); + + } + ok_callback(); + return true; + }.bind(panel), + onSave = function(view, saveBtn) { var m = view.model, d = m.toJSON(true), @@ -1535,9 +1551,11 @@ define('pgadmin.browser.node', [ warnBeforeAttributeChange.call( panel, function() { - setTimeout(function() { - onSave.call(this, view, btn); - }, 0); + informBeforeAttributeChange.call(panel, function(){ + setTimeout(function() { + onSave.call(this, view, btn); + }, 0); + }) } ); }); diff --git a/web/pgadmin/model/__init__.py b/web/pgadmin/model/__init__.py index ab98583..03681e1 100644 --- a/web/pgadmin/model/__init__.py +++ b/web/pgadmin/model/__init__.py @@ -290,3 +290,18 @@ class QueryHistoryModel(db.Model): dbname = db.Column(db.String(), nullable=False, primary_key=True) query_info = db.Column(db.String(), nullable=False) last_updated_flag = db.Column(db.String(), nullable=False) + + +class Database(db.Model): + """ + Define a Database. + """ + __tablename__ = 'database' + id = db.Column(db.Integer, primary_key=True) + schema_res = db.Column(db.String(256), nullable=True) + server = db.Column( + db.Integer, + db.ForeignKey('server.id'), + nullable=False, + primary_key=True + )