Skip to content

Commit bf95146

Browse files
authored
Merge pull request #3 from envor/main
refactor to invokable classes
2 parents 011c16d + 2780f5b commit bf95146

15 files changed

+526
-251
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Schema::connection('mariadb')->trashDatabase('schema_demo');
117117
The `emptyTrash()` method will erase all `trashed` databases from disk which are reachable from the current connection:
118118

119119
> [!TIP]
120-
> To only permanently erase databases trahed later than a given age and keep those which are newer,
120+
> To only permanently erase databases trashed later than a given age and keep those which are newer,
121121
> you may pass the maximum age in days for the databases you want to keep.
122122
123123
```php
@@ -144,7 +144,7 @@ Schema::connection('mysql')->emptyTrash();
144144

145145
> [!IMPORTANT]
146146
> Tests use [spatie/docker](https://github.com/spatie/docker) for testing against various database servers.
147-
> Docker is required for running tests!
147+
> Docker is required for running tests locally!
148148
149149
```bash
150150
composer test

phpstan-baseline.neon

Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,56 @@
11
parameters:
22
ignoreErrors:
33
-
4-
message: "#^Call to method createDatabase\\(\\) on an unknown class Illuminate\\\\Database\\\\MySqlBuilder\\.$#"
5-
count: 2
6-
path: src/SchemaMacrosServiceProvider.php
7-
8-
-
9-
message: "#^Call to method createDatabaseIfNotExists\\(\\) on an unknown class Illuminate\\\\Database\\\\SQLiteBuilder\\.$#"
4+
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Schema\\\\MySqlBuilder\\:\\:mysqlDatabaseExists\\(\\)\\.$#"
105
count: 1
11-
path: src/SchemaMacrosServiceProvider.php
12-
13-
-
14-
message: "#^Call to method dropDatabaseIfExists\\(\\) on an unknown class Illuminate\\\\Database\\\\MySqlBuilder\\.$#"
15-
count: 3
16-
path: src/SchemaMacrosServiceProvider.php
6+
path: src/MySql/MySqlCreateDatabaseIfNotExists.php
177

188
-
199
message: "#^Call to method getConnection\\(\\) on an unknown class Illuminate\\\\Database\\\\MySqlBuilder\\.$#"
20-
count: 3
21-
path: src/SchemaMacrosServiceProvider.php
22-
23-
-
24-
message: "#^Call to method getTableListing\\(\\) on an unknown class Illuminate\\\\Database\\\\MySqlBuilder\\.$#"
25-
count: 1
26-
path: src/SchemaMacrosServiceProvider.php
27-
28-
-
29-
message: "#^Call to method mysqlDatabaseExists\\(\\) on an unknown class Illuminate\\\\Database\\\\MySqlBuilder\\.$#"
30-
count: 2
31-
path: src/SchemaMacrosServiceProvider.php
32-
33-
-
34-
message: "#^Call to method setConnection\\(\\) on an unknown class Illuminate\\\\Database\\\\MySqlBuilder\\.$#"
3510
count: 1
36-
path: src/SchemaMacrosServiceProvider.php
37-
38-
-
39-
message: "#^Instanceof between \\$this\\(Envor\\\\SchemaMacros\\\\SchemaMacrosServiceProvider\\) and Illuminate\\\\Database\\\\Schema\\\\MySqlBuilder will always evaluate to false\\.$#"
40-
count: 4
41-
path: src/SchemaMacrosServiceProvider.php
42-
43-
-
44-
message: "#^Instanceof between \\$this\\(Envor\\\\SchemaMacros\\\\SchemaMacrosServiceProvider\\) and Illuminate\\\\Database\\\\Schema\\\\SQLiteBuilder will always evaluate to false\\.$#"
45-
count: 3
46-
path: src/SchemaMacrosServiceProvider.php
11+
path: src/MySql/MySqlDatabaseExists.php
4712

4813
-
4914
message: "#^PHPDoc tag @var for variable \\$this contains unknown class Illuminate\\\\Database\\\\MySqlBuilder\\.$#"
50-
count: 4
51-
path: src/SchemaMacrosServiceProvider.php
52-
53-
-
54-
message: "#^PHPDoc tag @var for variable \\$this contains unknown class Illuminate\\\\Database\\\\SQLiteBuilder\\.$#"
5515
count: 1
56-
path: src/SchemaMacrosServiceProvider.php
16+
path: src/MySql/MySqlDatabaseExists.php
5717

5818
-
59-
message: "#^Parameter \\#1 \\$format of method Carbon\\\\Carbon\\:\\:format\\(\\) expects string, mixed given\\.$#"
19+
message: "#^Cannot cast mixed to string\\.$#"
6020
count: 1
61-
path: src/SchemaMacrosServiceProvider.php
21+
path: src/MySql/MySqlEmptyTrash.php
6222

