@@ -106,13 +106,33 @@ private function sortTablesByConstraintsRecursive(string $schema, array $tables,
106106
107107 private function getRefNameByConstraintName (string $ schema , string $ constraintName ): string
108108 {
109- $ connection = $ this ->getConnection ();
110- $ id = $ schema . '/ ' . $ constraintName ;
111- $ result = $ connection ->selectOne (
112- "SELECT replace(REF_NAME, ?, '') refName FROM information_schema.INNODB_SYS_FOREIGN where id = ? " ,
113- [$ schema . '/ ' , $ id ]
114- );
109+ $ conn = $ this ->getConnection ();
110+ $ version = $ this ->getMysqlVersion ($ conn );
111+
112+ if (version_compare ($ version , '8.0.0 ' , '>= ' )) {
113+ $ r = $ conn ->selectOne ("
114+ SELECT kcu.referenced_table_name AS refName
115+ FROM information_schema.referential_constraints rc
116+ JOIN information_schema.key_column_usage kcu
117+ ON rc.constraint_name = kcu.constraint_name
118+ AND rc.constraint_schema = kcu.constraint_schema
119+ WHERE rc.constraint_schema = ? AND rc.constraint_name = ? LIMIT 1
120+ " , [$ schema , $ constraintName ]);
121+ return $ r ?->refName ?? '' ;
122+ }
123+
124+ $ id = "$ schema/ $ constraintName " ;
125+ $ r = $ conn ->selectOne ("
126+ SELECT REPLACE(REF_NAME, ?, '') AS refName
127+ FROM information_schema.INNODB_SYS_FOREIGN
128+ WHERE id = ?
129+ " , ["$ schema/ " , $ id ]);
115130
116- return $ result ->refName ;
131+ return $ r ?->refName ?? '' ;
132+ }
133+
134+ private function getMysqlVersion (ConnectionInterface $ connection ): string
135+ {
136+ return $ connection ->selectOne ('SELECT VERSION() as version ' )->version ;
117137 }
118138}
0 commit comments