@@ -119,22 +119,16 @@ public function getTableSchema(): ?TableSchema
119119 protected function getTablePrimaryKey (): TablePrimaryKey
120120 {
121121 $ data = [];
122- if (method_exists ($ this ->db ->schema , 'getTablePrimaryKey ' )) {
123- /* @var $constraint \yii\db\Constraint */
124- $ constraint = $ this ->db ->schema ->getTablePrimaryKey ($ this ->tableName , true );
125- if ($ constraint ) {
126- $ data = [
127- 'columns ' => $ constraint ->columnNames ,
128- 'name ' => $ constraint ->name ,
129- ];
130- }
131- } elseif ($ this ->tableSchema instanceof TableSchema) {
132- if ($ this ->tableSchema ->primaryKey ) {
133- $ data = [
134- 'columns ' => $ this ->tableSchema ->primaryKey ,
135- ];
136- }
122+
123+ /* @var $constraint \yii\db\Constraint */
124+ $ constraint = $ this ->db ->schema ->getTablePrimaryKey ($ this ->tableName , true );
125+ if ($ constraint ) {
126+ $ data = [
127+ 'columns ' => $ constraint ->columnNames ,
128+ 'name ' => $ constraint ->name ,
129+ ];
137130 }
131+
138132 return new TablePrimaryKey ($ data );
139133 }
140134
@@ -184,34 +178,20 @@ protected function getTableColumns(array $indexes = []): array
184178 protected function getTableForeignKeys (): array
185179 {
186180 $ data = [];
187- if (method_exists ($ this ->db ->schema , 'getTableForeignKeys ' )) {
188- $ fks = $ this ->db ->schema ->getTableForeignKeys ($ this ->tableName , true );
189- /* @var $fk \yii\db\ForeignKeyConstraint */
190- foreach ($ fks as $ fk ) {
191- $ data [$ fk ->name ] = new TableForeignKey ([
192- 'name ' => $ fk ->name ,
193- 'columns ' => $ fk ->columnNames ,
194- 'refTable ' => $ fk ->foreignTableName ,
195- 'refColumns ' => $ fk ->foreignColumnNames ,
196- 'onDelete ' => $ fk ->onDelete ,
197- 'onUpdate ' => $ fk ->onUpdate ,
198- ]);
199- }
200- } elseif ($ this ->tableSchema instanceof TableSchema) {
201- foreach ($ this ->tableSchema ->foreignKeys as $ name => $ key ) {
202- $ fk = new TableForeignKey ([
203- 'name ' => $ name ,
204- 'refTable ' => ArrayHelper::remove ($ key , 0 ),
205- 'onDelete ' => null ,
206- 'onUpdate ' => null ,
207- ]);
208- foreach ($ key as $ col => $ ref ) {
209- $ fk ->columns [] = $ col ;
210- $ fk ->refColumns [] = $ ref ;
211- }
212- $ data [$ name ] = $ fk ;
213- }
181+
182+ $ fks = $ this ->db ->schema ->getTableForeignKeys ($ this ->tableName , true );
183+ /* @var $fk \yii\db\ForeignKeyConstraint */
184+ foreach ($ fks as $ fk ) {
185+ $ data [$ fk ->name ] = new TableForeignKey ([
186+ 'name ' => $ fk ->name ,
187+ 'columns ' => $ fk ->columnNames ,
188+ 'refTable ' => $ fk ->foreignTableName ,
189+ 'refColumns ' => $ fk ->foreignColumnNames ,
190+ 'onDelete ' => $ fk ->onDelete ,
191+ 'onUpdate ' => $ fk ->onUpdate ,
192+ ]);
214193 }
194+
215195 return $ data ;
216196 }
217197
@@ -223,30 +203,19 @@ protected function getTableForeignKeys(): array
223203 protected function getTableIndexes (): array
224204 {
225205 $ data = [];
226- if (method_exists ($ this ->db ->schema , 'getTableIndexes ' )) {
227- $ idxs = $ this ->db ->schema ->getTableIndexes ($ this ->tableName , true );
228- /* @var $idx \yii\db\IndexConstraint */
229- foreach ($ idxs as $ idx ) {
230- if (!$ idx ->isPrimary ) {
231- $ data [$ idx ->name ] = new TableIndex ([
232- 'name ' => $ idx ->name ,
233- 'unique ' => $ idx ->isUnique ,
234- 'columns ' => $ idx ->columnNames
235- ]);
236- }
206+
207+ $ idxs = $ this ->db ->schema ->getTableIndexes ($ this ->tableName , true );
208+ /* @var $idx \yii\db\IndexConstraint */
209+ foreach ($ idxs as $ idx ) {
210+ if (!$ idx ->isPrimary ) {
211+ $ data [$ idx ->name ] = new TableIndex ([
212+ 'name ' => $ idx ->name ,
213+ 'unique ' => $ idx ->isUnique ,
214+ 'columns ' => $ idx ->columnNames
215+ ]);
237216 }
238- } else {
239- try {
240- $ uidxs = $ this ->db ->schema ->findUniqueIndexes ($ this ->tableSchema );
241- foreach ($ uidxs as $ name => $ cols ) {
242- $ data [$ name ] = new TableIndex ([
243- 'name ' => $ name ,
244- 'unique ' => true ,
245- 'columns ' => $ cols
246- ]);
247- }
248- } catch (NotSupportedException $ exc ) {}
249217 }
218+
250219 return $ data ;
251220 }
252221
0 commit comments