directus
1import type { Knex } from 'knex';
2import { AutoSequenceHelper } from '../types.js';
3
4export class AutoIncrementHelperPostgres extends AutoSequenceHelper {
5/**
6* Resets the auto increment sequence based on the max value of the PK column.
7* The sequence name is determined using a sub query.
8*
9* The table name value for getting the sequence name needs to be escaped explicitly,
10* otherwise PostgreSQL would throw an error for capitalized table names saying "relation x does not exist".
11*/
12override async resetAutoIncrementSequence(table: string, column: string): Promise<Knex.Raw | void> {
13return await this.knex.raw(
14`WITH sequence_infos AS (SELECT pg_get_serial_sequence(?, ?) AS seq_name, MAX(??) as max_val FROM ??) SELECT SETVAL(seq_name, max_val) FROM sequence_infos;`,
15[`"${table}"`, column, column, table],
16);
17}
18}
19