1
0
mirror of https://github.com/django/django.git synced 2025-01-08 17:37:20 +00:00

Update reset sequence sql to allow restarting sequences

This commit is contained in:
David Sanders 2024-04-01 22:13:51 +11:00
parent 6775022253
commit c13dd65d28

View File

@ -38,25 +38,24 @@ class DatabaseOperations(BaseDatabaseOperations):
# TODO: colorize this SQL code with style.SQL_KEYWORD(), etc. # TODO: colorize this SQL code with style.SQL_KEYWORD(), etc.
_sequence_reset_sql = """ _sequence_reset_sql = """
DECLARE DECLARE
table_value integer; identity_col_found integer;
seq_value integer; new_start integer;
seq_name user_tab_identity_cols.sequence_name%%TYPE;
BEGIN BEGIN
BEGIN SELECT COUNT(*) INTO identity_col_found FROM user_tab_identity_cols
SELECT sequence_name INTO seq_name FROM user_tab_identity_cols WHERE table_name = '%(table_name)s' AND
WHERE table_name = '%(table_name)s' AND column_name = '%(column_name)s';
column_name = '%(column_name)s';
EXCEPTION WHEN NO_DATA_FOUND THEN
seq_name := '%(no_autofield_sequence_name)s';
END;
SELECT NVL(MAX(%(column)s), 0) INTO table_value FROM %(table)s; IF identity_col_found >= 1 THEN
SELECT NVL(last_number - cache_size, 0) INTO seq_value FROM user_sequences EXECUTE IMMEDIATE 'ALTER TABLE %(table)s ' ||
WHERE sequence_name = seq_name; 'MODIFY %(column)s ' ||
WHILE table_value > seq_value LOOP 'GENERATED BY DEFAULT ON NULL AS IDENTITY ' ||
EXECUTE IMMEDIATE 'SELECT "'||seq_name||'".nextval%(suffix)s' 'START WITH LIMIT VALUE';
INTO seq_value; ELSE
END LOOP; SELECT NVL(MAX(%(column)s), 0) + 1 INTO new_start FROM %(table)s;
EXECUTE IMMEDIATE 'ALTER SEQUENCE "%(no_autofield_sequence_name)s" ' ||
'RESTART START WITH ' || new_start;
END IF;
END; END;
/""" /"""
@ -537,7 +536,6 @@ END;
"column": column, "column": column,
"table_name": strip_quotes(table), "table_name": strip_quotes(table),
"column_name": strip_quotes(column), "column_name": strip_quotes(column),
"suffix": self.connection.features.bare_select_suffix,
} }
sql.append(query) sql.append(query)
return sql return sql
@ -561,7 +559,6 @@ END;
"column": column, "column": column,
"table_name": strip_quotes(table), "table_name": strip_quotes(table),
"column_name": strip_quotes(column), "column_name": strip_quotes(column),
"suffix": self.connection.features.bare_select_suffix,
} }
) )
# Only one AutoField is allowed per model, so don't # Only one AutoField is allowed per model, so don't