Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

112 rindas
3.2KB

  1. <?php
  2. namespace Framework\Database\Command;
  3. use Framework\Database\Factory;
  4. use Framework\Database\Connection\Connection;
  5. use Framework\Database\Connection\MysqlConnection;
  6. use Framework\Database\Connection\SqliteConnection;
  7. use Symfony\Component\Console\Command\Command;
  8. use Symfony\Component\Console\Input\InputArgument;
  9. use Symfony\Component\Console\Input\InputInterface;
  10. use Symfony\Component\Console\Input\InputOption;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. class MigrateCommand extends Command
  13. {
  14. protected static $defaultName = 'migrate';
  15. protected function configure()
  16. {
  17. $this
  18. ->setDescription('Migrates the database')
  19. ->addOption('fresh', null, InputOption::VALUE_NONE, 'Delete all tables before running the migrations')
  20. ->setHelp('This command looks for all migration files and runs them');
  21. }
  22. protected function execute(InputInterface $input, OutputInterface $output)
  23. {
  24. $current = getcwd();
  25. $pattern = 'database/migrations/*.php';
  26. $paths = glob("{$current}/{$pattern}");
  27. if (count($paths) < 1) {
  28. $this->writeln('No migrations found');
  29. return Command::SUCCESS;
  30. }
  31. // $connection = $this->connection();
  32. $connection = app('database');
  33. if ($input->getOption('fresh')) {
  34. $output->writeln('Dropping existing database tables');
  35. $connection->dropTables();
  36. // $connection = $this->connection();
  37. $connection = app('database');
  38. }
  39. $applied = [];
  40. if (!$connection->hasTable('migrations')) {
  41. $output->writeln('Creating migrations table');
  42. $this->createMigrationsTable($connection);
  43. } else {
  44. $applied = array_column(
  45. $connection->query()->from('migrations')->select(['name'])->all(),
  46. 'name'
  47. );
  48. }
  49. foreach ($paths as $path) {
  50. [$prefix, $file] = explode('_', $path);
  51. [$class, $extension] = explode('.', $file);
  52. if (in_array($class, $applied)) {
  53. continue;
  54. }
  55. require $path;
  56. $output->writeln("Migrating: {$class}");
  57. $obj = new $class();
  58. $obj->migrate($connection);
  59. $connection
  60. ->query()
  61. ->from('migrations')
  62. ->insert(['name'], ['name' => $class]);
  63. }
  64. return Command::SUCCESS;
  65. }
  66. // private function connection(): Connection
  67. // {
  68. // $factory = new Factory();
  69. // $factory->addConnector('mysql', function($config) {
  70. // return new MysqlConnection($config);
  71. // });
  72. // $factory->addConnector('sqlite', function($config) {
  73. // return new SqliteConnection($config);
  74. // });
  75. // $config = require getcwd() . '/config/database.php';
  76. // return $factory->connect($config[$config['default']]);
  77. // }
  78. private function createMigrationsTable(Connection $connection)
  79. {
  80. $table = $connection->createTable('migrations');
  81. $table->id('id');
  82. $table->string('name');
  83. $table->execute();
  84. }
  85. }

Powered by TurnKey Linux.