6323
-
64-
message: "#^Parameter \\#1 \\$format of static method Carbon\\\\Carbon\\:\\:createFromFormat\\(\\) expects string, mixed given\\.$#"
24+
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Schema\\\\MySqlBuilder\\:\\:mysqlDatabaseExists\\(\\)\\.$#"
6525
count: 1
66-
path: src/SchemaMacrosServiceProvider.php
26+
path: src/MySql/MySqlTrashDatabase.php
6727

6828
-
69-
message: "#^Parameter \\#1 \\$object of function get_object_vars expects object, mixed given\\.$#"
29+
message: "#^Parameter \\#1 \\.\\.\\.\\$arrays of function array_merge expects array, mixed given\\.$#"
7030
count: 1
71-
path: src/SchemaMacrosServiceProvider.php
31+
path: src/MySql/MySqlTrashDatabase.php
7232

7333
-
74-
message: "#^Parameter \\#1 \\.\\.\\.\\$arrays of function array_merge expects array, mixed given\\.$#"
34+
message: "#^Negated boolean expression is always true\\.$#"
7535
count: 1
76-
path: src/SchemaMacrosServiceProvider.php
36+
path: src/SQLite/SQLiteCreateDatabaseIfNotExists.php
7737

7838
-
79-
message: "#^Parameter \\#2 \\$string of function explode expects string, mixed given\\.$#"
39+
message: "#^Unreachable statement \\- code above always terminates\\.$#"
8040
count: 1
81-
path: src/SchemaMacrosServiceProvider.php
41+
path: src/SQLite/SQLiteCreateDatabaseIfNotExists.php
8242

8343
-
84-
message: "#^Unable to resolve the template type TKey in call to function collect$#"
85-
count: 1
44+
message: "#^Instanceof between \\$this\\(Envor\\\\SchemaMacros\\\\SchemaMacrosServiceProvider\\) and Illuminate\\\\Database\\\\Schema\\\\MySqlBuilder will always evaluate to false\\.$#"
45+
count: 4
8646
path: src/SchemaMacrosServiceProvider.php
8747

8848
-
89-
message: "#^Unable to resolve the template type TValue in call to function collect$#"
90-
count: 1
49+
message: "#^Instanceof between \\$this\\(Envor\\\\SchemaMacros\\\\SchemaMacrosServiceProvider\\) and Illuminate\\\\Database\\\\Schema\\\\SQLiteBuilder will always evaluate to false\\.$#"
50+
count: 4
9151
path: src/SchemaMacrosServiceProvider.php
9252

9353
-
94-
message: "#^Unreachable statement \\- code above always terminates\\.$#"
95-
count: 4
54+
message: "#^Trying to invoke mixed but it's not a callable\\.$#"
55+
count: 2
9656
path: src/SchemaMacrosServiceProvider.php
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Envor\SchemaMacros\MySql;
4+
5+
use Stringable;
6+
7+
/**
8+
* Create the database if it does not exist
9+
*
10+
* @param string|Stringable $database
11+
*
12+
* @mixin \Illuminate\Database\Schema\MySqlBuilder
13+
*
14+
* @return bool
15+
*/
16+
class MySqlCreateDatabaseIfNotExists
17+
{
18+
public function __invoke(): callable
19+
{
20+
return function (string|Stringable $database): bool {
21+
$database = (string) $database;
22+
23+
/** @var \Illuminate\Database\Schema\MySqlBuilder $this */
24+
if ($this->mysqlDatabaseExists($database)) {
25+
return false;
26+
}
27+
28+
return $this->createDatabase($database);
29+
};
30+
}
31+
}

src/MySql/MySqlDatabaseExists.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Envor\SchemaMacros\MySql;
4+
5+
use Stringable;
6+
7+
/**
8+
* determine if the database exists
9+
*
10+
* @param string|Stringable $database
11+
*
12+
* @mixin \Illuminate\Database\Schema\MySqlBuilder
13+
*
14+
* @return bool
15+
*/
16+
class MySqlDatabaseExists
17+
{
18+
public function __invoke(): callable
19+
{
20+
return function (string|Stringable $database): bool {
21+
$database = (string) $database;
22+
23+
/** @var \Illuminate\Database\MySqlBuilder $this */
24+
return (bool) $this->getConnection()->select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '{$database}'");
25+
};
26+
}
27+
}

