@@ -2100,24 +2100,92 @@ public function update_sql_30410(): bool|array
21002100 {
21012101 $ errors = [];
21022102
2103- $ sql = [
2104- // Rename the column `enitityA` to `entityA`
2105- 'ALTER TABLE `zp_entity_relationship` CHANGE COLUMN `enitityA` `entityA` INT NULL; ' ,
2103+ // First, check if we need to rename the table from plural to singular
2104+ // Old migration (update_sql_20115) created zp_entity_relationships (plural)
2105+ // but the code now expects zp_entity_relationship (singular)
2106+ try {
2107+ $ pluralTableExists = $ this ->connection ->select (
2108+ "SELECT COUNT(*) as cnt FROM information_schema.tables
2109+ WHERE table_schema = DATABASE()
2110+ AND table_name = 'zp_entity_relationships' "
2111+ );
2112+
2113+ if ($ pluralTableExists [0 ]->cnt > 0 ) {
2114+ // Rename plural table to singular
2115+ $ this ->connection ->statement ('RENAME TABLE `zp_entity_relationships` TO `zp_entity_relationship`; ' );
2116+ }
2117+ } catch (\Exception $ e ) {
2118+ Log::error ('Failed to check/rename zp_entity_relationships table: ' .$ e ->getMessage ());
2119+ }
21062120
2107- // Drop the old index on `enitityA`
2108- 'ALTER TABLE `zp_entity_relationship` DROP INDEX `entityA`; ' ,
2121+ // Check if the singular table exists before trying to alter it
2122+ try {
2123+ $ tableExists = $ this ->connection ->select (
2124+ "SELECT COUNT(*) as cnt FROM information_schema.tables
2125+ WHERE table_schema = DATABASE()
2126+ AND table_name = 'zp_entity_relationship' "
2127+ );
2128+
2129+ if ($ tableExists [0 ]->cnt == 0 ) {
2130+ // Table doesn't exist - create it with correct schema
2131+ $ this ->connection ->statement ('
2132+ CREATE TABLE `zp_entity_relationship` (
2133+ `id` INT NOT NULL AUTO_INCREMENT,
2134+ `entityA` INT NULL,
2135+ `entityAType` VARCHAR(45) NULL,
2136+ `entityB` INT NULL,
2137+ `entityBType` VARCHAR(45) NULL,
2138+ `relationship` VARCHAR(45) NULL,
2139+ `createdOn` DATETIME NULL,
2140+ `createdBy` INT NULL,
2141+ `meta` TEXT NULL,
2142+ PRIMARY KEY (`id`),
2143+ INDEX `entityA` (`entityA` ASC, `entityAType` ASC, `relationship` ASC),
2144+ INDEX `entityB` (`entityB` ASC, `entityBType` ASC, `relationship` ASC)
2145+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
2146+ ' );
21092147
2110- // Create a new index on `entityA`
2111- 'ALTER TABLE `zp_entity_relationship` ADD INDEX `entityA` (`entityA` ASC, `entityAType` ASC, `relationship` ASC); ' ,
2112- ];
2148+ return true ;
2149+ }
2150+ } catch (\Exception $ e ) {
2151+ Log::error ('Failed to check/create zp_entity_relationship table: ' .$ e ->getMessage ());
21132152
2114- foreach ($ sql as $ statement ) {
2115- try {
2116- $ this ->connection ->statement ($ statement );
2117- } catch (\Exception $ e ) {
2118- Log::error ($ statement .' Failed: ' .$ e ->getMessage ());
2119- Log::error ($ e );
2153+ return true ;
2154+ }
2155+
2156+ // Check if the column needs to be renamed (has the typo)
2157+ try {
2158+ $ columnExists = $ this ->connection ->select (
2159+ "SELECT COUNT(*) as cnt FROM information_schema.columns
2160+ WHERE table_schema = DATABASE()
2161+ AND table_name = 'zp_entity_relationship'
2162+ AND column_name = 'enitityA' "
2163+ );
2164+
2165+ if ($ columnExists [0 ]->cnt > 0 ) {
2166+ // Column has the typo, fix it
2167+ $ sql = [
2168+ // Rename the column `enitityA` to `entityA`
2169+ 'ALTER TABLE `zp_entity_relationship` CHANGE COLUMN `enitityA` `entityA` INT NULL; ' ,
2170+
2171+ // Drop the old index on `enitityA`
2172+ 'ALTER TABLE `zp_entity_relationship` DROP INDEX `entityA`; ' ,
2173+
2174+ // Create a new index on `entityA`
2175+ 'ALTER TABLE `zp_entity_relationship` ADD INDEX `entityA` (`entityA` ASC, `entityAType` ASC, `relationship` ASC); ' ,
2176+ ];
2177+
2178+ foreach ($ sql as $ statement ) {
2179+ try {
2180+ $ this ->connection ->statement ($ statement );
2181+ } catch (\Exception $ e ) {
2182+ Log::error ($ statement .' Failed: ' .$ e ->getMessage ());
2183+ Log::error ($ e );
2184+ }
2185+ }
21202186 }
2187+ } catch (\Exception $ e ) {
2188+ Log::error ('Failed to check/fix enitityA column: ' .$ e ->getMessage ());
21212189 }
21222190
21232191 return true ;
@@ -2147,4 +2215,88 @@ public function update_sql_30411(): bool|array
21472215
21482216 return count ($ errors ) ? $ errors : true ;
21492217 }
2218+
2219+ public function update_sql_30412 (): bool |array
2220+ {
2221+ // This migration fixes the zp_entity_relationship table for users who:
2222+ // 1. Upgraded from older versions with plural table name (zp_entity_relationships)
2223+ // 2. Already ran the broken update_sql_30410 which failed
2224+ // 3. Have the table with the typo column (enitityA instead of entityA)
2225+
2226+ // Step 1: Check if plural table exists and rename to singular
2227+ try {
2228+ $ pluralTableExists = $ this ->connection ->select (
2229+ "SELECT COUNT(*) as cnt FROM information_schema.tables
2230+ WHERE table_schema = DATABASE()
2231+ AND table_name = 'zp_entity_relationships' "
2232+ );
2233+
2234+ if ($ pluralTableExists [0 ]->cnt > 0 ) {
2235+ $ this ->connection ->statement ('RENAME TABLE `zp_entity_relationships` TO `zp_entity_relationship`; ' );
2236+ }
2237+ } catch (\Exception $ e ) {
2238+ Log::error ('Migration 30412: Failed to check/rename plural table: ' .$ e ->getMessage ());
2239+ }
2240+
2241+ // Step 2: Check if singular table exists, create if not
2242+ try {
2243+ $ tableExists = $ this ->connection ->select (
2244+ "SELECT COUNT(*) as cnt FROM information_schema.tables
2245+ WHERE table_schema = DATABASE()
2246+ AND table_name = 'zp_entity_relationship' "
2247+ );
2248+
2249+ if ($ tableExists [0 ]->cnt == 0 ) {
2250+ $ this ->connection ->statement ('
2251+ CREATE TABLE `zp_entity_relationship` (
2252+ `id` INT NOT NULL AUTO_INCREMENT,
2253+ `entityA` INT NULL,
2254+ `entityAType` VARCHAR(45) NULL,
2255+ `entityB` INT NULL,
2256+ `entityBType` VARCHAR(45) NULL,
2257+ `relationship` VARCHAR(45) NULL,
2258+ `createdOn` DATETIME NULL,
2259+ `createdBy` INT NULL,
2260+ `meta` TEXT NULL,
2261+ PRIMARY KEY (`id`),
2262+ INDEX `entityA` (`entityA` ASC, `entityAType` ASC, `relationship` ASC),
2263+ INDEX `entityB` (`entityB` ASC, `entityBType` ASC, `relationship` ASC)
2264+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
2265+ ' );
2266+
2267+ return true ;
2268+ }
2269+ } catch (\Exception $ e ) {
2270+ Log::error ('Migration 30412: Failed to check/create table: ' .$ e ->getMessage ());
2271+
2272+ return true ;
2273+ }
2274+
2275+ // Step 3: Check if column typo exists and fix it
2276+ try {
2277+ $ columnExists = $ this ->connection ->select (
2278+ "SELECT COUNT(*) as cnt FROM information_schema.columns
2279+ WHERE table_schema = DATABASE()
2280+ AND table_name = 'zp_entity_relationship'
2281+ AND column_name = 'enitityA' "
2282+ );
2283+
2284+ if ($ columnExists [0 ]->cnt > 0 ) {
2285+ $ this ->connection ->statement ('ALTER TABLE `zp_entity_relationship` CHANGE COLUMN `enitityA` `entityA` INT NULL; ' );
2286+
2287+ // Try to fix the index too
2288+ try {
2289+ $ this ->connection ->statement ('ALTER TABLE `zp_entity_relationship` DROP INDEX `entityA`; ' );
2290+ } catch (\Exception $ e ) {
2291+ // Index may not exist or have different name, ignore
2292+ }
2293+
2294+ $ this ->connection ->statement ('ALTER TABLE `zp_entity_relationship` ADD INDEX `entityA` (`entityA` ASC, `entityAType` ASC, `relationship` ASC); ' );
2295+ }
2296+ } catch (\Exception $ e ) {
2297+ Log::error ('Migration 30412: Failed to fix column typo: ' .$ e ->getMessage ());
2298+ }
2299+
2300+ return true ;
2301+ }
21502302}
0 commit comments