From 8582ca06627bfd0da5199b0ac0e4d02eac095f75 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Mon, 24 Mar 2025 16:20:09 -0500 Subject: [PATCH 1/8] Upgrade PHP 8.4 and Fix Versions --- .github/FUNDING.md | 1 + .github/workflows/phpunit.yml | 3 +-- .gitpod.yml | 28 +++++++++++++++++++++++ .vscode/launch.json | 35 ++++++++++++++++++++++++++++ README.md | 5 +--- composer.json | 8 +++---- docker-compose.yml | 6 +---- phpunit.xml.dist | 29 +++++++++++------------ tests/BaseDatabase.php | 43 +++++++++++++++++++++-------------- tests/MigrationTest.php | 6 ++++- tests/MysqlDatabaseTest.php | 8 ++++++- 11 files changed, 124 insertions(+), 48 deletions(-) create mode 100644 .github/FUNDING.md create mode 100644 .gitpod.yml create mode 100644 .vscode/launch.json diff --git a/.github/FUNDING.md b/.github/FUNDING.md new file mode 100644 index 0000000..163530b --- /dev/null +++ b/.github/FUNDING.md @@ -0,0 +1 @@ +github: byjg \ No newline at end of file diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 2cc2446..ae4a3fa 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -8,8 +8,6 @@ on: pull_request: branches: - master - schedule: - - cron: "0 9 * * 1" jobs: Build: @@ -18,6 +16,7 @@ jobs: strategy: matrix: php-version: + - "8.4" - "8.3" - "8.2" - "8.1" diff --git a/.gitpod.yml b/.gitpod.yml new file mode 100644 index 0000000..335de09 --- /dev/null +++ b/.gitpod.yml @@ -0,0 +1,28 @@ +tasks: + - name: Run Composer + command: | + composer install + +image: byjg/gitpod-image:latest + +jetbrains: + phpstorm: + vmoptions: '-Xmx4g' + plugins: + - com.github.copilot + - com.intellij.kubernetes + - com.intellij.mermaid + - ru.adelf.idea.dotenv + - org.toml.lang + +vscode: + extensions: + - ikappas.composer + - hbenl.test-adapter-converter + - hbenl.vscode-test-explorer + - felixfbecker.php-debug + - neilbrayfield.php-docblocker + - bmewburn.vscode-intelephense-client + - getpsalm.psalm-vscode-plugin + - SonarSource.sonarlint-vscode + - recca0120.vscode-phpunit \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..2a5aac0 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,35 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Debug current Script in Console", + "type": "php", + "request": "launch", + "program": "${file}", + "cwd": "${fileDirname}", + "port": 9003, + "runtimeArgs": [ + "-dxdebug.start_with_request=yes" + ], + "env": { + "XDEBUG_MODE": "debug,develop", + "XDEBUG_CONFIG": "client_port=${port}" + } + }, + { + "name": "PHPUnit Debug", + "type": "php", + "request": "launch", + "program": "${workspaceFolder}/vendor/bin/phpunit", + "cwd": "${workspaceFolder}", + "port": 9003, + "runtimeArgs": [ + "-dxdebug.start_with_request=yes" + ], + "env": { + "XDEBUG_MODE": "debug,develop", + "XDEBUG_CONFIG": "client_port=${port}" + } + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 55544b5..ab8b864 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,8 @@ -# Database Migrations PHP - +[![Build Status](https://github.com/byjg/php-migration/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/php-migration/actions/workflows/phpunit.yml) [![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com) [![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/php-migration/) [![GitHub license](https://img.shields.io/github/license/byjg/php-migration.svg)](https://opensource.byjg.com/opensource/licensing.html) [![GitHub release](https://img.shields.io/github/release/byjg/php-migration.svg)](https://github.com/byjg/php-migration/releases/) -[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/byjg/migration/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/byjg/migration/?branch=master) -[![Build Status](https://github.com/byjg/migration/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/migration/actions/workflows/phpunit.yml) ## Features diff --git a/composer.json b/composer.json index 66a73ef..8dd99a0 100644 --- a/composer.json +++ b/composer.json @@ -5,12 +5,12 @@ "prefer-stable": true, "require": { "ext-pdo": "*", - "byjg/anydataset-db": "^5.0" + "byjg/anydataset-db": "^6.0", + "php": ">=8.1 <8.5" }, "require-dev": { - "php": ">=8.1 <8.4", - "phpunit/phpunit": "^9.6", - "vimeo/psalm": "^5.9" + "phpunit/phpunit": "^10.5|^11.5", + "vimeo/psalm": "^5.9|^6.2" }, "autoload": { "psr-4": { diff --git a/docker-compose.yml b/docker-compose.yml index 8b1f7a7..80ab50d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,6 @@ version: '3.4' services: mssql: - container_name: anydataset_db_mssql image: mcr.microsoft.com/mssql/server environment: - ACCEPT_EULA=Y @@ -9,13 +8,12 @@ services: ports: - "1433:1433" healthcheck: - test: ["CMD", "/opt/mssql-tools/bin/sqlcmd", "-S", "localhost,1433", "-U", "sa", "-P", "Pa55word", "-Q", "SELECT 1"] + test: ["CMD", "/opt/mssql-tools18/bin/sqlcmd", "-C", "-S", "localhost,1433", "-U", "sa", "-P", "Pa55word", "-Q", "SELECT 1"] timeout: 20s interval: 10s retries: 10 postgres: - container_name: anydataset_db_postgres image: postgres:14-alpine environment: - POSTGRES_USER=postgres @@ -29,9 +27,7 @@ services: retries: 10 mysql: - container_name: anydataset_db_mysql image: mysql:8 - command: --default-authentication-plugin=mysql_native_password environment: - MYSQL_ROOT_PASSWORD=password ports: diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 23c7c36..21585f4 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -6,14 +6,17 @@ and open the template in the editor. --> - + displayDetailsOnTestsThatTriggerDeprecations="true" + displayDetailsOnTestsThatTriggerErrors="true" + displayDetailsOnTestsThatTriggerNotices="true" + displayDetailsOnTestsThatTriggerWarnings="true" + displayDetailsOnPhpunitDeprecations="true" + stopOnFailure="false" + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"> @@ -21,17 +24,15 @@ and open the template in the editor. - - - ./src - - + + + ./src/ + + - - ./tests/SqliteDatabaseTest.php - ./tests/MigrationTest.php + ./tests/ diff --git a/tests/BaseDatabase.php b/tests/BaseDatabase.php index a40a5a6..ab52b0e 100644 --- a/tests/BaseDatabase.php +++ b/tests/BaseDatabase.php @@ -12,6 +12,7 @@ use ByJG\Serializer\Exception\InvalidArgumentException; use PHPUnit\Framework\TestCase; use Psr\Http\Message\UriInterface; +use PHPUnit\Framework\Attributes\Override; abstract class BaseDatabase extends TestCase { @@ -27,6 +28,7 @@ abstract class BaseDatabase extends TestCase /** * @throws DatabaseDoesNotRegistered */ + #[Override] public function setUp(): void { // create Migrate object in the parent!!! @@ -37,6 +39,7 @@ public function setUp(): void /** * @throws DatabaseDoesNotRegistered */ + #[Override] public function tearDown(): void { $this->migrate->getDbCommand()->dropDatabase(); @@ -160,21 +163,23 @@ protected function assertVersion0(): void $iterator = $this->migrate->getDbDriver()->getIterator('select * from users'); - $this->assertTrue($iterator->hasNext()); - $row = $iterator->moveNext(); + $this->assertNotNull($iterator->current()); + $row = $iterator->current(); $this->assertEquals( $this->getExpectedUsersVersion0()[0], $row->toArray() ); - $this->assertTrue($iterator->hasNext()); - $row = $iterator->moveNext(); + $iterator->next(); + $this->assertNotNull($iterator->current()); + $row = $iterator->current(); $this->assertEquals( $this->getExpectedUsersVersion0()[1], $row->toArray() ); - $this->assertFalse($iterator->hasNext()); + $iterator->next(); + $this->assertNull($iterator->current()); try { $this->migrate->getDbDriver()->getIterator('select * from roles'); @@ -195,21 +200,23 @@ protected function assertVersion1(): void $iterator = $this->migrate->getDbDriver()->getIterator('select * from users'); - $this->assertTrue($iterator->hasNext()); - $row = $iterator->moveNext(); + $this->assertNotNull($iterator->current()); + $row = $iterator->current(); $this->assertEquals( $this->getExpectedUsersVersion1()[0], $row->toArray() ); - $this->assertTrue($iterator->hasNext()); - $row = $iterator->moveNext(); + $iterator->next(); + $this->assertNotNull($iterator->current()); + $row = $iterator->current(); $this->assertEquals( $this->getExpectedUsersVersion1()[1], $row->toArray() ); - $this->assertFalse($iterator->hasNext()); + $iterator->next(); + $this->assertNull($iterator->current()); try { $this->migrate->getDbDriver()->getIterator('select * from roles'); @@ -232,27 +239,29 @@ protected function assertVersion2() // Users $iterator = $this->migrate->getDbDriver()->getIterator('select * from users'); - $this->assertTrue($iterator->hasNext()); - $row = $iterator->moveNext(); + $this->assertNotNull($iterator->current()); + $row = $iterator->current(); $this->assertEquals( $this->getExpectedUsersVersion1()[0], $row->toArray() ); - $this->assertTrue($iterator->hasNext()); - $row = $iterator->moveNext(); + $iterator->next(); + $this->assertNotNull($iterator->current()); + $row = $iterator->current(); $this->assertEquals( $this->getExpectedUsersVersion1()[1], $row->toArray() ); - $this->assertFalse($iterator->hasNext()); + $iterator->next(); + $this->assertNull($iterator->current()); // Posts $iterator = $this->migrate->getDbDriver()->getIterator('select * from posts'); - $this->assertTrue($iterator->hasNext()); - $row = $iterator->moveNext(); + $this->assertNotNull($iterator->current()); + $row = $iterator->current(); $this->assertEquals( $this->getExpectedPostsVersion2()[0], $row->toArray() diff --git a/tests/MigrationTest.php b/tests/MigrationTest.php index 45d2a2c..ef1543a 100644 --- a/tests/MigrationTest.php +++ b/tests/MigrationTest.php @@ -5,19 +5,23 @@ use ByJG\DbMigration\Exception\InvalidMigrationFile; use ByJG\DbMigration\Migration; use ByJG\Util\Uri; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\Override; -class MigrationTest extends \PHPUnit\Framework\TestCase +class MigrationTest extends TestCase { /** * @var Migration */ protected $object; + #[Override] public function setUp(): void { $this->object = new Migration(new Uri('mysql://localhost'), __DIR__ . '/dirstructure'); } + #[Override] public function tearDown(): void { $this->object = null; diff --git a/tests/MysqlDatabaseTest.php b/tests/MysqlDatabaseTest.php index 58063b1..ccec6bc 100644 --- a/tests/MysqlDatabaseTest.php +++ b/tests/MysqlDatabaseTest.php @@ -3,6 +3,8 @@ namespace Tests; use ByJG\DbMigration\Database\MySqlDatabase; +use ByJG\DbMigration\Exception\DatabaseDoesNotRegistered; +use ByJG\DbMigration\Exception\InvalidMigrationFile; use ByJG\DbMigration\Migration; use ByJG\Util\Uri; @@ -12,10 +14,14 @@ class MysqlDatabaseTest extends BaseDatabase { /** - * @var Migration + * @var Migration|null */ protected ?Migration $migrate = null; + /** + * @throws DatabaseDoesNotRegistered + * @throws InvalidMigrationFile + */ public function setUp(): void { $host = getenv('MYSQL_TEST_HOST'); From 747c62dff3de15f988d183ad92b55de13780525d Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Mon, 24 Mar 2025 19:21:36 -0500 Subject: [PATCH 2/8] Fix new version. --- .idea/runConfigurations/PHPUnit.xml | 2 +- .idea/runConfigurations/Run_Databases.xml | 1 + src/Database/AbstractDatabase.php | 15 +++++++++++++-- src/Database/DblibDatabase.php | 20 ++++++++++++-------- src/Database/MySqlDatabase.php | 13 +++++-------- src/Database/PgsqlDatabase.php | 14 ++++---------- tests/BaseDatabase.php | 11 ++++++++--- tests/PostgresDatabaseTest.php | 2 +- tests/SqlServerDblibDatabaseTest.php | 12 ++---------- 9 files changed, 47 insertions(+), 43 deletions(-) diff --git a/.idea/runConfigurations/PHPUnit.xml b/.idea/runConfigurations/PHPUnit.xml index 5b1e3d2..f81c245 100644 --- a/.idea/runConfigurations/PHPUnit.xml +++ b/.idea/runConfigurations/PHPUnit.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/runConfigurations/Run_Databases.xml b/.idea/runConfigurations/Run_Databases.xml index 56bd727..c561741 100644 --- a/.idea/runConfigurations/Run_Databases.xml +++ b/.idea/runConfigurations/Run_Databases.xml @@ -2,6 +2,7 @@ + + \ No newline at end of file diff --git a/psalm.xml b/psalm.xml index ebabb1a..b208114 100644 --- a/psalm.xml +++ b/psalm.xml @@ -4,6 +4,7 @@ resolveFromConfigFile="true" findUnusedBaselineEntry="true" findUnusedCode="false" + cacheDirectory="/tmp/psalm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" diff --git a/src/Migration.php b/src/Migration.php index d61fd4a..19050ad 100644 --- a/src/Migration.php +++ b/src/Migration.php @@ -238,7 +238,7 @@ public function prepareEnvironment(): void * @throws InvalidMigrationFile * @throws OldVersionSchemaException */ - public function reset(int $upVersion = null): void + public function reset(?int $upVersion = null): void { $fileInfo = $this->getFileContent($this->getBaseSql()); From 2d1e8ae699000bfff42ee2c04515f8381e6a457c Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Fri, 7 Nov 2025 09:54:03 -0500 Subject: [PATCH 7/8] Minor Fixes --- .gitignore | 3 +++ composer.json | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c51a039..25e8827 100644 --- a/.gitignore +++ b/.gitignore @@ -51,4 +51,7 @@ fabric.properties node_modules package-lock.json .usdocker +phpunit.coverage.xml +phpunit.report.xml .phpunit.result.cache +*.bak diff --git a/composer.json b/composer.json index 98e7ed5..253fd37 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ }, "require-dev": { "phpunit/phpunit": "^10.5|^11.5", - "vimeo/psalm": "^5.9|^6.2" + "vimeo/psalm": "^5.9|^6.12" }, "autoload": { "psr-4": { From 2d8242d73744786dbcc05b00017d018632a91c83 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 7 Nov 2025 15:43:49 +0000 Subject: [PATCH 8/8] Enhance documentation with Docusaurus features and fix accuracy issues - Fix API reference: correct class names (PgsqlDatabase, DblibDatabase) and getCurrentVersion() return type - Add Docusaurus frontmatter (title, description) to all documentation files - Add Docusaurus admonitions (:::note, :::tip, :::warning, :::info) for better readability - Improve documentation clarity with contextual warnings and tips - Maintain sidebar_position order matching README.md (1-5) - All docs follow Docusaurus markdown standards --- docs/api-reference.md | 15 +++++++++++---- docs/cli-usage.md | 2 ++ docs/database-setup.md | 8 ++++++-- docs/getting-started.md | 6 +++++- docs/migration-scripts.md | 10 ++++++++++ 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/docs/api-reference.md b/docs/api-reference.md index 1d1e275..b41b5f3 100644 --- a/docs/api-reference.md +++ b/docs/api-reference.md @@ -1,5 +1,7 @@ --- sidebar_position: 5 +title: API Reference +description: Complete PHP API documentation for database migrations --- # API Reference @@ -36,8 +38,9 @@ public function createVersion(): void // Check if database is versioned public function isDatabaseVersioned(): bool -// Get current version -public function getCurrentVersion(): int +// Get current version and status +// Returns: ['version' => int, 'status' => string] +public function getCurrentVersion(): array // Update version table structure public function updateTableVersion(): void @@ -124,9 +127,9 @@ interface DatabaseInterface ### Built-in Handlers - `MySqlDatabase`: MySQL/MariaDB -- `PostgresDatabase`: PostgreSQL +- `PgsqlDatabase`: PostgreSQL - `SqliteDatabase`: SQLite -- `SqlServerDatabase`: Microsoft SQL Server +- `DblibDatabase`: Microsoft SQL Server (both dblib and sqlsrv) ## Examples @@ -155,6 +158,10 @@ $migration->addCallbackProgress(function ($action, $version, $fileInfo) { ### With Transaction Support +:::note Transaction Support +Transaction support is not available for MySQL as it doesn't support DDL commands inside transactions. The setting will be silently ignored for MySQL databases. +::: + ```php $migration ->withTransactionEnabled(true) diff --git a/docs/cli-usage.md b/docs/cli-usage.md index 64b6b8a..06623b3 100644 --- a/docs/cli-usage.md +++ b/docs/cli-usage.md @@ -1,5 +1,7 @@ --- sidebar_position: 4 +title: CLI Usage +description: Command line interface guide for database migrations --- # Command Line Interface Usage diff --git a/docs/database-setup.md b/docs/database-setup.md index d96462a..27d49f2 100644 --- a/docs/database-setup.md +++ b/docs/database-setup.md @@ -1,5 +1,7 @@ --- sidebar_position: 2 +title: Database Setup +description: Configure database connections for MySQL, PostgreSQL, SQLite, and SQL Server --- # Database Setup and Configuration @@ -37,8 +39,10 @@ The library supports the following databases: ``` 4. Transaction Limitations: - - DDL statements not supported in transactions - - Transaction settings ignored for DDL + +:::warning MySQL Transaction Limitations +MySQL does not support DDL (Data Definition Language) statements within transactions. Any transaction settings will be ignored for DDL operations like CREATE TABLE, ALTER TABLE, etc. +::: ### PostgreSQL diff --git a/docs/getting-started.md b/docs/getting-started.md index 2c62299..98f5a2f 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -1,5 +1,7 @@ --- sidebar_position: 1 +title: Getting Started +description: Get started with PHP Database Migration - a framework-agnostic database versioning tool using pure SQL --- # Getting Started with PHP Database Migration @@ -28,7 +30,9 @@ composer require "byjg/migration" composer require "byjg/migration-cli" ``` -For CLI usage, see [CLI documentation](cli-usage.md). +:::tip +For CLI usage, see the [CLI documentation](cli-usage.md) for detailed commands and options. +::: ## Basic Usage diff --git a/docs/migration-scripts.md b/docs/migration-scripts.md index 719a965..d711853 100644 --- a/docs/migration-scripts.md +++ b/docs/migration-scripts.md @@ -1,5 +1,7 @@ --- sidebar_position: 3 +title: Migration Scripts +description: Learn how to write and organize database migration scripts --- # Writing Migration Scripts @@ -23,6 +25,10 @@ The number represents the version that the script will migrate to (up) or from ( ## Multi-Developer Workflow +:::info Handling Multiple Developers +When multiple developers work on different branches, use the `-dev` suffix to avoid version conflicts. +::: + When multiple developers work on different branches: 1. Use `-dev` suffix for work in progress: @@ -73,6 +79,10 @@ When multiple developers work on different branches: ### PostgreSQL-Specific Tips +:::warning Important for PostgreSQL Functions +Always add `--` comments after semicolons inside function definitions to prevent parsing errors. +::: + 1. Use `--` after semicolons in functions: ```sql CREATE FUNCTION update_timestamp() RETURNS trigger AS $$