Overview
Macuin uses PostgreSQL 16 as its primary database, running in a Docker container. The database schema is managed through Laravel migrations, providing version control for your database structure.Database Configuration
Docker Setup
The PostgreSQL database runs in a Docker container defined indocker-compose.yml:
docker-compose.yml
The database container includes a health check that ensures PostgreSQL is ready before the application container starts.
Laravel Configuration
Database connection settings are configured inconfig/database.php. The default connection is set via environment variables:
config/database.php
Environment Variables
The application container is configured with these database environment variables:docker-compose.yml
Database
Name: laravel
Port: 5432
Port: 5432
Credentials
User: laravel
Password: secret
Password: secret
Database Schema
Migrations
Macuin includes three core migrations that create the foundational database structure:1. Users and Authentication Tables
File:0001_01_01_000000_create_users_table.php
This migration creates tables for user management and authentication:
users Table
| Column | Type | Description |
|---|---|---|
id | bigint | Primary key |
name | varchar | User’s full name |
email | varchar | Email address (unique) |
email_verified_at | timestamp | Email verification time |
password | varchar | Hashed password |
remember_token | varchar | ”Remember me” token |
created_at | timestamp | Record creation time |
updated_at | timestamp | Last update time |
password_reset_tokens Table
| Column | Type | Description |
|---|---|---|
email | varchar | User email (primary key) |
token | varchar | Password reset token |
created_at | timestamp | Token creation time |
sessions Table
| Column | Type | Description |
|---|---|---|
id | varchar | Session ID (primary key) |
user_id | bigint | Foreign key to users |
ip_address | varchar | Client IP address |
user_agent | text | Browser user agent |
payload | longtext | Serialized session data |
last_activity | integer | Unix timestamp of last activity |
2. Cache Tables
File:0001_01_01_000001_create_cache_table.php
Tables for Laravel’s database cache driver:
cache Table
| Column | Type | Description |
|---|---|---|
key | varchar | Cache key (primary key) |
value | mediumtext | Cached value |
expiration | integer | Expiration timestamp |
cache_locks Table
| Column | Type | Description |
|---|---|---|
key | varchar | Lock key (primary key) |
owner | varchar | Lock owner identifier |
expiration | integer | Lock expiration timestamp |
3. Queue and Job Tables
File:0001_01_01_000002_create_jobs_table.php
Tables for Laravel’s queue system:
jobs Table
| Column | Type | Description |
|---|---|---|
id | bigint | Primary key |
queue | varchar | Queue name (indexed) |
payload | longtext | Serialized job data |
attempts | tinyint | Number of attempts |
reserved_at | integer | When job was reserved |
available_at | integer | When job becomes available |
created_at | integer | Job creation timestamp |
failed_jobs Table
| Column | Type | Description |
|---|---|---|
id | bigint | Primary key |
uuid | varchar | Unique job identifier |
connection | text | Queue connection |
queue | text | Queue name |
payload | longtext | Job payload |
exception | longtext | Exception details |
failed_at | timestamp | Failure timestamp |
Models
User Model
TheUser model is located at app/Models/User.php:
app/Models/User.php
The
password attribute is automatically hashed when set, thanks to the 'password' => 'hashed' cast.Running Migrations
To run migrations in the Docker environment:Migrations are tracked in the
migrations table, which is automatically created by Laravel.Database Tools
Connecting to PostgreSQL
Connect to the database directly:Useful PostgreSQL Commands
Best Practices
Always Use Migrations
Never modify the database schema directly. Always create migrations for schema changes.
Test Migrations
Test rollback functionality to ensure migrations can be reversed safely.
Seed Test Data
Use seeders to populate test data for development environments.
Backup Production
Always backup production data before running migrations.