File size: 816 Bytes
3533fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from sqlalchemy import text
from sqlalchemy.orm import Session


def sync_pk_sequence(db: Session, table_name: str, column: str = "id") -> None:
    """
    Realign a PostgreSQL serial/identity sequence with MAX(id) in the table.
    Safe to call when rows were inserted with explicit IDs or after manual imports.
    """
    if db.get_bind().dialect.name != "postgresql":
        return

    max_id = db.execute(
        text(f"SELECT COALESCE(MAX({column}), 0) FROM {table_name}")
    ).scalar()
    db.execute(
        text(
            "SELECT setval("
            "pg_get_serial_sequence(:table, :column), "
            ":max_id, :is_called)"
        ),
        {
            "table": table_name,
            "column": column,
            "max_id": max_id,
            "is_called": max_id > 0,
        },
    )