Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to PHP 8.x #52

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ jobs:
# apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/community docker
# docker-compose up -d postgres mysql
- run: composer install
- run: ./vendor/bin/psalm
- run: ./vendor/bin/phpunit
- run: ./vendor/bin/phpunit tests/SqliteDatabase*
- run: ./vendor/bin/phpunit tests/MysqlDatabase*
Expand Down
5 changes: 5 additions & 0 deletions .idea/runConfigurations/Psalm.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"byjg/anydataset-db": "4.9.*",
"ext-pdo": "*"
"ext-pdo": "*",
"byjg/anydataset-db": "^5.0"
},
"require-dev": {
"phpunit/phpunit": "^9.6"
"php": ">=8.1 <8.4",
"phpunit/phpunit": "^9.6",
"vimeo/psalm": "^5.9"
},
"autoload": {
"psr-4": {
"ByJG\\DbMigration\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"license": "MIT"
}
18 changes: 18 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
resolveFromConfigFile="true"
findUnusedBaselineEntry="true"
findUnusedCode="false"
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"
>
<projectFiles>
<directory name="src" />
<directory name="tests" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
59 changes: 30 additions & 29 deletions src/Database/AbstractDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,33 @@
use ByJG\AnyDataset\Db\Factory;
use ByJG\DbMigration\Exception\DatabaseNotVersionedException;
use ByJG\DbMigration\Exception\OldVersionSchemaException;
use ByJG\DbMigration\Migration;
use ByJG\DbMigration\MigrationStatus;
use Exception;
use Psr\Http\Message\UriInterface;

abstract class AbstractDatabase implements DatabaseInterface
{
/**
* @var DbDriverInterface
* @var DbDriverInterface|null
*/
private $dbDriver;
private ?DbDriverInterface $dbDriver = null;

/**
* @var UriInterface
*/
private $uri;
private UriInterface $uri;
/**
* @var string
*/
private $migrationTable;
private string $migrationTable;

/**
* Command constructor.
*
* @param UriInterface $uri
* @param string $migrationTable
*/
public function __construct(UriInterface $uri, $migrationTable = 'migration_version')
public function __construct(UriInterface $uri, string $migrationTable = 'migration_version')
{
$this->uri = $uri;
$this->migrationTable = $migrationTable;
Expand All @@ -40,94 +41,94 @@ public function __construct(UriInterface $uri, $migrationTable = 'migration_vers
/**
* @return string
*/
public function getMigrationTable()
public function getMigrationTable(): string
{
return $this->migrationTable;
}

/**
* @return DbDriverInterface
*/
public function getDbDriver()
public function getDbDriver(): DbDriverInterface
{
if (is_null($this->dbDriver)) {
$this->dbDriver = Factory::getDbRelationalInstance($this->uri->__toString());
$this->dbDriver = Factory::getDbInstance($this->uri->__toString());
}
return $this->dbDriver;
}

/**
* @return array
* @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
* @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
* @throws DatabaseNotVersionedException
* @throws OldVersionSchemaException
*/
public function getVersion()
public function getVersion(): array
{
$result = [];
try {
$result['version'] = $this->getDbDriver()->getScalar('SELECT version FROM ' . $this->getMigrationTable());
} catch (\Exception $ex) {
} catch (Exception $ex) {
throw new DatabaseNotVersionedException('This database does not have a migration version. Please use "migrate reset" or "migrate install" to create one.');
}

try {
$result['status'] = $this->getDbDriver()->getScalar('SELECT status FROM ' . $this->getMigrationTable());
} catch (\Exception $ex) {
} catch (Exception $ex) {
throw new OldVersionSchemaException('This database does not have a migration version. Please use "migrate install" for update it.');
}

return $result;
}

/**
* @param $version
* @param $status
* @param int $version
* @param MigrationStatus $status
*/
public function setVersion($version, $status)
public function setVersion(int $version, MigrationStatus $status): void
{
$this->getDbDriver()->execute(
'UPDATE ' . $this->getMigrationTable() . ' SET version = :version, status = :status',
[
'version' => $version,
'status' => $status,
'status' => $status->value,
]
);
}

/**
* @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
* @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
* @throws DatabaseNotVersionedException
* @throws OldVersionSchemaException
*/
protected function checkExistsVersion()
protected function checkExistsVersion(): void
{
// Get the version to check if exists
$versionInfo = $this->getVersion();
if ($versionInfo['version'] === false) {
$this->getDbDriver()->execute(sprintf(
"insert into %s values(0, '%s')",
$this->getMigrationTable(),
Migration::VERSION_STATUS_UNKNOWN)
MigrationStatus::unknown->value)
);
}
}

/**
*
*/
public function updateVersionTable()
public function updateVersionTable(): void
{
$currentVersion = $this->getDbDriver()->getScalar(sprintf('select version from %s', $this->getMigrationTable()));
$this->getDbDriver()->execute(sprintf('drop table %s', $this->getMigrationTable()));
$this->createVersion();
$this->setVersion($currentVersion, Migration::VERSION_STATUS_UNKNOWN);
$this->setVersion($currentVersion, MigrationStatus::unknown);
}

protected function isTableExists($schema, $table)
protected function isTableExists(?string $schema, string $table): bool
{
$count = $this->getDbDriver()->getScalar(
'SELECT count(*) FROM information_schema.tables ' .
' WHERE table_schema = [[schema]] ' .
' AND table_name = [[table]] ',
' WHERE table_schema = :schema ' .
' AND table_name = :table ',
[
"schema" => $schema,
"table" => $table
Expand All @@ -137,12 +138,12 @@ protected function isTableExists($schema, $table)
return (intval($count) !== 0);
}

public function isDatabaseVersioned()
public function isDatabaseVersioned(): bool
{
return $this->isTableExists(ltrim($this->getDbDriver()->getUri()->getPath(), "/"), $this->getMigrationTable());
}

public function supportsTransaction()
public function supportsTransaction(): bool
{
return true;
}
Expand Down
32 changes: 18 additions & 14 deletions src/Database/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,42 @@

namespace ByJG\DbMigration\Database;

use ByJG\AnyDataset\Db\DbDriverInterface;
use ByJG\DbMigration\Exception\DatabaseNotVersionedException;
use ByJG\DbMigration\Exception\OldVersionSchemaException;
use ByJG\DbMigration\MigrationStatus;
use Psr\Http\Message\UriInterface;

interface DatabaseInterface
{
public static function schema();

public static function prepareEnvironment(UriInterface $dbDriver);
public static function prepareEnvironment(UriInterface $uri);

public function createDatabase();
public function createDatabase(): void;

public function dropDatabase();
public function dropDatabase(): void;

/**
* @return array
* @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
* @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
* @throws DatabaseNotVersionedException
* @throws OldVersionSchemaException
*/
public function getVersion();
public function getVersion(): array;

public function updateVersionTable();
public function updateVersionTable(): void;

public function executeSql($sql);
public function executeSql(string $sql): void;

public function setVersion($version, $status);
public function setVersion(int $version, MigrationStatus $status): void;

public function createVersion();
public function createVersion(): void;

public function isDatabaseVersioned();
public function isDatabaseVersioned(): bool;

public function getDbDriver();
public function getDbDriver(): DbDriverInterface;

public function getMigrationTable();
public function getMigrationTable(): string;

public function supportsTransaction();
public function supportsTransaction(): bool;
}
34 changes: 18 additions & 16 deletions src/Database/DblibDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,45 @@
namespace ByJG\DbMigration\Database;

use ByJG\AnyDataset\Db\Factory;
use ByJG\DbMigration\Exception\DatabaseNotVersionedException;
use ByJG\DbMigration\Exception\OldVersionSchemaException;
use ByJG\Util\Uri;
use Psr\Http\Message\UriInterface;

class DblibDatabase extends AbstractDatabase
{
public static function schema()
public static function schema(): array
{
return ['dblib', 'sqlsrv'];
}

public static function prepareEnvironment(UriInterface $uri)
public static function prepareEnvironment(UriInterface $uri): void
{
$database = preg_replace('~^/~', '', $uri->getPath());

$customUri = new Uri($uri->__toString());

$dbDriver = Factory::getDbRelationalInstance($customUri->withPath('/')->__toString());
$dbDriver = Factory::getDbInstance($customUri->withPath('/')->__toString());
$dbDriver->execute("IF NOT EXISTS(select * from sys.databases where name='$database') CREATE DATABASE $database");
}

public function createDatabase()
public function createDatabase(): void
{
$database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());

$this->getDbDriver()->execute("IF NOT EXISTS(select * from sys.databases where name='$database') CREATE DATABASE $database");
$this->getDbDriver()->execute("USE $database");
}

public function dropDatabase()
public function dropDatabase(): void
{
$database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());

$this->getDbDriver()->execute("use master");
$this->getDbDriver()->execute("drop database $database");
}

protected function createTableIfNotExists($database, $createTable)
protected function createTableIfNotExists(string $database, string $createTable): void
{
$this->getDbDriver()->execute("use $database");

Expand All @@ -55,18 +57,18 @@ protected function createTableIfNotExists($database, $createTable)
}

/**
* @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
* @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
* @throws DatabaseNotVersionedException
* @throws OldVersionSchemaException
*/
public function createVersion()
public function createVersion(): void
{
$database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());
$createTable = 'CREATE TABLE ' . $this->getMigrationTable() . ' (version int, status varchar(20), PRIMARY KEY (version))';
$this->createTableIfNotExists($database, $createTable);
$this->checkExistsVersion();
}

public function executeSql($sql)
public function executeSql(string $sql): void
{
$statements = preg_split("/;(\r\n|\r|\n)/", $sql);

Expand All @@ -75,7 +77,7 @@ public function executeSql($sql)
}
}

protected function executeSqlInternal($sql)
protected function executeSqlInternal(string $sql): void
{
if (empty(trim($sql))) {
return;
Expand All @@ -84,16 +86,16 @@ protected function executeSqlInternal($sql)
}

/**
* @param $schema
* @param $table
* @param string|null $schema
* @param string $table
* @return bool
*/
protected function isTableExists($schema, $table)
protected function isTableExists(?string $schema, string $table): bool
{
$count = $this->getDbDriver()->getScalar(
'SELECT count(*) FROM information_schema.tables ' .
' WHERE table_catalog = [[schema]] ' .
' AND table_name = [[table]] ',
' WHERE table_catalog = :schema ' .
' AND table_name = :table ',
[
"schema" => $schema,
"table" => $table
Expand Down
Loading