fix: use cast when chaninging name type from varchar to bigint in postgres

This commit is contained in:
phot0n 2022-04-30 16:16:41 +05:30
parent 9f0c40dbba
commit cfc905b567
2 changed files with 14 additions and 8 deletions

View file

@ -125,6 +125,9 @@ class DocType(Document):
if self.default_print_format and not self.custom:
frappe.throw(_("Standard DocType cannot have default print format, use Customize Form"))
if self.can_change_name_type:
setup_name_type_and_sequence(self)
def validate_field_name_conflicts(self):
"""Check if field names dont conflict with controller properties and methods"""
core_doctypes = [
@ -172,9 +175,6 @@ class DocType(Document):
)
def after_insert(self):
if self.can_change_name_type:
setup_name_type_and_sequence(self)
# clear user cache so that on the next reload this doctype is included in boot
clear_user_cache(frappe.session.user)
@ -954,10 +954,15 @@ def setup_name_type_and_sequence(dt: DocType) -> None:
def change_name_column_type(doctype_name: str, type: str) -> None:
frappe.db.change_column_type(
doctype_name, "name", type, True if frappe.db.db_type == "mariadb" else False
# postgres requires cast when converting from varchar to bigint
args = (
(doctype_name, "name", type, False, True)
if (frappe.db.db_type == "postgres")
else (doctype_name, "name", type, True)
)
frappe.db.change_column_type(*args)
def validate_links_table_fieldnames(meta):
"""Validate fieldnames in Links table"""

View file

@ -211,18 +211,19 @@ class PostgresDatabase(Database):
)
def change_column_type(
self, doctype: str, column: str, type: str, nullable: bool = False
self, doctype: str, column: str, type: str, nullable: bool = False, use_cast: bool = False
) -> Union[List, Tuple]:
table_name = get_table_name(doctype)
null_constraint = "SET NOT NULL" if not nullable else "DROP NOT NULL"
using_cast = f'using "{column}"::{type}' if use_cast else ""
# postgres allows ddl in transactions but since we've currently made
# things same as mariadb (raising exception on ddl commands if the transaction has any writes),
# hence using sql_ddl here for committing and then moving forward.
return self.sql_ddl(
f"""ALTER TABLE "{table_name}"
ALTER COLUMN "{column}" TYPE {type},
ALTER COLUMN "{column}" {null_constraint}"""
ALTER COLUMN "{column}" TYPE {type} {using_cast},
ALTER COLUMN "{column}" {null_constraint}"""
)
def create_auth_table(self):