|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\TestSetupDeclarationModule8\Setup; |
| 9 | + |
| 10 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 11 | +use Magento\Framework\DB\Ddl\Table; |
| 12 | +use Magento\Framework\Setup\InstallSchemaInterface; |
| 13 | +use Magento\Framework\Setup\ModuleContextInterface; |
| 14 | +use Magento\Framework\Setup\SchemaSetupInterface; |
| 15 | + |
| 16 | +/** |
| 17 | + * Install schema script for the TestSetupDeclarationModule8 module. |
| 18 | + */ |
| 19 | +class InstallSchema implements InstallSchemaInterface |
| 20 | +{ |
| 21 | + /** |
| 22 | + * The name of the main table of Module8. |
| 23 | + */ |
| 24 | + const MAIN_TABLE = 'module8_test_main_table'; |
| 25 | + |
| 26 | + /** |
| 27 | + * The name of the second table of Module8. |
| 28 | + */ |
| 29 | + const SECOND_TABLE = 'module8_test_second_table'; |
| 30 | + |
| 31 | + /** |
| 32 | + * The name of the second table of Module8. |
| 33 | + */ |
| 34 | + const TEMP_TABLE = 'module8_test_install_temp_table'; |
| 35 | + |
| 36 | + /** |
| 37 | + * @inheritdoc |
| 38 | + * @throws \Zend_Db_Exception |
| 39 | + */ |
| 40 | + public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) |
| 41 | + { |
| 42 | + $setup->startSetup(); |
| 43 | + |
| 44 | + $this->createTables($setup); |
| 45 | + |
| 46 | + $setup->endSetup(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Create tables. |
| 51 | + * |
| 52 | + * @param SchemaSetupInterface $installer |
| 53 | + * @throws \Zend_Db_Exception |
| 54 | + */ |
| 55 | + private function createTables(SchemaSetupInterface $installer) |
| 56 | + { |
| 57 | + $mainTableName = $installer->getTable(self::MAIN_TABLE); |
| 58 | + $this->dropTableIfExists($installer, $mainTableName); |
| 59 | + $mainTable = $installer->getConnection()->newTable($mainTableName); |
| 60 | + $mainTable->setComment('Main Test Table for Module8'); |
| 61 | + $this->addColumnsToMainTable($mainTable); |
| 62 | + $this->addIndexesToMainTable($mainTable); |
| 63 | + $installer->getConnection()->createTable($mainTable); |
| 64 | + |
| 65 | + $secondTableName = $installer->getTable(self::SECOND_TABLE); |
| 66 | + $this->dropTableIfExists($installer, $secondTableName); |
| 67 | + $secondTable = $installer->getConnection()->newTable($secondTableName); |
| 68 | + $secondTable->setComment('Second Test Table for Module8'); |
| 69 | + $this->addColumnsToSecondTable($secondTable); |
| 70 | + $this->addIndexesToSecondTable($secondTable); |
| 71 | + $this->addConstraintsToSecondTable($secondTable); |
| 72 | + $installer->getConnection()->createTable($secondTable); |
| 73 | + |
| 74 | + $this->createSimpleTable($installer, self::TEMP_TABLE); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Drop existing tables. |
| 79 | + * |
| 80 | + * @param SchemaSetupInterface $installer |
| 81 | + * @param string $table |
| 82 | + */ |
| 83 | + private function dropTableIfExists($installer, $table) |
| 84 | + { |
| 85 | + $connection = $installer->getConnection(); |
| 86 | + if ($connection->isTableExists($installer->getTable($table))) { |
| 87 | + $connection->dropTable( |
| 88 | + $installer->getTable($table) |
| 89 | + ); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Add tables to main table. |
| 95 | + * |
| 96 | + * @param Table $table |
| 97 | + * @throws \Zend_Db_Exception |
| 98 | + */ |
| 99 | + private function addColumnsToMainTable($table) |
| 100 | + { |
| 101 | + $table |
| 102 | + ->addColumn( |
| 103 | + 'module8_email_contact_id', |
| 104 | + Table::TYPE_INTEGER, |
| 105 | + 10, |
| 106 | + [ |
| 107 | + 'primary' => true, |
| 108 | + 'identity' => true, |
| 109 | + 'unsigned' => true, |
| 110 | + 'nullable' => false |
| 111 | + ], |
| 112 | + 'Email Contact ID' |
| 113 | + )->addColumn( |
| 114 | + 'module8_contact_group_id', |
| 115 | + Table::TYPE_INTEGER, |
| 116 | + 10, |
| 117 | + [ |
| 118 | + 'unsigned' => true, |
| 119 | + 'nullable' => false |
| 120 | + ], |
| 121 | + 'Contact Group ID' |
| 122 | + )->addColumn( |
| 123 | + 'module8_is_guest', |
| 124 | + Table::TYPE_SMALLINT, |
| 125 | + null, |
| 126 | + [ |
| 127 | + 'unsigned' => true, |
| 128 | + 'nullable' => true |
| 129 | + ], |
| 130 | + 'Is Guest' |
| 131 | + )->addColumn( |
| 132 | + 'module8_contact_id', |
| 133 | + Table::TYPE_TEXT, |
| 134 | + 15, |
| 135 | + [ |
| 136 | + 'unsigned' => true, |
| 137 | + 'nullable' => true |
| 138 | + ], |
| 139 | + 'Contact ID' |
| 140 | + )->addColumn( |
| 141 | + 'module8_content', |
| 142 | + Table::TYPE_TEXT, |
| 143 | + 15, |
| 144 | + [ |
| 145 | + 'nullable' => false, |
| 146 | + ], |
| 147 | + 'Content' |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Add indexes to main table. |
| 153 | + * |
| 154 | + * @param Table $table |
| 155 | + * @throws \Zend_Db_Exception |
| 156 | + */ |
| 157 | + private function addIndexesToMainTable($table) |
| 158 | + { |
| 159 | + $table |
| 160 | + ->addIndex( |
| 161 | + 'MODULE8_INSTALL_INDEX_1', |
| 162 | + ['module8_email_contact_id'] |
| 163 | + )->addIndex( |
| 164 | + 'MODULE8_INSTALL_UNIQUE_INDEX_2', |
| 165 | + ['module8_email_contact_id', 'module8_is_guest'], |
| 166 | + ['type' => AdapterInterface::INDEX_TYPE_UNIQUE] |
| 167 | + )->addIndex( |
| 168 | + 'MODULE8_INSTALL_INDEX_3', |
| 169 | + ['module8_is_guest'] |
| 170 | + )->addIndex( |
| 171 | + 'MODULE8_INSTALL_INDEX_4', |
| 172 | + ['module8_contact_id'] |
| 173 | + )->addIndex( |
| 174 | + 'MODULE8_INSTALL_INDEX_TEMP', |
| 175 | + ['module8_content'] |
| 176 | + )->addIndex( |
| 177 | + 'MODULE8_INSTALL_UNIQUE_INDEX_TEMP', |
| 178 | + ['module8_contact_group_id'], |
| 179 | + ['type' => AdapterInterface::INDEX_TYPE_UNIQUE] |
| 180 | + ); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Add tables to second table. |
| 185 | + * |
| 186 | + * @param Table $table |
| 187 | + * @throws \Zend_Db_Exception |
| 188 | + */ |
| 189 | + private function addColumnsToSecondTable($table) |
| 190 | + { |
| 191 | + $table |
| 192 | + ->addColumn( |
| 193 | + 'module8_entity_id', |
| 194 | + Table::TYPE_INTEGER, |
| 195 | + 10, |
| 196 | + [ |
| 197 | + 'primary' => true, |
| 198 | + 'identity' => true, |
| 199 | + 'unsigned' => true, |
| 200 | + 'nullable' => false |
| 201 | + ], |
| 202 | + 'Entity ID' |
| 203 | + )->addColumn( |
| 204 | + 'module8_contact_id', |
| 205 | + Table::TYPE_INTEGER, |
| 206 | + null, |
| 207 | + [], |
| 208 | + 'Contact ID' |
| 209 | + )->addColumn( |
| 210 | + 'module8_address', |
| 211 | + Table::TYPE_TEXT, |
| 212 | + 15, |
| 213 | + [ |
| 214 | + 'nullable' => false, |
| 215 | + ], |
| 216 | + 'Address' |
| 217 | + )->addColumn( |
| 218 | + 'module8_counter_with_multiline_comment', |
| 219 | + Table::TYPE_SMALLINT, |
| 220 | + null, |
| 221 | + [ |
| 222 | + 'unsigned' => true, |
| 223 | + 'nullable' => true, |
| 224 | + 'default' => 0 |
| 225 | + ], |
| 226 | + 'Empty |
| 227 | + Counter |
| 228 | + Multiline |
| 229 | + Comment' |
| 230 | + )->addColumn( |
| 231 | + 'module8_second_address', |
| 232 | + Table::TYPE_TEXT, |
| 233 | + 15, |
| 234 | + [ |
| 235 | + 'unsigned' => true, |
| 236 | + 'nullable' => true |
| 237 | + ], |
| 238 | + 'Second Address' |
| 239 | + )->addColumn( |
| 240 | + 'module8_temp_column', |
| 241 | + Table::TYPE_TEXT, |
| 242 | + 15, |
| 243 | + [ |
| 244 | + 'unsigned' => true, |
| 245 | + 'nullable' => true |
| 246 | + ], |
| 247 | + 'Temp column for remove' |
| 248 | + ); |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * Add indexes to second table. |
| 253 | + * |
| 254 | + * @param Table $table |
| 255 | + * @throws \Zend_Db_Exception |
| 256 | + */ |
| 257 | + private function addIndexesToSecondTable($table) |
| 258 | + { |
| 259 | + $table |
| 260 | + ->addIndex( |
| 261 | + 'MODULE8_INSTALL_SECOND_TABLE_INDEX_1', |
| 262 | + ['module8_entity_id'] |
| 263 | + )->addIndex( |
| 264 | + 'MODULE8_INSTALL_SECOND_TABLE_INDEX_2', |
| 265 | + ['module8_address'] |
| 266 | + )->addIndex( |
| 267 | + 'MODULE8_INSTALL_SECOND_TABLE_INDEX_3_TEMP', |
| 268 | + ['module8_second_address'] |
| 269 | + ); |
| 270 | + } |
| 271 | + |
| 272 | + /** |
| 273 | + * Add constraints to second table. |
| 274 | + * |
| 275 | + * @param Table $table |
| 276 | + * @throws \Zend_Db_Exception |
| 277 | + */ |
| 278 | + private function addConstraintsToSecondTable($table) |
| 279 | + { |
| 280 | + $table |
| 281 | + ->addForeignKey( |
| 282 | + 'MODULE8_INSTALL_FK_ENTITY_ID_TEST_MAIN_TABLE_EMAIL_CONTACT_ID', |
| 283 | + 'module8_entity_id', |
| 284 | + self::MAIN_TABLE, |
| 285 | + 'module8_email_contact_id' |
| 286 | + )->addForeignKey( |
| 287 | + 'MODULE8_INSTALL_FK_ADDRESS_TEST_MAIN_TABLE_CONTACT_ID', |
| 288 | + 'module8_address', |
| 289 | + self::MAIN_TABLE, |
| 290 | + 'module8_contact_id' |
| 291 | + )->addForeignKey( |
| 292 | + 'MODULE8_INSTALL_FK_ADDRESS_TEST_MAIN_TABLE_MODULE8_CONTENT_TEMP', |
| 293 | + 'module8_address', |
| 294 | + self::MAIN_TABLE, |
| 295 | + 'module8_content' |
| 296 | + ); |
| 297 | + } |
| 298 | + |
| 299 | + /** |
| 300 | + * Create a simple table. |
| 301 | + * |
| 302 | + * @param SchemaSetupInterface $setup |
| 303 | + * @param $tableName |
| 304 | + * @throws \Zend_Db_Exception |
| 305 | + */ |
| 306 | + private function createSimpleTable(SchemaSetupInterface $setup, $tableName): void |
| 307 | + { |
| 308 | + $table = $setup->getConnection()->newTable($tableName); |
| 309 | + $table |
| 310 | + ->addColumn( |
| 311 | + 'module8_entity_id', |
| 312 | + Table::TYPE_INTEGER, |
| 313 | + null, |
| 314 | + [ |
| 315 | + 'primary' => true, |
| 316 | + 'identity' => true, |
| 317 | + 'nullable' => false, |
| 318 | + 'unsigned' => true, |
| 319 | + ], |
| 320 | + 'Entity ID' |
| 321 | + )->addColumn( |
| 322 | + 'module8_counter', |
| 323 | + Table::TYPE_INTEGER, |
| 324 | + null, |
| 325 | + [ |
| 326 | + 'unsigned' => true, |
| 327 | + 'nullable' => true, |
| 328 | + 'default' => 100 |
| 329 | + ], |
| 330 | + 'Counter' |
| 331 | + ); |
| 332 | + $setup->getConnection()->createTable($table); |
| 333 | + } |
| 334 | +} |
0 commit comments