22h / questdb-migration
A lightweight migration manager for QuestDB in PHP.
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Forks: 0
pkg:composer/22h/questdb-migration
Requires
- php: ^8.4
- 22h/questdb-client: ^0.2.1
- psr/log: ^1.0|^2.0|^3.0
Requires (Dev)
- guzzlehttp/guzzle: ^7.9.2
- php-http/mock-client: ^1.0
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.0
- slevomat/coding-standard: ^8.20
- squizlabs/php_codesniffer: ^3.13
- symfony/clock: ^8.0 | ^7.0
- symfony/finder: ^8.0 | ^7.0
- symfony/var-dumper: ^7.3
Suggests
- symfony/clock: For DatePoint support
- symfony/finder: For FileMigrationLoader support
README
A lightweight package for managing database migrations for QuestDB in PHP.
This package allows you to manage versioned SQL migrations and execute them on a QuestDB instance. It supports forward migration (Up) and rolling back (Rollback) of migrations.
Features
- Easy definition of migrations via PHP classes.
- Automatic tracking of installed migrations in a QuestDB table.
- Support for Symfony Finder for automatic discovery of migration files.
- Rollback functionality.
Installation
Install the package via Composer:
composer require 22h/questdb-migration
Optional Dependencies
If you want to use the SymfonyFileMigrationLoader, also install:
composer require symfony/finder
Usage
1. Create a Migration
Create a PHP class that implements the MigrationInterface. The filename should match the class name (e.g., Version20250101000000.php).
<?php
declare(strict_types=1);
namespace App\Migrations;
use TwentyTwo\QuestDB\Client;
use TwentyTwo\QuestDBMigration\MigrationInterface;
class Version20250101000000 implements MigrationInterface
{
public function getDescription(): string
{
return 'Creates the "sensor_data" table';
}
public function up(Client $client): void
{
$client->execute()->execute(
"CREATE TABLE sensor_data (
ts TIMESTAMP,
sensor_id SYMBOL,
reading DOUBLE
) timestamp(ts) PARTITION BY DAY"
);
}
public function down(Client $client): void
{
$client->execute()->execute("DROP TABLE sensor_data");
}
}
2. Execute Migrations
Use the MigrationManager to apply the migrations:
<?php
use TwentyTwo\QuestDB\Client;
use TwentyTwo\QuestDBMigration\MigrationManager;
use TwentyTwo\QuestDBMigration\QuestDbMigrationRepository;
use TwentyTwo\QuestDBMigration\SymfonyFileMigrationLoader;
require_once 'vendor/autoload.php';
// Configure QuestDB Client
$client = new Client();
$client->setUrl('http://localhost:9000');
// Repository for tracking versions
$repository = new QuestDbMigrationRepository($client);
// Loader for finding migration classes
$loader = new SymfonyFileMigrationLoader([
'App\\Migrations' => __DIR__ . '/migrations'
]);
$manager = new MigrationManager($loader, $repository, $client);
// Execute all pending migrations
$manager->migrate();
3. Rollback (Undo the last migration)
$manager->rollback();
License
This project is licensed under the MIT License.