src/MySql/MySqlEmptyTrash.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Envor\SchemaMacros\MySql;
4+
5+
use Envor\SchemaMacros\SchemaMacros;
6+
use Illuminate\Support\Carbon;
7+
8+
/**
9+
* Erase trashed databases
10+
*
11+
* @param int $daysOld
12+
*
13+
* @mixin \Illuminate\Database\Schema\MySqlBuilder
14+
*
15+
* @return int
16+
*/
17+
class MySqlEmptyTrash
18+
{
19+
public function __invoke(): callable
20+
{
21+
return function (int $daysOld = 0): int {
22+
/** @var \Illuminate\Database\Schema\MySqlBuilder $this */
23+
$trashDatabases = collect($this->getConnection()->select("SHOW DATABASES LIKE 'trashed_%'"))
24+
->map(fn ($database) => array_values(get_object_vars($database))[0]);
25+
26+
$deleted = 0;
27+
28+
foreach ($trashDatabases as $trashDatabase) {
29+
$trashDatabase = (string) $trashDatabase;
30+
31+
if ($daysOld > 0) {
32+
$dateSlice = array_slice(explode('_', $trashDatabase), 1, 6);
33+
$dateString = implode('_', $dateSlice);
34+
$date = Carbon::createFromFormat(SchemaMacros::TRASH_DATE_FORMAT, $dateString)->getTimestamp();
35+
36+
if ($date > now()->subDays($daysOld)->getTimestamp()) {
37+
continue;
38+
}
39+
}
40+
41+
if ($this->dropDatabaseIfExists($trashDatabase)) {
42+
$deleted++;
43+
}
44+
}
45+
46+
return $deleted;
47+
};
48+
}
49+
}

src/MySql/MySqlTrashDatabase.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Envor\SchemaMacros\MySql;
4+
5+
use Envor\SchemaMacros\SchemaMacros;
6+
use Illuminate\Support\Facades\DB;
7+
use Stringable;
8+
9+
/**
10+
* Move the database to the trash
11+
*
12+
* @param string|Stringable $database
13+
*
14+
* @mixin \Illuminate\Database\Schema\MySqlBuilder
15+
*
16+
* @return bool|string
17+
*/
18+
class MySqlTrashDatabase
19+
{
20+
public function __invoke(): callable
21+
{
22+
return function (string|Stringable $database) {
23+
$database = (string) $database;
24+
25+
/** @var \Illuminate\Database\Schema\MySqlBuilder $this */
26+
if (! $this->mysqlDatabaseExists($database)) {
27+
return false;
28+
}
29+
30+
try {
31+
32+
$trashedAt = now()->format(SchemaMacros::TRASH_DATE_FORMAT);
33+
$trashedDatabase = "trashed_{$trashedAt}_{$database}";
34+
$this->dropDatabaseIfExists($trashedDatabase);
35+
$this->createDatabase($trashedDatabase);
36+
37+
$currentConnection = $this->getConnection();
38+
$currentConnectionName = $currentConnection->getName();
39+
40+
config(['database.connections.new_connection_for_database' => array_merge(config("database.connections.{$currentConnectionName}"), [
41+
'database' => $database,
42+
])]);
43+
44+
$db = DB::connection('new_connection_for_database');
45+
46+
$db->statement("SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';");
47+
48+
$tables = $this->getTableListing();
49+
50+
foreach ($tables as $table) {
51+
$db->statement("create table if not exists `{$trashedDatabase}`.`{$table}` like `{$database}`.`{$table}`;");
52+
$db->statement("insert into `{$trashedDatabase}`.`{$table}` select * from `{$database}`.`{$table}`;");
53+
}
54+
55+
$this->dropDatabaseIfExists($database);
56+
$this->setConnection($currentConnection);
57+
58+
return $trashedDatabase;
59+
} catch (\Exception $e) {
60+
return false;
61+
}
62+
};
63+
}
64+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Envor\SchemaMacros\SQLite;
4+
5+
use Illuminate\Support\Facades\File;
6+
use Stringable;
7+
8+
/**
9+
* Create the database if it does not exist
10+
*
11+
* @param string|Stringable $database
12+
* @param bool $recursive = true
13+
*
14+
* @mixin \Illuminate\Database\Schema\SQLiteBuilder
15+
*
16+
* @return bool
17+
*/
18+
class SQLiteCreateDatabaseIfNotExists
19+
{
20+
public function __invoke(): callable
21+
{
22+
return function (string|Stringable $database, bool $recursive = true): bool {
23+
$database = (string) $database;
24+
25+
if (File::exists($database)) {
26+
return false;
27+
}
28+
29+
$directory = dirname($database);
30+
31+
if (! File::isDirectory($directory) && $recursive) {
32+
File::makeDirectory($directory, 0755, true);
33+
}
34+
35+
if (! File::exists($database)) {
36+
37+
/** @var \Illuminate\Database\Schema\SQLiteBuilder $this */
38+
return $this->createDatabase($database);
39+
}
40+
41+
return false;
42+
};
43+
}
44+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Envor\SchemaMacros\SQLite;
4+
5+
use Illuminate\Support\Facades\File;
6+
use Stringable;
7+
8+
/**
9+
* determine if the database exists
10+
*
11+
* @param string|Stringable $database
12+
*
13+
* @mixin \Illuminate\Database\Schema\SQLiteBuilder
14+
*
15+
* @return bool
16+
*/
17+
class SQLiteDatabaseExists
18+
{
19+
public function __invoke(): callable
20+
{
21+
return function (string|Stringable $database): bool {
22+
$database = (string) $database;
23+
24+
return File::exists($database);
25+
};
26+
}
27+
}

0 commit comments

Comments
 (0)