Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
MWPostgreSqlPlatform
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 6
210
0.00% covered (danger)
0.00%
0 / 1
 getDefaultValueDeclarationSQL
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
42
 _getCreateTableSQL
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 getBlobTypeDeclarationSQL
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getBinaryTypeDeclarationSQL
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFloatDeclarationSQL
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDateTimeTzTypeDeclarationSQL
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Wikimedia\Rdbms\DBAL;
4
5use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
6use Wikimedia\Timestamp\ConvertibleTimestamp;
7use Wikimedia\Timestamp\TimestampFormat as TS;
8
9class MWPostgreSqlPlatform extends PostgreSQLPlatform {
10    /**
11     * Handles Postgres unique timestamp format
12     * @inheritDoc
13     *
14     * @param mixed[] $column The column definition array.
15     * @return string Postgres specific SQL code portion needed to set a default value.
16     */
17    public function getDefaultValueDeclarationSQL( $column ): string {
18        $type = $column['type'];
19        $default = $column['default'] ?? null;
20
21        if ( $type instanceof TimestampType && $default ) {
22            if ( isset( $column['allowInfinite'] ) &&
23                $column['allowInfinite'] &&
24                $default === 'infinity'
25            ) {
26                $pgTimestamp = $default;
27            } else {
28                $timestamp = new ConvertibleTimestamp( $default );
29                $pgTimestamp = $timestamp->getTimestamp( TS::POSTGRES );
30            }
31
32            return " DEFAULT '$pgTimestamp";
33        }
34
35        return parent::getDefaultValueDeclarationSQL( $column );
36    }
37
38    /**
39     * @inheritDoc
40     * @phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
41     */
42    protected function _getCreateTableSQL( $name, $columns, array $options = [] ): array {
43        // phpcs:enable
44        $tableSql = parent::_getCreateTableSQL( $name, $columns, $options );
45        foreach ( $columns as $column ) {
46            if ( $column['type'] instanceof EnumType && $column['fixed'] ) {
47                // PostgreSQL does support ENUM datatype but they need to be
48                // created severally with CREATE TYPE command for each column
49                // as it's not possible to feed the values directly in the
50                // column declaration as it could be done in MySQL.
51                $typeSql = $column['type']->makeEnumTypeSql( $column, $this );
52                array_unshift( $tableSql, $typeSql );
53            }
54        }
55
56        return $tableSql;
57    }
58
59    /**
60     * @inheritDoc
61     */
62    public function getBlobTypeDeclarationSQL( array $column ): string {
63        // MySQL goes with varbinary for collation reasons, but postgres can't
64        // properly understand BYTEA type and works just fine with TEXT type
65        // FIXME: This should be fixed at some point (T257755)
66        return 'TEXT';
67    }
68
69    /**
70     * @inheritDoc
71     */
72    public function getBinaryTypeDeclarationSQL( array $column ): string {
73        // MySQL goes with varbinary for collation reasons, but postgres can't
74        // properly understand BYTEA type and works just fine with TEXT type
75        // FIXME: This should be fixed at some point (T257755)
76        return 'TEXT';
77    }
78
79    /**
80     * @inheritDoc
81     */
82    public function getFloatDeclarationSQL( array $column ): string {
83        return 'FLOAT';
84    }
85
86    /**
87     * @inheritDoc
88     */
89    public function getDateTimeTzTypeDeclarationSQL( array $column ): string {
90        return 'TIMESTAMPTZ';
91    }
92}