This is a utility library and commands for managing database migrations in SQL.
Use Composer
composer require alphasoft-fr/sql-migration
- PHP version 8.1
- Create a configuration file named
migration-config.php
at the root of your project. You can use the following example as a starting point:
<?php
return [
'connection' => new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password'),
'migrations_directory' => __DIR__ . '/migrations',
// Other configuration options...
];
- Customize the configuration options according to your needs. You can provide the PDO connection instance and specify the directory where migration files will be stored.
You can generate a new migration file using the following command:
php vendor/bin/sqlmigration sql:migration:generate
This will create a new migration file in the specified migrations directory with placeholder content for both the up and down migrations.
Modify the generated migration file with SQL queries corresponding to the intended migration:
-- UP MIGRATION --
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- DOWN MIGRATION --
DROP TABLE users;
You can apply pending migrations using the following command:
php vendor/bin/sqlmigration sql:migration:migrate
This command will execute all pending migrations in ascending order of their version numbers. Successfully applied migrations will be displayed in the console output.
You can revert the last applied migration using the following command:
php vendor/bin/sqlmigration sql:migration:down <version>
Replace <version>
with the version number of the migration you want to revert. This command will execute the down migration for the specified version, effectively rolling back the changes made by that migration.
If you encounter any issues or have suggestions for improvements, please feel free to open an issue on the GitHub repository.
This project is licensed under the MIT License. See the LICENSE file for details.