123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php namespace Laravel\CLI\Tasks\Migrate;
- use Laravel\Str;
- use Laravel\File;
- use Laravel\Bundle;
- use Laravel\CLI\Tasks\Task;
- use Laravel\Database\Schema;
- class Migrator extends Task {
-
- protected $resolver;
-
- protected $database;
-
- public function __construct(Resolver $resolver, Database $database)
- {
- $this->resolver = $resolver;
- $this->database = $database;
- }
-
- public function run($arguments = array())
- {
-
-
-
-
- if (count($arguments) == 0)
- {
- $this->migrate();
- }
- else
- {
- $this->migrate(array_get($arguments, 0));
- }
- }
-
- public function migrate($bundle = null, $version = null)
- {
- $migrations = $this->resolver->outstanding($bundle);
- if (count($migrations) == 0)
- {
- echo "No outstanding migrations.";
- return;
- }
-
-
-
- $batch = $this->database->batch() + 1;
- foreach ($migrations as $migration)
- {
- $migration['migration']->up();
- echo 'Migrated: '.$this->display($migration).PHP_EOL;
-
-
-
- $this->database->log($migration['bundle'], $migration['name'], $batch);
- }
- }
-
- public function rollback($arguments = array())
- {
- $migrations = $this->resolver->last();
- if (count($migrations) == 0)
- {
- echo "Nothing to rollback.";
- return false;
- }
-
-
-
- foreach (array_reverse($migrations) as $migration)
- {
- $migration['migration']->down();
- echo 'Rolled back: '.$this->display($migration).PHP_EOL;
-
-
-
- $this->database->delete($migration['bundle'], $migration['name']);
- }
- return true;
- }
-
- public function reset($arguments = array())
- {
- while ($this->rollback()) {};
- }
-
- public function install()
- {
- Schema::table('laravel_migrations', function($table)
- {
- $table->create();
-
-
-
-
- $table->string('bundle', 50);
- $table->string('name', 200);
-
-
-
-
- $table->integer('batch');
- $table->primary(array('bundle', 'name'));
- });
- echo "Migration table created successfully.";
- }
-
- public function make($arguments = array())
- {
- if (count($arguments) == 0)
- {
- throw new \Exception("I need to know what to name the migration.");
- }
- list($bundle, $migration) = Bundle::parse($arguments[0]);
-
-
-
-
- $prefix = date('Y_m_d_His');
- $path = Bundle::path($bundle).'migrations'.DS;
-
-
-
- if ( ! is_dir($path)) mkdir($path);
- $file = $path.$prefix.'_'.$migration.EXT;
- File::put($file, $this->stub($bundle, $migration));
- echo "Great! New migration created!";
-
-
-
- return $file;
- }
-
- protected function stub($bundle, $migration)
- {
- $stub = File::get(path('sys').'cli/tasks/migrate/stub'.EXT);
- $prefix = Bundle::class_prefix($bundle);
-
-
-
- $class = $prefix.Str::classify($migration);
- return str_replace('{{class}}', $class, $stub);
- }
-
- protected function display($migration)
- {
- return $migration['bundle'].'/'.$migration['name'];
- }
- }
|