Skip to content

Commit

Permalink
Merge pull request #9 from envor/main
Browse files Browse the repository at this point in the history
Support Mariadb Builder
  • Loading branch information
inmanturbo authored Mar 14, 2024
2 parents 80f6a78 + fcbccba commit 9a151d6
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
coverage: none

- name: Install composer dependencies
uses: ramsey/composer-install@v2
uses: ramsey/composer-install@v3

- name: Run PHPStan
run: ./vendor/bin/phpstan --error-format=github
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-schema-macros` will be documented in this file.

## v1.1.3 - 2024-02-15

**Full Changelog**: https://github.com/envor/laravel-schema-macros/compare/v1.1.1...v1.1.3

## v1.1.2 - 2024-02-05

### What's Changed
Expand Down
120 changes: 120 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,125 @@
parameters:
ignoreErrors:
-
message: "#^Call to method createDatabase\\(\\) on an unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbCreateDatabaseIfNotExists.php

-
message: "#^Call to method databaseExists\\(\\) on an unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbCreateDatabaseIfNotExists.php

-
message: "#^PHPDoc tag @mixin contains unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbCreateDatabaseIfNotExists.php

-
message: "#^PHPDoc tag @var for variable \\$this contains unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbCreateDatabaseIfNotExists.php

-
message: "#^Call to method getConnection\\(\\) on an unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbDatabaseExists.php

-
message: "#^PHPDoc tag @mixin contains unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbDatabaseExists.php

-
message: "#^PHPDoc tag @var for variable \\$this contains unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbDatabaseExists.php

-
message: "#^Call to method dropDatabaseIfExists\\(\\) on an unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbEmptyTrash.php

-
message: "#^Call to method getConnection\\(\\) on an unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbEmptyTrash.php

-
message: "#^Cannot cast mixed to string\\.$#"
count: 1
path: src/MariaDb/MariaDbEmptyTrash.php

-
message: "#^PHPDoc tag @mixin contains unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbEmptyTrash.php

-
message: "#^PHPDoc tag @var for variable \\$this contains unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbEmptyTrash.php

-
message: "#^Parameter \\#1 \\$object of function get_object_vars expects object, mixed given\\.$#"
count: 1
path: src/MariaDb/MariaDbEmptyTrash.php

-
message: "#^Unable to resolve the template type TKey in call to function collect$#"
count: 1
path: src/MariaDb/MariaDbEmptyTrash.php

-
message: "#^Unable to resolve the template type TValue in call to function collect$#"
count: 1
path: src/MariaDb/MariaDbEmptyTrash.php

-
message: "#^Call to method MariaDbDatabaseExists\\(\\) on an unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbTrashDatabase.php

-
message: "#^Call to method createDatabase\\(\\) on an unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbTrashDatabase.php

-
message: "#^Call to method dropDatabaseIfExists\\(\\) on an unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 2
path: src/MariaDb/MariaDbTrashDatabase.php

-
message: "#^Call to method getConnection\\(\\) on an unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbTrashDatabase.php

-
message: "#^Call to method getTableListing\\(\\) on an unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbTrashDatabase.php

-
message: "#^Call to method setConnection\\(\\) on an unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbTrashDatabase.php

-
message: "#^PHPDoc tag @mixin contains unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbTrashDatabase.php

-
message: "#^PHPDoc tag @var for variable \\$this contains unknown class Illuminate\\\\Database\\\\Schema\\\\MariaDbBuilder\\.$#"
count: 1
path: src/MariaDb/MariaDbTrashDatabase.php

-
message: "#^Parameter \\#1 \\.\\.\\.\\$arrays of function array_merge expects array, mixed given\\.$#"
count: 1
path: src/MariaDb/MariaDbTrashDatabase.php

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Schema\\\\MySqlBuilder\\:\\:databaseExists\\(\\)\\.$#"
count: 1
Expand Down
31 changes: 31 additions & 0 deletions src/MariaDb/MariaDbCreateDatabaseIfNotExists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

Check failure on line 1 in src/MariaDb/MariaDbCreateDatabaseIfNotExists.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^Call to method createDatabase\(\) on an unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbCreateDatabaseIfNotExists.php was not matched in reported errors.

Check failure on line 1 in src/MariaDb/MariaDbCreateDatabaseIfNotExists.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^Call to method databaseExists\(\) on an unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbCreateDatabaseIfNotExists.php was not matched in reported errors.

Check failure on line 1 in src/MariaDb/MariaDbCreateDatabaseIfNotExists.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^PHPDoc tag @mixin contains unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbCreateDatabaseIfNotExists.php was not matched in reported errors.

Check failure on line 1 in src/MariaDb/MariaDbCreateDatabaseIfNotExists.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^PHPDoc tag @var for variable \$this contains unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbCreateDatabaseIfNotExists.php was not matched in reported errors.

Check failure on line 1 in src/MariaDb/MariaDbCreateDatabaseIfNotExists.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^Call to method createDatabase\(\) on an unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbCreateDatabaseIfNotExists.php was not matched in reported errors.

Check failure on line 1 in src/MariaDb/MariaDbCreateDatabaseIfNotExists.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^Call to method databaseExists\(\) on an unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbCreateDatabaseIfNotExists.php was not matched in reported errors.

Check failure on line 1 in src/MariaDb/MariaDbCreateDatabaseIfNotExists.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^PHPDoc tag @mixin contains unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbCreateDatabaseIfNotExists.php was not matched in reported errors.

Check failure on line 1 in src/MariaDb/MariaDbCreateDatabaseIfNotExists.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^PHPDoc tag @var for variable \$this contains unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbCreateDatabaseIfNotExists.php was not matched in reported errors.

namespace Envor\SchemaMacros\MariaDb;

use Stringable;

/**
* Create the database if it does not exist
*
* @param string|Stringable $database
*
* @mixin \Illuminate\Database\Schema\MariaDbBuilder
*
* @return bool
*/
class MariaDbCreateDatabaseIfNotExists
{
public function __invoke(): callable
{
return function (string|Stringable $database): bool {
$database = (string) $database;

/** @var \Illuminate\Database\Schema\MariaDbBuilder $this */
if ($this->databaseExists($database)) {

Check failure on line 24 in src/MariaDb/MariaDbCreateDatabaseIfNotExists.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Schema\MariaDbBuilder::databaseExists().

Check failure on line 24 in src/MariaDb/MariaDbCreateDatabaseIfNotExists.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Schema\MariaDbBuilder::databaseExists().
return false;
}

return $this->createDatabase($database);
};
}
}
27 changes: 27 additions & 0 deletions src/MariaDb/MariaDbDatabaseExists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

Check failure on line 1 in src/MariaDb/MariaDbDatabaseExists.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^Call to method getConnection\(\) on an unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbDatabaseExists.php was not matched in reported errors.

Check failure on line 1 in src/MariaDb/MariaDbDatabaseExists.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^PHPDoc tag @mixin contains unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbDatabaseExists.php was not matched in reported errors.

Check failure on line 1 in src/MariaDb/MariaDbDatabaseExists.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^PHPDoc tag @var for variable \$this contains unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbDatabaseExists.php was not matched in reported errors.

Check failure on line 1 in src/MariaDb/MariaDbDatabaseExists.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^Call to method getConnection\(\) on an unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbDatabaseExists.php was not matched in reported errors.

Check failure on line 1 in src/MariaDb/MariaDbDatabaseExists.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^PHPDoc tag @mixin contains unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbDatabaseExists.php was not matched in reported errors.

Check failure on line 1 in src/MariaDb/MariaDbDatabaseExists.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^PHPDoc tag @var for variable \$this contains unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbDatabaseExists.php was not matched in reported errors.

namespace Envor\SchemaMacros\MariaDb;

use Stringable;

/**
* determine if the database exists
*
* @param string|Stringable $database
*
* @mixin \Illuminate\Database\Schema\MariaDbBuilder
*
* @return bool
*/
class MariaDbDatabaseExists
{
public function __invoke(): callable
{
return function (string|Stringable $database): bool {
$database = (string) $database;

/** @var \Illuminate\Database\Schema\MariaDbBuilder $this */
return (bool) $this->getConnection()->select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '{$database}'");
};
}
}
49 changes: 49 additions & 0 deletions src/MariaDb/MariaDbEmptyTrash.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

Check failure on line 1 in src/MariaDb/MariaDbEmptyTrash.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^Call to method dropDatabaseIfExists\(\) on an unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbEmptyTrash.php was not matched in reported errors.

Check failure on line 1 in src/MariaDb/MariaDbEmptyTrash.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^Call to method getConnection\(\) on an unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbEmptyTrash.php was not matched in reported errors.

Check failure on line 1 in src/MariaDb/MariaDbEmptyTrash.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^Call to method dropDatabaseIfExists\(\) on an unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbEmptyTrash.php was not matched in reported errors.

Check failure on line 1 in src/MariaDb/MariaDbEmptyTrash.php

View workflow job for this annotation

GitHub Actions / phpstan

Ignored error pattern #^Call to method getConnection\(\) on an unknown class Illuminate\\Database\\Schema\\MariaDbBuilder\.$# in path /home/runner/work/laravel-schema-macros/laravel-schema-macros/src/MariaDb/MariaDbEmptyTrash.php was not matched in reported errors.

namespace Envor\SchemaMacros\MariaDb;

use Envor\SchemaMacros\SchemaMacros;
use Illuminate\Support\Carbon;

/**
* Erase trashed databases
*
* @param int $daysOld
*
* @mixin \Illuminate\Database\Schema\MariaDbBuilder
*
* @return int
*/
class MariaDbEmptyTrash
{
public function __invoke(): callable
{
return function (int $daysOld = 0): int {
/** @var \Illuminate\Database\Schema\MariaDbBuilder $this */
$trashDatabases = collect($this->getConnection()->select("SHOW DATABASES LIKE 'trashed_%'"))
->map(fn ($database) => array_values(get_object_vars($database))[0]);

$deleted = 0;

foreach ($trashDatabases as $trashDatabase) {
$trashDatabase = (string) $trashDatabase;

if ($daysOld > 0) {
$dateSlice = array_slice(explode('_', $trashDatabase), 1, 6);
$dateString = implode('_', $dateSlice);
$date = Carbon::createFromFormat(SchemaMacros::TRASH_DATE_FORMAT, $dateString)->getTimestamp();

if ($date > now()->subDays($daysOld)->getTimestamp()) {
continue;
}
}

if ($this->dropDatabaseIfExists($trashDatabase)) {
$deleted++;
}
}

return $deleted;
};
}
}
64 changes: 64 additions & 0 deletions src/MariaDb/MariaDbTrashDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Envor\SchemaMacros\MariaDb;

use Envor\SchemaMacros\SchemaMacros;
use Illuminate\Support\Facades\DB;
use Stringable;

/**
* Move the database to the trash
*
* @param string|Stringable $database
*
* @mixin \Illuminate\Database\Schema\MariaDbBuilder
*
* @return bool|string
*/
class MariaDbTrashDatabase
{
public function __invoke(): callable
{
return function (string|Stringable $database) {
$database = (string) $database;

/** @var \Illuminate\Database\Schema\MariaDbBuilder $this */
if (! $this->mariadbDatabaseExists($database)) {
return false;
}

try {

$trashedAt = now()->format(SchemaMacros::TRASH_DATE_FORMAT);
$trashedDatabase = "trashed_{$trashedAt}_{$database}";
$this->dropDatabaseIfExists($trashedDatabase);
$this->createDatabase($trashedDatabase);

$currentConnection = $this->getConnection();
$currentConnectionName = $currentConnection->getName();

config(['database.connections.new_connection_for_database' => array_merge(config("database.connections.{$currentConnectionName}"), [
'database' => $database,
])]);

$db = DB::connection('new_connection_for_database');

$db->statement("SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';");

$tables = $this->getTableListing();

foreach ($tables as $table) {
$db->statement("create table if not exists `{$trashedDatabase}`.`{$table}` like `{$database}`.`{$table}`;");
$db->statement("insert into `{$trashedDatabase}`.`{$table}` select * from `{$database}`.`{$table}`;");
}

$this->dropDatabaseIfExists($database);
$this->setConnection($currentConnection);

return $trashedDatabase;
} catch (\Exception $e) {
return false;
}
};
}
}
4 changes: 4 additions & 0 deletions src/SchemaMacrosServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,22 @@ private function macros(): array
'databaseExists' => [
'sqlite' => \Envor\SchemaMacros\SQLite\SQLiteDatabaseExists::class,
'mysql' => \Envor\SchemaMacros\MySql\MySqlDatabaseExists::class,
'mariadb' => \Envor\SchemaMacros\MariaDb\MariaDbDatabaseExists::class,
],
'createDatabaseIfNotExists' => [
'sqlite' => \Envor\SchemaMacros\SQLite\SQLiteCreateDatabaseIfNotExists::class,
'mysql' => \Envor\SchemaMacros\MySql\MySqlCreateDatabaseIfNotExists::class,
'mariadb' => \Envor\SchemaMacros\MariaDb\MariaDbCreateDatabaseIfNotExists::class,
],
'trashDatabase' => [
'sqlite' => \Envor\SchemaMacros\SQLite\SQLiteTrashDatabase::class,
'mysql' => \Envor\SchemaMacros\MySql\MySqlTrashDatabase::class,
'mariadb' => \Envor\SchemaMacros\MariaDb\MariaDbTrashDatabase::class,
],
'emptyTrash' => [
'sqlite' => \Envor\SchemaMacros\SQLite\SQLiteEmptyTrash::class,
'mysql' => \Envor\SchemaMacros\MySql\MySqlEmptyTrash::class,
'mariadb' => \Envor\SchemaMacros\MariaDb\MariaDbEmptyTrash::class,
],
];
}
Expand Down

0 comments on commit 9a151d6

Please sign in to comment.