1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GinoPane\BlogTaxonomy\Updates; |
4
|
|
|
|
5
|
|
|
use GinoPane\BlogTaxonomy\Models\Series; |
6
|
|
|
use Schema; |
7
|
|
|
use System\Classes\PluginManager; |
8
|
|
|
use GinoPane\BlogTaxonomy\Models\Tag; |
9
|
|
|
use October\Rain\Database\Updates\Migration; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class CreateTaxonomiesTables |
13
|
|
|
* |
14
|
|
|
* @package GinoPane\BlogTaxonomy\Updates |
15
|
|
|
*/ |
16
|
|
|
class CreateTaxonomiesTables extends Migration |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Execute migrations |
20
|
|
|
*/ |
21
|
|
|
public function up() |
22
|
|
|
{ |
23
|
|
|
if (PluginManager::instance()->hasPlugin('RainLab.Blog')) { |
24
|
|
|
$this->createTags(); |
25
|
|
|
|
26
|
|
|
$this->createSeries(); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Rollback migrations |
32
|
|
|
*/ |
33
|
|
|
public function down() |
34
|
|
|
{ |
35
|
|
|
if (PluginManager::instance()->hasPlugin('RainLab.Blog')) { |
36
|
|
|
$this->dropTags(); |
37
|
|
|
|
38
|
|
|
$this->dropSeries(); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Rollback Tags migration |
44
|
|
|
*/ |
45
|
|
|
private function dropTags() |
46
|
|
|
{ |
47
|
|
|
Schema::dropIfExists('ginopane_blogtaxonomy_post_tag'); |
48
|
|
|
Schema::dropIfExists(Tag::TABLE_NAME); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Rollback Series migration |
53
|
|
|
*/ |
54
|
|
|
private function dropSeries() |
55
|
|
|
{ |
56
|
|
|
Schema::table('rainlab_blog_posts', static function ($table) { |
57
|
|
|
$table->dropForeign([Series::TABLE_NAME . '_id']); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
View Code Duplication |
if (Schema::hasColumn('rainlab_blog_posts', Series::TABLE_NAME . '_id')) { |
|
|
|
|
61
|
|
|
Schema::table('rainlab_blog_posts', static function ($table) { |
62
|
|
|
$table->dropColumn(Series::TABLE_NAME . '_id'); |
63
|
|
|
}); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
Schema::dropIfExists(Series::TABLE_NAME); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Create Tags table |
71
|
|
|
*/ |
72
|
|
|
private function createTags() |
73
|
|
|
{ |
74
|
|
|
if (!Schema::hasTable(Tag::TABLE_NAME)) { |
75
|
|
|
Schema::create( |
76
|
|
|
Tag::TABLE_NAME, |
77
|
|
View Code Duplication |
static function ($table) { |
|
|
|
|
78
|
|
|
$table->engine = 'InnoDB'; |
79
|
|
|
|
80
|
|
|
$table->increments('id'); |
81
|
|
|
$table->string('name')->unique(); |
82
|
|
|
$table->string('slug')->unique(); |
83
|
|
|
$table->timestamps(); |
84
|
|
|
} |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
if (!Schema::hasTable('ginopane_blogtaxonomy_post_tag')) { |
|
|
|
|
89
|
|
|
Schema::create( |
90
|
|
|
'ginopane_blogtaxonomy_post_tag', |
91
|
|
|
static function ($table) { |
92
|
|
|
$table->engine = 'InnoDB'; |
93
|
|
|
|
94
|
|
|
$table->integer('tag_id')->unsigned()->nullable()->default(null); |
95
|
|
|
$table->integer('post_id')->unsigned()->nullable()->default(null); |
96
|
|
|
$table->index(['tag_id', 'post_id']); |
97
|
|
|
$table->foreign('tag_id')->references('id')->on(Tag::TABLE_NAME)->onDelete('cascade'); |
98
|
|
|
$table->foreign('post_id')->references('id')->on('rainlab_blog_posts')->onDelete('cascade'); |
99
|
|
|
} |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Create Series table |
106
|
|
|
*/ |
107
|
|
|
private function createSeries() |
108
|
|
|
{ |
109
|
|
|
if (!Schema::hasTable(Series::TABLE_NAME)) { |
110
|
|
|
Schema::create( |
111
|
|
|
Series::TABLE_NAME, |
112
|
|
View Code Duplication |
static function ($table) { |
|
|
|
|
113
|
|
|
$table->engine = 'InnoDB'; |
114
|
|
|
|
115
|
|
|
$table->increments('id'); |
116
|
|
|
$table->string('title')->unique(); |
117
|
|
|
$table->string('slug')->unique(); |
118
|
|
|
$table->string('description')->nullable(); |
119
|
|
|
$table->timestamps(); |
120
|
|
|
} |
121
|
|
|
); |
122
|
|
|
|
123
|
|
View Code Duplication |
Schema::table('rainlab_blog_posts', function ($table) { |
|
|
|
|
124
|
|
|
$table->integer(Series::TABLE_NAME . '_id')->unsigned()->nullable()->default(null); |
125
|
|
|
$table->foreign(Series::TABLE_NAME . '_id')->references('id')->on(Series::TABLE_NAME)->onDelete('cascade'); |
126
|
|
|
}); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
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.