Database Connections
Relevant source files
The following files were used as context for generating this wiki page:
Database connections in this project are managed through a centralized configuration system that supports multiple database engines, including SQLite, MySQL, MariaDB, PostgreSQL, and SQL Server. This system allows the application to interact with various storage backends for primary data, session persistence, and background job queuing.
The configuration is primarily driven by environment variables, enabling seamless transitions between development, testing, and production environments. For instance, the testing environment typically utilizes an in-memory SQLite database to ensure speed and isolation during test execution.
Sources: config/database.php:12-20, phpunit.xml:27-28
Connection Architecture
The application uses a driver-based architecture for database interactions. Each connection defined in the configuration specifies a driver that dictates how the application communicates with the underlying database system.
Supported Drivers and Configurations
| Driver | Default Port | Default Charset | Description |
|---|---|---|---|
sqlite | N/A | N/A | Self-contained, file-based database. |
mysql | 3306 | utf8mb4 | Popular open-source relational database. |
mariadb | 3306 | utf8mb4 | Community-developed fork of MySQL. |
pgsql | 5432 | utf8 | Advanced open-source relational database. |
sqlsrv | 1433 | utf8 | Microsoft SQL Server. |
Sources: config/database.php:26-95
Configuration Flow
The following diagram illustrates how the application resolves database configuration from environment variables to active connections.
This flow ensures that while multiple connections can be defined, only one is used as the default unless explicitly overridden.
Sources: config/database.php:18-20, config/database.php:22-96
Database Schema and Migrations
The project utilizes a migration system to manage database schema versioning. Migrations define the structure of tables such as users, sessions, and jobs.
Primary Data Structures
The core schema includes tables for user management and session tracking.
| Table | Purpose | Key Fields |
|---|---|---|
users | Stores user account information. | id, name, email, password |
sessions | Stores application session data. | id, user_id, ip_address, payload |
password_reset_tokens | Manages security tokens for password resets. | email, token, created_at |
Sources: database/migrations/0001_01_01_000000_create_users_table.php:13-37
Queue and Job Management
Background tasks are handled via specific database tables when the database queue driver is utilized.
The diagram shows the relationship between active jobs, batched execution, and error logging for failed background tasks.
Sources: database/migrations/0001_01_01_000002_create_jobs_table.php:13-48
Redis Integration
Redis is supported as an advanced key-value store, often used for caching and high-performance queuing. The configuration allows for a primary "default" connection and a dedicated "cache" connection to separate concerns.
Redis Configuration Options
- Client: Default is
phpredis. - Prefix: Automatically generated using the application name to avoid key collisions.
- Retries: Configurable
max_retries(default 3) with adecorrelated_jitterbackoff algorithm.
Sources: config/database.php:118-154
System-Level Database Usage
The database connection system is leveraged by other core modules of the application to persist state.
Session Management
The session system can be configured to use a database connection to store user state across requests.
- Driver:
database(controlled bySESSION_DRIVER). - Table:
sessions. - Connection: Optional specific connection via
SESSION_CONNECTION.
Sources: config/session.php:23-74
Queue Management
When using the database as a queue backend, the system tracks job status through the jobs table.
- Default Connection: Defined by
QUEUE_CONNECTION. - Retry Policy:
retry_aftersetting (default 90 seconds) determines when a job is released back to the queue if not finished.
Sources: config/queue.php:18-53
Conclusion
The database connection system provides a robust and flexible foundation for data persistence. By abstracting the specific database engine behind a unified configuration and migration system, the application maintains high portability and ease of maintenance across different infrastructure environments.