@@ -20,6 +20,9 @@ class Schema extends \yii\db\pgsql\Schema
20
20
const TYPE_BIT = 'bit ' ;
21
21
const TYPE_COMPOSITE = 'composite ' ;
22
22
23
+ const DATE_TYPES = [self ::TYPE_TIMESTAMP , self ::TYPE_DATETIME , self ::TYPE_DATE , self ::TYPE_TIME ];
24
+ const CURRENT_TIME_DEFAULTS = ['now() ' , 'CURRENT_TIMESTAMP ' , 'CURRENT_DATE ' , 'CURRENT_TIME ' ];
25
+
23
26
/**
24
27
* @var array mapping from composite column types (keys) to PHP types (classes in configuration style).
25
28
* `array` by default, `object` also available as PHP type then a result will be converted to \stdClass.
@@ -62,6 +65,12 @@ protected function findColumns($table)
62
65
{
63
66
$ tableName = $ this ->db ->quoteValue ($ table ->name );
64
67
$ schemaName = $ this ->db ->quoteValue ($ table ->schemaName );
68
+
69
+ $ orIdentity = '' ;
70
+ if (version_compare ($ this ->db ->serverVersion , '12.0 ' , '>= ' )) {
71
+ $ orIdentity = 'OR a.attidentity != \'\'' ;
72
+ }
73
+
65
74
$ sql = <<<SQL
66
75
SELECT
67
76
d.nspname AS table_schema,
@@ -74,7 +83,8 @@ protected function findColumns($table)
74
83
COALESCE(NULLIF(a.atttypmod, -1), t.typtypmod) AS modifier,
75
84
NOT (a.attnotnull OR t.typnotnull) AS is_nullable,
76
85
COALESCE(t.typdefault, pg_get_expr(ad.adbin, ad.adrelid)::varchar) AS column_default,
77
- COALESCE(pg_get_expr(ad.adbin, ad.adrelid) ~ 'nextval', false) AS is_autoinc,
86
+ COALESCE(pg_get_expr(ad.adbin, ad.adrelid) ~ 'nextval', false) {$ orIdentity } AS is_autoinc,
87
+ pg_get_serial_sequence(quote_ident(d.nspname) || '.' || quote_ident(c.relname), a.attname) AS sequence_name,
78
88
CASE WHEN COALESCE(td.typtype, tb.typtype, t.typtype) = 'e'::char
79
89
THEN array_to_string((SELECT array_agg(enumlabel) FROM pg_enum WHERE enumtypid = COALESCE(td.oid, tb.oid, a.atttypid))::varchar[], ',')
80
90
ELSE NULL
@@ -95,7 +105,7 @@ protected function findColumns($table)
95
105
LEFT JOIN pg_namespace d ON d.oid = c.relnamespace
96
106
LEFT JOIN pg_constraint ct ON ct.conrelid = c.oid AND ct.contype = 'p'
97
107
WHERE
98
- a.attnum > 0 AND t.typname != ''
108
+ a.attnum > 0 AND t.typname != '' AND NOT a.attisdropped
99
109
AND c.relname = {$ tableName }
100
110
AND d.nspname = {$ schemaName }
101
111
ORDER BY
@@ -114,16 +124,14 @@ protected function findColumns($table)
114
124
$ table ->columns [$ column ->name ] = $ column ;
115
125
if ($ column ->isPrimaryKey ) {
116
126
$ table ->primaryKey [] = $ column ->name ;
117
- if ($ table ->sequenceName === null && preg_match ( " /nextval \\ (' \" ? \\ w+ \" ?\.? \" ? \\ w+ \" ?'(::regclass)? \\ )/ " , $ column -> defaultValue ) === 1 ) {
118
- $ table ->sequenceName = preg_replace ([ ' /nextval/ ' , ' /::/ ' , ' /regclass/ ' , ' / \' \)/ ' , ' /\( \' / ' ], '' , $ column ->defaultValue ) ;
127
+ if ($ table ->sequenceName === null ) {
128
+ $ table ->sequenceName = $ column ->sequenceName ;
119
129
}
120
130
$ column ->defaultValue = null ;
121
131
} elseif ($ column ->defaultValue ) {
122
- if (in_array ($ column ->type , [static ::TYPE_TIMESTAMP , static ::TYPE_DATETIME , static ::TYPE_DATE , static ::TYPE_TIME ]) && $ column ->defaultValue === 'now() ' ) {
123
- $ column ->defaultValue = new \DateTime ;
124
- } elseif ($ column ->type === static ::TYPE_BIT && !$ column ->dimension ) {
125
- $ column ->defaultValue = $ column ->phpTypecast (trim ($ column ->defaultValue , 'B \'' ));
126
- } elseif (preg_match ("/^'(.*?)'::/ " , $ column ->defaultValue , $ matches )) {
132
+ if (in_array ($ column ->type , static ::DATE_TYPES ) && in_array ($ column ->defaultValue , static ::CURRENT_TIME_DEFAULTS )) {
133
+ $ column ->defaultValue = new \DateTime ();
134
+ } elseif (preg_match ("/^B?'(.*?)'::/ " , $ column ->defaultValue , $ matches )) {
127
135
$ column ->defaultValue = $ column ->phpTypecast ($ matches [1 ]);
128
136
} elseif (preg_match ('/^(\()?(.*?)(?(1)\))(?:::.+)?$/ ' , $ column ->defaultValue , $ matches )) {
129
137
if ($ matches [2 ] === 'NULL ' ) {
0 commit comments