Overview
ElectroFix AI uses Laravel migrations to manage database schema. Migrations are version control for your database, allowing you to modify and share the application’s database schema definition.Migration Files
All migrations are located indatabase/migrations/ and run in chronological order.
Running Migrations
Run All Migrations
Run Migrations (Production)
Rollback Last Migration
Reset All Migrations
Refresh Database
Fresh Migration (Drop All Tables)
Migration List
Migrations execute in the following order:1. Core Laravel Tables
0001_01_01_000000_create_users_table.php
Creates initial user authentication tables.
Tables Created:
users- User accountspassword_reset_tokens- Password reset tokenssessions- User sessions
id- Primary keyname- User full nameemail- Unique email addressrole- User role (default: ‘worker’)email_verified_at- Email verification timestamppassword- Hashed passwordremember_token- Remember me tokentimestamps- created_at, updated_at
0001_01_01_000001_create_cache_table.php
Creates cache storage tables.
Tables Created:
cache- Cache entriescache_locks- Cache locks for atomic operations
0001_01_01_000002_create_jobs_table.php
Creates queue job tables.
Tables Created:
jobs- Queued jobsjob_batches- Batch job trackingfailed_jobs- Failed job records
2. Multi-Tenancy Foundation
2026_03_02_000100_create_companies_table.php
Creates the companies table for multi-tenant architecture.
Table: companies
Columns:
id- Primary keyname- Company nameowner_name- Owner’s full nameowner_email- Owner’s emailowner_phone- Owner’s phone numbertax_id- Tax identification number (RFC in Mexico)billing_email- Billing contact emailbilling_phone- Billing contact phoneaddress_line- Street addresscity- Citystate- State/provincecountry- Country code (default: ‘MX’)postal_code- Postal/ZIP codecurrency- Currency code (default: ‘MXN’)notes- Additional notestimestamps- created_at, updated_at
2026_03_02_000200_create_subscriptions_table.php
Creates subscription management for companies.
Table: subscriptions
Columns:
id- Primary keycompany_id- Foreign key to companies (unique, cascade delete)plan- Enum: ‘starter’, ‘pro’, ‘enterprise’, ‘developer_test’status- Enum: ‘active’, ‘trial’, ‘past_due’, ‘canceled’, ‘suspended’starts_at- Subscription start dateends_at- Subscription end datebilling_cycle- Enum: ‘monthly’, ‘yearly’user_limit- Maximum number of users allowedtimestamps- created_at, updated_at
- One subscription per company
- Cascade delete when company is deleted
2026_03_02_000300_alter_users_for_multi_tenant_and_permissions.php
Extends users table for multi-tenancy and permissions.
Adds to users table:
company_id- Foreign key to companies (null on delete)role- Modified to enum: ‘admin’, ‘worker’, ‘developer’is_active- Boolean flag for active userscan_access_billing- Permission for billing modulecan_access_inventory- Permission for inventory moduledeleted_at- Soft delete timestamp
- Composite index on (company_id, role, is_active)
3. Core Business Tables
2026_03_02_000400_create_customers_table.php
Creates customer management table.
Table: customers
Columns:
id- Primary keycompany_id- Foreign key to companies (cascade delete)name- Customer nameemail- Customer emailphone- Contact phone numberaddress- Physical addresstimestamps- created_at, updated_at
- (company_id, name)
- (company_id, email)
2026_03_02_000500_create_equipments_table.php
Creates equipment tracking table.
Table: equipments
Columns:
id- Primary keycompany_id- Foreign key to companies (cascade delete)customer_id- Foreign key to customers (cascade delete)type- Equipment type (e.g., ‘Lavadora’, ‘Refrigerador’)brand- Manufacturer brandmodel- Model number/nameserial_number- Unique serial numbertimestamps- created_at, updated_at
- (company_id, serial_number)
- (company_id, customer_id)
2026_03_02_000600_create_orders_table.php
Creates repair order management table.
Table: orders
Columns:
id- Primary keycompany_id- Foreign key to companies (cascade delete)customer_id- Foreign key to customers (cascade delete)equipment_id- Foreign key to equipments (cascade delete)technician- Assigned technician namesymptoms- Reported symptoms/issuesstatus- Enum: ‘received’, ‘diagnostic’, ‘repairing’, ‘quote’, ‘ready’, ‘delivered’, ‘not_repaired’estimated_cost- Estimated repair cost (decimal 12,2)ai_potential_causes- JSON array of AI-suggested causesai_estimated_time- AI-estimated repair timeai_suggested_parts- JSON array of suggested partsai_technical_advice- AI-generated technical advicetimestamps- created_at, updated_at
- (company_id, status, created_at)
4. Inventory Management
2026_03_02_000700_create_inventory_items_table.php
Creates inventory item management.
Table: inventory_items
Columns:
id- Primary keycompany_id- Foreign key to companies (cascade delete)name- Item nameinternal_code- Internal SKU/code (max 120 chars)quantity- Current stock quantitylow_stock_threshold- Minimum stock alert level (default: 5)is_sale_enabled- Can be sold directly to customerssale_price- Sale price (decimal 12,2, nullable)timestamps- created_at, updated_at
- Unique (company_id, internal_code)
- (company_id, quantity)
2026_03_02_000710_create_inventory_movements_table.php
Creates inventory movement tracking.
Table: inventory_movements
Columns:
id- Primary keycompany_id- Foreign key to companies (cascade delete)inventory_item_id- Foreign key to inventory_items (cascade delete)order_id- Optional foreign key to orders (null on delete)user_id- Foreign key to users (cascade delete)movement_type- Enum: ‘purchase’, ‘adjustment’, ‘sale’, ‘use_in_repair’, ‘return’quantity_change- Signed integer (positive or negative)reason- Reason for movementtimestamps- created_at, updated_at
- (company_id, created_at)
- (inventory_item_id)
2026_03_02_000720_create_notifications_table.php
Creates system notifications.
Table: notifications
Columns:
id- UUID primary keytype- Notification class namenotifiable_type- Polymorphic type (User, Company)notifiable_id- Polymorphic IDdata- JSON notification dataread_at- Timestamp when readcreated_at- When notification was created
- (notifiable_type, notifiable_id)
5. Billing System
2026_03_02_000800_add_vat_percentage_to_companies_table.php
Adds VAT configuration to companies.
Adds to companies table:
vat_percentage- Default VAT/IVA percentage (decimal 5,2, default: 16.00)
2026_03_02_000810_create_billing_documents_table.php
Creates billing document management.
Table: billing_documents
Columns:
id- Primary keycompany_id- Foreign key to companies (cascade delete)user_id- Foreign key to users who created it (cascade delete)customer_id- Optional foreign key to customers (null on delete)document_number- Unique document number (max 40 chars)document_type- Enum: ‘quote’, ‘invoice’customer_mode- Enum: ‘registered’, ‘walk_in’walk_in_name- Name for walk-in customers (max 180 chars)source- Enum: ‘repair’, ‘sale’, ‘mixed’tax_mode- Enum: ‘included’, ‘excluded’vat_percentage- VAT percentage applied (decimal 5,2)subtotal- Subtotal before tax (decimal 12,2)vat_amount- Tax amount (decimal 12,2)total- Total amount including tax (decimal 12,2)notes- Additional notesissued_at- Document issue timestamptimestamps- created_at, updated_at
- Unique (company_id, document_number)
- (company_id, document_type, created_at)
2026_03_02_000820_create_billing_document_items_table.php
Creates line items for billing documents.
Table: billing_document_items
Columns:
id- Primary keybilling_document_id- Foreign key to billing_documents (cascade delete)inventory_item_id- Optional foreign key to inventory_items (null on delete)item_kind- Enum: ‘service’, ‘product’description- Item descriptionquantity- Quantity (decimal 10,2)unit_price- Price per unit (decimal 12,2)line_subtotal- Line subtotal (decimal 12,2)line_vat- Line VAT amount (decimal 12,2)line_total- Line total (decimal 12,2)timestamps- created_at, updated_at
- (billing_document_id)
2026_03_02_000830_add_order_id_to_billing_document_items_table.php
Links billing items to repair orders.
Adds to billing_document_items table:
order_id- Optional foreign key to orders (null on delete)
6. AI Tracking & Analytics
2026_03_03_000900_add_ai_tracking_columns_to_orders_table.php
Adds AI usage tracking to orders.
Adds to orders table:
ai_diagnosed_at- Timestamp when AI diagnosis ranai_tokens_used- Number of tokens consumedai_provider- AI provider name (max 80 chars)ai_model- AI model used (max 120 chars)ai_requires_parts_replacement- Boolean flagai_cost_repair_labor- Estimated labor cost (decimal 12,2)ai_cost_replacement_parts- Estimated parts cost (decimal 12,2)ai_cost_replacement_total- Total estimated cost (decimal 12,2)
- (company_id, ai_diagnosed_at)
2026_03_03_001000_create_company_ai_usages_table.php
Creates AI usage analytics and quota tracking.
Table: company_ai_usages
Columns:
id- Primary keycompany_id- Foreign key to companies (cascade delete)order_id- Optional foreign key to orders (null on delete)year_month- Period identifier (YYYY-MM format, 7 chars)plan_snapshot- Plan at time of usage (max 40 chars)prompt_chars- Character count of promptresponse_chars- Character count of responseprompt_tokens_estimated- Estimated tokens in promptresponse_tokens_estimated- Estimated tokens in responsetotal_tokens_estimated- Total estimated tokensstatus- Enum: ‘success’, ‘blocked_plan’, ‘blocked_quota’, ‘blocked_tokens’, ‘error’error_message- Error details if failedtimestamps- created_at, updated_at
- (company_id, year_month)
- (company_id, status)
- (order_id)
Database Schema Overview
Relationships
Multi-Tenancy
All business data is scoped bycompany_id, ensuring complete data isolation between companies:
- customers
- equipments
- orders
- inventory_items
- inventory_movements
- billing_documents
- company_ai_usages
Cascade Behavior
Cascade Delete (when parent is deleted, children are deleted):- Company → All related records
- Customer → Equipments, Orders
- Equipment → Orders
- Inventory Item → Movements
- Billing Document → Billing Document Items
- Order deletion → Inventory Movements, Billing Items, AI Usages
- Customer deletion → Billing Documents
- Inventory Item deletion → Billing Document Items
Migration Dependencies
Migrations must run in order due to foreign key dependencies:- Core tables (users, cache, jobs)
- Companies
- Subscriptions (depends on companies)
- Users extensions (depends on companies)
- Customers (depends on companies)
- Equipments (depends on companies, customers)
- Orders (depends on companies, customers, equipments)
- Inventory (depends on companies)
- Inventory movements (depends on inventory, orders, users)
- Billing documents (depends on companies, users, customers)
- Billing items (depends on billing_documents, inventory, orders)
- AI tracking (depends on orders, companies)
Next Steps
- Seeders Reference - Learn about demo data structure
- Configuration Guide - Database configuration
- Local Development - Run migrations locally