diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_table_column_update.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_table_column_update.py new file mode 100644 index 0000000..9c59c1e --- /dev/null +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_table_column_update.py @@ -0,0 +1,90 @@ +########################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2018, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +########################################################################## + +import json +import uuid + +from pgadmin.browser.server_groups.servers.databases.schemas.tests import \ + utils as schema_utils +from pgadmin.browser.server_groups.servers.databases.tests import utils as \ + database_utils +from pgadmin.utils.route import BaseTestGenerator +from regression import parent_node_dict +from regression.python_test_utils import test_utils as utils +from . import utils as tables_utils + + +class TableNotNullUpdateTestCase(BaseTestGenerator): + """This class will add new collation under schema node.""" + scenarios = [ + ('Update Table with not null field', dict(url='/browser/table/obj/')), + ] + + def setUp(self): + self.db_name = parent_node_dict["database"][-1]["db_name"] + schema_info = parent_node_dict["schema"][-1] + self.server_id = schema_info["server_id"] + self.db_id = schema_info["db_id"] + db_con = database_utils.connect_database( + self, utils.SERVER_GROUP, self.server_id, self.db_id + ) + if not db_con['data']["connected"]: + raise Exception("Could not connect to database to add a table.") + self.schema_id = schema_info["schema_id"] + self.schema_name = schema_info["schema_name"] + schema_response = schema_utils.verify_schemas(self.server, + self.db_name, + self.schema_name) + if not schema_response: + raise Exception("Could not find the schema to add a table.") + + self.table_name = "test_table_column_put_%s" % (str(uuid.uuid4())[1:8]) + + custom_sql = 'column_1 "char" NOT NULL' + + self.table_id = tables_utils.create_table( + self.server, + self.db_name, + self.schema_name, + self.table_name, + custom_sql + ) + + def runTest(self): + """This function will fetch added table under schema node.""" + table_response = tables_utils.verify_table( + self.server, self.db_name, self.table_id + ) + if not table_response: + raise Exception("Could not find the table to update.") + + data = { + "id": self.table_id, + "columns": { + "changed": [ + { + "attnum": 1, + "attnotnull": False + } + ] + } + } + + response = self.tester.put( + self.url + str(utils.SERVER_GROUP) + '/' + + str(self.server_id) + '/' + str(self.db_id) + '/' + + str(self.schema_id) + '/' + str(self.table_id), + data=json.dumps(data), follow_redirects=True + ) + + self.assertEquals(response.status_code, 200) + + def tearDown(self): + # Disconnect the database + database_utils.disconnect_database(self, self.server_id, self.db_id) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/utils.py index 4cec323..b722147 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/utils.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/utils.py @@ -15,7 +15,8 @@ import traceback from regression.python_test_utils import test_utils as utils -def create_table(server, db_name, schema_name, table_name): +def create_table(server, db_name, schema_name, table_name, + custom_column_sql=None): """ This function creates a table under provided schema. :param server: server details @@ -39,9 +40,13 @@ def create_table(server, db_name, schema_name, table_name): old_isolation_level = connection.isolation_level connection.set_isolation_level(0) pg_cursor = connection.cursor() - query = "CREATE TABLE %s.%s(id serial UNIQUE NOT NULL, name text," \ - " location text)" %\ - (schema_name, table_name) + if custom_column_sql: + query = "CREATE TABLE %s.%s(%s)" % \ + (schema_name, table_name, custom_column_sql) + else: + query = "CREATE TABLE %s.%s(id serial UNIQUE NOT NULL, " \ + "name text, location text)" % \ + (schema_name, table_name) pg_cursor.execute(query) connection.set_isolation_level(old_isolation_level) connection.commit() diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/utils.py index 45403d1..36b6a3b 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/utils.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/utils.py @@ -1779,8 +1779,18 @@ class BaseTableView(PGChildNodeView): ) or matchObj.group(1) c['attprecision'] = None else: - c['attlen'] = None - c['attprecision'] = None + # Use the old values to avoid unnecessary + # sql generation + if 'attlen' in old_data: + if old_data['attlen'] != '-1': + c['attlen'] = old_data.get( + 'attlen', None + ) + if 'attprecision' in old_data: + if old_data['attprecision'] != '-1': + c['attprecision'] = old_data.get( + 'attprecision', None + ) if 'cltype' in c: typename = c['cltype']