๐Ÿ“ฆ Migrations

Cardikit uses a simple migration system for creating and managing your database schema.

Migrations are PHP files that return an anonymous class extending the Migration base class. Each migration defines up() and down() methods to apply and rollback changes.


๐Ÿ“‚ Migration Files

Migration files are stored in the database/migrations/ directory. They must return a class with up() and down() methods.

Example

use App\Core\Migration;

return new class extends Migration {
    public function up(): void
    {
        $this->execute("
            CREATE TABLE IF NOT EXISTS users (
                id INT AUTO_INCREMENT PRIMARY KEY,
                name VARCHAR(255) NOT NULL,
                email VARCHAR(255) NOT NULL UNIQUE,
                password VARCHAR(255) NOT NULL,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
            )"
        );
    }

    public function down(): void
    {
        $this->execute("DROP TABLE IF EXISTS users");
    }
};

๐Ÿš€ Running Migrations

Cardikit provides CLI commands to manage your database schema.

โž• migrate

Creates all tables defined in the up() methods of your migrations.

./cardikit migrate

โœ… Automatically runs inside the Docker container if Docker is used in the .env.


๐Ÿ”„ rollback

Drops all tables by calling each migrationโ€™s down() method.

./cardikit rollback

โš ๏ธ Warning: This will remove all data and tables. Use with caution in production environments.

  • See Cli for more information about CLI commands.
  • See Config for more information about configuration files.

๐Ÿ› ๏ธ Migration Base Class

All migration classes extend:

abstract class Migration {
    protected function execute(string $sql): void
}

Use $this->execute(...) to run raw SQL queries inside up() and down().


๐Ÿง  Best Practices

  • Each migration file should focus on one logical change (e.g., creating one table).
  • Use clear, timestamped filenames (optional, but recommended for future ordering).
  • Keep backups before running rollback.