Skip to content

Commit 1cad454

Browse files
committed
Update README, improve database handling, and upgrade dependencies
- Add sponsor badge to README - Enhance environment preparation logic for `Uri` handling across databases - Ensure proper handling of empty SQL statements in database classes - Upgrade PHP requirement to `>=8.3` and dependencies to latest versions - Refactor `Migration` methods for better error handling and type safety - Update Psalm configuration: adjust error level, exclude test directory - Revise GitHub Actions to drop PHP 8.1 and 8.2, add PHP 8.5, and separate Psalm workflow - Improve composer scripts and static analysis setup
1 parent 6de7da8 commit 1cad454

File tree

9 files changed

+77
-29
lines changed

9 files changed

+77
-29
lines changed

.github/workflows/phpunit.yml

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ jobs:
1818
strategy:
1919
matrix:
2020
php-version:
21+
- "8.5"
2122
- "8.4"
2223
- "8.3"
23-
- "8.2"
24-
- "8.1"
2524

2625
services:
2726
mysql:
@@ -66,21 +65,41 @@ jobs:
6665

6766
steps:
6867
- uses: actions/checkout@v5
69-
# - name: Spin up databases
70-
# run: |
71-
# apk add --no-cache python3 python3-dev py3-pip build-base libffi-dev
72-
# pip3 install --upgrade pip
73-
# pip3 install docker-compose
74-
# apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/community docker
75-
# docker-compose up -d postgres mysql
7668
- run: composer install
77-
- run: ./vendor/bin/psalm
78-
- run: ./vendor/bin/phpunit
69+
- run: composer test
7970
- run: ./vendor/bin/phpunit tests/SqliteDatabase*
8071
- run: ./vendor/bin/phpunit tests/MysqlDatabase*
8172
- run: ./vendor/bin/phpunit tests/PostgresDatabase*
82-
# - run: ./vendor/bin/phpunit tests/SqlServerSqlsrv*
83-
# - run: ./vendor/bin/phpunit tests/SqlServerDblib*
73+
74+
Psalm:
75+
name: Psalm Static Analyzer
76+
runs-on: ubuntu-latest
77+
permissions:
78+
# for github/codeql-action/upload-sarif to upload SARIF results
79+
security-events: write
80+
container:
81+
image: byjg/php:8.4-cli
82+
options: --user root --privileged
83+
84+
steps:
85+
- name: Git checkout
86+
uses: actions/checkout@v4
87+
88+
- name: Composer
89+
run: composer install
90+
91+
- name: Psalm
92+
# Note: Ignoring error code 2, which just signals that some
93+
# flaws were found, not that Psalm itself failed to run.
94+
run: ./vendor/bin/psalm
95+
--show-info=true
96+
--report=psalm-results.sarif || [ $? = 2 ]
97+
98+
- name: Upload Analysis results to GitHub
99+
uses: github/codeql-action/upload-sarif@v4
100+
if: github.ref == 'refs/heads/master'
101+
with:
102+
sarif_file: psalm-results.sarif
84103

85104
Documentation:
86105
if: github.ref == 'refs/heads/master'

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Database Migration
22

