1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace tomzx\IRCStats; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Connection; |
6
|
|
|
use Illuminate\Database\Schema\Blueprint; |
7
|
|
|
|
8
|
|
|
class DatabaseSchema { |
9
|
|
|
/** |
10
|
|
|
* @param \Illuminate\Database\Connection $db |
11
|
|
|
*/ |
12
|
1 |
|
public function initialize(Connection $db) |
13
|
|
|
{ |
14
|
1 |
|
$builder = $db->getSchemaBuilder(); |
15
|
|
|
|
16
|
|
|
$tables = [ |
17
|
|
|
'networks' => function (Blueprint $table) { |
18
|
|
|
$table->increments('id'); |
19
|
|
|
$table->string('server'); |
20
|
|
|
$table->unique(['server']); |
21
|
1 |
|
}, |
22
|
|
View Code Duplication |
'channels' => function (Blueprint $table) { |
|
|
|
|
23
|
|
|
$table->increments('id'); |
24
|
|
|
$table->integer('network_id')->unsigned(); |
25
|
|
|
$table->string('channel'); |
26
|
|
|
$table->unique(['network_id', 'channel']); |
27
|
|
|
|
28
|
|
|
$table->foreign('network_id')->references('id')->on('networks'); |
29
|
1 |
|
}, |
30
|
|
View Code Duplication |
'nicks' => function (Blueprint $table) { |
|
|
|
|
31
|
|
|
$table->increments('id'); |
32
|
|
|
$table->integer('network_id')->unsigned(); |
33
|
|
|
$table->string('nick'); |
34
|
|
|
$table->unique(['network_id', 'nick']); |
35
|
|
|
|
36
|
|
|
$table->foreign('network_id')->references('id')->on('networks'); |
37
|
1 |
|
}, |
38
|
|
|
'logs' => function (Blueprint $table) { |
39
|
|
|
$table->increments('id'); |
40
|
|
|
$table->integer('channel_id')->unsigned(); |
41
|
|
|
$table->integer('nick_id')->unsigned(); |
42
|
|
|
$table->timestamp('timestamp'); |
43
|
|
|
$table->text('message'); |
44
|
|
|
|
45
|
|
|
$table->foreign('channel_id')->references('id')->on('channels'); |
46
|
|
|
$table->foreign('nick_id')->references('id')->on('nicks'); |
47
|
1 |
|
}, |
48
|
|
|
// Generated data tables based on the content of the previous tables |
|
|
|
|
49
|
|
|
// 'dictionary' => function (Blueprint $table) { |
50
|
|
|
// $table->increments('id'); |
51
|
|
|
// $table->string('word', 40); |
52
|
|
|
// }, |
53
|
|
|
'words' => function (Blueprint $table) { |
54
|
|
|
$table->increments('id'); |
55
|
|
|
$table->string('word', 40); |
56
|
1 |
|
}, |
57
|
1 |
|
'logs_words' => function (Blueprint $table) { |
58
|
|
|
$table->increments('id'); |
59
|
|
|
$table->integer('logs_id')->unsigned(); |
60
|
|
|
$table->integer('word_id')->unsigned(); |
61
|
|
|
|
62
|
|
|
$table->foreign('logs_id')->references('id')->on('logs'); |
63
|
|
|
$table->foreign('word_id')->references('id')->on('words'); |
64
|
1 |
|
}, |
65
|
1 |
|
]; |
66
|
|
|
|
67
|
1 |
|
foreach ($tables as $table => $callback) { |
68
|
1 |
|
if ($builder->hasTable($table)) { |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
1 |
|
$builder->create($table, $callback); |
72
|
1 |
|
} |
73
|
1 |
|
} |
74
|
|
|
} |
75
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.