-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDatabase.php
More file actions
114 lines (87 loc) · 2.96 KB
/
Database.php
File metadata and controls
114 lines (87 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
namespace Tests\Concerns;
use DragonCode\MigrateDB\Constants\Drivers;
use Illuminate\Database\Schema\Builder as SchemaBuilder;
use Illuminate\Support\Facades\Config;
use Tests\Configurations\BaseConfiguration;
use Tests\Configurations\Manager;
use Tests\Connectors\MySqlConnection;
use Tests\Connectors\PostgresConnection;
use Tests\Connectors\SqlServerConnection;
/** @mixin \Tests\Concerns\Connections */
trait Database
{
use HasUuidAndUlid;
use Seeders;
protected array $connectors = [
Drivers::MYSQL => MySqlConnection::class,
Drivers::POSTGRES => PostgresConnection::class,
Drivers::SQL_SERVER => SqlServerConnection::class,
];
protected string $table_foo = 'foo';
protected string $table_bar = 'bar';
protected string $table_baz = 'baz';
protected string $table_ulid = 'ulid_table';
protected string $table_uuid = 'uuid_table';
protected string $choice_target = 'target';
protected string $choice_source = 'source';
protected array $choices = [
'target',
'source',
'none',
];
protected function setDatabases($app): void
{
$this->setDatabaseConnections($app);
}
protected function freshDatabase(): void
{
$this->createDatabases();
$this->runMigrations();
$this->fillTables();
}
protected function createDatabases(): void
{
$this->createDatabase($this->source_connection, $this->defaultSourceConnectionName());
$this->createDatabase($this->target_connection, $this->defaultTargetConnectionName());
}
protected function createDatabase(string $database, string $driver): void
{
$instance = $this->getDatabaseConnector($driver);
$config = $this->getConnectionConfiguration($database, $driver);
$instance::make()
->of($database, $driver)
->configuration($config)
->dropDatabase()
->createDatabase();
}
/**
* @return string|\Tests\Connectors\BaseConnection
*/
protected function getDatabaseConnector(string $connection): string
{
return $this->connectors[$connection];
}
protected function getConnectionConfiguration(string $connection, string $driver): BaseConfiguration
{
$config = Config::get('database.connections.' . $connection);
return Manager::make()
->get($driver)
->merge($config)
->setDatabase();
}
protected function runMigrations(): void
{
$this->artisan('migrate', ['--database' => $this->source_connection])->run();
}
protected function getTables(SchemaBuilder $builder): array
{
$schema = method_exists($builder, 'getCurrentSchemaName')
? $builder->getCurrentSchemaName()
: null;
return method_exists($builder, 'getAllTables')
? $builder->getAllTables()
: $builder->getTables($schema);
}
}