-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.php
76 lines (56 loc) · 1.68 KB
/
install.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
<?php
if (PHP_SAPI !== 'cli') {
exit('Run this script via the command line instead.'."\n");
}
$config = require __DIR__.'/bootstrap.php';
const CODE_NUMBER = 500_000;
const CODE_LENGTH = 10;
const INSERT_CHUNK = 50_000;
function generateCode(int $length): string
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$maxCharIndex = strlen($characters) - 1;
$code = '';
for ($i = 0; $i < $length; $i++) {
$code .= $characters[mt_rand(0, $maxCharIndex)];
}
return $code;
}
$db = new \App\Database\Database(
$config['db']['host'],
$config['db']['name'],
$config['db']['user'],
$config['db']['password']
);
$createPromoCodesTable = <<<SQL
CREATE TABLE IF NOT EXISTS `promocodes`(
`id` INT AUTO_INCREMENT,
`user_id` INT NULL,
`code` VARCHAR(10) NOT NULL,
`busy_at` TIMESTAMP NULL,
PRIMARY KEY(`id`),
UNIQUE KEY user_id_unique (`user_id`)
);
SQL;
$startTime = microtime(1);
$db->getConnection()->exec($createPromoCodesTable);
$exists = $db->query('SELECT EXISTS(SELECT 1 FROM `promocodes`) AS `already`')->fetch();
if ($exists['already']) {
exit('The database is already full. Aborted.'."\n");
}
echo 'Processing...'."\n";
$codes = [];
$count = 0;
while ($count < CODE_NUMBER) {
$code = generateCode(CODE_LENGTH);
if (empty($codes[$code])) {
$codes[$code] = "('$code')";
++$count;
}
}
foreach (array_chunk($codes, INSERT_CHUNK) as $chunk) {
$insertValues = implode(',', $chunk);
$db->getConnection()->exec("INSERT INTO `promocodes` (`code`) VALUES $insertValues");
}
$endTime = microtime(1) - $startTime;
echo 'Done in '.$endTime.'s. '.CODE_NUMBER.' rows inserted.'."\n";