ff-storage
A comprehensive storage package for Fenixflow applications, providing async connection pools for modern Python applications, database connections, object storage abstractions, migration management, and model utilities. Supports PostgreSQL, MySQL, MongoDB, local filesystem storage, S3-compatible services, and Azure Blob Storage.
Created by Ben Moag at Fenixflow
Version 6.5.0 - Opt-in Transactional Batch Inserts
6.5.0 adds bulk_insert=True to repository and UnitOfWork create_many() for built-in non-temporal PostgreSQL models inside a transaction. Existing calls retain single-row INSERTs. Both this path and the existing count-returning bulk helper share SQL construction and bind-limit splitting, while keeping their separate metadata and return-value contracts.
Batching reduces statement overhead; it does not reduce stored rows. Review trigger compatibility and benchmark your workload before opting in. See the batch-insert guide for usage and rollout guidance.
See the full changelog for release history.
Version 6.4.2 - PostgreSQL Primary-Key Synchronization
- Schema synchronization preserves primary keys declared by supported models and adds missing keys to existing tables, in both synchronous and asynchronous managers.
- Duplicate or null legacy key values reject the migration without rewriting or deleting rows. Existing key changes require an explicit migration.
- Copy-on-change audit keys use the same synchronization path. SCD2 still permits multiple versions of the same logical ID, and separately declared unique indexes remain intact.
Before upgrading from 6.4.1 or earlier: check existing data and allow for validation and table locks when previously unenforced keys are installed. Read the primary-key upgrade guide.
Version 6.0.2 - Patch Release
6.0.2 extends the 6.0.1 Relationship-on-PydanticModel integration so the relationship is fully invisible to annotation walkers, not just to the Pydantic field machinery.
__pydantic_init_subclass__now also pops the relationship's annotation fromcls.__annotations__. Without this,typing.get_type_hints(cls)still tries to resolve the relationship's forward ref against the module's runtime globals — but relationship targets are typically imported underif TYPE_CHECKING:to avoid circular imports, so the eval raisedNameErrorand broke schema-derivation factories,pydantic.create_modelcallers, and anything else that walks annotations.Relationship.__set_name__is now idempotent — re-firing it (e.g. from a testsetup_methodafter a registry reset) preserves the originally-derivedRelationshipConfiginstead of clobberingtarget_modelwith an empty string when the annotation has already been stripped.
Version 6.0.1 - Patch Release
6.0.1 ships first-class support for Relationship descriptors declared directly on PydanticModel subclasses.
Relationshipdeclarations likeposts: list["Post"] = Relationship(back_populates="author")no longer leak through Pydantic's field machinery — they are stripped frommodel_fields, never appear in DDL, are excluded frommodel_dump()/model_dump_for_db(), and instance access (author.posts) returns the cached value (or the empty default) via the descriptor protocol instead of aRelationshipProxy.Relationship.__set_name__correctly parses string-form annotations underfrom __future__ import annotations(PEP 563), recognisinglist[X]/List[X]spellings so collection relationships are no longer silently misclassified as many-to-one references.
Version 6.0.0 - Contract Alignment
6.0.0 is a major contract-alignment release. Repository CRUD remains the stable cross-database API, while backend-tiered capabilities are now enforced explicitly instead of being implied by shared marketing language.
- PostgreSQL and MongoDB production behavior is preserved.
- SQL Server support has been removed from the package surface.
- Backend support expectations are now enforced explicitly: repository CRUD is the shared contract, while query builder and transaction capabilities remain backend-tiered.
- Packaging metadata and release documentation are aligned for public distribution.
Backend Contract at a Glance
| Layer | PostgreSQL | MongoDB | MySQL |
|---|---|---|---|
| Pool lifecycle | connect, disconnect |
connect, disconnect |
connect, disconnect |
| Low-level reads | fetch_one, fetch_all |
find_one, find |
fetch_one, fetch_all |
| Low-level writes | execute, execute_many, execute_query |
native insert/update/delete methods | execute, execute_many, execute_query |
| Repository CRUD | aligned | aligned | aligned |
| Query builder | strongest support | separate Mongo compiler path | partial |
| Transactions / Unit of Work | strongest support | backend-specific / no equivalent | limited |
The stable cross-database contract in 6.0.0 is the repository CRUD surface. Lower-level pool, query, and transaction features remain backend-specific where documented.
Configurable Auto-Indexes
Built-in SQL auto-indexes remain enabled by default. On PostgreSQL tables with high write volume, you can now opt out of specific built-in patterns without changing default behavior for other models:
from typing import ClassVar
class Product(PydanticModel):
__table_name__ = "products"
__temporal_strategy__ = "scd2"
__exclude_auto_indexes__: ClassVar[set[str]] = {
"updated_at",
"valid_period",
"not_deleted",
}
Excluded built-in indexes are skipped during creation and treated as unmanaged by sync_schema(). Existing indexes are not auto-dropped.
Installation
Install only the backends you use:
pip install ff-storage[postgres]
pip install ff-storage[postgres,mongo]
pip install ff-storage[all]
The base package no longer installs database drivers automatically in 6.0.0. This avoids unnecessary native/system-library requirements in environments that only need a subset of the supported backends.
🧪 Version 4.8.0 - Mock Data Generation & ERD
New testing infrastructure:
- Mock Factory - Generate realistic test data from Pydantic models
- ERD Builder - Auto-discover models and generate Entity Relationship Diagrams
- Generator Extensions - Add domain-specific patterns for your industry
from ff_storage import PydanticModel, Field
from ff_storage.mock import MockFactory, GeneratorExtension
from ff_storage.erd import ERDBuilder, to_mermaid
# Simple mock creation - uses Field() constraints
user = User.create_mock(seed=42)
users = User.create_mock_batch(100, seed=42)
# Custom patterns for your domain
class InsuranceExtension(GeneratorExtension):
NAME_PATTERNS = [
(r"^policy_number$", lambda f, m: f.bothify("POL-####-????").upper()),
(r"^premium$", lambda f, m: Decimal(str(f.pyfloat(100, 10000)))),
]
factory = MockFactory(seed=42)
factory.registry.extend(InsuranceExtension())
policy = factory.create(Policy)
# ERD generation
builder = ERDBuilder()
erd = builder.build()
print(to_mermaid(erd)) # Mermaid diagram syntax
100% backward compatible - all existing code works unchanged.
🔥 Version 4.7.0 - Query Builder & Relationships
Major new features:
- Query Builder - Fluent, type-safe query API with JOINs and aggregations
- Relationships - ORM-style model relationships with eager loading
- Transactions - Full transaction management with Unit of Work pattern
- Bulk Operations - Efficient batch insert/update/delete
from ff_storage import Query, F, Relationship
# Fluent query API
results = await (
Query(Product)
.filter(F.price > 100)
.filter(F.status == "active")
.order_by(F.created_at.desc())
.limit(10)
.execute(db_pool, tenant_id=tenant)
)
# Model relationships
class Author(PydanticModel):
posts: list["Post"] = Relationship(back_populates="author")
# Eager loading (prevents N+1 queries)
authors = await Query(Author).load(["posts"]).execute(db_pool)
100% backward compatible - all existing code works unchanged.
🎯 Version 4.4.0 - Multi-Tenant Permissive Scope
Flexible multi-tenant access with separate tenant_id and tenant_ids parameters:
- 🔒 Strict Scope (
tenant_id): Single UUID - forces tenant_id on writes, strict isolation for broker/UW operations - 🌐 Permissive Scope (
tenant_ids): List of UUIDs - validates writes, enables admin cross-tenant queries - ✅ Clear Semantics - Different behavior for single vs multi-tenant use cases
# Strict scope (broker writes) - forces tenant_id on all records
repo = PydanticRepository(Product, db_pool, tenant_id=org_id)
# Permissive scope (admin reads) - IN clause filtering
repo_admin = PydanticRepository(Product, db_pool, tenant_ids=[tenant1, tenant2])
🎉 Version 3.0.0 - Pydantic ORM & Temporal Data Management
Major features in v3.0.0: Production-ready Pydantic ORM with built-in temporal data management!
- 🔥 Pydantic Models - Type-safe models with automatic schema generation
- ⏱️ Temporal Strategies - Choose from 3 strategies: none, copy_on_change (audit trail), scd2 (time travel)
- 🎯 Multi-Tenant by Default - Automatic tenant_id injection and filtering
- 📝 Audit Trails - Field-level change tracking with copy_on_change
- ⏰ Time Travel - Query historical data with scd2 strategy
- 🔧 Rich Field Metadata - Complete SQL control (FK, CHECK, defaults, partial indexes)
- 🚀 Auto-Sync Schema - SchemaManager now creates auxiliary tables (audit tables)
📚 Documentation | ⚡ Quickstart Guide | 🎯 Strategy Selection
Backwards Compatible: All v2 features work unchanged. v3 is fully opt-in.
Version 2.0.0 - Schema Sync System
Added in 2.0.0: Terraform-like automatic schema synchronization! Define schema in model classes and let SchemaManager handle migrations automatically.
Important: v3.3.0 fixes critical false positive detection bugs in schema sync. Upgrade from v2.x/v3.2.x immediately.
Breaking Change in 2.0.0: Removed file-based migrations (MigrationManager). Use SchemaManager for automatic schema sync from model definitions.
New in 1.1.0: Added Azure Blob Storage backend with support for both Azurite (local development) and production Azure Blob Storage.
Quick Start
Installation
From PyPI
pip install ff-storage[postgres]
pip install ff-storage[postgres,mongo]
From GitLab
pip install "ff-storage[postgres] @ git+https://gitlab.com/fenixflow/fenix-packages.git#subdirectory=ff-storage"
Async Pool (FastAPI, Production)
from ff_storage.db import PostgresPool
# Create async connection pool
pool = PostgresPool(
dbname="fenix_db",
user="fenix",
password="password",
host="localhost",
port=5432,
min_size=10,
max_size=20
)
# Connect once at startup
await pool.connect()
# Use many times - pool handles connections internally
# Returns dictionaries by default for easy access
results = await pool.fetch_all("SELECT id, title, status FROM documents WHERE status = $1", "active")
# results = [{'id': 1, 'title': 'Doc 1', 'status': 'active'}, ...]
print(results[0]['title']) # Access by column name - intuitive!
# Fetch single row
user = await pool.fetch_one("SELECT id, name, email FROM users WHERE id = $1", 123)
# user = {'id': 123, 'name': 'Alice', 'email': 'alice@example.com'}
# Disconnect once at shutdown
await pool.disconnect()
Sync Connection (Scripts, Simple Apps)
from ff_storage.db import Postgres
# Create direct connection
db = Postgres(
dbname="fenix_db",
user="fenix",
password="password",
host="localhost",
port=5432
)
# Connect and query - returns dicts by default
db.connect()
results = db.read_query("SELECT id, title, status FROM documents WHERE status = %(status)s", {"status": "active"})
# results = [{'id': 1, 'title': 'Doc 1', 'status': 'active'}, ...]
print(results[0]['title']) # Easy access by column name
db.close_connection()
FastAPI Integration
from fastapi import FastAPI
from ff_storage.db import PostgresPool
app = FastAPI()
# Create pool once
app.state.db = PostgresPool(
dbname="fenix_db",
user="fenix",
password="password",
host="localhost",
min_size=10,
max_size=20
)
@app.on_event("startup")
async def startup():
await app.state.db.connect()
@app.on_event("shutdown")
async def shutdown():
await app.state.db.disconnect()
@app.get("/users/{user_id}")
async def get_user(user_id: int):
# Pool handles connection automatically
user = await app.state.db.fetch_one(
"SELECT * FROM users WHERE id = $1", user_id
)
return user
Migration Guide
v3.2.x → v3.3.0 (Backward Compatible)
No action required - Internal architecture improvements only.
Changes:
- Schema normalization now centralized (eliminates false positives)
- WHERE clause parser added (fixes precedence bugs)
- All public APIs unchanged
Impact: If using schema sync, upgrade immediately to eliminate false positives causing index recreation on every run.
v2.x → v3.0.0 (Breaking - Pydantic ORM)
See the quickstart guide for the full migration guide.
v0.x → v1.0.0 (Breaking - Async Pools)
Pools are now async - all *Pool classes require await. Use direct connections for sync code (Postgres and MySQL remain unchanged).
Features
Database Operations
- Async Connection Pools: High-performance async pools for PostgreSQL, MySQL, and MongoDB
- Sync Direct Connections: Simple sync connections for scripts and non-async code
- Multi-Database Support: Repository CRUD aligned across PostgreSQL, MongoDB, and MySQL
- Transaction Management: PostgreSQL-first transaction support with explicit capability guards on unsupported backends
- Batch Operations: Execute many queries efficiently with bulk insert/update/delete
- Query Builder: Fluent, type-safe query API with JOINs and aggregations
- Model Relationships: ORM-style relationships with eager loading (N+1 prevention)
Schema Sync System (v2.0.0+, Fixed in v3.3.0)
- Production-Ready: v3.3.0 fixes critical false positive detection bugs
- Normalization Framework: Centralized schema comparison (eliminates index churn)
- WHERE Clause Parser: SQL AST parsing with proper precedence handling
- Terraform-like Migrations: Define schema in code, auto-sync on startup
- Automatic Detection: Detects schema changes from model definitions
- Safe by Default: Additive changes auto-apply, destructive changes require explicit approval
- Dry Run Mode: Preview changes without applying them
- Transaction-Wrapped: All changes in single atomic transaction
- Provider Detection: Auto-detects PostgreSQL or MySQL
Object Storage
- Multiple Backends: Local filesystem, S3/S3-compatible services, and Azure Blob Storage
- Async Operations: Non-blocking I/O for better performance
- Streaming Support: Handle large files without memory overhead
- Atomic Writes: Safe file operations with temp file + rename
- Metadata Management: Store and retrieve metadata with objects
Create an object without overwriting
await storage.create(key, data) returns True when it publishes a new object
and False when the key already exists. This is supported by local, Azure and S3
storage. Backends without an implementation raise NotImplementedError. Existing write() calls
continue to replace objects. An exists() check followed by write() cannot
provide create-only behavior when writers race.
Azure creation stages blocks with unique IDs for each upload and commits only
that upload's blocks with a server-side absence condition. This also protects
large objects. Local creation publishes a synced temporary file with an atomic
hard link on the destination filesystem. Local bytes, streams and metadata share
the same staging and cleanup implementation. Path components beginning with
.ff-storage-tmp- are reserved, inaccessible as object keys, and hidden from
listings. Payloads and metadata sidecars remain separate atomic publications.
S3 creation uses If-None-Match: * on PutObject or, for large payloads, on
CompleteMultipartUpload, reusing the existing multipart uploader. Failed or
cancelled multipart operations attempt to abort only their own upload. Configure
an S3 lifecycle rule to expire incomplete uploads after crashes or cleanup
failures. Only 412 PreconditionFailed at publication returns False; a 409
delete conflict and other errors raise OSError, allowing the caller to retry
the whole operation. In versioned buckets this checks the current version;
a current delete marker permits creation. S3-compatible endpoints must enforce
both conditional APIs; there is no unconditional fallback. See
AWS conditional write behavior.
Contract tests use Azurite and a pinned Moto S3 server in CI. To run Azure tests locally, set
FF_STORAGE_AZURE_TEST_CONNECTION_STRING to a disposable emulator/test account
and run python -m pytest tests/object_integration/test_azure_create.py from
ff-storage. For S3 tests, set FF_STORAGE_S3_TEST_ENDPOINT_URL to a disposable
S3 test endpoint and run python -m pytest tests/object_integration/test_s3_create.py.
The default test credentials are ffstorage / ffstorage-test-secret; override
them with FF_STORAGE_S3_TEST_ACCESS_KEY_ID and FF_STORAGE_S3_TEST_SECRET_ACCESS_KEY.
These tests create and remove unique test buckets.
They exercise real SDK requests and emulator semantics; they do not replace
validation against AWS or a deployment's chosen S3-compatible provider.
Core Components
Database Connections
PostgreSQL with Connection Pooling
from ff_storage import PostgresPool
# Initialize pool
db = PostgresPool(
dbname="fenix_db",
user="fenix",
password="password",
host="localhost",
port=5432,
pool_size=20
)
# Use connection from pool - returns dicts by default
db.connect()
try:
# Execute queries - returns list of dicts
results = db.read_query("SELECT id, title, status FROM documents WHERE status = %s", {"status": "active"})
# results = [{'id': 1, 'title': 'Doc 1', 'status': 'active'}, ...]
print(results[0]['title']) # Easy access by column name
# Execute with RETURNING
new_id = db.execute_query(
"INSERT INTO documents (title) VALUES (%s) RETURNING id",
{"title": "New Document"}
)
# new_id = [{'id': 123}]
# Transaction example
db.begin_transaction()
try:
db.execute("UPDATE documents SET status = %s WHERE id = %s", {"status": "archived", "id": 123})
db.execute("INSERT INTO audit_log (action) VALUES (%s)", {"action": "archive"})
db.commit_transaction()
except Exception:
db.rollback_transaction()
raise
finally:
# Return connection to pool
db.close_connection()
MySQL with Connection Pooling
from ff_storage import MySQLPool
# Initialize pool
db = MySQLPool(
dbname="fenix_db",
user="root",
password="password",
host="localhost",
port=3306,
pool_size=10
)
# Similar usage pattern as PostgreSQL - returns dicts by default
db.connect()
results = db.read_query("SELECT id, title, status FROM documents WHERE status = %s", {"status": "active"})
# results = [{'id': 1, 'title': 'Doc 1', 'status': 'active'}, ...]
print(results[0]['title']) # Easy access by column name
db.close_connection()
Object Storage
Local Filesystem Storage
from ff_storage import LocalObjectStorage
import asyncio
async def main():
# Initialize local storage
storage = LocalObjectStorage("/var/data/documents")
# Write file with metadata
await storage.write(
"reports/2025/quarterly.pdf",
pdf_bytes,
metadata={"content-type": "application/pdf", "author": "system"}
)
# Read file
data = await storage.read("reports/2025/quarterly.pdf")
# Check existence
exists = await storage.exists("reports/2025/quarterly.pdf")
# List files with prefix
files = await storage.list_keys(prefix="reports/2025/")
# Delete file
await storage.delete("reports/2025/quarterly.pdf")
asyncio.run(main())
S3-Compatible Storage
from ff_storage import S3ObjectStorage
import asyncio
async def main():
# AWS S3
s3 = S3ObjectStorage(
bucket="fenix-documents",
region="us-east-1"
)
# Or MinIO/other S3-compatible
s3 = S3ObjectStorage(
bucket="fenix-documents",
endpoint_url="http://localhost:9000",
access_key="minioadmin",
secret_key="minioadmin"
)
# Write file
await s3.write("docs/report.pdf", pdf_bytes)
# Stream large files
async for chunk in s3.read_stream("large_file.bin", chunk_size=8192):
await process_chunk(chunk)
# Multipart upload for large files (automatic)
await s3.write("huge_file.bin", huge_data) # Automatically uses multipart if > 5MB
asyncio.run(main())
Azure Blob Storage
from ff_storage import AzureBlobObjectStorage
from azure.identity import DefaultAzureCredential
import asyncio
async def main():
# Azurite (local development with connection string)
storage = AzureBlobObjectStorage(
container_name="fenix-documents",
connection_string="DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;"
)
# Production with connection string (access key)
storage = AzureBlobObjectStorage(
container_name="fenix-documents",
connection_string="DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=...;EndpointSuffix=core.windows.net",
prefix="documents/" # Optional prefix for all keys
)
# Production with Managed Identity (DefaultAzureCredential)
storage = AzureBlobObjectStorage(
container_name="fenix-documents",
account_url="https://mystorageaccount.blob.core.windows.net"
)
# Production with custom credential
storage = AzureBlobObjectStorage(
container_name="fenix-documents",
account_url="https://mystorageaccount.blob.core.windows.net",
credential=DefaultAzureCredential()
)
# Write file with metadata
await storage.write(
"reports/2025/quarterly.pdf",
pdf_bytes,
metadata={"content-type": "application/pdf", "author": "system"}
)
# Read file
data = await storage.read("reports/2025/quarterly.pdf")
# Stream large files
async for chunk in storage.read_stream("large_file.bin", chunk_size=8192):
await process_chunk(chunk)
# Check existence
exists = await storage.exists("reports/2025/quarterly.pdf")
# List blobs with prefix
files = await storage.list_keys(prefix="reports/2025/")
# Get metadata
metadata = await storage.get_metadata("reports/2025/quarterly.pdf")
print(metadata["content-type"])
# Delete blob
await storage.delete("reports/2025/quarterly.pdf")
asyncio.run(main())
Note: Azure Blob Storage has restrictions on metadata keys (must be valid C# identifiers). The implementation automatically converts hyphens to underscores (e.g., content-type becomes content_type) when storing and converts them back when retrieving.
Schema Sync (Terraform-like Migrations)
Optional-column synchronization (6.5.4)
Changing an existing field from required to optional applies automatically on
PostgreSQL when nullability is the only normalized column difference (6.5.4+).
Both schema managers emit only ALTER COLUMN ... DROP NOT NULL, preserving the
type, default and existing rows. allow_destructive=False continues to exclude
mixed changes and stricter nullability; primary keys stay required. Other providers
must implement the dedicated operation and never fall back to a general alteration.
Review optional model fields before upgrading: previously skipped relaxations may
now apply across the application. PostgreSQL still takes a table lock, so serialize
schema changes and use a deployment window where needed.
Run tests/integration/test_nullability_sync.py with
FF_STORAGE_NULLABILITY_TEST_DSN set to an explicitly disposable database to test
both managers in temporary schemas, including dry-run and repeat synchronization.
Release 6.5.3 (schema comparison, defaults and reporting) precedes 6.5.4. Validate 6.5.3 before adopting optional-column relaxation; consumer FK declarations must also satisfy the 6.5.2 requirements below.
Schema synchronization fixes (6.5.3)
Both managers now distinguish detected, applied, skipped and failed changes.
sync_schema() still returns the number of applied changes (zero for dry runs),
while manager.last_sync_report exposes the counts for the latest attempt.
Zero applied changes does not mean the schema matches: check skipped changes
and errors too. Logs include schema-qualified table names and retain genuine
destructive differences instead of ending with a misleading clean-state message.
The report describes the attempted plan, not a post-apply catalogue verification.
Dry runs perform no schema writes. Namespace creation is included in the real PostgreSQL migration transaction; failed SQL generation withholds the affected table's changes rather than applying an incomplete plan. Tables sharing a name in different schemas now have independent comparison state. Sync and async catalogue readers share index operator-class and array metadata handling.
See schema sync state and review boundaries for report semantics, regression coverage and remaining limitations.
Enum defaults now use the member's value: Status.OPEN with value "open"
produces DEFAULT 'open'. String literals also escape embedded apostrophes.
An explicit db_default remains authoritative.
PostgreSQL sync now applies default-only additions, replacements and removals
with allow_destructive=False. It emits only ALTER COLUMN SET DEFAULT or
DROP DEFAULT, leaving stored rows, types and nullability unchanged. A mixed
change to the same column still requires the existing destructive-change path;
primary-key repair guards also remain in place. Both schema managers use this
behavior. Custom migration generators must implement generate_alter_column_default
to support the new change type; it never falls back to a full column alteration.
Review the dry-run plan before upgrading: a model/DB default discrepancy that was previously skipped will now change future inserts that omit the column. PostgreSQL still takes a table lock. Existing rows containing an old enum-member string are not repaired. Any backfill needs a separate data migration. Upgrading from 6.5.1 or earlier also activates the foreign-key checks described below; validate consumer declarations and existing relationships before rollout.
Foreign-key synchronization fix (6.5.2)
ff-storage 6.5.1 dropped model-generated foreign keys while rebuilding CREATE
TABLE SQL and did not repair missing keys on existing tables. Both schema
managers now preserve single-column db_foreign_key="schema.table(column)"
links with the existing generated SQL's immediate, default NO ACTION behavior.
db_on_delete / db_on_update metadata activation is outside this fix.
Tables, columns and referenced keys/indexes are prepared before FKs are added,
including child-first registration. Public compare_schemas() plans have the
same ordering. Equivalent validated manual constraints are reused regardless of
name; repeated startup adds nothing. Incompatible or NOT VALID constraints
require an explicit migration. Unsupported advanced declarations fail explicitly;
this fix does not add composite keys, custom actions, MATCH or deferral support.
Before upgrading, inventory existing declarations, ensure referenced targets are unique, and check for orphan rows on a restored database. Previously ignored relationships throughout the application will now be enforced. Validation can scan/lock tables; serialize schema changes and allow for operational migration where needed. Failed FK installation rolls back the migration transaction without repairing or deleting data. Schema namespace creation happens earlier. A package rollback does not remove constraints already installed. Dry-run logs show the plan but cannot prove existing data is valid.
The focused PostgreSQL suite requires FF_STORAGE_FOREIGN_KEY_TEST_DSN pointing
to a disposable database and tests both managers in unique temporary schemas.
PostgreSQL synchronization preserves declared primary keys and adds missing
keys on existing tables. Upgrading to 6.4.2 can therefore validate previously
unenforced keys during startup, even with allow_destructive=False. Read the
primary-key upgrade guide before adoption.
from ff_storage import Postgres, SchemaManager, PydanticModel, Field
# Define your model with PydanticModel
class Document(PydanticModel):
__table_name__ = "documents"
__schema__ = "public"
title: str = Field(max_length=255)
content: str | None = None
status: str = Field(default="draft", max_length=50)
# Connect to database
db = Postgres(dbname="mydb", user="user", password="pass", host="localhost", port=5432)
db.connect()
# Create schema manager (auto-detects PostgreSQL)
manager = SchemaManager(db)
# Dry run to preview changes
print("Preview of changes:")
manager.sync_schema(
models=[Document],
allow_destructive=False,
dry_run=True
)
# Apply changes automatically
changes_applied = manager.sync_schema(
models=[Document],
allow_destructive=False, # Safe by default
dry_run=False
)
print(f"Applied {changes_applied} schema changes")
Features:
- Automatic Detection: Detects new tables, missing columns, and indexes
- Safe by Default: Additive changes (CREATE, ADD) auto-apply; destructive changes (DROP) require explicit flag
- Dry Run Mode: Preview all changes before applying
- Transaction-Wrapped: All changes in a single atomic transaction
- Provider-Agnostic at Repository Layer: CRUD works across supported backends; advanced query and transaction features remain backend-tiered
Advanced Features
Transaction Management
# Context manager for automatic transaction handling
async def transfer_ownership(db, doc_id, new_owner_id):
db.begin_transaction()
try:
# Multiple operations in single transaction
db.execute("UPDATE documents SET owner_id = %s WHERE id = %s",
{"owner_id": new_owner_id, "id": doc_id})
db.execute("INSERT INTO audit_log (action, doc_id, user_id) VALUES (%s, %s, %s)",
{"action": "transfer", "doc_id": doc_id, "user_id": new_owner_id})
db.commit_transaction()
except Exception as e:
db.rollback_transaction()
raise
Connection Pool Monitoring
# Check pool statistics
pool = PostgresPool(...)
open_connections = pool.get_open_connections()
print(f"Open connections: {open_connections}")
# Graceful shutdown
pool.close_all_connections()
Fluent Query Builder (v4.7.0+)
from ff_storage import Query, F, AND, OR
# Simple filtering
products = await (
Query(Product)
.filter(F.price > 100)
.filter(F.status == "active")
.order_by(F.created_at.desc())
.limit(10)
.execute(db_pool, tenant_id=tenant)
)
# Complex filters with AND/OR
results = await (
Query(Product)
.filter(AND(
F.category == "electronics",
OR(F.price < 50, F.on_sale == True)
))
.execute(db_pool)
)
# String operations
results = await (
Query(User)
.filter(F.email.icontains("@example.com"))
.filter(F.name.startswith("John"))
.execute(db_pool)
)
# Aggregations
from ff_storage import func
total = await (
Query(Order)
.filter(F.status == "completed")
.select(func.sum(F.total))
.execute(db_pool)
)
# First result or None
user = await Query(User).filter(F.email == email).first(db_pool)
# Check existence
exists = await Query(User).filter(F.email == email).exists(db_pool)
Model Relationships (v4.7.0+)
from ff_storage import PydanticModel, Relationship, Field
class Author(PydanticModel):
__table_name__ = "authors"
name: str = Field(max_length=255)
# One-to-many relationship
posts: list["Post"] = Relationship(back_populates="author")
class Post(PydanticModel):
__table_name__ = "posts"
title: str = Field(max_length=255)
author_id: UUID # Foreign key
# Many-to-one relationship
author: "Author" = Relationship(back_populates="posts")
# Query with JOINs
results = await (
Query(Author)
.filter(F.name.contains("John"))
.join(Author.posts) # JOIN posts table
.execute(db_pool)
)
# Eager loading (prevents N+1 queries)
authors = await (
Query(Author)
.load(["posts"]) # Batch load all posts
.execute(db_pool)
)
for author in authors:
print(f"{author.name} has {len(author.posts)} posts")
Transactions (v4.7.0+)
from ff_storage import Transaction, IsolationLevel
# Simple transaction
async with Transaction(db_pool) as tx:
await tx.execute("INSERT INTO orders ...")
await tx.execute("UPDATE inventory ...")
# Auto-commits on success, rollbacks on exception
# With isolation level
async with Transaction(db_pool, isolation=IsolationLevel.SERIALIZABLE) as tx:
await tx.execute("SELECT ... FOR UPDATE")
await tx.execute("UPDATE ...")
# Savepoints for nested transactions
async with Transaction(db_pool) as tx:
await tx.execute("INSERT INTO parent ...")
async with tx.savepoint("child_ops"):
await tx.execute("INSERT INTO child ...")
# Can rollback just this savepoint
Legacy Query Builder Utilities
from ff_storage.db.sql import build_insert, build_update, build_select
# Build INSERT query
query, params = build_insert("documents", {
"title": "New Doc",
"status": "draft"
})
# Build UPDATE query
query, params = build_update("documents",
{"status": "published"},
{"id": doc_id}
)
# Build SELECT with conditions
query, params = build_select("documents",
columns=["id", "title"],
where={"status": "published", "author_id": user_id}
)
Error Handling
from ff_storage.exceptions import StorageError, DatabaseError
try:
db.connect()
results = db.read_query("SELECT * FROM documents")
except DatabaseError as e:
print(f"Database error: {e}")
except StorageError as e:
print(f"Storage error: {e}")
finally:
db.close_connection()
Testing
# Run tests
pytest tests/
# With coverage
pytest --cov=ff_storage tests/
# Run specific test file
pytest tests/test_postgres.py
# Run with verbose output
pytest -v tests/
Configuration
Environment Variables
# Database
export DB_HOST=localhost
export DB_PORT=5432
export DB_NAME=fenix_db
export DB_USER=fenix
export DB_PASSWORD=secret
# S3 Storage
export AWS_ACCESS_KEY_ID=your-key
export AWS_SECRET_ACCESS_KEY=your-secret
export AWS_DEFAULT_REGION=us-east-1
# Local Storage
export STORAGE_PATH=/var/data/documents
Configuration File
# config.py
from ff_storage import PostgresPool, S3ObjectStorage
# Database configuration
DATABASE = {
"dbname": os.getenv("DB_NAME", "fenix_db"),
"user": os.getenv("DB_USER", "fenix"),
"password": os.getenv("DB_PASSWORD"),
"host": os.getenv("DB_HOST", "localhost"),
"port": int(os.getenv("DB_PORT", 5432)),
"pool_size": 20
}
# Storage configuration
STORAGE = {
"bucket": os.getenv("S3_BUCKET", "fenix-documents"),
"region": os.getenv("AWS_DEFAULT_REGION", "us-east-1")
}
# Initialize
db = PostgresPool(**DATABASE)
storage = S3ObjectStorage(**STORAGE)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - See the LICENSE file for details.
Author
Created and maintained by Ben Moag at Fenixflow
For more information, visit the GitLab repository.
Metadata
Release files for ff-storage 6.6.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ff_storage-6.6.3-py3-none-any.whl | Python 3 | none | any | Details for ff_storage-6.6.3-py3-none-any.whl |
Release files / ff_storage-6.6.3-py3-none-any.whl
| Download URL | ff_storage-6.6.3-py3-none-any.whl |
|---|---|
| Size | 298.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cd6e03bcbf8b67a64136ae6c34358b84d21b575db38de88ce1174434ec80cb06
|
|
BLAKE2b-256 checksum How to use checksums |
036fe84391397eeb52314e42c66b7c1b619dc650568e35b8d2010755a99e066e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.16
|
Security for ff-storage 6.6.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.