* fix: misc fixes local.x gets resetted on every request so switched to a simple dict simplified is_val_used in set_next_val function for sequences * chore: use multisql for sequence methods * fix: fields not updating on form * minor(base_input): removed unnecessary branching in update_input * chore: remove prints and rename autoincremented_status_map * chore: added proper type hint + comment + formatting * fix: added searching in cast_name rather than handling it manually * fix: share condition query + test_build_match_conditions * fix: add cast_name to more places * test: test for sequence * fix: sequence functions * fix: inherit frappetestcase * minor: attach sequence methods to db context local * chore: update sequence function names in Database use frappe.db for sequences in naming.py * fix: convert filename to str (for autoincremented doctypes) * chore: better regex for modifying values for postgres * minor: allow changing name column type (if no data is present in the doctype) * refactor: validate_autoname converted it to a simple function enabled changing autoincrement autoname from customize form * fix: use sql_ddl for change_column_type in postgres * fix: use not null constraint in postgres when changing name type * fix(test): updated test_autoincremented_doctype_transition with transitioning when no data is present * fix(test): updated test_cast_name probably messed up during rebase * fix(test): used rollback upon error in transaction for postgres * chore: use frappe.db.x methods for sequences * minor: use temporary sequences in test * minor: use generate_hash for sequence naming in sequence tests * chore: replace sequence imports with frappe.db.x * chore: move out casting name fields to a separate method * refactor: cast_name more explicit cases for casts and added docstring * fix: added space in test_cast_name * chore: fix linter * chore: better naming for can_change_name_column_type * chore: add comment for autoincremented_site_status_map * chore: update/add docstrings
79 lines
1.7 KiB
Python
79 lines
1.7 KiB
Python
from frappe import db, scrub
|
|
|
|
|
|
def create_sequence(
|
|
doctype_name: str,
|
|
*,
|
|
slug: str = "_id_seq",
|
|
temporary=False,
|
|
check_not_exists: bool = False,
|
|
cycle: bool = False,
|
|
cache: int = 0,
|
|
start_value: int = 0,
|
|
increment_by: int = 0,
|
|
min_value: int = 0,
|
|
max_value: int = 0,
|
|
) -> str:
|
|
|
|
query = "create sequence" if not temporary else "create temporary sequence"
|
|
sequence_name = scrub(doctype_name + slug)
|
|
|
|
if check_not_exists:
|
|
query += " if not exists"
|
|
|
|
query += f" {sequence_name}"
|
|
|
|
if increment_by:
|
|
# default is 1
|
|
query += f" increment by {increment_by}"
|
|
|
|
if min_value:
|
|
# default is 1
|
|
query += f" minvalue {min_value}"
|
|
|
|
if max_value:
|
|
query += f" maxvalue {max_value}"
|
|
|
|
if start_value:
|
|
# default is 1
|
|
query += f" start {start_value}"
|
|
|
|
# in postgres, the default is cache 1 / no cache
|
|
if cache:
|
|
query += f" cache {cache}"
|
|
elif db.db_type == "mariadb":
|
|
query += " nocache"
|
|
|
|
if not cycle:
|
|
# in postgres, default is no cycle
|
|
if db.db_type == "mariadb":
|
|
query += " nocycle"
|
|
else:
|
|
query += " cycle"
|
|
|
|
db.sql(query)
|
|
|
|
return sequence_name
|
|
|
|
|
|
def get_next_val(doctype_name: str, slug: str = "_id_seq") -> int:
|
|
return db.multisql(
|
|
{
|
|
"postgres": f"select nextval('\"{scrub(doctype_name + slug)}\"')",
|
|
"mariadb": f"select nextval(`{scrub(doctype_name + slug)}`)",
|
|
}
|
|
)[0][0]
|
|
|
|
|
|
def set_next_val(
|
|
doctype_name: str, next_val: int, *, slug: str = "_id_seq", is_val_used: bool = False
|
|
) -> None:
|
|
|
|
is_val_used = "false" if not is_val_used else "true"
|
|
|
|
db.multisql(
|
|
{
|
|
"postgres": f"SELECT SETVAL('\"{scrub(doctype_name + slug)}\"', {next_val}, {is_val_used})",
|
|
"mariadb": f"SELECT SETVAL(`{scrub(doctype_name + slug)}`, {next_val}, {is_val_used})",
|
|
}
|
|
)
|