MediaWiki  master
DoctrineSchemaBuilderFactory.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Wikimedia\Rdbms;
4 
5 use Doctrine\DBAL\Platforms\AbstractPlatform;
6 use Doctrine\DBAL\Platforms\SqlitePlatform;
7 use Doctrine\DBAL\Types\Type;
8 use InvalidArgumentException;
9 
15 
20  public function getSchemaBuilder( string $platform ) {
21  return new DoctrineSchemaBuilder( $this->getPlatform( $platform ) );
22  }
23 
28  public function getSchemaChangeBuilder( $platform ) {
29  return new DoctrineSchemaChangeBuilder( $this->getPlatform( $platform ) );
30  }
31 
36  private function getPlatform( string $platform ) {
37  if ( $platform === 'mysql' ) {
38  // T270740 - HACK: In doctrine/dbal 3.0.0, they renamed MySql -> MySQL
39  // https://github.com/doctrine/dbal/commit/885bf615a5820c56ddee60a8fbd3d7ce973587ed
40  // So this looks for the old case (in dbal < 3.0.0) and uses that, else uses
41  // the new case... When we drop support for dbal < 3.0.0 (which can be done
42  // after we drop PHP 7.2 support in master - T261872; would be fine in MW 1.35),
43  // this can be cleaned up to just use MySQLPlatform and be imported at the top.
44  if ( class_exists( \Doctrine\DBAL\Platforms\MySqlPlatform::class ) ) {
45  $platformObject = new \Doctrine\DBAL\Platforms\MySqlPlatform();
46  } else {
47  $platformObject = new \Doctrine\DBAL\Platforms\MySQLPlatform();
48  }
49  } elseif ( $platform === 'postgres' ) {
50  $platformObject = new MWPostgreSqlPlatform();
51  } elseif ( $platform === 'sqlite' ) {
52  $platformObject = new SqlitePlatform();
53  } else {
54  throw new InvalidArgumentException( 'Unknown platform: ' . $platform );
55  }
56 
57  $customTypes = [
58  'Enum' => [ EnumType::class, EnumType::ENUM ],
59  'Tinyint' => [ TinyIntType::class, TinyIntType::TINYINT ],
60  'Timestamp' => [ TimestampType::class, TimestampType::TIMESTAMP ],
61  ];
62 
63  foreach ( $customTypes as $type => [ $class, $name ] ) {
64  if ( !Type::hasType( $name ) ) {
65  Type::addType( $name, $class );
66  }
67  $platformObject->registerDoctrineTypeMapping( $type, $name );
68  }
69 
70  return $platformObject;
71  }
72 }
Wikimedia\Rdbms\DoctrineSchemaBuilderFactory\getSchemaBuilder
getSchemaBuilder(string $platform)
Definition: DoctrineSchemaBuilderFactory.php:20
Wikimedia\Rdbms\TinyIntType\TINYINT
const TINYINT
Definition: TinyIntType.php:13
Wikimedia\Rdbms\DoctrineSchemaBuilderFactory\getPlatform
getPlatform(string $platform)
Definition: DoctrineSchemaBuilderFactory.php:36
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
Wikimedia\Rdbms\DoctrineSchemaBuilderFactory\getSchemaChangeBuilder
getSchemaChangeBuilder( $platform)
Definition: DoctrineSchemaBuilderFactory.php:28
Wikimedia\Rdbms\MWPostgreSqlPlatform
PhanRedefinedExtendedClass.
Definition: MWPostgreSqlPlatform.php:10
Wikimedia\Rdbms\DoctrineSchemaBuilder
@experimental @unstable
Definition: DoctrineSchemaBuilder.php:12
Wikimedia\Rdbms\TimestampType\TIMESTAMP
const TIMESTAMP
Definition: TimestampType.php:13
Wikimedia\Rdbms\DoctrineSchemaBuilderFactory
@experimental @unstable
Definition: DoctrineSchemaBuilderFactory.php:14
Wikimedia\Rdbms\DoctrineSchemaChangeBuilder
@experimental @unstable
Definition: DoctrineSchemaChangeBuilder.php:13
Wikimedia\Rdbms\EnumType\ENUM
const ENUM
Definition: EnumType.php:18
$type
$type
Definition: testCompression.php:52