@@ -35,210 +35,210 @@ discard block |
||
| 35 | 35 | class Database extends \Myth\Controllers\CLIController |
| 36 | 36 | { |
| 37 | 37 | |
| 38 | - protected $descriptions = [ |
|
| 39 | - 'migrate' => ['migrate [$to]', 'Runs the migrations up or down until schema at version \$to'], |
|
| 40 | - 'quietMigrate' => ['quiteMigrate [$to]', 'Same as migrate but without any feedback.'], |
|
| 41 | - 'refresh' => ['refresh', 'Runs migrations back to version 0 (uninstall) and then back to the most recent migration.'], |
|
| 42 | - 'newMigration' => ['newMigration [$name]', 'Creates a new migration file.'], |
|
| 43 | - 'seed' => ['seed [$name]', 'Runs the named database seeder.'] |
|
| 44 | - ]; |
|
| 45 | - |
|
| 46 | - protected $long_descriptions = [ |
|
| 47 | - 'migrate' => '', |
|
| 48 | - 'quietMigrate' => '', |
|
| 49 | - 'refresh' => '', |
|
| 50 | - 'newMigration' => '', |
|
| 51 | - 'seed' => '' |
|
| 52 | - ]; |
|
| 53 | - |
|
| 54 | - //------------------------------------------------------------------- |
|
| 55 | - |
|
| 56 | - //-------------------------------------------------------------------- |
|
| 57 | - // Migration Methods |
|
| 58 | - //-------------------------------------------------------------------- |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Provides a command-line interface to the migration scripts. |
|
| 62 | - * If no $to is provided, will migrate to the latest version. |
|
| 63 | - * |
|
| 64 | - * Example: |
|
| 65 | - * > php index.php database migrate |
|
| 66 | - * |
|
| 67 | - * @param string $type 'app', 'myth', 'all' or {module_name} |
|
| 68 | - * @param null $to |
|
| 69 | - * @param bool $silent If TRUE, will NOT display any prompts for verification. |
|
| 70 | - * @return bool|void |
|
| 71 | - */ |
|
| 72 | - public function migrate($type=null, $to = null, $silent = false) |
|
| 73 | - { |
|
| 74 | - $this->load->library('migration'); |
|
| 75 | - |
|
| 76 | - if (empty($type)) |
|
| 77 | - { |
|
| 78 | - $type = CLI::prompt("Migration group to refresh?", $this->migration->default_migration_path()); |
|
| 79 | - |
|
| 80 | - if (empty($type)) |
|
| 81 | - { |
|
| 82 | - return $silent ? false : CLI::error("\tYou must supply a group to refresh."); |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - // We need to append 'mod:' to properly handle modules, if |
|
| 87 | - // the $type is not one of the recognized migration groups. |
|
| 88 | - $this->config->load('migration'); |
|
| 89 | - $groups = config_item('migration_paths'); |
|
| 90 | - $groups = array_keys($groups); |
|
| 91 | - |
|
| 92 | - // If it's not in the groups list, then assume it's a module. |
|
| 93 | - if (! in_array($type, $groups)) |
|
| 94 | - { |
|
| 95 | - if (strpos($type, 'mod:') !== 0) |
|
| 96 | - { |
|
| 97 | - $type = 'mod:'. $type; |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - unset($groups); |
|
| 102 | - |
|
| 103 | - // Get our stats on the migrations |
|
| 104 | - $latest = $this->migration->get_latest($type); |
|
| 105 | - $latest = empty($latest) ? 0 : substr($latest, 0, strpos($latest, '_')); |
|
| 106 | - |
|
| 107 | - if (empty($latest)) { |
|
| 108 | - return CLI::write("\tNo migrations found.", 'yellow'); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - $current = $this->migration->get_version($type); |
|
| 112 | - |
|
| 113 | - // Already at the desired version? |
|
| 114 | - if ((! is_null($to) && $current == $to) OR (is_null($to) && $current == $latest)) |
|
| 115 | - { |
|
| 116 | - return $silent ? true : CLI::write("\tDatabase is already at the desired version ({$current})", 'yellow'); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - $target = is_null($to) ? $latest : $to; |
|
| 120 | - |
|
| 121 | - // Just to be safe, verify with the user they want to migrate |
|
| 122 | - // to the latest version. |
|
| 123 | - if (is_null($to)) { |
|
| 124 | - // If we're in silent mode, don't prompt, just go to the latest... |
|
| 125 | - if (! $silent) { |
|
| 126 | - $go_ahead = CLI::prompt('Migrate to the latest available version?', array('y', 'n')); |
|
| 127 | - |
|
| 128 | - if ($go_ahead == 'n') { |
|
| 129 | - return CLI::write('Bailing...', 'yellow'); |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - if (! $this->migration->latest($type)) { |
|
| 134 | - return CLI::error("\n\tERROR: " . $this->migration->error_string() . "\n"); |
|
| 135 | - } |
|
| 136 | - } else { |
|
| 137 | - if ($this->migration->version($type, $to) === false) { |
|
| 138 | - return CLI::error("\n\tERROR: " . $this->migration->error_string() . "\n"); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return $silent ? true : |
|
| 143 | - CLI::write("\n\tSuccessfully migrated database from version {$current} to {$target}.\n", 'green'); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - //-------------------------------------------------------------------- |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Performs a migration that does not prompt for any information. |
|
| 150 | - * Suitable for use within automated scripts that can't be |
|
| 151 | - * bothered with answering questions. |
|
| 152 | - * |
|
| 153 | - * @param string $type 'app', 'myth', 'all' or {module_name} |
|
| 154 | - * @param null $to |
|
| 155 | - * @return bool|void |
|
| 156 | - */ |
|
| 157 | - public function quietMigrate($type='app', $to = null) |
|
| 158 | - { |
|
| 159 | - return $this->migrate($type, $to, true); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - //-------------------------------------------------------------------- |
|
| 163 | - |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Migrates the database back to 0, then back up to the latest version. |
|
| 167 | - * |
|
| 168 | - * @param string $type The group or module to refresh. |
|
| 169 | - * @return mixed |
|
| 170 | - */ |
|
| 171 | - public function refresh($type=null) |
|
| 172 | - { |
|
| 173 | - $this->load->library('migration'); |
|
| 38 | + protected $descriptions = [ |
|
| 39 | + 'migrate' => ['migrate [$to]', 'Runs the migrations up or down until schema at version \$to'], |
|
| 40 | + 'quietMigrate' => ['quiteMigrate [$to]', 'Same as migrate but without any feedback.'], |
|
| 41 | + 'refresh' => ['refresh', 'Runs migrations back to version 0 (uninstall) and then back to the most recent migration.'], |
|
| 42 | + 'newMigration' => ['newMigration [$name]', 'Creates a new migration file.'], |
|
| 43 | + 'seed' => ['seed [$name]', 'Runs the named database seeder.'] |
|
| 44 | + ]; |
|
| 45 | + |
|
| 46 | + protected $long_descriptions = [ |
|
| 47 | + 'migrate' => '', |
|
| 48 | + 'quietMigrate' => '', |
|
| 49 | + 'refresh' => '', |
|
| 50 | + 'newMigration' => '', |
|
| 51 | + 'seed' => '' |
|
| 52 | + ]; |
|
| 53 | + |
|
| 54 | + //------------------------------------------------------------------- |
|
| 55 | + |
|
| 56 | + //-------------------------------------------------------------------- |
|
| 57 | + // Migration Methods |
|
| 58 | + //-------------------------------------------------------------------- |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Provides a command-line interface to the migration scripts. |
|
| 62 | + * If no $to is provided, will migrate to the latest version. |
|
| 63 | + * |
|
| 64 | + * Example: |
|
| 65 | + * > php index.php database migrate |
|
| 66 | + * |
|
| 67 | + * @param string $type 'app', 'myth', 'all' or {module_name} |
|
| 68 | + * @param null $to |
|
| 69 | + * @param bool $silent If TRUE, will NOT display any prompts for verification. |
|
| 70 | + * @return bool|void |
|
| 71 | + */ |
|
| 72 | + public function migrate($type=null, $to = null, $silent = false) |
|
| 73 | + { |
|
| 74 | + $this->load->library('migration'); |
|
| 75 | + |
|
| 76 | + if (empty($type)) |
|
| 77 | + { |
|
| 78 | + $type = CLI::prompt("Migration group to refresh?", $this->migration->default_migration_path()); |
|
| 79 | + |
|
| 80 | + if (empty($type)) |
|
| 81 | + { |
|
| 82 | + return $silent ? false : CLI::error("\tYou must supply a group to refresh."); |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + // We need to append 'mod:' to properly handle modules, if |
|
| 87 | + // the $type is not one of the recognized migration groups. |
|
| 88 | + $this->config->load('migration'); |
|
| 89 | + $groups = config_item('migration_paths'); |
|
| 90 | + $groups = array_keys($groups); |
|
| 91 | + |
|
| 92 | + // If it's not in the groups list, then assume it's a module. |
|
| 93 | + if (! in_array($type, $groups)) |
|
| 94 | + { |
|
| 95 | + if (strpos($type, 'mod:') !== 0) |
|
| 96 | + { |
|
| 97 | + $type = 'mod:'. $type; |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + unset($groups); |
|
| 102 | + |
|
| 103 | + // Get our stats on the migrations |
|
| 104 | + $latest = $this->migration->get_latest($type); |
|
| 105 | + $latest = empty($latest) ? 0 : substr($latest, 0, strpos($latest, '_')); |
|
| 106 | + |
|
| 107 | + if (empty($latest)) { |
|
| 108 | + return CLI::write("\tNo migrations found.", 'yellow'); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + $current = $this->migration->get_version($type); |
|
| 112 | + |
|
| 113 | + // Already at the desired version? |
|
| 114 | + if ((! is_null($to) && $current == $to) OR (is_null($to) && $current == $latest)) |
|
| 115 | + { |
|
| 116 | + return $silent ? true : CLI::write("\tDatabase is already at the desired version ({$current})", 'yellow'); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + $target = is_null($to) ? $latest : $to; |
|
| 120 | + |
|
| 121 | + // Just to be safe, verify with the user they want to migrate |
|
| 122 | + // to the latest version. |
|
| 123 | + if (is_null($to)) { |
|
| 124 | + // If we're in silent mode, don't prompt, just go to the latest... |
|
| 125 | + if (! $silent) { |
|
| 126 | + $go_ahead = CLI::prompt('Migrate to the latest available version?', array('y', 'n')); |
|
| 127 | + |
|
| 128 | + if ($go_ahead == 'n') { |
|
| 129 | + return CLI::write('Bailing...', 'yellow'); |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + if (! $this->migration->latest($type)) { |
|
| 134 | + return CLI::error("\n\tERROR: " . $this->migration->error_string() . "\n"); |
|
| 135 | + } |
|
| 136 | + } else { |
|
| 137 | + if ($this->migration->version($type, $to) === false) { |
|
| 138 | + return CLI::error("\n\tERROR: " . $this->migration->error_string() . "\n"); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + return $silent ? true : |
|
| 143 | + CLI::write("\n\tSuccessfully migrated database from version {$current} to {$target}.\n", 'green'); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + //-------------------------------------------------------------------- |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Performs a migration that does not prompt for any information. |
|
| 150 | + * Suitable for use within automated scripts that can't be |
|
| 151 | + * bothered with answering questions. |
|
| 152 | + * |
|
| 153 | + * @param string $type 'app', 'myth', 'all' or {module_name} |
|
| 154 | + * @param null $to |
|
| 155 | + * @return bool|void |
|
| 156 | + */ |
|
| 157 | + public function quietMigrate($type='app', $to = null) |
|
| 158 | + { |
|
| 159 | + return $this->migrate($type, $to, true); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + //-------------------------------------------------------------------- |
|
| 163 | + |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Migrates the database back to 0, then back up to the latest version. |
|
| 167 | + * |
|
| 168 | + * @param string $type The group or module to refresh. |
|
| 169 | + * @return mixed |
|
| 170 | + */ |
|
| 171 | + public function refresh($type=null) |
|
| 172 | + { |
|
| 173 | + $this->load->library('migration'); |
|
| 174 | 174 | |
| 175 | - //Load the migration config |
|
| 176 | - $this->config->load('migration'); |
|
| 175 | + //Load the migration config |
|
| 176 | + $this->config->load('migration'); |
|
| 177 | 177 | |
| 178 | - if (empty($type)) |
|
| 179 | - { |
|
| 180 | - $type = CLI::prompt("Migration group to refresh?", $this->migration->default_migration_path()); |
|
| 178 | + if (empty($type)) |
|
| 179 | + { |
|
| 180 | + $type = CLI::prompt("Migration group to refresh?", $this->migration->default_migration_path()); |
|
| 181 | 181 | |
| 182 | - if (empty($type)) |
|
| 183 | - { |
|
| 184 | - return CLI::error("\tYou must supply a group to refresh."); |
|
| 185 | - } |
|
| 186 | - } |
|
| 182 | + if (empty($type)) |
|
| 183 | + { |
|
| 184 | + return CLI::error("\tYou must supply a group to refresh."); |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - if ($this->migration->version($type, 0) === false) { |
|
| 189 | - return CLI::error("\tERROR: " . $this->migration->error_string()); |
|
| 190 | - } |
|
| 188 | + if ($this->migration->version($type, 0) === false) { |
|
| 189 | + return CLI::error("\tERROR: " . $this->migration->error_string()); |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | - CLI::write(CLI::color("\tCleared the database.", 'green')); |
|
| 192 | + CLI::write(CLI::color("\tCleared the database.", 'green')); |
|
| 193 | 193 | |
| 194 | - if ($this->migration->latest($type) === false) { |
|
| 195 | - return CLI::error("\tERROR: " . $this->migration->error_string()); |
|
| 196 | - } |
|
| 194 | + if ($this->migration->latest($type) === false) { |
|
| 195 | + return CLI::error("\tERROR: " . $this->migration->error_string()); |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | - CLI::write("\tRe-installed the database to the latest migration.", 'green'); |
|
| 199 | - } |
|
| 198 | + CLI::write("\tRe-installed the database to the latest migration.", 'green'); |
|
| 199 | + } |
|
| 200 | 200 | |
| 201 | - //-------------------------------------------------------------------- |
|
| 201 | + //-------------------------------------------------------------------- |
|
| 202 | 202 | |
| 203 | - /** |
|
| 204 | - * Creates a new migration file ready to be used. |
|
| 205 | - * |
|
| 206 | - * @param $name |
|
| 207 | - */ |
|
| 208 | - public function newMigration($name = null, $type = 'app') |
|
| 209 | - { |
|
| 210 | - if (empty($name)) { |
|
| 211 | - $name = CLI::prompt('Migration name? '); |
|
| 203 | + /** |
|
| 204 | + * Creates a new migration file ready to be used. |
|
| 205 | + * |
|
| 206 | + * @param $name |
|
| 207 | + */ |
|
| 208 | + public function newMigration($name = null, $type = 'app') |
|
| 209 | + { |
|
| 210 | + if (empty($name)) { |
|
| 211 | + $name = CLI::prompt('Migration name? '); |
|
| 212 | 212 | |
| 213 | - if (empty($name)) { |
|
| 214 | - return CLI::error("\tYou must provide a migration name.", 'red'); |
|
| 215 | - } |
|
| 216 | - } |
|
| 213 | + if (empty($name)) { |
|
| 214 | + return CLI::error("\tYou must provide a migration name.", 'red'); |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | - $this->load->library('migration'); |
|
| 218 | + $this->load->library('migration'); |
|
| 219 | 219 | |
| 220 | - $path = $this->migration->determine_migration_path($type); |
|
| 220 | + $path = $this->migration->determine_migration_path($type); |
|
| 221 | 221 | |
| 222 | - // Does the alias path exist in our config? |
|
| 223 | - if (! $path) { |
|
| 224 | - return CLI::error("\tThe migration path for '{$type}' does not exist.'"); |
|
| 225 | - } |
|
| 222 | + // Does the alias path exist in our config? |
|
| 223 | + if (! $path) { |
|
| 224 | + return CLI::error("\tThe migration path for '{$type}' does not exist.'"); |
|
| 225 | + } |
|
| 226 | 226 | |
| 227 | - // Does the path really exist? |
|
| 228 | - if (! is_dir($path)) { |
|
| 229 | - return CLI::error("\tThe path for '{$type}' is not a directory."); |
|
| 230 | - } |
|
| 227 | + // Does the path really exist? |
|
| 228 | + if (! is_dir($path)) { |
|
| 229 | + return CLI::error("\tThe path for '{$type}' is not a directory."); |
|
| 230 | + } |
|
| 231 | 231 | |
| 232 | - // Is the folder writeable? |
|
| 233 | - if (! is_writeable($path)) { |
|
| 234 | - return CLI::error("\tThe folder for '{$type}' migrations is not writeable."); |
|
| 235 | - } |
|
| 232 | + // Is the folder writeable? |
|
| 233 | + if (! is_writeable($path)) { |
|
| 234 | + return CLI::error("\tThe folder for '{$type}' migrations is not writeable."); |
|
| 235 | + } |
|
| 236 | 236 | |
| 237 | - $file = $this->migration->make_name($name); |
|
| 237 | + $file = $this->migration->make_name($name); |
|
| 238 | 238 | |
| 239 | - $path = rtrim($path, '/') .'/'. $file; |
|
| 239 | + $path = rtrim($path, '/') .'/'. $file; |
|
| 240 | 240 | |
| 241 | - $contents = <<<EOT |
|
| 241 | + $contents = <<<EOT |
|
| 242 | 242 | <?php |
| 243 | 243 | |
| 244 | 244 | /** |
@@ -265,36 +265,36 @@ discard block |
||
| 265 | 265 | |
| 266 | 266 | } |
| 267 | 267 | EOT; |
| 268 | - $contents = str_replace('{name}', $name, $contents); |
|
| 269 | - $contents = str_replace('{date}', date('Y-m-d H:i:s a'), $contents); |
|
| 270 | - $contents = str_replace('{clean_name}', ucwords(str_replace('_', ' ', $name)), $contents); |
|
| 268 | + $contents = str_replace('{name}', $name, $contents); |
|
| 269 | + $contents = str_replace('{date}', date('Y-m-d H:i:s a'), $contents); |
|
| 270 | + $contents = str_replace('{clean_name}', ucwords(str_replace('_', ' ', $name)), $contents); |
|
| 271 | 271 | |
| 272 | - $this->load->helper('file'); |
|
| 272 | + $this->load->helper('file'); |
|
| 273 | 273 | |
| 274 | - if (write_file($path, $contents)) { |
|
| 275 | - return CLI::write("\tNew migration created: " . CLI::color($file, 'yellow'), 'green'); |
|
| 276 | - } |
|
| 274 | + if (write_file($path, $contents)) { |
|
| 275 | + return CLI::write("\tNew migration created: " . CLI::color($file, 'yellow'), 'green'); |
|
| 276 | + } |
|
| 277 | 277 | |
| 278 | - return CLI::error("\tUnkown error trying to create migration: {$file}", 'red'); |
|
| 279 | - } |
|
| 278 | + return CLI::error("\tUnkown error trying to create migration: {$file}", 'red'); |
|
| 279 | + } |
|
| 280 | 280 | |
| 281 | - //-------------------------------------------------------------------- |
|
| 281 | + //-------------------------------------------------------------------- |
|
| 282 | 282 | |
| 283 | - //-------------------------------------------------------------------- |
|
| 284 | - // Seeding Methods |
|
| 285 | - //-------------------------------------------------------------------- |
|
| 283 | + //-------------------------------------------------------------------- |
|
| 284 | + // Seeding Methods |
|
| 285 | + //-------------------------------------------------------------------- |
|
| 286 | 286 | |
| 287 | - /** |
|
| 288 | - * Installs any database seeds stored in database/seeds. Seeds just need to |
|
| 289 | - * extend the Seeder class and have a method named run. |
|
| 290 | - */ |
|
| 291 | - public function seed($name = null) |
|
| 292 | - { |
|
| 293 | - $this->load->library('seeder'); |
|
| 287 | + /** |
|
| 288 | + * Installs any database seeds stored in database/seeds. Seeds just need to |
|
| 289 | + * extend the Seeder class and have a method named run. |
|
| 290 | + */ |
|
| 291 | + public function seed($name = null) |
|
| 292 | + { |
|
| 293 | + $this->load->library('seeder'); |
|
| 294 | 294 | |
| 295 | - $this->seeder->call($name); |
|
| 296 | - } |
|
| 295 | + $this->seeder->call($name); |
|
| 296 | + } |
|
| 297 | 297 | |
| 298 | - //-------------------------------------------------------------------- |
|
| 298 | + //-------------------------------------------------------------------- |
|
| 299 | 299 | |
| 300 | 300 | } |