Database Migrations
This directory contains SQL migrations for the Copper Tone Technologies platform database.
Migration Files
Migrations are numbered sequentially and come in pairs:
XXX_description.up.sql- Applies the migrationXXX_description.down.sql- Rolls back the migration
Current Migrations
-
001_create_users_and_identities - User authentication and multi-factor identity system
userstable: Core user informationidentitiestable: Multiple authentication methods (email/password, blockchain, DIDs)user_rolestable: Role-based access control (ADMIN, STAFF, CLIENT)
-
002_create_projects_and_tasks - Work management system
projectstable: Project tracking with IPFS metadata supporttaskstable: Task management with assignments and time trackingwork_orderstable: Work order documentation with IPFS supporttask_commentstable: Collaboration and task discussions
-
003_create_invoices_and_payments - Payment and invoicing system
invoicestable: Invoice management with blockchain transaction supportpaymentstable: Multi-modal payments (credit card, crypto, blockchain tokens)invoice_itemstable: Line items for invoices- Automatic invoice status updates based on payments
Running Migrations
Using podman-compose
Migrations are automatically applied when starting the stack:
podman-compose up --build
The db-init service will wait for PostgreSQL to be ready and then apply all migrations in order.
Manual Migration
To run migrations manually:
# From the backend directory
./scripts/init-db.sh
Environment Variables
DB_HOST- Database host (default:db)DB_USER- Database user (default:user)DB_PASSWORD- Database password (default:password)DB_NAME- Database name (default:coppertone_db)MIGRATIONS_DIR- Path to migrations directory (default:/migrations)
Schema Features
Multi-Factor Authentication
The identity system supports multiple authentication methods per user:
- Email/password authentication
- Blockchain address authentication (with signature verification)
- Decentralized Identifiers (DIDs)
- Users can link multiple identities to a single account
IPFS Integration
Several tables include CID (Content Identifier) fields for storing references to data on IPFS:
- Project metadata
- Work order documents
- Invoice PDFs
Blockchain Support
- Identity verification via blockchain signatures
- Payment tracking with blockchain transaction hashes
- Support for multiple blockchain networks (Ethereum, Bitcoin, Polygon, etc.)
Automatic Triggers
updated_atcolumns are automatically updated on row modifications- Invoice status is automatically updated when payments are marked as completed
Adding New Migrations
-
Create a new migration pair with the next sequential number:
touch migrations/004_your_description.up.sql touch migrations/004_your_description.down.sql -
Write the SQL for applying the migration in the
.up.sqlfile -
Write the SQL for rolling back the migration in the
.down.sqlfile -
Rebuild and restart the
db-initservice:podman-compose up --build db-init
Migration Best Practices
- Always include both
.up.sqland.down.sqlfiles - Use
IF NOT EXISTSandIF EXISTSclauses to make migrations idempotent - Include appropriate indexes for foreign keys and frequently queried columns
- Use database enums for status fields to maintain data integrity
- Add comments to complex SQL for clarity
- Test migrations on a development database before applying to production