pgsqlasync2fast-fastapi 0.4.0__py3-none-any.whl → 0.4.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -52,6 +52,8 @@ from pgsqlasync2fast_fastapi.seeder import (
52
52
  seed_all,
53
53
  # Shared idempotent insert-if-missing primitive
54
54
  insert_if_missing,
55
+ # Shared PostgreSQL sequence re-sync primitive
56
+ sync_table_sequence,
55
57
  )
56
58
 
57
59
  __all__ = [
@@ -90,4 +92,6 @@ __all__ = [
90
92
  "seed_all",
91
93
  # Shared idempotent insert-if-missing primitive
92
94
  "insert_if_missing",
95
+ # Shared PostgreSQL sequence re-sync primitive
96
+ "sync_table_sequence",
93
97
  ]
@@ -1 +1 @@
1
- __version__ = "0.4.0"
1
+ __version__ = "0.4.1"
@@ -593,6 +593,60 @@ def _resolve_load_order(manifest: dict[str, Any]) -> list[str]:
593
593
  # ============================================================================
594
594
 
595
595
 
596
+ async def sync_table_sequence(
597
+ session: Any,
598
+ model_class: type,
599
+ ) -> None:
600
+ """
601
+ Re-sync a PostgreSQL table identity sequence to its current MAX(id).
602
+
603
+ The seeder inserts rows with explicit ``id`` values straight from the
604
+ manifest JSON. PostgreSQL's plain ``serial``/``sequence``-backed columns do
605
+ NOT advance the sequence when a row is inserted with an explicit id, so a
606
+ later sequence-backed insert (e.g. ``insert_if_missing``) can generate an
607
+ id that collides with an existing explicit-id row (UniqueViolation).
608
+
609
+ This issues one ``setval`` to ``MAX(id)`` after the explicit-id inserts so
610
+ the next auto-generated id continues past the seeded rows. It only runs
611
+ against PostgreSQL — SQLite assigns ``INTEGER PRIMARY KEY`` as max+1
612
+ automatically and has no incompatible sequence catalog, so it is a no-op
613
+ there.
614
+
615
+ Args:
616
+ session: SQLModel AsyncSession bound to the target DB.
617
+ model_class: The SQLModel/SQLAlchemy table class to re-sync.
618
+ """
619
+ from sqlalchemy import text
620
+
621
+ try:
622
+ bind = getattr(session, "bind", None)
623
+ if bind is None:
624
+ return
625
+ dialect = getattr(bind.dialect, "name", None)
626
+ if dialect != "postgresql":
627
+ return
628
+
629
+ table = model_class.__tablename__
630
+ seq = f"{table}_id_seq"
631
+ async with bind.begin() as conn:
632
+ max_id = await conn.scalar(
633
+ text(f'SELECT COALESCE(MAX(id), 0) FROM "{table}"')
634
+ )
635
+ if max_id:
636
+ await conn.execute(
637
+ text("SELECT setval(:seq, :val)"),
638
+ {"seq": seq, "val": max_id},
639
+ )
640
+ logger.info(
641
+ f"Synced sequence '{seq}' to MAX(id)={max_id} "
642
+ f"(table '{table}')"
643
+ )
644
+ except Exception as exc: # pragma: no cover - defensive, non-fatal
645
+ logger.warning(
646
+ f"Could not re-sync sequence for '{model_class.__name__}': {exc}"
647
+ )
648
+
649
+
596
650
  async def _seed_table_idempotent(
597
651
  session: Any,
598
652
  table_name: str,
@@ -658,6 +712,9 @@ async def _seed_table_idempotent(
658
712
  logger.error(f"Failed to insert row in table '{table_name}': {e}")
659
713
  raise
660
714
 
715
+ if model_class is not None:
716
+ await sync_table_sequence(session, model_class)
717
+
661
718
  return rows_inserted, rows_skipped
662
719
 
663
720
 
@@ -727,6 +784,8 @@ async def _seed_table_idempotent_generic(
727
784
  logger.error(f"Failed to insert row in table '{table_name}': {e}")
728
785
  raise
729
786
 
787
+ await sync_table_sequence(session, model_class)
788
+
730
789
  return rows_inserted, rows_skipped
731
790
 
732
791
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pgsqlasync2fast-fastapi
3
- Version: 0.4.0
3
+ Version: 0.4.1
4
4
  Summary: Simple and fast PostgreSQL async module for FastAPI with multi-database support
5
5
  Author-email: Angel Daniel Sanchez Castillo <angeldaniel.sanchezcastillo@gmail.com>
6
6
  License: MIT License
@@ -0,0 +1,13 @@
1
+ pgsqlasync2fast_fastapi/__init__.py,sha256=jc45a0ClPL76d2BTkBIM6DvRzJj3O9mv1sgGEn17dmA,2474
2
+ pgsqlasync2fast_fastapi/__version__.py,sha256=pMtTmSUht-XtbR_7Doz6bsQqopJJd8rZ8I8zy2HwwoA,22
3
+ pgsqlasync2fast_fastapi/connection.py,sha256=vyR3SLpLTENYGKWkEoou0ud5IwUOJ9ksfkysUW5tz-I,6168
4
+ pgsqlasync2fast_fastapi/database.py,sha256=t6i8V4Bbec0sPoWYsBPScSqQpde1tVkqGx0CBRnAupk,8856
5
+ pgsqlasync2fast_fastapi/dependencies.py,sha256=jgX14S6h6i4Uk6edMDGh-wiDpxTlB16qvYMlIm1QofQ,4444
6
+ pgsqlasync2fast_fastapi/seeder.py,sha256=b_7fKP19Qh08NaTyY4kA7HL48dqZIE-Re5I-ki3M5a4,33284
7
+ pgsqlasync2fast_fastapi/settings.py,sha256=9SAmEVtotKJYlbTjI4qkKBjEOghTTtaICAFBBrR-eHk,6678
8
+ pgsqlasync2fast_fastapi/skills/SKILL.md,sha256=Yqge3ydNnZXO5hN5Ac4aynCG0hn-WYPCy_nkzToUDsM,6787
9
+ pgsqlasync2fast_fastapi-0.4.1.dist-info/licenses/LICENSE,sha256=CkISX1hNEwxxrPTOXet3IYMEH28Bn7SoUyKniRjg68I,1086
10
+ pgsqlasync2fast_fastapi-0.4.1.dist-info/METADATA,sha256=_giXkOTGxUOtf__J-zPoRXtzyznR0WedKi-42Bgi4Ws,10105
11
+ pgsqlasync2fast_fastapi-0.4.1.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
12
+ pgsqlasync2fast_fastapi-0.4.1.dist-info/top_level.txt,sha256=7pllDm9nlFGWKJDtG5zukxqdADqjZddU-GzaQJPLMVc,24
13
+ pgsqlasync2fast_fastapi-0.4.1.dist-info/RECORD,,
@@ -1,13 +0,0 @@
1
- pgsqlasync2fast_fastapi/__init__.py,sha256=rfPB2UJsmImEeCR-FfZ3Gg0cfuVLc4bymp-A1gi5Do4,2320
2
- pgsqlasync2fast_fastapi/__version__.py,sha256=42STGor_9nKYXumfeV5tiyD_M8VdcddX7CEexmibPBk,22
3
- pgsqlasync2fast_fastapi/connection.py,sha256=vyR3SLpLTENYGKWkEoou0ud5IwUOJ9ksfkysUW5tz-I,6168
4
- pgsqlasync2fast_fastapi/database.py,sha256=t6i8V4Bbec0sPoWYsBPScSqQpde1tVkqGx0CBRnAupk,8856
5
- pgsqlasync2fast_fastapi/dependencies.py,sha256=jgX14S6h6i4Uk6edMDGh-wiDpxTlB16qvYMlIm1QofQ,4444
6
- pgsqlasync2fast_fastapi/seeder.py,sha256=Pl9xEkgqOcOlZgFAHuTGgPUSCZ-Mt-M7wvYJZ6VLM98,31122
7
- pgsqlasync2fast_fastapi/settings.py,sha256=9SAmEVtotKJYlbTjI4qkKBjEOghTTtaICAFBBrR-eHk,6678
8
- pgsqlasync2fast_fastapi/skills/SKILL.md,sha256=Yqge3ydNnZXO5hN5Ac4aynCG0hn-WYPCy_nkzToUDsM,6787
9
- pgsqlasync2fast_fastapi-0.4.0.dist-info/licenses/LICENSE,sha256=CkISX1hNEwxxrPTOXet3IYMEH28Bn7SoUyKniRjg68I,1086
10
- pgsqlasync2fast_fastapi-0.4.0.dist-info/METADATA,sha256=rA3ZltgZR83lOfPOidK2yKxpOZxX1KbGL50cP5LqMys,10105
11
- pgsqlasync2fast_fastapi-0.4.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
12
- pgsqlasync2fast_fastapi-0.4.0.dist-info/top_level.txt,sha256=7pllDm9nlFGWKJDtG5zukxqdADqjZddU-GzaQJPLMVc,24
13
- pgsqlasync2fast_fastapi-0.4.0.dist-info/RECORD,,