diff --git a/frappe/core/doctype/communication/communication.py b/frappe/core/doctype/communication/communication.py index 77ccefba71..4f9f07d803 100644 --- a/frappe/core/doctype/communication/communication.py +++ b/frappe/core/doctype/communication/communication.py @@ -265,8 +265,8 @@ def get_permission_query_conditions_for_communication(user): distinct=True, order_by="idx") if not accounts: - return """tabCommunication.communication_medium!='Email'""" + return """`tabCommunication`.communication_medium!='Email'""" email_accounts = [ '"%s"'%account.get("email_account") for account in accounts ] - return """tabCommunication.email_account in ({email_accounts})"""\ + return """`tabCommunication`.email_account in ({email_accounts})"""\ .format(email_accounts=','.join(email_accounts)) diff --git a/frappe/database/postgres/schema.py b/frappe/database/postgres/schema.py index 05b6b19a9a..b5129b60bb 100644 --- a/frappe/database/postgres/schema.py +++ b/frappe/database/postgres/schema.py @@ -40,7 +40,20 @@ class PostgresTable(DBTable): query.append("ADD COLUMN `{}` {}".format(col.fieldname, col.get_definition())) for col in self.change_type: - query.append("ALTER COLUMN `{}` TYPE {}".format(col.fieldname, get_definition(col.fieldtype, precision=col.precision, length=col.length))) + using_clause = "" + if col.fieldtype in ("Datetime"): + # The USING option of SET DATA TYPE can actually specify any expression + # involving the old values of the row + # read more https://www.postgresql.org/docs/9.1/sql-altertable.html + using_clause = "USING {}::timestamp without time zone".format(col.fieldname) + elif col.fieldtype in ("Check"): + using_clause = "USING {}::smallint".format(col.fieldname) + + query.append("ALTER COLUMN {0} TYPE {1} {2}".format( + col.fieldname, + get_definition(col.fieldtype, precision=col.precision, length=col.length), + using_clause) + ) for col in self.set_default: if col.fieldname=="name": @@ -93,4 +106,4 @@ class PostgresTable(DBTable): fieldname, self.table_name))) raise e else: - raise e \ No newline at end of file + raise e diff --git a/frappe/desk/doctype/todo/todo.py b/frappe/desk/doctype/todo/todo.py index 45c3874806..c9fa96c716 100644 --- a/frappe/desk/doctype/todo/todo.py +++ b/frappe/desk/doctype/todo/todo.py @@ -94,7 +94,7 @@ def get_permission_query_conditions(user): if "System Manager" in frappe.get_roles(user): return None else: - return """(tabToDo.owner = {user} or tabToDo.assigned_by = {user})"""\ + return """(`tabToDo`.owner = {user} or `tabToDo`.assigned_by = {user})"""\ .format(user=frappe.db.escape(user)) def has_permission(doc, user): @@ -108,4 +108,4 @@ def new_todo(description): frappe.get_doc({ 'doctype': 'ToDo', 'description': description - }).insert() \ No newline at end of file + }).insert() diff --git a/frappe/model/__init__.py b/frappe/model/__init__.py index 5f804e3c36..3a8868ec2c 100644 --- a/frappe/model/__init__.py +++ b/frappe/model/__init__.py @@ -76,27 +76,43 @@ def delete_fields(args_dict, delete=0): args_dict = { dt: [field names] } """ import frappe.utils - for dt in list(args_dict): + for dt in args_dict: fields = args_dict[dt] - if not fields: continue + if not fields: + continue - frappe.db.sql("""\ + frappe.db.sql(""" DELETE FROM `tabDocField` - WHERE parent=%s AND fieldname IN (%s) - """ % ('%s', ", ".join(['"' + f + '"' for f in fields])), dt) + WHERE parent='%s' AND fieldname IN (%s) + """ % (dt, ", ".join(["'{}'".format(f) for f in fields]))) - # Delete the data / column only if delete is specified - if not delete: continue + # Delete the data/column only if delete is specified + if not delete: + continue if frappe.db.get_value("DocType", dt, "issingle"): - frappe.db.sql("""\ + frappe.db.sql(""" DELETE FROM `tabSingles` - WHERE doctype=%s AND field IN (%s) - """ % ('%s', ", ".join(['"' + f + '"' for f in fields])), dt) + WHERE doctype='%s' AND field IN (%s) + """ % (dt, ", ".join(["'{}'".format(f) for f in fields]))) else: - existing_fields = frappe.db.sql("desc `tab%s`" % dt) + existing_fields = frappe.db.multisql({ + "mariadb": "DESC `tab%s`" % dt, + "postgres": """ + SELECT + COLUMN_NAME + FROM + information_schema.COLUMNS + WHERE + TABLE_NAME = 'tab%s'; + """ % dt, + }) existing_fields = existing_fields and [e[0] for e in existing_fields] or [] + fields_need_to_delete = set(fields) & set(existing_fields) + if not fields_need_to_delete: + continue query = "ALTER TABLE `tab%s` " % dt + \ - ", ".join(["DROP COLUMN `%s`" % f for f in fields if f in existing_fields]) - frappe.db.commit() + ", ".join(["DROP COLUMN `%s`" % f for f in fields_need_to_delete]) frappe.db.sql(query) + # commit the results to db + frappe.db.commit() diff --git a/frappe/patches/v11_0/set_default_letter_head_source.py b/frappe/patches/v11_0/set_default_letter_head_source.py index 069f4e3d2e..a43ea397e4 100644 --- a/frappe/patches/v11_0/set_default_letter_head_source.py +++ b/frappe/patches/v11_0/set_default_letter_head_source.py @@ -6,4 +6,4 @@ def execute(): frappe.reload_doctype('Letter Head') # source of all existing letter heads must be HTML - frappe.db.sql('update `tabLetter Head` set source = "HTML"') \ No newline at end of file + frappe.db.sql("update `tabLetter Head` set source = 'HTML'") diff --git a/frappe/patches/v12_0/init_desk_settings.py b/frappe/patches/v12_0/init_desk_settings.py index 782ced8a26..31c6cf9207 100644 --- a/frappe/patches/v12_0/init_desk_settings.py +++ b/frappe/patches/v12_0/init_desk_settings.py @@ -8,4 +8,4 @@ from frappe.desk.moduleview import get_onboard_items def execute(): """Reset the initial customizations for desk, with modules, indices and links.""" frappe.reload_doc("core", "doctype", "user") - frappe.db.sql("""update tabUser set home_settings = %s""", (''), debug=True) + frappe.db.sql("""update `tabUser` set home_settings = %s""", (''), debug=True) diff --git a/frappe/patches/v12_0/setup_comments_from_communications.py b/frappe/patches/v12_0/setup_comments_from_communications.py index 1a7a5aef84..b52304bc05 100644 --- a/frappe/patches/v12_0/setup_comments_from_communications.py +++ b/frappe/patches/v12_0/setup_comments_from_communications.py @@ -25,4 +25,4 @@ def execute(): new_comment.db_insert() # clean up - frappe.db.sql('delete from tabCommunication where communication_type = "Comment"') \ No newline at end of file + frappe.db.sql("delete from `tabCommunication` where communication_type = 'Comment'")