Migrations
in package
Class Migrations
The Migrations class is responsible for handling database migration operations. It provides functionality to adapt SQL migration queries by replacing placeholders with actual values and manages the underlying database configuration and datetime operations required for migration processes.
This class serves as a utility for database schema evolution, allowing developers to version control database changes and apply them systematically across different environments through placeholder replacement and configuration management.
Table of Contents
Properties
Methods
- __construct() : void
- Class constructor
- adaptMigration() : string
- Adapts SQL migration queries by replacing placeholders with actual values
- applyMigrations() : void
- Applies all migrations that haven't been applied yet.
- prepare() : PDOStatement
- Prepares a SQL statement via the application's PDO connection.
- createMigrationsTable() : void
- Creates the migrations table in the database.
- getAppliedMigrations() : array<string|int, mixed>
- Returns an array of all applied migrations.
- saveMigrations() : void
- Saves the new migrations to the database.
- log() : void
- Logs a message with a timestamp to the console
Properties
$config
public
array<string|int, mixed>
$config
$database
public
Database
$database
$datetime
public
DateTime
$datetime
Methods
__construct()
Class constructor
public
__construct(Database $database, DateTime $datetime) : void
Initializes a new instance of the class and stores references to the database and datetime objects. This constructor accepts two dependencies through the parameters and assigns them to their respective class properties, ensuring that the class has access to database configuration and datetime functionality.
Parameters
- $database : Database
-
The database instance used for database operations
- $datetime : DateTime
-
The datetime instance used for date/time operations
adaptMigration()
Adapts SQL migration queries by replacing placeholders with actual values
public
adaptMigration(string $sql) : string
This public method takes an SQL statement as a string and replaces various placeholders with actual database and system information. It's commonly used in database migration scripts to make them dynamic and environment-specific.
Placeholders replaced:
- {{prefix}} - Database table prefix from configuration
- {{hostname}} - Hostname of the server
- {{osname}} - Operating system name
- {{osversion}} - Operating system version
- {{osip}} - Server IP address
- {{datetimeutcsql}} - Current UTC datetime in SQL format
- {{domain}} - Database URI from configuration
Parameters
- $sql : string
-
The SQL statement containing placeholders to be replaced
Return values
string —The SQL statement with all placeholders replaced with actual values
applyMigrations()
Applies all migrations that haven't been applied yet.
public
applyMigrations() : void
This method checks which migrations have been applied and then applies all migrations that haven't been applied yet.
prepare()
Prepares a SQL statement via the application's PDO connection.
public
prepare(string $sql) : PDOStatement
This method takes a raw SQL string and binds it as a named parameter via the application's PDO connection.
Parameters
- $sql : string
-
Raw SQL with named placeholders.
Return values
PDOStatement —Prepared statement.
createMigrationsTable()
Creates the migrations table in the database.
protected
createMigrationsTable() : void
This method creates the migrations table in the database if it doesn't already exist. The table is used to keep track of which migrations have been applied.
getAppliedMigrations()
Returns an array of all applied migrations.
protected
getAppliedMigrations() : array<string|int, mixed>
This method returns an array of all applied migrations. The array contains the names of the migrations that have been applied.
Return values
array<string|int, mixed> —An array of all applied migrations.
saveMigrations()
Saves the new migrations to the database.
protected
saveMigrations(array<string|int, mixed> $newMigrations) : void
This method saves the new migrations to the database. It takes an array of new migrations as an argument.
Parameters
- $newMigrations : array<string|int, mixed>
-
An array of new migrations to save to the database.
log()
Logs a message with a timestamp to the console
private
log(string $message) : void
This private method takes a message as input and outputs it with a timestamp in the format [YYYY-MM-DD HH:MM:SS] followed by a new line character. The method uses PHP's built-in date() function to generate the current timestamp and outputs the formatted message using echo.
Parameters
- $message : string
-
The message to be logged with timestamp