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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
* Copyright © 2015 Ihor Vansach (ihor@magefan.com). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*
* Glory to Ukraine! Glory to the heroes!
*/
namespace Magefan\Blog\Setup;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
/**
* Blog update
*/
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$setup->startSetup();
$version = $context->getVersion();
$connection = $setup->getConnection();
if (version_compare($version, '2.0.1') < 0) {
foreach (['magefan_blog_post_relatedpost', 'magefan_blog_post_relatedproduct'] as $tableName) {
// Get module table
$tableName = $setup->getTable($tableName);
// Check if the table already exists
if ($connection->isTableExists($tableName) == true) {
$columns = [
'position' => [
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
'nullable' => false,
'comment' => 'Position',
],
];
foreach ($columns as $name => $definition) {
$connection->addColumn($tableName, $name, $definition);
}
}
}
$connection->addColumn(
$setup->getTable('magefan_blog_post'),
'featured_img',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'nullable' => true,
'comment' => 'Thumbnail Image',
]
);
}
if (version_compare($version, '2.2.0') < 0) {
/* Add author field to posts tabel */
$connection->addColumn(
$setup->getTable('magefan_blog_post'),
'author_id',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
'nullable' => true,
'comment' => 'Author ID',
]
);
$connection->addIndex(
$setup->getTable('magefan_blog_post'),
$setup->getIdxName($setup->getTable('magefan_blog_post'), ['author_id']),
['author_id']
);
}
if (version_compare($version, '2.2.5') < 0) {
/* Add layout field to posts and category tabels */
foreach (['magefan_blog_post', 'magefan_blog_category'] as $table) {
$table = $setup->getTable($table);
$connection->addColumn(
$setup->getTable($table),
'page_layout',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'nullable' => true,
'comment' => 'Post Layout',
]
);
$connection->addColumn(
$setup->getTable($table),
'layout_update_xml',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => '64k',
'nullable' => true,
'comment' => 'Post Layout Update Content',
]
);
$connection->addColumn(
$setup->getTable($table),
'custom_theme',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 100,
'nullable' => true,
'comment' => 'Post Custom Theme',
]
);
$connection->addColumn(
$setup->getTable($table),
'custom_layout',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'nullable' => true,
'comment' => 'Post Custom Template',
]
);
$connection->addColumn(
$setup->getTable($table),
'custom_layout_update_xml',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => '64k',
'nullable' => true,
'comment' => 'Post Custom Layout Update Content',
]
);
$connection->addColumn(
$setup->getTable($table),
'custom_theme_from',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_DATE,
'nullable' => true,
'comment' => 'Post Custom Theme Active From Date',
]
);
$connection->addColumn(
$setup->getTable($table),
'custom_theme_to',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_DATE,
'nullable' => true,
'comment' => 'Post Custom Theme Active To Date',
]
);
}
}
if (version_compare($version, '2.3.0') < 0) {
/* Add meta title field to posts tabel */
$connection->addColumn(
$setup->getTable('magefan_blog_post'),
'meta_title',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'nullable' => true,
'comment' => 'Post Meta Title',
'after' => 'title'
]
);
/* Add og tags fields to post tabel */
foreach (['type', 'img', 'description', 'title'] as $type) {
$connection->addColumn(
$setup->getTable('magefan_blog_post'),
'og_' . $type,
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'nullable' => true,
'comment' => 'Post OG ' . ucfirst($type),
'after' => 'identifier'
]
);
}
/* Add meta title field to category tabel */
$connection->addColumn(
$setup->getTable('magefan_blog_category'),
'meta_title',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'nullable' => true,
'comment' => 'Category Meta Title',
'after' => 'title'
]
);
/**
* Create table 'magefan_blog_tag'
*/
$table = $setup->getConnection()->newTable(
$setup->getTable('magefan_blog_tag')
)->addColumn(
'tag_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
['identity' => true, 'nullable' => false, 'primary' => true],
'Tag ID'
)->addColumn(
'title',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
['nullable' => true],
'Tag Title'
)->addColumn(
'identifier',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
100,
['nullable' => true, 'default' => null],
'Tag String Identifier'
)->addIndex(
$setup->getIdxName('magefan_blog_tag', ['identifier']),
['identifier']
)->setComment(
'Magefan Blog Tag Table'
);
$setup->getConnection()->createTable($table);
/**
* Create table 'magefan_blog_post_tag'
*/
$table = $setup->getConnection()->newTable(
$setup->getTable('magefan_blog_post_tag')
)->addColumn(
'post_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
['nullable' => false, 'primary' => true],
'Post ID'
)->addColumn(
'tag_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
['nullable' => false, 'primary' => true],
'Tag ID'
)->addIndex(
$setup->getIdxName('magefan_blog_post_tag', ['tag_id']),
['tag_id']
)->addForeignKey(
$setup->getFkName('magefan_blog_post_tag', 'post_id', 'magefan_blog_post', 'post_id'),
'post_id',
$setup->getTable('magefan_blog_post'),
'post_id',
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
)->addForeignKey(
$setup->getFkName('magefan_blog_post_tag', 'tag_id', 'magefan_blog_tag', 'tag_id'),
'tag_id',
$setup->getTable('magefan_blog_tag'),
'tag_id',
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
)->setComment(
'Magefan Blog Post To Category Linkage Table'
);
$setup->getConnection()->createTable($table);
}
$setup->endSetup();
}
}