thecodingmachine /
tdbm
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* |
||
| 6 | Copyright (C) 2006-2014 David Négrier - THE CODING MACHINE |
||
| 7 | |||
| 8 | This program is free software; you can redistribute it and/or modify |
||
| 9 | it under the terms of the GNU General Public License as published by |
||
| 10 | the Free Software Foundation; either version 2 of the License, or |
||
| 11 | (at your option) any later version. |
||
| 12 | |||
| 13 | This program is distributed in the hope that it will be useful, |
||
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 16 | GNU General Public License for more details. |
||
| 17 | |||
| 18 | You should have received a copy of the GNU General Public License |
||
| 19 | along with this program; if not, write to the Free Software |
||
| 20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
||
| 21 | */ |
||
| 22 | |||
| 23 | namespace TheCodingMachine\TDBM; |
||
| 24 | |||
| 25 | use DateTime; |
||
| 26 | use DateTimeImmutable; |
||
| 27 | use Doctrine\Common\Cache\ArrayCache; |
||
| 28 | use Doctrine\DBAL\Connection; |
||
| 29 | use Doctrine\DBAL\Platforms\MySQLPlatform; |
||
| 30 | use Doctrine\DBAL\Platforms\OraclePlatform; |
||
| 31 | use Doctrine\DBAL\Types\Type; |
||
| 32 | use Doctrine\DBAL\Types\Types; |
||
| 33 | use PHPUnit\Framework\TestCase; |
||
| 34 | use TheCodingMachine\FluidSchema\TdbmFluidSchema; |
||
| 35 | use TheCodingMachine\TDBM\Fixtures\Interfaces\TestUserDaoInterface; |
||
| 36 | use TheCodingMachine\TDBM\Fixtures\Interfaces\TestUserInterface; |
||
| 37 | use TheCodingMachine\TDBM\Fixtures\Traits\TestOtherUserTrait; |
||
| 38 | use TheCodingMachine\TDBM\Fixtures\Traits\TestUserDaoTrait; |
||
| 39 | use TheCodingMachine\TDBM\Fixtures\Traits\TestUserTrait; |
||
| 40 | use TheCodingMachine\TDBM\Utils\Annotation\AnnotationParser; |
||
| 41 | use TheCodingMachine\TDBM\Utils\DefaultNamingStrategy; |
||
| 42 | use TheCodingMachine\TDBM\Utils\PathFinder\PathFinder; |
||
| 43 | |||
| 44 | use function stripos; |
||
| 45 | |||
| 46 | abstract class TDBMAbstractServiceTest extends TestCase |
||
| 47 | { |
||
| 48 | /** |
||
| 49 | * @var Connection |
||
| 50 | */ |
||
| 51 | protected static $dbConnection; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var TDBMService |
||
| 55 | */ |
||
| 56 | protected $tdbmService; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var DummyGeneratorListener |
||
| 60 | */ |
||
| 61 | private $dummyGeneratorListener; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var ConfigurationInterface |
||
| 65 | */ |
||
| 66 | private $configuration; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var ArrayCache |
||
| 70 | */ |
||
| 71 | private static $cache; |
||
| 72 | |||
| 73 | public static function setUpBeforeClass(): void |
||
| 74 | { |
||
| 75 | self::resetConnection(); |
||
| 76 | |||
| 77 | self::$cache = new ArrayCache(); |
||
| 78 | |||
| 79 | $dbConnection = ConnectionFactory::resetDatabase( |
||
| 80 | $GLOBALS['db_driver'], |
||
| 81 | $GLOBALS['db_host'] ?? null, |
||
| 82 | $GLOBALS['db_port'] ?? null, |
||
| 83 | $GLOBALS['db_username'] ?? null, |
||
| 84 | $GLOBALS['db_admin_username'] ?? null, |
||
| 85 | $GLOBALS['db_password'] ?? null, |
||
| 86 | $GLOBALS['db_name'] ?? null |
||
| 87 | ); |
||
| 88 | |||
| 89 | self::$dbConnection = $dbConnection; |
||
| 90 | |||
| 91 | self::initSchema($dbConnection); |
||
| 92 | } |
||
| 93 | |||
| 94 | private static function resetConnection(): void |
||
| 95 | { |
||
| 96 | if (self::$dbConnection !== null) { |
||
| 97 | self::$dbConnection->close(); |
||
| 98 | } |
||
| 99 | self::$dbConnection = null; |
||
| 100 | } |
||
| 101 | |||
| 102 | protected static function getConnection(): Connection |
||
| 103 | { |
||
| 104 | if (self::$dbConnection === null) { |
||
| 105 | self::$dbConnection = ConnectionFactory::createConnection( |
||
| 106 | $GLOBALS['db_driver'], |
||
| 107 | $GLOBALS['db_host'] ?? null, |
||
| 108 | $GLOBALS['db_port'] ?? null, |
||
| 109 | $GLOBALS['db_username'] ?? null, |
||
| 110 | $GLOBALS['db_password'] ?? null, |
||
| 111 | $GLOBALS['db_name'] ?? null |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | return self::$dbConnection; |
||
| 115 | } |
||
| 116 | |||
| 117 | protected function onlyMySql() |
||
| 118 | { |
||
| 119 | if (!self::getConnection()->getDatabasePlatform() instanceof MySqlPlatform) { |
||
| 120 | $this->markTestSkipped('MySQL specific test'); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | protected function setUp(): void |
||
| 125 | { |
||
| 126 | $this->tdbmService = new TDBMService($this->getConfiguration()); |
||
| 127 | } |
||
| 128 | |||
| 129 | protected function getDummyGeneratorListener(): DummyGeneratorListener |
||
| 130 | { |
||
| 131 | if ($this->dummyGeneratorListener === null) { |
||
| 132 | $this->dummyGeneratorListener = new DummyGeneratorListener(); |
||
| 133 | } |
||
| 134 | return $this->dummyGeneratorListener; |
||
| 135 | } |
||
| 136 | |||
| 137 | protected function getCache(): ArrayCache |
||
| 138 | { |
||
| 139 | return self::$cache; |
||
| 140 | } |
||
| 141 | |||
| 142 | protected function getConfiguration(): ConfigurationInterface |
||
| 143 | { |
||
| 144 | if ($this->configuration === null) { |
||
| 145 | $this->configuration = new Configuration('TheCodingMachine\\TDBM\\Test\\Dao\\Bean', 'TheCodingMachine\\TDBM\\Test\\Dao', self::getConnection(), $this->getNamingStrategy(), $this->getCache(), null, null, [$this->getDummyGeneratorListener()]); |
||
| 146 | $this->configuration->setPathFinder(new PathFinder(null, dirname(__DIR__, 4))); |
||
| 147 | } |
||
| 148 | return $this->configuration; |
||
| 149 | } |
||
| 150 | |||
| 151 | protected function getNamingStrategy() |
||
| 152 | { |
||
| 153 | $strategy = new DefaultNamingStrategy(AnnotationParser::buildWithDefaultAnnotations([]), self::getConnection()->createSchemaManager()); |
||
| 154 | $strategy->setBeanPrefix(''); |
||
| 155 | $strategy->setBeanSuffix('Bean'); |
||
| 156 | $strategy->setBaseBeanPrefix(''); |
||
| 157 | $strategy->setBaseBeanSuffix('BaseBean'); |
||
| 158 | $strategy->setDaoPrefix(''); |
||
| 159 | $strategy->setDaoSuffix('Dao'); |
||
| 160 | $strategy->setBaseDaoPrefix(''); |
||
| 161 | $strategy->setBaseDaoSuffix('BaseDao'); |
||
| 162 | |||
| 163 | return $strategy; |
||
| 164 | } |
||
| 165 | |||
| 166 | private static function initSchema(Connection $connection): void |
||
| 167 | { |
||
| 168 | $fromSchema = $connection->createSchemaManager()->createSchema(); |
||
| 169 | $toSchema = clone $fromSchema; |
||
| 170 | |||
| 171 | $db = new TdbmFluidSchema($toSchema, new \TheCodingMachine\FluidSchema\DefaultNamingStrategy($connection->getDatabasePlatform())); |
||
| 172 | |||
| 173 | $db->table('country') |
||
| 174 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 175 | ->column('label')->string(255)->unique(); |
||
| 176 | |||
| 177 | $db->table('person') |
||
| 178 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 179 | ->column('name')->string(255); |
||
| 180 | |||
| 181 | if ($connection->getDatabasePlatform() instanceof OraclePlatform) { |
||
| 182 | $toSchema->getTable($connection->quoteIdentifier('person')) |
||
| 183 | ->addColumn( |
||
| 184 | $connection->quoteIdentifier('created_at'), |
||
| 185 | 'datetime', |
||
| 186 | ['columnDefinition' => 'TIMESTAMP(0) DEFAULT SYSDATE NOT NULL'] |
||
| 187 | ); |
||
| 188 | } else { |
||
| 189 | $toSchema->getTable('person') |
||
| 190 | ->addColumn( |
||
| 191 | $connection->quoteIdentifier('created_at'), |
||
| 192 | 'datetime', |
||
| 193 | ['columnDefinition' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'] |
||
| 194 | ); |
||
| 195 | } |
||
| 196 | |||
| 197 | $db->table('person') |
||
| 198 | ->column('modified_at')->datetime()->null()->index() |
||
| 199 | ->column('order')->integer()->null(); |
||
| 200 | |||
| 201 | |||
| 202 | $db->table('contact') |
||
| 203 | ->extends('person') |
||
| 204 | ->column('email')->string(255) |
||
| 205 | ->column('manager_id')->references('contact')->null(); |
||
| 206 | |||
| 207 | $db->table('users') |
||
| 208 | ->addAnnotation('AddTrait', ['name' => TestUserTrait::class], false) |
||
| 209 | ->addAnnotation('AddTrait', ['name' => TestOtherUserTrait::class, 'modifiers' => ['\\' . TestOtherUserTrait::class . '::method1 insteadof \\' . TestUserTrait::class, '\\' . TestUserTrait::class . '::method1 as method1renamed']], false) |
||
| 210 | ->addAnnotation('AddTraitOnDao', ['name' => TestUserDaoTrait::class], false) |
||
| 211 | ->implementsInterface(TestUserInterface::class) |
||
| 212 | ->implementsInterfaceOnDao(TestUserDaoInterface::class) |
||
| 213 | ->extends('contact') |
||
| 214 | ->column('login')->string(255) |
||
| 215 | ->column('password')->string(255)->null() |
||
| 216 | ->column('status')->string(10)->null()->default(null) |
||
| 217 | ->column('country_id')->references('country') |
||
| 218 | // Used to test generation for a column that starts with a digit |
||
| 219 | ->then()->column('3d_view')->boolean()->default(true); |
||
| 220 | |||
| 221 | $db->table('rights') |
||
| 222 | ->column('label')->string(255)->primaryKey()->comment('Non autoincrementable primary key'); |
||
| 223 | |||
| 224 | $db->table('roles') |
||
| 225 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 226 | ->column('name')->string(255) |
||
| 227 | ->column('created_at')->date()->null() |
||
| 228 | ->column('status')->boolean()->null()->default(1); |
||
| 229 | |||
| 230 | $db->table('roles_rights') |
||
| 231 | ->column('role_id')->references('roles') |
||
| 232 | ->column('right_label')->references('rights')->then() |
||
| 233 | ->primaryKey(['role_id', 'right_label']); |
||
| 234 | |||
| 235 | $db->table('users_roles') |
||
| 236 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 237 | ->column('user_id')->references('users') |
||
| 238 | ->column('role_id')->references('roles'); |
||
| 239 | |||
| 240 | $db->table('all_nullable') |
||
| 241 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 242 | ->column('label')->string(255)->null() |
||
| 243 | ->column('country_id')->references('country')->null(); |
||
| 244 | |||
| 245 | $db->table('animal') |
||
| 246 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 247 | ->column('name')->string(45)->index() |
||
| 248 | ->column('UPPERCASE_COLUMN')->string(45)->null() |
||
| 249 | ->column('order')->integer()->null(); |
||
| 250 | |||
| 251 | $db->table('dog') |
||
| 252 | ->extends('animal') |
||
| 253 | ->column('race')->string(45)->null(); |
||
| 254 | |||
| 255 | $db->table('cat') |
||
| 256 | ->extends('animal') |
||
| 257 | ->column('cuteness_level')->integer()->null(); |
||
| 258 | |||
| 259 | $db->table('panda') |
||
| 260 | ->extends('animal') |
||
| 261 | ->column('weight')->float()->null(); |
||
| 262 | |||
| 263 | $db->table('boats') |
||
| 264 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 265 | ->column('name')->string(255) |
||
| 266 | ->column('anchorage_country')->references('country')->notNull()->then() |
||
| 267 | ->column('current_country')->references('country')->null()->then() |
||
| 268 | ->column('length')->decimal(10, 2)->null()->then() |
||
| 269 | ->unique(['anchorage_country', 'name']); |
||
| 270 | |||
| 271 | $db->table('sailed_countries') |
||
| 272 | ->column('boat_id')->references('boats') |
||
| 273 | ->column('country_id')->references('country') |
||
| 274 | ->then()->primaryKey(['boat_id', 'country_id']); |
||
| 275 | |||
| 276 | $db->table('category') |
||
| 277 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 278 | ->column('label')->string(255) |
||
| 279 | ->column('parent_id')->references('category')->null(); |
||
| 280 | |||
| 281 | $db->table('article') |
||
| 282 | ->column('id')->string(36)->primaryKey()->comment('@UUID') |
||
| 283 | ->column('content')->string(255) |
||
| 284 | ->column('author_id')->references('users')->null() |
||
| 285 | ->column('attachment')->blob()->null(); |
||
| 286 | |||
| 287 | $db->table('article2') |
||
| 288 | ->column('id')->string(36)->primaryKey()->comment('@UUID v4') |
||
| 289 | ->column('content')->string(255); |
||
| 290 | |||
| 291 | $db->table('files') |
||
| 292 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 293 | ->column('file')->blob() |
||
| 294 | ->column('md5')->string()->null()->comment("@ProtectedGetter\n@ProtectedSetter") |
||
| 295 | ->column('article_id')->references('article')->null()->comment("@ProtectedGetter\n@ProtectedSetter\n@ProtectedOneToMany"); |
||
| 296 | |||
| 297 | $toSchema->getTable('users') |
||
| 298 | ->addUniqueIndex([$connection->quoteIdentifier('login')], 'users_login_idx') |
||
| 299 | ->addIndex([$connection->quoteIdentifier('status'), $connection->quoteIdentifier('country_id')], 'users_status_country_idx'); |
||
| 300 | |||
| 301 | // We create the same index twice |
||
| 302 | // except for Oracle that won't let us create twice the same index. |
||
| 303 | if (!$connection->getDatabasePlatform() instanceof OraclePlatform) { |
||
| 304 | $toSchema->getTable('users') |
||
| 305 | ->addUniqueIndex([$connection->quoteIdentifier('login')], 'users_login_idx_2'); |
||
| 306 | } |
||
| 307 | |||
| 308 | // A table with a foreign key that references a non primary key. |
||
| 309 | $db->table('ref_no_prim_key') |
||
| 310 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 311 | ->column('from')->string(50) |
||
| 312 | ->column('to')->string(50)->unique(); |
||
| 313 | |||
| 314 | $toSchema->getTable($connection->quoteIdentifier('ref_no_prim_key'))->addForeignKeyConstraint($connection->quoteIdentifier('ref_no_prim_key'), [$connection->quoteIdentifier('from')], [$connection->quoteIdentifier('to')]); |
||
| 315 | |||
| 316 | // A table with multiple primary keys. |
||
| 317 | $db->table('states') |
||
| 318 | ->column('country_id')->references('country') |
||
| 319 | ->column('code')->string(3) |
||
| 320 | ->column('name')->string(50)->then() |
||
| 321 | ->primaryKey(['country_id', 'code']); |
||
| 322 | |||
| 323 | // Tables using @Json annotations |
||
| 324 | $db->table('accounts') |
||
| 325 | ->column('id')->integer()->primaryKey()->autoIncrement()->notNull() |
||
| 326 | ->column('name')->string(); |
||
| 327 | |||
| 328 | $db->table('nodes') |
||
| 329 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment("@JsonIgnore\n@Autoincrement") |
||
| 330 | ->column('alias_id')->references('nodes')->null()->comment('@JsonRecursive') |
||
| 331 | ->column('parent_id')->references('nodes')->null()->comment('@JsonInclude') |
||
| 332 | ->column('root_id')->references('nodes')->null()->comment('@JsonIgnore') |
||
| 333 | ->column('owner_id')->references('accounts')->null()->comment('@JsonFormat(property="name") @JsonInclude') |
||
| 334 | ->column('name')->string()->comment('@JsonKey("basename")') |
||
| 335 | ->column('size')->integer()->notNull()->default(0)->comment('@JsonFormat(unit=" o")') |
||
| 336 | ->column('weight')->float()->null()->comment('@JsonFormat(decimals=2,unit="g")') |
||
| 337 | ->column('created_at')->date()->null()->comment('@JsonFormat("Y-m-d")'); |
||
| 338 | |||
| 339 | $db->table('nodes_guests') |
||
| 340 | ->column('node_id')->references('nodes')->comment('@JsonIgnore') |
||
| 341 | ->column('guest_id')->references('accounts')->comment('@JsonKey("guests") @JsonFormat(method="getName")'); |
||
| 342 | |||
| 343 | $db->table('node_entries') |
||
| 344 | ->column('id')->integer()->primaryKey()->autoIncrement() |
||
| 345 | ->column('node_id')->references('nodes')->comment('@JsonCollection("entries") @JsonFormat(property="entry")') |
||
| 346 | ->column('entry')->string()->null(); |
||
| 347 | |||
| 348 | $db->table('artists') |
||
| 349 | ->column('id')->integer()->primaryKey()->autoIncrement() |
||
| 350 | ->column('children')->array()->null() //used to test conflicts with autopivot |
||
| 351 | ->column('name')->string(); |
||
| 352 | |||
| 353 | $db->table('albums') |
||
| 354 | ->column('id')->integer()->primaryKey()->autoIncrement() |
||
| 355 | ->column('artist_id')->references('artists')->comment('@JsonCollection(key="discography")') |
||
| 356 | ->column('account_id')->references('accounts') |
||
| 357 | ->column('node_id')->references('nodes')->null() |
||
| 358 | ->column('title')->string() |
||
| 359 | ->then()->unique(['artist_id', 'account_id'])->unique(['artist_id', 'node_id']); |
||
| 360 | |||
| 361 | $db->table('tracks') |
||
| 362 | ->column('id')->integer()->primaryKey()->autoIncrement() |
||
| 363 | ->column('album_id')->references('albums')->comment('@JsonCollection @JsonRecursive') |
||
| 364 | ->column('title')->string() |
||
| 365 | ->column('duration')->time()->comment('@JsonFormat("H:i:s")'); |
||
| 366 | |||
| 367 | $db->table('featuring') |
||
| 368 | ->column('id')->integer()->primaryKey()->autoIncrement() |
||
| 369 | ->column('track_id')->references('tracks') |
||
| 370 | ->column('artist_id')->references('artists')->comment('@JsonKey("feat") @JsonInclude'); |
||
| 371 | |||
| 372 | $db->table('artists_relations') //used to test the auto pivot case |
||
| 373 | ->column('id')->integer()->primaryKey()->autoIncrement() |
||
| 374 | ->column('parent_id')->references('artists') |
||
| 375 | ->column('child_id')->references('artists'); |
||
| 376 | |||
| 377 | $db->table('children') //used to test conflicts with autopivot |
||
| 378 | ->column('id')->integer()->primaryKey()->autoIncrement() |
||
| 379 | ->column('artist_id')->references('artists'); |
||
| 380 | |||
| 381 | $db->junctionTable('person', 'boats'); |
||
| 382 | |||
| 383 | if (!$connection->getDatabasePlatform() instanceof OraclePlatform) { |
||
| 384 | $db->table('base_objects') |
||
| 385 | ->column('id')->integer()->primaryKey()->autoIncrement() |
||
| 386 | ->column('label')->string(); |
||
| 387 | $db->table('inherited_objects') |
||
| 388 | ->column('id')->integer()->primaryKey()->autoIncrement() |
||
| 389 | ->column('base_object_id')->references('base_objects')->unique()->comment('@JsonCollection'); |
||
| 390 | } |
||
| 391 | |||
| 392 | $db->table('composite_fk_target_reference') |
||
| 393 | ->column('id')->integer()->primaryKey()->autoIncrement() |
||
| 394 | ->column('label')->string(); |
||
| 395 | |||
| 396 | $targetTable = $db->table('composite_fk_target') |
||
| 397 | ->column('id_1')->references('composite_fk_target_reference') |
||
| 398 | ->column('id_2')->integer() |
||
| 399 | ->then()->primaryKey(['id_1', 'id_2']); |
||
| 400 | $db->table('composite_fk_source') |
||
| 401 | ->column('id')->integer()->primaryKey()->autoIncrement() |
||
| 402 | ->column('fk_1')->integer() |
||
| 403 | ->column('fk_2')->integer() |
||
| 404 | ->then()->getDbalTable()->addForeignKeyConstraint($targetTable->getDbalTable(), [$connection->quoteIdentifier('fk_1'), $connection->quoteIdentifier('fk_2')], [$connection->quoteIdentifier('id_1'), $connection->quoteIdentifier('id_2')]); |
||
| 405 | |||
| 406 | // Test case, the problem here is: |
||
| 407 | // - `inheritance_agency` have an FK to `inheritance_society.**id_entity**` |
||
| 408 | // - `inheritance_society` have an FK to `inheritance_entity.**id**` |
||
| 409 | $db->table('inheritance_entity') |
||
| 410 | ->column('id')->integer()->primaryKey()->autoIncrement(); |
||
| 411 | $db->table('inheritance_society') |
||
| 412 | ->column('id_entity')->references('inheritance_entity')->primaryKey() |
||
| 413 | ->then(); |
||
| 414 | $db->table('inheritance_agency') |
||
| 415 | ->column('id')->integer()->primaryKey()->autoIncrement() |
||
| 416 | ->column('id_parent_society')->references('inheritance_society'); |
||
| 417 | |||
| 418 | // Uppercase table and column names: |
||
| 419 | if (!$connection->getDatabasePlatform() instanceof OraclePlatform) { |
||
| 420 | $db->table('UPPERCASE_A') |
||
| 421 | ->column('id')->integer()->primaryKey()->autoIncrement(); |
||
| 422 | |||
| 423 | $db->table('UPPERCASE_B') |
||
| 424 | ->column('id')->integer()->primaryKey()->autoIncrement() |
||
| 425 | ->column('UNIQUE_UPPERCASE_A_ID')->references('UPPERCASE_A')->unique(); |
||
| 426 | |||
| 427 | $db->table('UPPERCASE_A') |
||
| 428 | ->column('UPPERCASE_B_ID')->references('UPPERCASE_B'); |
||
| 429 | } |
||
| 430 | |||
| 431 | // Reserved keywords from https://dev.mysql.com/doc/refman/8.0/en/keywords.html |
||
| 432 | // and https://docs.oracle.com/cd/B19306_01/em.102/b40103/app_oracle_reserved_words.htm |
||
| 433 | $db->table('values') |
||
| 434 | ->column('key')->integer()->primaryKey()->autoIncrement(); |
||
| 435 | |||
| 436 | $db->table('accessible') |
||
| 437 | ->column('add')->integer()->primaryKey()->autoIncrement(); |
||
| 438 | |||
| 439 | $db->table('all') |
||
| 440 | ->column('analyze')->references('values') |
||
| 441 | ->column('and')->references('accessible'); |
||
| 442 | |||
| 443 | $sqlStmts = $toSchema->getMigrateFromSql($fromSchema, $connection->getDatabasePlatform()); |
||
| 444 | |||
| 445 | foreach ($sqlStmts as $sqlStmt) { |
||
| 446 | //echo $sqlStmt."\n"; |
||
| 447 | $connection->exec($sqlStmt); |
||
| 448 | } |
||
| 449 | |||
| 450 | // Let's generate computed columns |
||
| 451 | if ($connection->getDatabasePlatform() instanceof MySqlPlatform && !self::isMariaDb($connection)) { |
||
| 452 | $connection->exec('CREATE TABLE `players` ( |
||
| 453 | `id` INT UNSIGNED AUTO_INCREMENT NOT NULL, |
||
| 454 | `player_and_games` JSON NOT NULL, |
||
| 455 | `names_virtual` VARCHAR(20) GENERATED ALWAYS AS (`player_and_games` ->> \'$.name\') NOT NULL COMMENT \'@ReadOnly\', |
||
| 456 | `animal_id` INT COMMENT \'@ReadOnly\', |
||
| 457 | PRIMARY KEY (`id`), |
||
| 458 | FOREIGN KEY (animal_id) REFERENCES animal(id) |
||
| 459 | ); |
||
| 460 | '); |
||
| 461 | } |
||
| 462 | |||
| 463 | self::insert($connection, 'country', [ |
||
| 464 | 'label' => 'France', |
||
| 465 | ]); |
||
| 466 | self::insert($connection, 'country', [ |
||
| 467 | 'label' => 'uk', |
||
| 468 | ]); |
||
| 469 | self::insert($connection, 'country', [ |
||
| 470 | 'label' => 'Jamaica', |
||
| 471 | ]); |
||
| 472 | |||
| 473 | self::insert($connection, 'person', [ |
||
| 474 | 'name' => 'John Smith', |
||
| 475 | 'created_at' => '2015-10-24 11:57:13', |
||
| 476 | ]); |
||
| 477 | self::insert($connection, 'person', [ |
||
| 478 | 'name' => 'Jean Dupont', |
||
| 479 | 'created_at' => '2015-10-24 11:57:13', |
||
| 480 | ]); |
||
| 481 | self::insert($connection, 'person', [ |
||
| 482 | 'name' => 'Robert Marley', |
||
| 483 | 'created_at' => '2015-10-24 11:57:13', |
||
| 484 | ]); |
||
| 485 | self::insert($connection, 'person', [ |
||
| 486 | 'name' => 'Bill Shakespeare', |
||
| 487 | 'created_at' => '2015-10-24 11:57:13', |
||
| 488 | ]); |
||
| 489 | |||
| 490 | self::insert($connection, 'contact', [ |
||
| 491 | 'id' => 1, |
||
| 492 | 'email' => '[email protected]', |
||
| 493 | 'manager_id' => null, |
||
| 494 | ]); |
||
| 495 | self::insert($connection, 'contact', [ |
||
| 496 | 'id' => 2, |
||
| 497 | 'email' => '[email protected]', |
||
| 498 | 'manager_id' => null, |
||
| 499 | ]); |
||
| 500 | self::insert($connection, 'contact', [ |
||
| 501 | 'id' => 3, |
||
| 502 | 'email' => '[email protected]', |
||
| 503 | 'manager_id' => null, |
||
| 504 | ]); |
||
| 505 | self::insert($connection, 'contact', [ |
||
| 506 | 'id' => 4, |
||
| 507 | 'email' => '[email protected]', |
||
| 508 | 'manager_id' => 1, |
||
| 509 | ]); |
||
| 510 | |||
| 511 | self::insert($connection, 'rights', [ |
||
| 512 | 'label' => 'CAN_SING', |
||
| 513 | ]); |
||
| 514 | self::insert($connection, 'rights', [ |
||
| 515 | 'label' => 'CAN_WRITE', |
||
| 516 | ]); |
||
| 517 | |||
| 518 | self::insert($connection, 'roles', [ |
||
| 519 | 'name' => 'Admins', |
||
| 520 | 'created_at' => '2015-10-24' |
||
| 521 | ]); |
||
| 522 | self::insert($connection, 'roles', [ |
||
| 523 | 'name' => 'Writers', |
||
| 524 | 'created_at' => '2015-10-24' |
||
| 525 | ]); |
||
| 526 | self::insert($connection, 'roles', [ |
||
| 527 | 'name' => 'Singers', |
||
| 528 | 'created_at' => '2015-10-24' |
||
| 529 | ]); |
||
| 530 | |||
| 531 | self::insert($connection, 'roles_rights', [ |
||
| 532 | 'role_id' => 1, |
||
| 533 | 'right_label' => 'CAN_SING' |
||
| 534 | ]); |
||
| 535 | self::insert($connection, 'roles_rights', [ |
||
| 536 | 'role_id' => 3, |
||
| 537 | 'right_label' => 'CAN_SING' |
||
| 538 | ]); |
||
| 539 | self::insert($connection, 'roles_rights', [ |
||
| 540 | 'role_id' => 1, |
||
| 541 | 'right_label' => 'CAN_WRITE' |
||
| 542 | ]); |
||
| 543 | self::insert($connection, 'roles_rights', [ |
||
| 544 | 'role_id' => 2, |
||
| 545 | 'right_label' => 'CAN_WRITE' |
||
| 546 | ]); |
||
| 547 | |||
| 548 | self::insert($connection, 'users', [ |
||
| 549 | 'id' => 1, |
||
| 550 | 'login' => 'john.smith', |
||
| 551 | 'password' => null, |
||
| 552 | 'status' => 'on', |
||
| 553 | 'country_id' => 2 |
||
| 554 | ]); |
||
| 555 | self::insert($connection, 'users', [ |
||
| 556 | 'id' => 2, |
||
| 557 | 'login' => 'jean.dupont', |
||
| 558 | 'password' => null, |
||
| 559 | 'status' => 'on', |
||
| 560 | 'country_id' => 1 |
||
| 561 | ]); |
||
| 562 | self::insert($connection, 'users', [ |
||
| 563 | 'id' => 3, |
||
| 564 | 'login' => 'robert.marley', |
||
| 565 | 'password' => null, |
||
| 566 | 'status' => 'off', |
||
| 567 | 'country_id' => 3 |
||
| 568 | ]); |
||
| 569 | self::insert($connection, 'users', [ |
||
| 570 | 'id' => 4, |
||
| 571 | 'login' => 'bill.shakespeare', |
||
| 572 | 'password' => null, |
||
| 573 | 'status' => 'off', |
||
| 574 | 'country_id' => 2 |
||
| 575 | ]); |
||
| 576 | |||
| 577 | self::insert($connection, 'users_roles', [ |
||
| 578 | 'user_id' => 1, |
||
| 579 | 'role_id' => 1, |
||
| 580 | ]); |
||
| 581 | self::insert($connection, 'users_roles', [ |
||
| 582 | 'user_id' => 2, |
||
| 583 | 'role_id' => 1, |
||
| 584 | ]); |
||
| 585 | self::insert($connection, 'users_roles', [ |
||
| 586 | 'user_id' => 3, |
||
| 587 | 'role_id' => 3, |
||
| 588 | ]); |
||
| 589 | self::insert($connection, 'users_roles', [ |
||
| 590 | 'user_id' => 4, |
||
| 591 | 'role_id' => 2, |
||
| 592 | ]); |
||
| 593 | self::insert($connection, 'users_roles', [ |
||
| 594 | 'user_id' => 3, |
||
| 595 | 'role_id' => 2, |
||
| 596 | ]); |
||
| 597 | |||
| 598 | self::insert($connection, 'ref_no_prim_key', [ |
||
| 599 | 'from' => 'foo', |
||
| 600 | 'to' => 'foo', |
||
| 601 | ]); |
||
| 602 | |||
| 603 | self::insert($connection, 'accounts', [ |
||
| 604 | 'id' => 1, |
||
| 605 | 'name' => 'root' |
||
| 606 | ]); |
||
| 607 | self::insert($connection, 'accounts', [ |
||
| 608 | 'id' => 2, |
||
| 609 | 'name' => 'user' |
||
| 610 | ]); |
||
| 611 | self::insert($connection, 'accounts', [ |
||
| 612 | 'id' => 3, |
||
| 613 | 'name' => 'www' |
||
| 614 | ]); |
||
| 615 | self::insert($connection, 'nodes', [ |
||
| 616 | 'id' => 1, |
||
| 617 | 'owner_id' => 1, |
||
| 618 | 'name' => '/', |
||
| 619 | 'created_at' => (new DateTime('last year'))->format('Y-m-d 00:00:00'), |
||
| 620 | ]); |
||
| 621 | self::insert($connection, 'nodes', [ |
||
| 622 | 'id' => 2, |
||
| 623 | 'name' => 'private', |
||
| 624 | 'created_at' => (new DateTime('last year'))->format('Y-m-d 00:00:00'), |
||
| 625 | 'parent_id' => 1, |
||
| 626 | ]); |
||
| 627 | self::insert($connection, 'nodes', [ |
||
| 628 | 'id' => 3, |
||
| 629 | 'name' => 'var', |
||
| 630 | 'created_at' => (new DateTime('last year'))->format('Y-m-d 00:00:00'), |
||
| 631 | 'parent_id' => 2, |
||
| 632 | ]); |
||
| 633 | self::insert($connection, 'nodes', [ |
||
| 634 | 'id' => 4, |
||
| 635 | 'name' => 'var', |
||
| 636 | 'created_at' => (new DateTime('last year'))->format('Y-m-d 00:00:00'), |
||
| 637 | 'parent_id' => 1, |
||
| 638 | 'alias_id' => 3 |
||
| 639 | ]); |
||
| 640 | self::insert($connection, 'nodes', [ |
||
| 641 | 'id' => 5, |
||
| 642 | 'name' => 'www', |
||
| 643 | 'created_at' => (new DateTime('last week'))->format('Y-m-d 00:00:00'), |
||
| 644 | 'parent_id' => 4 |
||
| 645 | ]); |
||
| 646 | self::insert($connection, 'nodes', [ |
||
| 647 | 'id' => 6, |
||
| 648 | 'owner_id' => 2, |
||
| 649 | 'name' => 'index.html', |
||
| 650 | 'created_at' => (new DateTime('now'))->format('Y-m-d 00:00:00'), |
||
| 651 | 'size' => 512, |
||
| 652 | 'weight' => 42.5, |
||
| 653 | 'parent_id' => 5 |
||
| 654 | ]); |
||
| 655 | self::insert($connection, 'nodes', [ |
||
| 656 | 'id' => 7, |
||
| 657 | 'name' => 'index.html', |
||
| 658 | 'created_at' => (new DateTime('now'))->format('Y-m-d 00:00:00'), |
||
| 659 | 'alias_id' => 6, |
||
| 660 | 'parent_id' => 1 |
||
| 661 | ]); |
||
| 662 | self::insert($connection, 'nodes', [ |
||
| 663 | 'id' => 8, |
||
| 664 | 'name' => 'index.htm', |
||
| 665 | 'created_at' => (new DateTime('now'))->format('Y-m-d 00:00:00'), |
||
| 666 | 'alias_id' => 7, |
||
| 667 | 'parent_id' => 1 |
||
| 668 | ]); |
||
| 669 | self::insert($connection, 'nodes_guests', [ |
||
| 670 | 'node_id' => 6, |
||
| 671 | 'guest_id' => 1 |
||
| 672 | ]); |
||
| 673 | self::insert($connection, 'nodes_guests', [ |
||
| 674 | 'node_id' => 6, |
||
| 675 | 'guest_id' => 3 |
||
| 676 | ]); |
||
| 677 | self::insert($connection, 'node_entries', [ |
||
| 678 | 'node_id' => 6, |
||
| 679 | 'entry' => '<h1>' |
||
| 680 | ]); |
||
| 681 | self::insert($connection, 'node_entries', [ |
||
| 682 | 'node_id' => 6, |
||
| 683 | 'entry' => 'Hello, World' |
||
| 684 | ]); |
||
| 685 | self::insert($connection, 'node_entries', [ |
||
| 686 | 'node_id' => 6, |
||
| 687 | 'entry' => '</h1>' |
||
| 688 | ]); |
||
| 689 | |||
| 690 | |||
| 691 | self::insert($connection, 'artists', [ |
||
| 692 | 'id' => 1, |
||
| 693 | 'name' => 'Pink Floyd' |
||
| 694 | ]); |
||
| 695 | self::insert($connection, 'artists', [ |
||
| 696 | 'id' => 2, |
||
| 697 | 'name' => 'Roger Waters' |
||
| 698 | ]); |
||
| 699 | self::insert($connection, 'artists', [ |
||
| 700 | 'id' => 3, |
||
| 701 | 'name' => 'David Gilmour' |
||
| 702 | ]); |
||
| 703 | self::insert($connection, 'albums', [ |
||
| 704 | 'id' => 1, |
||
| 705 | 'artist_id' => 1, |
||
| 706 | 'account_id' => 1, |
||
| 707 | 'title' => 'Animals' |
||
| 708 | ]); |
||
| 709 | self::insert($connection, 'artists_relations', [ |
||
| 710 | 'parent_id' => 1, |
||
| 711 | 'child_id' => 2 |
||
| 712 | ]); |
||
| 713 | |||
| 714 | $timeType = Type::getType(Types::TIME_IMMUTABLE); |
||
| 715 | |||
| 716 | self::insert($connection, 'tracks', [ |
||
| 717 | 'album_id' => 1, |
||
| 718 | 'title' => 'Pigs on the Wing 1', |
||
| 719 | // Note: Oracle does not have a TIME column type |
||
| 720 | 'duration' => $timeType->convertToDatabaseValue(new DateTimeImmutable('1970-01-01 00:01:25'), $connection->getDatabasePlatform()), |
||
| 721 | ]); |
||
| 722 | self::insert($connection, 'tracks', [ |
||
| 723 | 'album_id' => 1, |
||
| 724 | 'title' => 'Dogs', |
||
| 725 | 'duration' => $timeType->convertToDatabaseValue(new DateTimeImmutable('1970-01-01 00:17:04'), $connection->getDatabasePlatform()), |
||
| 726 | ]); |
||
| 727 | self::insert($connection, 'tracks', [ |
||
| 728 | 'album_id' => 1, |
||
| 729 | 'title' => 'Pigs (Three Different Ones)', |
||
| 730 | 'duration' => $timeType->convertToDatabaseValue(new DateTimeImmutable('1970-01-01 00:11:22'), $connection->getDatabasePlatform()), |
||
| 731 | ]); |
||
| 732 | self::insert($connection, 'tracks', [ |
||
| 733 | 'album_id' => 1, |
||
| 734 | 'title' => 'Sheep', |
||
| 735 | 'duration' => $timeType->convertToDatabaseValue(new DateTimeImmutable('1970-01-01 00:10:24'), $connection->getDatabasePlatform()), |
||
| 736 | ]); |
||
| 737 | self::insert($connection, 'tracks', [ |
||
| 738 | 'album_id' => 1, |
||
| 739 | 'title' => 'Pigs on the Wing 2', |
||
| 740 | 'duration' => $timeType->convertToDatabaseValue(new DateTimeImmutable('1970-01-01 00:01:26'), $connection->getDatabasePlatform()), |
||
| 741 | ]); |
||
| 742 | self::insert($connection, 'featuring', [ |
||
| 743 | 'track_id' => 1, |
||
| 744 | 'artist_id' => 2 |
||
| 745 | ]); |
||
| 746 | self::insert($connection, 'featuring', [ |
||
| 747 | 'track_id' => 2, |
||
| 748 | 'artist_id' => 3 |
||
| 749 | ]); |
||
| 750 | self::insert($connection, 'featuring', [ |
||
| 751 | 'track_id' => 2, |
||
| 752 | 'artist_id' => 2 |
||
| 753 | ]); |
||
| 754 | self::insert($connection, 'featuring', [ |
||
| 755 | 'track_id' => 3, |
||
| 756 | 'artist_id' => 2 |
||
| 757 | ]); |
||
| 758 | self::insert($connection, 'featuring', [ |
||
| 759 | 'track_id' => 4, |
||
| 760 | 'artist_id' => 2 |
||
| 761 | ]); |
||
| 762 | self::insert($connection, 'featuring', [ |
||
| 763 | 'track_id' => 5, |
||
| 764 | 'artist_id' => 2 |
||
| 765 | ]); |
||
| 766 | |||
| 767 | self::insert($connection, 'boats', [ |
||
| 768 | 'name' => 'RoseBud', |
||
| 769 | 'anchorage_country' => 1, |
||
| 770 | 'current_country' => 1, |
||
| 771 | 'length' => '13.5', |
||
| 772 | ]); |
||
| 773 | |||
| 774 | self::insert($connection, 'person_boats', [ |
||
| 775 | 'person_id' => 1, |
||
| 776 | 'boat_id' => 1, |
||
| 777 | ]); |
||
| 778 | self::insert($connection, 'composite_fk_target_reference', [ |
||
| 779 | 'id' => 1, |
||
| 780 | 'label' => 'test' |
||
| 781 | ]); |
||
| 782 | self::insert($connection, 'composite_fk_target', [ |
||
| 783 | 'id_1' => 1, |
||
| 784 | 'id_2' => 1 |
||
| 785 | ]); |
||
| 786 | self::insert($connection, 'composite_fk_source', [ |
||
| 787 | 'id' => 1, |
||
| 788 | 'fk_1' => 1, |
||
| 789 | 'fk_2' => 1 |
||
| 790 | ]); |
||
| 791 | |||
| 792 | self::insert($connection, 'values', ['key' => 1]); |
||
| 793 | self::insert($connection, 'accessible', ['add' => 1]); |
||
| 794 | self::insert($connection, 'all', ['analyze' => 1, 'and' => 1]); |
||
| 795 | } |
||
| 796 | |||
| 797 | private static function quoteKeys(Connection $connection, array $data): array |
||
| 798 | { |
||
| 799 | $quotedData = []; |
||
| 800 | foreach ($data as $id => $value) { |
||
| 801 | $quotedData[$connection->quoteIdentifier($id)] = $value; |
||
| 802 | } |
||
| 803 | return $quotedData; |
||
| 804 | } |
||
| 805 | |||
| 806 | public static function insert(Connection $connection, string $tableName, array $data): void |
||
| 807 | { |
||
| 808 | $connection->insert($connection->quoteIdentifier($tableName), self::quoteKeys($connection, $data)); |
||
| 809 | } |
||
| 810 | |||
| 811 | public static function update(Connection $connection, string $tableName, array $data, array $criteria): void |
||
| 812 | { |
||
| 813 | $connection->update($connection->quoteIdentifier($tableName), self::quoteKeys($connection, $data), self::quoteKeys($connection, $criteria)); |
||
| 814 | } |
||
| 815 | |||
| 816 | protected static function delete(Connection $connection, string $tableName, array $criteria): void |
||
| 817 | { |
||
| 818 | $connection->delete($connection->quoteIdentifier($tableName), self::quoteKeys($connection, $criteria)); |
||
| 819 | } |
||
| 820 | |||
| 821 | protected static function isMariaDb(Connection $connection): bool |
||
| 822 | { |
||
| 823 | if (!$connection->getDatabasePlatform() instanceof MySqlPlatform) { |
||
| 824 | return false; |
||
| 825 | } |
||
| 826 | $version = $connection->fetchOne('SELECT VERSION()'); |
||
| 827 | return stripos($version, 'maria') !== false; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 828 | } |
||
| 829 | } |
||
| 830 |