Overview
Thedata/tools/ directory contains migration scripts for evolving the database schema. Tattoo Studio Manager uses manual migration scripts rather than an automated framework like Alembic, giving you full control over schema changes.
Migration Philosophy
Idempotent
Migrations can be run multiple times safely without duplicating changes or causing errors.
Explicit
Each migration is a standalone Python script that clearly documents what it does.
Version-Controlled
Migration files use date prefixes (e.g.,
2025_10_09_) for clear chronological ordering.Raw SQL
Direct SQLite operations for maximum control and transparency.
Migration Naming Convention
Examples
2025_10_09_add_txn_timestamps.py- Add timestamp columns to transactions2025_10_15_extend_portfolio.py- Extend portfolio table with new fields2025_10_17_add_client_is_active.py- Add is_active flag to clients
Common Migration Patterns
Pattern 1: Adding Columns
data/tools/migrate_client_columns.py
Pattern 2: Adding Timestamps with Triggers
data/tools/2025_10_09_add_txn_timestamps.py
Pattern 3: Adding Indexes
data/tools/2025_10_15_extend_portfolio.py:18-28
Available Migration Scripts
Schema Migrations
| Script | Purpose | Key Changes |
|---|---|---|
migrate_client_columns.py | Extend client table | Instagram, city, state, health fields, emergency contacts |
migrate_users_add_fields.py | Extend user profiles | Name, birthdate, email, phone, Instagram |
2025_10_09_add_txn_timestamps.py | Transaction timestamps | Add created_at, updated_at, trigger |
2025_10_15_extend_portfolio.py | Portfolio metadata | Client/session/transaction links, style, body area |
2025_10_16_portfolio_artist_nullable.py | Portfolio flexibility | Make artist_id nullable |
2025_10_17_add_client_is_active.py | Client status | Add is_active flag |
2025_11_06_add_client_prefs.py | Client preferences | Add preference fields |
2025_11_06_txn_allow_cashloose.py | Transaction flexibility | Allow loose cash handling |
Data Scripts
| Script | Purpose | Details |
|---|---|---|
seed.py | Populate test data | Creates clients, artists, sessions, products, users |
backfill_preferred_artist.py | Data backfill | Populate preferred artist from session history |
purge_artists.py | Clean data | Remove artists (use with caution) |
Running Migrations
Manual Execution
From Code
Seeding the Database
Theseed.py script provides idempotent test data population:
What It Creates
Clients (40)
Clients (40)
Random client data using Faker library with Mexican locale:
- Names, phone numbers, emails
- Only created if clients table is empty
Artists (4)
Artists (4)
Predefined artists with commission rates:
- Dylan Bourjac (55%)
- Jesus Esquer (50%)
- Pablo Velasquez (45%)
- Alex Chavez (50%)
Products (3)
Products (3)
Default inventory items:
- Tinta Negra (Black Ink)
- Guantes (Gloves)
- Agujas 5RL (5RL Needles)
Sessions (60)
Sessions (60)
Tattoo sessions over the last 30 days:
- Random clients and artists
- Status: Active, Completed, Waiting
- Prices: 600-2200 MXN
Transactions
Transactions
Payment records for completed sessions:
- Methods: Cash, Card, Transfer
- Linked to session end times
Users (3)
Users (3)
Test accounts:
admin/admin123(Admin role)assistant/assistant123(Assistant role)jesus/tattoo123(Artist role, linked to Jesus Esquer)
data/tools/seed.py:185-219
Creating New Migrations
Step 1: Create Migration File
Step 2: Write Idempotent Script
Step 3: Test Migration
Migration Best Practices
Always Check Before Modifying
Use
PRAGMA table_info() to check if columns exist before adding them.Enable Foreign Keys
Always execute
PRAGMA foreign_keys = ON when connecting to ensure referential integrity.Use Transactions
Wrap all changes in transactions so they can be rolled back on error.
Handle Both Environments
Support both development (
./dev.db) and production (via DB_PATH env var) paths.Add Helpful Output
Print what the migration is doing so users understand the progress.
Document the Purpose
Include a docstring explaining what the migration does and why.
SQLite Limitations
Related Documentation
- Session Management - Database connection and session handling
- Client Model - Client data model definition
- User Model - User authentication model
- Product Model - Inventory product model