| @@ 14-69 (lines=56) @@ | ||
| 11 | /** |
|
| 12 | * MigrateCommand |
|
| 13 | */ |
|
| 14 | class MigrateCommand extends Command |
|
| 15 | { |
|
| 16 | use DbHelper; |
|
| 17 | ||
| 18 | /** |
|
| 19 | * @var string Table name where migrations info is kept |
|
| 20 | */ |
|
| 21 | const MIGRATIONS_TABLE = 'migrations'; |
|
| 22 | ||
| 23 | /** |
|
| 24 | * Configuration of command |
|
| 25 | */ |
|
| 26 | protected function configure() |
|
| 27 | { |
|
| 28 | $this |
|
| 29 | ->setName('migrate') |
|
| 30 | ->setDescription('Command for run migration') |
|
| 31 | ; |
|
| 32 | } |
|
| 33 | ||
| 34 | /** |
|
| 35 | * Execute method of command |
|
| 36 | * |
|
| 37 | * @param InputInterface $input |
|
| 38 | * @param OutputInterface $output |
|
| 39 | * |
|
| 40 | * @return void |
|
| 41 | */ |
|
| 42 | protected function execute(InputInterface $input, OutputInterface $output) |
|
| 43 | { |
|
| 44 | if (!is_dir(MIGRATIONS_PATH) || !is_readable(MIGRATIONS_PATH)) { |
|
| 45 | throw new \RunTimeException(sprintf('Migrations path `%s` is not good', MIGRATIONS_PATH)); |
|
| 46 | } |
|
| 47 | ||
| 48 | $output->writeln([ |
|
| 49 | '<info>Run migrations</info>', |
|
| 50 | sprintf('Ensure table `%s` presence', self::MIGRATIONS_TABLE) |
|
| 51 | ]); |
|
| 52 | ||
| 53 | try { |
|
| 54 | $this->safeCreateTable(self::MIGRATIONS_TABLE); |
|
| 55 | } catch (\Exception $e) { |
|
| 56 | $output->writeln([ |
|
| 57 | sprintf('Can\'t ensure table `%s` presence. Please verify DB connection params and presence of database named', self::MIGRATIONS_TABLE), |
|
| 58 | sprintf('Error: `%s`', $e->getMessage()), |
|
| 59 | ]); |
|
| 60 | } |
|
| 61 | ||
| 62 | $finder = new Finder(); |
|
| 63 | $finder->files()->name('*.php')->in(MIGRATIONS_PATH); |
|
| 64 | ||
| 65 | $this->runActions($finder, $output, self::MIGRATIONS_TABLE, 'up'); |
|
| 66 | ||
| 67 | return; |
|
| 68 | } |
|
| 69 | } |
|
| 70 | ||
| @@ 14-69 (lines=56) @@ | ||
| 11 | /** |
|
| 12 | * SeedCommand |
|
| 13 | */ |
|
| 14 | class SeedCommand extends Command |
|
| 15 | { |
|
| 16 | use DbHelper; |
|
| 17 | ||
| 18 | /** |
|
| 19 | * @var string Table name where migrations info is kept |
|
| 20 | */ |
|
| 21 | const SEEDS_TABLE = 'seeds'; |
|
| 22 | ||
| 23 | /** |
|
| 24 | * Configuration of command |
|
| 25 | */ |
|
| 26 | protected function configure() |
|
| 27 | { |
|
| 28 | $this |
|
| 29 | ->setName('seed') |
|
| 30 | ->setDescription('Command for run seed') |
|
| 31 | ; |
|
| 32 | } |
|
| 33 | ||
| 34 | /** |
|
| 35 | * Execute method of command |
|
| 36 | * |
|
| 37 | * @param InputInterface $input |
|
| 38 | * @param OutputInterface $output |
|
| 39 | * |
|
| 40 | * @return int|null|void |
|
| 41 | */ |
|
| 42 | protected function execute(InputInterface $input, OutputInterface $output) |
|
| 43 | { |
|
| 44 | if (!is_dir(SEEDS_PATH) || !is_readable(SEEDS_PATH)) { |
|
| 45 | throw new \RunTimeException(sprintf('Seeds path `%s` is not good', SEEDS_PATH)); |
|
| 46 | } |
|
| 47 | ||
| 48 | $output->writeln([ |
|
| 49 | '<info>Run seeds</info>', |
|
| 50 | sprintf('Ensure table `%s` presence', self::SEEDS_TABLE) |
|
| 51 | ]); |
|
| 52 | ||
| 53 | try { |
|
| 54 | $this->safeCreateTable(self::SEEDS_TABLE); |
|
| 55 | } catch (\Exception $e) { |
|
| 56 | $output->writeln([ |
|
| 57 | sprintf('Can\'t ensure table `%s` presence. Please verify DB connection params and presence of database named', self::SEEDS_TABLE), |
|
| 58 | sprintf('Error: `%s`', $e->getMessage()), |
|
| 59 | ]); |
|
| 60 | } |
|
| 61 | ||
| 62 | $finder = new Finder(); |
|
| 63 | $finder->files()->name('*.php')->in(SEEDS_PATH); |
|
| 64 | ||
| 65 | $this->runActions($finder, $output, self::SEEDS_TABLE, 'run'); |
|
| 66 | ||
| 67 | return; |
|
| 68 | } |
|
| 69 | } |
|
| 70 | ||