Merge pull request #7419 from josejibin/fix/postgres-compatible

fix(postgres): Make db queries postgres compatible
This commit is contained in:
Rushabh Mehta 2019-05-20 18:02:30 +05:30 committed by GitHub
commit 28ed21b94c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 55 additions and 26 deletions

View file

@ -309,10 +309,10 @@ 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))
def get_contacts(email_strings):

View file

@ -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
raise e

View file

@ -97,7 +97,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):
@ -111,4 +111,4 @@ def new_todo(description):
frappe.get_doc({
'doctype': 'ToDo',
'description': description
}).insert()
}).insert()

View file

@ -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()

View file

@ -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"')
frappe.db.sql("update `tabLetter Head` set source = 'HTML'")

View file

@ -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)

View file

@ -25,4 +25,4 @@ def execute():
new_comment.db_insert()
# clean up
frappe.db.sql('delete from tabCommunication where communication_type = "Comment"')
frappe.db.sql("delete from `tabCommunication` where communication_type = 'Comment'")

View file

@ -3,11 +3,11 @@ import frappe
def execute():
frappe.db.sql('''
UPDATE `tabPrint Format`
SET `print_format_type` = "Jinja"
WHERE `print_format_type` in ("Server", "Client")
SET `print_format_type` = 'Jinja'
WHERE `print_format_type` in ('Server', 'Client')
''')
frappe.db.sql('''
UPDATE `tabPrint Format`
SET `print_format_type` = "JS"
WHERE `print_format_type` = "Js"
SET `print_format_type` = 'JS'
WHERE `print_format_type` = 'Js'
''')