-
Notifications
You must be signed in to change notification settings - Fork 11.2k
/
Copy pathDatabaseTruncation.php
171 lines (143 loc) · 4.96 KB
/
DatabaseTruncation.php
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace Illuminate\Foundation\Testing;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Foundation\Testing\Traits\CanConfigureMigrationCommands;
trait DatabaseTruncation
{
use CanConfigureMigrationCommands;
/**
* The cached names of the database tables for each connection.
*
* @var array
*/
protected static array $allTables;
/**
* Truncate the database tables for all configured connections.
*
* @return void
*/
protected function truncateDatabaseTables(): void
{
$this->beforeTruncatingDatabase();
// Migrate and seed the database on first run...
if (! RefreshDatabaseState::$migrated) {
$this->artisan('migrate:fresh', $this->migrateFreshUsing());
$this->app[Kernel::class]->setArtisan(null);
RefreshDatabaseState::$migrated = true;
return;
}
// Always clear any test data on subsequent runs...
$this->truncateTablesForAllConnections();
if ($seeder = $this->seeder()) {
// Use a specific seeder class...
$this->artisan('db:seed', ['--class' => $seeder]);
} elseif ($this->shouldSeed()) {
// Use the default seeder class...
$this->artisan('db:seed');
}
$this->afterTruncatingDatabase();
}
/**
* Truncate the database tables for all configured connections.
*
* @return void
*/
protected function truncateTablesForAllConnections(): void
{
$database = $this->app->make('db');
collect($this->connectionsToTruncate())
->each(function ($name) use ($database) {
$connection = $database->connection($name);
$connection->getSchemaBuilder()->withoutForeignKeyConstraints(
fn () => $this->truncateTablesForConnection($connection, $name)
);
});
}
/**
* Truncate the database tables for the given database connection.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param string|null $name
* @return void
*/
protected function truncateTablesForConnection(ConnectionInterface $connection, ?string $name): void
{
$dispatcher = $connection->getEventDispatcher();
$connection->unsetEventDispatcher();
collect(static::$allTables[$name] ??= $connection->getDoctrineSchemaManager()->listTableNames())
->when(
property_exists($this, 'tablesToTruncate'),
fn ($tables) => $tables->intersect($this->tablesToTruncate),
fn ($tables) => $tables->diff($this->exceptTables($name))
)
->filter(fn ($table) => $connection->table($this->withoutTablePrefix($connection, $table))->exists())
->each(fn ($table) => $connection->table($this->withoutTablePrefix($connection, $table))->truncate());
$connection->setEventDispatcher($dispatcher);
}
/**
* Remove the table prefix from a table name, if it exists.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param string $table
* @return string
*/
protected function withoutTablePrefix(ConnectionInterface $connection, string $table)
{
$prefix = $connection->getTablePrefix();
return strpos($table, $prefix) === 0
? substr($table, strlen($prefix))
: $table;
}
/**
* The database connections that should have their tables truncated.
*
* @return array
*/
protected function connectionsToTruncate(): array
{
return property_exists($this, 'connectionsToTruncate')
? $this->connectionsToTruncate : [null];
}
/**
* Get the tables that should not be truncated.
*
* @param string|null $connectionName
* @return array
*/
protected function exceptTables(?string $connectionName): array
{
if (property_exists($this, 'exceptTables')) {
$migrationsTable = $this->app['config']->get('database.migrations');
if (array_is_list($this->exceptTables ?? [])) {
return array_merge(
$this->exceptTables ?? [],
[$migrationsTable],
);
}
return array_merge(
$this->exceptTables[$connectionName] ?? [],
[$migrationsTable],
);
}
return [$this->app['config']->get('database.migrations')];
}
/**
* Perform any work that should take place before the database has started truncating.
*
* @return void
*/
protected function beforeTruncatingDatabase(): void
{
//
}
/**
* Perform any work that should take place once the database has finished truncating.
*
* @return void
*/
protected function afterTruncatingDatabase(): void
{
//
}
}