|
3 | 3 | namespace StellarWP\Slic; |
4 | 4 |
|
5 | 5 | /** |
6 | | - * Updates a SQL dump file. Optionally, installs a specific WordPress version. |
| 6 | + * Updates a SQL dump file by importing it, running database updates, and exporting it back. |
| 7 | + * Optionally tests the dump with a specific WordPress version before restoring the original version. |
7 | 8 | * |
8 | 9 | * @var bool $is_help Whether we're handling an `help` request on this command or not. |
9 | 10 | * @var string $subcommand This command. |
|
14 | 15 | $help = <<< HELP |
15 | 16 | SUMMARY: |
16 | 17 |
|
17 | | - Updates a SQL dump file. Optionally, installs a specific WordPress version. |
| 18 | + Updates a SQL dump file by importing it, running database updates, and exporting it back. |
| 19 | + Optionally upgrades the dump to a specific WordPress version (then restores original version). |
| 20 | + |
| 21 | + The process: imports the dump.sql → optionally installs a WP version → runs core update-db → |
| 22 | + exports an updated dump.sql → restores original WP version (if version was specified). |
18 | 23 |
|
19 | 24 | USAGE: |
20 | 25 |
|
|
23 | 28 | EXAMPLES: |
24 | 29 |
|
25 | 30 | <light_cyan>$cli_name $subcommand tests/_data/dump.sql</light_cyan> |
26 | | - Update the dump file using slic's currently installed version of WordPress. |
| 31 | + Update the dump file's database structure using the current WordPress version. |
27 | 32 | |
28 | 33 | <light_cyan>$cli_name $subcommand tests/_data/dump.sql latest</light_cyan> |
29 | | - Update the WordPress version to the latest and update the dump file. |
| 34 | + Update the dump file to be compatible with the latest WordPress version. |
30 | 35 |
|
31 | 36 | <light_cyan>$cli_name $subcommand tests/_data/dump.sql 6.4.3</light_cyan> |
32 | | - Update the WordPress version to 6.4.3 and update the dump file. |
| 37 | + Update the dump file to be compatible with WordPress 6.4.3. |
33 | 38 | HELP; |
34 | 39 |
|
35 | 40 | echo colorize( $help ); |
|
45 | 50 | $file = trim( $command[0] ); |
46 | 51 | $version = trim( $command[1] ?? '' ); |
47 | 52 |
|
48 | | -// Build the path inside the slic container. |
| 53 | +// Build the paths. |
49 | 54 | $container_path = remove_double_separators( trailingslashit( get_project_container_path() ) . $file ); |
| 55 | +$host_path = remove_double_separators( trailingslashit( get_project_local_path() ) . $file ); |
| 56 | + |
| 57 | +// Proper line endings. |
| 58 | +$lb = PHP_EOL; |
| 59 | + |
| 60 | +// Check if the existing dump file exists. |
| 61 | +if ( ! file_exists( $host_path ) ) { |
| 62 | + echo magenta( sprintf( |
| 63 | + "Error: Dump file '%s' does not exist.\nPlease create it first with: %s wp db export --add-drop-table %s", |
| 64 | + $host_path, |
| 65 | + $cli_name, |
| 66 | + $file |
| 67 | + ) ); |
| 68 | + |
| 69 | + exit( 1 ); |
| 70 | +} |
50 | 71 |
|
51 | | -// Run core update if a version was provided, otherwise run core update-db. |
| 72 | +ensure_wordpress_installed(); |
| 73 | + |
| 74 | +$original_version = null; |
| 75 | + |
| 76 | +// If $version is passed, capture the existing WP version to restore later. |
52 | 77 | if ( $version ) { |
53 | | - $update_command = cli_command( [ |
54 | | - 'core', |
55 | | - 'update', |
56 | | - '--force', |
57 | | - sprintf( '--version=%s', $version ), |
58 | | - ], true ); |
59 | | -} else { |
60 | | - $update_command = cli_command( [ |
| 78 | + $version_command = cli_command( [ |
61 | 79 | 'core', |
62 | | - 'update-db', |
| 80 | + 'version', |
63 | 81 | ], true ); |
| 82 | + |
| 83 | + ob_start(); |
| 84 | + $result = slic_realtime()( $version_command ); |
| 85 | + $output = trim( ob_get_clean() ); |
| 86 | + |
| 87 | + if ( $result === 0 && ! empty( $output ) ) { |
| 88 | + $original_version = $output; |
| 89 | + echo colorize( sprintf( "<light_cyan>Captured current WordPress version: %s</light_cyan>{$lb}", $original_version ) ); |
| 90 | + } else { |
| 91 | + echo magenta( 'Warning: Could not capture current WordPress version.' ); |
| 92 | + } |
64 | 93 | } |
65 | 94 |
|
66 | | -$commands = [ |
67 | | - cli_command( [ |
68 | | - 'cli', |
69 | | - 'cache', |
70 | | - 'clear', |
71 | | - ] ), |
72 | | - $update_command, |
73 | | - cli_command( [ |
74 | | - 'db', |
75 | | - 'reset', |
76 | | - '--yes', |
77 | | - ] ), |
78 | | - cli_command( [ |
| 95 | +// Clear the cache. |
| 96 | +echo colorize( "<light_cyan>Clearing cache...</light_cyan>{$lb}" ); |
| 97 | +$result = slic_realtime()( cli_command( [ |
| 98 | + 'cli', |
| 99 | + 'cache', |
| 100 | + 'clear', |
| 101 | +] ) ); |
| 102 | + |
| 103 | +if ( $result !== 0 ) { |
| 104 | + echo magenta( 'Error: Failed to clear cache.' ); |
| 105 | + exit( 1 ); |
| 106 | +} |
| 107 | + |
| 108 | +// Reset database. |
| 109 | +echo colorize( "<light_cyan>Resetting database...</light_cyan>{$lb}" ); |
| 110 | +$result = slic_realtime()( cli_command( [ |
| 111 | + 'db', |
| 112 | + 'reset', |
| 113 | + '--yes', |
| 114 | +] ) ); |
| 115 | + |
| 116 | +if ( $result !== 0 ) { |
| 117 | + echo magenta( 'Error: Failed to reset database.' ); |
| 118 | + exit( 1 ); |
| 119 | +} |
| 120 | + |
| 121 | +// Import existing dump file. |
| 122 | +echo colorize( "<light_cyan>Importing dump file...</light_cyan>{$lb}" ); |
| 123 | +$result = slic_realtime()( cli_command( [ |
| 124 | + 'db', |
| 125 | + 'import', |
| 126 | + $container_path, |
| 127 | +] ) ); |
| 128 | + |
| 129 | +if ( $result !== 0 ) { |
| 130 | + echo magenta( sprintf( 'Error: Failed to import dump file: %s', $container_path ) ); |
| 131 | + exit( 1 ); |
| 132 | +} |
| 133 | + |
| 134 | +// If $version is passed, install that WP version. |
| 135 | +if ( $version ) { |
| 136 | + echo colorize( sprintf( "<light_cyan>Installing WordPress version %s...</light_cyan>{$lb}", $version ) ); |
| 137 | + $result = slic_realtime()( cli_command( [ |
79 | 138 | 'core', |
80 | | - 'version', |
81 | | - '--extra', |
82 | | - ], true ), |
83 | | - cli_command( [ |
84 | | - 'db', |
85 | | - 'export', |
86 | | - '--add-drop-table', |
87 | | - $container_path, |
88 | | - ] ), |
89 | | -]; |
90 | | - |
91 | | -// Execute the command chain. |
92 | | -foreach ( $commands as $arguments ) { |
93 | | - $result = slic_passive()( $arguments ); |
94 | | - |
95 | | - // 0 is success on command line. |
96 | | - if ( $result === 0 ) { |
97 | | - continue; |
| 139 | + 'update', |
| 140 | + '--force', |
| 141 | + sprintf( '--version=%s', $version ), |
| 142 | + ], true ) ); |
| 143 | + |
| 144 | + if ( $result !== 0 ) { |
| 145 | + echo magenta( sprintf( 'Error: Failed to install WordPress version: %s', $version ) ); |
| 146 | + exit( 1 ); |
98 | 147 | } |
| 148 | +} |
99 | 149 |
|
100 | | - echo magenta( sprintf( 'Error: Command Failed: %s', implode( ' ', $arguments ) ) ); |
101 | | - exit ( 1 ); |
| 150 | +// Run core update-db. |
| 151 | +echo colorize( "<light_cyan>Updating database...</light_cyan>{$lb}" ); |
| 152 | +$result = slic_realtime()( cli_command( [ |
| 153 | + 'core', |
| 154 | + 'update-db', |
| 155 | +] ) ); |
| 156 | + |
| 157 | +if ( $result !== 0 ) { |
| 158 | + echo magenta( 'Error: Failed to update database.' ); |
| 159 | + exit( 1 ); |
102 | 160 | } |
103 | 161 |
|
104 | | -ensure_wordpress_installed(); |
| 162 | +// Export the dump.sql to the same file. |
| 163 | +echo colorize( "<light_cyan>Exporting updated dump...</light_cyan>{$lb}" ); |
| 164 | +$result = slic_realtime()( cli_command( [ |
| 165 | + 'db', |
| 166 | + 'export', |
| 167 | + '--add-drop-table', |
| 168 | + $container_path, |
| 169 | +] ) ); |
| 170 | + |
| 171 | +if ( $result !== 0 ) { |
| 172 | + echo magenta( 'Error: Failed to export database.' ); |
| 173 | + exit( 1 ); |
| 174 | +} |
| 175 | + |
| 176 | +// If $version was passed, restore the original WP version and run core update-db again. |
| 177 | +if ( $version && $original_version ) { |
| 178 | + echo colorize( sprintf( "<light_cyan>Restoring original WordPress version %s...</light_cyan>{$lb}", $original_version ) ); |
| 179 | + $result = slic_realtime()( cli_command( [ |
| 180 | + 'core', |
| 181 | + 'update', |
| 182 | + '--force', |
| 183 | + sprintf( '--version=%s', $original_version ), |
| 184 | + ], true ) ); |
| 185 | + |
| 186 | + if ( $result !== 0 ) { |
| 187 | + echo magenta( sprintf( 'Warning: Failed to restore WordPress version: %s', $original_version ) ); |
| 188 | + } else { |
| 189 | + echo colorize( "<light_cyan>Updating database for restored version...</light_cyan>{$lb}" ); |
| 190 | + $result = slic_realtime()( cli_command( [ |
| 191 | + 'core', |
| 192 | + 'update-db', |
| 193 | + ] ) ); |
| 194 | + |
| 195 | + if ( $result !== 0 ) { |
| 196 | + echo magenta( 'Warning: Failed to update database after restoring version.' ); |
| 197 | + } |
| 198 | + } |
| 199 | +} |
105 | 200 |
|
106 | 201 | echo green( sprintf( |
107 | 202 | "Success: Exported to host path '%s'.", |
108 | | - remove_double_separators( trailingslashit( get_project_local_path() ) . $file ) |
| 203 | + $host_path |
109 | 204 | ) ); |
110 | 205 |
|
111 | 206 | exit( 0 ); |
0 commit comments