/
20180718_0001_create_tables.php
200 lines (181 loc) · 8.12 KB
/
20180718_0001_create_tables.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
declare(strict_types=1);
namespace Vdlp\Redirect\Updates;
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use October\Rain\Database\Updates\Migration;
use Psr\Log\LoggerInterface;
use Throwable;
use Vdlp\Redirect\Models\Category;
use Vdlp\Redirect\Models\Redirect;
use Vdlp\Redirect\Models\Settings;
class CreateTables extends Migration
{
public function up(): void
{
/** @var DatabaseManager $database */
$database = resolve('db');
$schema = $database->getSchemaBuilder();
// Drop any existing index keys on SQLite databases #24.
if (
$schema->hasTable('adrenth_redirect_redirects')
&& $database->getDriverName() === 'sqlite'
) {
$statements = [
'DROP INDEX redirect_dmy;',
'DROP INDEX redirect_my;',
'DROP INDEX redirect_log_dmy;',
'DROP INDEX redirect_log_my;',
'DROP INDEX month_year;',
];
foreach ($statements as $statement) {
try {
$database->statement($statement);
} catch (Throwable $e) {
/** @var LoggerInterface $logger */
$logger = resolve(LoggerInterface::class);
$logger->error(sprintf('Vdlp.Redirect: Unable to drop index: %s'. $e->getMessage()));
continue;
}
}
}
if (!Schema::hasTable('vdlp_redirect_categories')) {
Schema::create('vdlp_redirect_categories', static function (Blueprint $table): void {
// Table configuration
$table->engine = 'InnoDB';
// Columns
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Category::create(['name' => 'General']);
}
if (!Schema::hasTable('vdlp_redirect_redirects')) {
Schema::create('vdlp_redirect_redirects', static function (Blueprint $table): void {
// Table MySQL configuration
$table->engine = 'InnoDB';
// Columns
$table->increments('id');
$table->unsignedInteger('category_id')->nullable();
// @see 20200408_0008_change_column_types_from_char_to_varchar.php
$table->char('match_type', 12)->nullable();
// @see 20200408_0008_change_column_types_from_char_to_varchar.php
$table->char('target_type', 12)
->default(Redirect::TARGET_TYPE_PATH_URL);
// @see 20200408_0008_change_column_types_from_char_to_varchar.php
$table->char('from_scheme', 5)
->default(Redirect::SCHEME_AUTO);
$table->mediumText('from_url')->nullable();
$table->char('to_scheme', 5)
->default(Redirect::SCHEME_AUTO);
$table->mediumText('to_url')->nullable();
$table->mediumText('test_url')->nullable();
$table->string('cms_page')->nullable();
$table->string('static_page')->nullable();
$table->text('requirements')->nullable();
// @see 20200408_0008_change_column_types_from_char_to_varchar.php
$table->char('status_code', 3);
$table->unsignedInteger('hits')->default(0);
$table->date('from_date')->nullable();
$table->date('to_date')->nullable();
$table->unsignedInteger('sort_order')->default(0);
$table->boolean('is_enabled')->default(0);
$table->boolean('test_lab')->default(0);
$table->mediumText('test_lab_path')->nullable();
$table->boolean('system')->default(0);
$table->timestamp('last_used_at')->nullable();
$table->timestamps();
// Indexes
$table->index('sort_order');
$table->index('is_enabled');
// Foreign keys
$table->foreign('category_id')
->references('id')
->on('vdlp_redirect_categories')
->onDelete('set null');
});
}
if (!Schema::hasTable('vdlp_redirect_clients')) {
Schema::create('vdlp_redirect_clients', static function (Blueprint $table): void {
// Table MySQL configuration
$table->engine = 'InnoDB';
// Columns
$table->increments('id');
$table->unsignedInteger('redirect_id');
$table->timestamp('timestamp')->nullable();
$table->unsignedTinyInteger('day');
$table->unsignedTinyInteger('month');
$table->unsignedSmallInteger('year');
$table->string('crawler')->nullable();
// Indexes
$table->index(['redirect_id', 'day', 'month', 'year'], 'redirect_dmy');
$table->index(['redirect_id', 'month', 'year'], 'redirect_my');
// Foreign keys
$table->foreign('redirect_id', 'vdlp_redirect_client')
->references('id')
->on('vdlp_redirect_redirects')
->onDelete('cascade');
});
}
if (!Schema::hasTable('vdlp_redirect_redirect_logs')) {
Schema::create('vdlp_redirect_redirect_logs', static function (Blueprint $table): void {
// Table MySQL configuration
$table->engine = 'InnoDB';
// Columns
$table->increments('id');
$table->unsignedInteger('redirect_id');
$table->mediumText('from_url');
$table->mediumText('to_url');
// @see 20200408_0008_change_column_types_from_char_to_varchar.php
$table->char('status_code', 3);
$table->unsignedTinyInteger('day');
$table->unsignedTinyInteger('month');
$table->unsignedSmallInteger('year');
$table->dateTime('date_time');
$table->timestamps();
// Indexes
$table->index(['redirect_id', 'day', 'month', 'year'], 'redirect_log_dmy');
$table->index(['redirect_id', 'month', 'year'], 'redirect_log_my');
// Foreign keys
$table->foreign('redirect_id', 'vdlp_redirect_log')
->references('id')
->on('vdlp_redirect_redirects')
->onDelete('cascade');
});
}
try {
/** @noinspection PhpDynamicAsStaticMethodCallInspection */
$settings = Settings::instance();
$settings->logging_enabled = '1';
$settings->statistics_enabled = '1';
$settings->test_lab_enabled = '1';
$settings->save();
} catch (Throwable $e) {
/** @var LoggerInterface $logger */
$logger = resolve(LoggerInterface::class);
$logger->error(sprintf(
'Vdlp.Redirect: Unable to save default settings: %s',
$e->getMessage()
));
}
}
public function down(): void
{
try {
Schema::disableForeignKeyConstraints();
Schema::dropIfExists('vdlp_redirect_clients');
Schema::dropIfExists('vdlp_redirect_redirect_logs');
Schema::dropIfExists('vdlp_redirect_redirects');
Schema::dropIfExists('vdlp_redirect_categories');
Schema::enableForeignKeyConstraints();
} catch (Throwable $e) {
/** @var LoggerInterface $logger */
$logger = resolve(LoggerInterface::class);
$logger->error(sprintf(
'Vdlp.Redirect: Unable to drop all tables: %s',
$e->getMessage()
));
}
}
}