Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/ElthonJohan/Sistema-MRP/llms.txt

Use this file to discover all available pages before exploring further.

Sistema MRP supports both SQLite for local development and PostgreSQL for production. The active database is selected at runtime through a single DATABASE_URL environment variable, with no code changes required between environments. Schema creation, incremental migrations, and the superadmin seed are all handled by init_db.py.

DATABASE_URL resolution

database.py resolves the connection string in the following order:
1

Streamlit secrets

Reads st.secrets["DATABASE_URL"]. This is the preferred method for Streamlit Cloud deployments. Set it in the app’s Secrets panel in the dashboard.
2

.env file fallback

If the secret is absent (e.g. running locally), python-dotenv loads DATABASE_URL from a .env file in the project root.
3

Error if missing

If neither source provides a value, database.py raises ValueError: No se encontro DATABASE_URL and the app refuses to start.
Example .env entries:
# SQLite — local development
DATABASE_URL=sqlite:///./mrp.db

# PostgreSQL — production
DATABASE_URL=postgresql://user:pass@host:5432/dbname

SessionLocal

The session factory is defined in database.py and is the only object pages should import to open a database session:
from sqlalchemy.orm import sessionmaker

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Pages call db = SessionLocal() and are responsible for calling db.close() — typically in a try/finally block. Services receive db as a parameter and must never create or close sessions themselves.

Engine configuration

The engine is created differently depending on the database backend detected from DATABASE_URL.
engine = create_engine(
    DATABASE_URL,
    echo=False,
    connect_args={"check_same_thread": False},
)
check_same_thread=False is required because Streamlit runs page scripts in threads that are different from the thread that created the connection.

init_db.py

Run init_db.py once on first setup. It is safe to re-run at any time.
python init_db.py
It performs three operations in sequence:
1

Create all tables

Calls Base.metadata.create_all(bind=engine), which creates every table defined across all model files. Tables that already exist are left unchanged.
2

Run migrations

Calls run_migrations(), which executes a series of additive ALTER TABLE statements. Each statement adds a column only if it does not already exist, so the function is fully idempotent.
run_migrations() is safe to re-run on an existing database. It never drops columns, changes types, or removes data. Running init_db.py again after a code update will apply any new columns added since the last run without affecting existing rows.
3

Seed superadmin

Checks whether a user with role="superadmin" already exists. If not, it creates one with username superadmin and password Admin123! (bcrypt-hashed). The superadmin account is never overwritten by subsequent runs.

Build docs developers (and LLMs) love