3+
[![Sponsor](https://img.shields.io/badge/Sponsor-%23ea4aaa?logo=githubsponsors&logoColor=white&labelColor=0d1117)](https://github.com/sponsors/byjg)
34
[![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)
45
[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)
56
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/php-migration/)

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
22
"name": "byjg/migration",
3-
"description": "Simple library in PHP for database version control. Supports Sqlite, MySql, Sql Server and Postgres.",
3+
"description": "A simple, framework-agnostic database migration tool that uses pure SQL commands for versioning your database.",
44
"minimum-stability": "dev",
55
"prefer-stable": true,
66
"require": {
77
"ext-pdo": "*",
88
"byjg/anydataset-db": "^6.0",
9-
"php": ">=8.1 <8.5"
9+
"php": ">=8.3 <8.6"
1010
},
1111
"require-dev": {
1212
"phpunit/phpunit": "^10.5|^11.5",
13-
"vimeo/psalm": "^5.9|^6.12"
13+
"vimeo/psalm": "^6.13"
1414
},
1515
"autoload": {
1616
"psr-4": {
@@ -27,7 +27,7 @@
2727
],
2828
"scripts": {
2929
"test": "vendor/bin/phpunit",
30-
"psalm": "vendor/bin/psalm"
30+
"psalm": "vendor/bin/psalm --threads=1"
3131
},
3232
"license": "MIT"
3333
}

psalm.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<psalm
3-
errorLevel="4"
3+
errorLevel="3"
44
resolveFromConfigFile="true"
55
findUnusedBaselineEntry="true"
66
findUnusedCode="false"
@@ -11,7 +11,6 @@
1111
>
1212
<projectFiles>
1313
<directory name="src" />
14-
<directory name="tests" />
1514
<ignoreFiles>
1615
<directory name="vendor" />
1716
</ignoreFiles>

src/Database/DblibDatabase.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ protected static function getDatabaseName(Uri $uri): string
2525
#[\Override]
2626
public static function prepareEnvironment(UriInterface|Uri $uri): void
2727
{
28-
$database = static::getDatabaseName($uri);
28+
$uriInstance = $uri instanceof Uri ? $uri : new Uri($uri->__toString());
29+
$database = static::getDatabaseName($uriInstance);
2930
$dbDriver = static::getDbDriverWithoutDatabase($uri);
3031
$dbDriver->execute("IF NOT EXISTS(select * from sys.databases where name='$database') CREATE DATABASE $database");
3132
}
@@ -82,6 +83,10 @@ public function executeSql(string $sql): void
8283
{
8384
$statements = preg_split("/;(\r\n|\r|\n)/", $sql);
8485

86+
if ($statements === false) {
87+
$statements = [$sql];
88+
}
89+
8590
foreach ($statements as $sql) {
8691
$this->executeSqlInternal($sql);
8792
}

src/Database/MySqlDatabase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public static function schema(): array
1919
#[\Override]
2020
public static function prepareEnvironment(UriInterface|Uri $uri): void
2121
{
22-
$database = static::getDatabaseName($uri);
22+
$uriInstance = $uri instanceof Uri ? $uri : new Uri($uri->__toString());
23+
$database = static::getDatabaseName($uriInstance);
2324
$dbDriver = static::getDbDriverWithoutDatabase($uri);
2425
$dbDriver->execute("CREATE SCHEMA IF NOT EXISTS `$database` DEFAULT CHARACTER SET utf8 ;");
2526
}

src/Database/PgsqlDatabase.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public static function schema(): string
2020
#[\Override]
2121
public static function prepareEnvironment(UriInterface|Uri $uri): void
2222
{
23-
$database = static::getDatabaseName($uri);
23+
$uriInstance = $uri instanceof Uri ? $uri : new Uri($uri->__toString());
24+
$database = static::getDatabaseName($uriInstance);
2425
$dbDriver = static::getDbDriverWithoutDatabase($uri, 'postgres');
2526
static::createDatabaseIfNotExists($dbDriver, $database);
2627
}
@@ -75,6 +76,10 @@ public function executeSql(string $sql): void
7576
{
7677
$statements = preg_split("/;(\r\n|\r|\n)/", $sql);
7778

79+
if ($statements === false) {
80+
$statements = [$sql];
81+
}
82+
7883
foreach ($statements as $sql) {
7984
$this->executeSqlInternal($sql);
8085
}

src/Database/SqliteDatabase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public function executeSql(string $sql): void
6565
{
6666
$statements = preg_split("/;(\r\n|\r|\n)/", $sql);
6767

68+
if ($statements === false) {
69+
$statements = [$sql];
70+
}
71+
6872
foreach ($statements as $sql) {
6973
$this->executeSqlInternal($sql);
7074
}

src/Migration.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ public function withTransactionEnabled($enabled = true): static
8282
*/
8383
public static function registerDatabase(string $class): void
8484
{
85-
if (!in_array(DatabaseInterface::class, class_implements($class))) {
85+
$implements = class_implements($class);
86+
if ($implements === false || !in_array(DatabaseInterface::class, $implements)) {
8687
throw new InvalidArgumentException('Class not implements DatabaseInterface!');
8788
}
8889

@@ -110,7 +111,9 @@ public function getDbCommand(): DatabaseInterface
110111
{
111112
if (is_null($this->dbCommand)) {
112113
$class = $this->getDatabaseClassName();
113-
$this->dbCommand = new $class($this->uri, $this->migrationTable);
114+
/** @var DatabaseInterface $instance */
115+
$instance = new $class($this->uri, $this->migrationTable);
116+
$this->dbCommand = $instance;
114117
}
115118
return $this->dbCommand;
116119
}
@@ -167,7 +170,12 @@ public function getMigrationSql(int $version, int $increment): ?string
167170
. "/" . ($increment < 0 ? "down" : "up")
168171
. "/*.sql";
169172

170-
$result = array_filter(glob($filePattern), function ($file) use ($version) {
173+
$files = glob($filePattern);
174+
if ($files === false) {
175+
$files = [];
176+
}
177+
178+
$result = array_filter($files, function ($file) use ($version) {
171179
return preg_match("/^0*$version(-[\w\d-]*)?\.sql$/", basename($file));
172180
});
173181

@@ -204,14 +212,20 @@ public function getFileContent(string|null $file): array
204212
return $data;
205213
}
206214

207-
$data["content"] = file_get_contents($file);
215+
$content = file_get_contents($file);
216+
217+
if ($content === false) {
218+
return $data;
219+
}
220+
221+
$data["content"] = $content;
208222

209-
if (preg_match("/--\s*@description:\s*(?<name>.*)/", $data["content"], $description)) {
223+
if (preg_match("/--\s*@description:\s*(?<name>.*)/", $content, $description)) {
210224
$data["description"] = $description["name"];
211225
}
212226

213227
$data["exists"] = true;
214-
$data["checksum"] = sha1($data["content"]);
228+
$data["checksum"] = sha1($content);
215229

216230
return $data;
217231
}
@@ -417,7 +431,7 @@ public function down(?int $upVersion = null, bool $force = false): void
417431
*/
418432
public function addCallbackProgress(callable $callable): void
419433
{
420-
$this->callableProgress = $callable;
434+
$this->callableProgress = $callable instanceof Closure ? $callable : Closure::fromCallable($callable);
421435
}
422436

423437
/**

0 commit comments

Comments
 (0)