-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTableSeeder.php
184 lines (160 loc) · 5.38 KB
/
TableSeeder.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
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
namespace diecoding\seeder;
use Faker\Factory;
use Yii;
use yii\base\NotSupportedException;
use yii\db\Exception;
use yii\db\Migration;
use yii\helpers\ArrayHelper;
/**
* Class TableSeeder
*
* @package diecoding\seeder
*
* @link [sugeng-sulistiyawan.github.io](sugeng-sulistiyawan.github.io)
* @author Sugeng Sulistiyawan <[email protected]>
* @copyright Copyright (c) 2023
*/
abstract class TableSeeder extends Migration
{
/** @var \Faker\Generator|Factory */
public $faker;
/** @var bool `true` for truncate tables before run seeder, default `true` */
public $truncateTable = true;
/** @var string|null if `null` use `Yii::$app->language`, default `null` */
public $locale;
/** @var array */
protected $insertedColumns = [];
/** @var array */
protected $batch = [];
/**
* TableSeeder constructor.
*
* @param array $config
*/
public function __construct(array $config = [])
{
if ($this->locale === null) {
$this->locale = str_replace('-', '_', Yii::$app->language);
}
$this->faker = Factory::create($this->locale);
parent::__construct($config);
}
/**
* TableSeeder destructor.
*
* @throws Exception
* @throws NotSupportedException
*/
public function __destruct()
{
if ($this->truncateTable) {
$this->disableForeignKeyChecks();
foreach ($this->batch as $table => $values) {
$this->truncateTable($table);
}
$this->enableForeignKeyChecks();
}
foreach ($this->batch as $table => $values) {
$total = 0;
foreach ($values as $columns => $rows) {
$total += count($rows);
parent::batchInsert($table, explode(',', $columns), $rows);
}
echo " $total row" . ($total > 1 ? 's' : null) . " inserted in $table" . "\n";
}
$this->checkMissingColumns($this->insertedColumns);
}
/**
* @return void
* @throws Exception
* @throws NotSupportedException
*/
public function disableForeignKeyChecks()
{
$this->db->createCommand()->checkIntegrity(false)->execute();
}
/**
* @return void
* @throws Exception
* @throws NotSupportedException
*/
public function enableForeignKeyChecks()
{
$this->db->createCommand()->checkIntegrity(true)->execute();
}
/**
* Creates and executes an INSERT SQL statement.
* The method will properly escape the column names, and bind the values to be inserted.
* @param string $table the table that new rows will be inserted into.
* @param array $columns the column data (name => value) to be inserted into the table.
*/
public function insert($table, $columns)
{
$this->insertedColumns[$table] = ArrayHelper::merge(
array_keys($columns),
isset($this->insertedColumns[$table]) ? $this->insertedColumns[$table] : []
);
$this->batch[$table][implode(',', array_keys($columns))][] = array_values($columns);
}
/**
* Creates and executes a batch INSERT SQL statement.
* The method will properly escape the column names, and bind the values to be inserted.
* @param string $table the table that new rows will be inserted into.
* @param array $columns the column names.
* @param array $rows the rows to be batch inserted into the table
*/
public function batchInsert($table, $columns, $rows)
{
$this->insertedColumns[$table] = ArrayHelper::merge(
$columns,
isset($this->insertedColumns[$table]) ? $this->insertedColumns[$table] : []
);
foreach ($rows as $row) {
$this->batch[$table][implode(',', $columns)][] = $row;
}
}
/**
* @return static
*/
public static function create()
{
return new static;
}
/**
* @return void
*/
abstract public function run();
/**
* Check missing column
*
* @param array $insertedColumns
* @return void
*/
protected function checkMissingColumns($insertedColumns)
{
$missingColumns = [];
foreach ($insertedColumns as $table => $columns) {
$tableColumns = $this->db->getTableSchema($table)->columns;
foreach ($tableColumns as $tableColumn) {
if (!$tableColumn->autoIncrement && !in_array($tableColumn->name, $columns, true)) {
$missingColumns[$table][] = [$tableColumn->name, $tableColumn->dbType];
}
}
}
if (count($missingColumns)) {
echo " > #" . str_pad(' MISSING COLUMNS ', 70, '#', STR_PAD_BOTH) . "#\n";
foreach ($missingColumns as $table => $columns) {
echo " > #" . str_pad('', 70, ' ') . "#\n";
echo " > #" . str_pad(" TABLE: {$table}", 70, ' ') . "#\n";
echo " > # " . str_pad('', 68, '-') . " #\n";
foreach ($columns as [$tableColumn, $type]) {
echo " > #" . str_pad(" - $tableColumn => $type", 70, ' ') . "#\n";
}
echo " > # " . str_pad('', 68, '-') . " #\n";
echo " > #" . str_pad('', 70, ' ') . "#\n";
}
echo " > #" . str_pad('', 70, '#') . "#\n";
}
}
}