Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pxianyu committed Mar 8, 2023
0 parents commit fc72940
Show file tree
Hide file tree
Showing 25 changed files with 1,379 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.idea
/.vscode
/vendor
*.log
.env
/tests/tmp
/tests/.phpunit.result.cache
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# webman-migration
和laravel migration 使用方法类似
- php webman migrate:created create_users_table 生成迁移文件
- php webman migrate:migrate 执行迁移
- php webman migrate:rollback 回滚迁移
- php webman migrate:status 查看迁移状态
- php webman migrate:fresh
- php webman seed:run 执行数据填充
- php webman seed:created UserSeeder 生成数据填充文件
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "pxianyu/migrations",
"type": "library",
"license": "MIT",
"description": "Webman plugin eloquent-ORM migrations",
"minimum-stability": "stable",
"require": {
"php": ">=8.1",
"workerman/webman-framework": "^1.5.0",
"webman/console": "^1.2",
"illuminate/database": "^10.0",
"illuminate/console": "^10.0",
"illuminate/filesystem": "^10.0"
},
"autoload": {
"psr-4": {
"Eloquent\\Migrations\\": "src"
}
},
"config": {
"sort-packages": true
}
}
17 changes: 17 additions & 0 deletions data/Seeder.php.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace database\seeders;
use Eloquent\Migrations\Seeds\Seeder;

class {{ class }} extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run() :void
{
// Insert your seed data here
}
}
24 changes: 24 additions & 0 deletions data/elmigrator.php.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

$capsule = new \Illuminate\Database\Capsule\Manager();
$capsule->addConnection([
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'pm',
'username' => 'pm',
'password' => 'thepmpassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'schema' => 'public'
]);

return [
'default_environment' => 'developpment',
'paths' => [
'migrations' => 'database/migrations',
'seeds' => 'database/seeders',
],
'migration_table' => 'el_migrations',
'db' => $capsule->getDatabaseManager()
];
25 changes: 25 additions & 0 deletions data/stubs/migration.create.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Eloquent\Migrations\Migrations\Migration;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
$this->schema()->create('{{ table }}', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
$this->schema()->dropIfExists('{{ table }}');
}
};
28 changes: 28 additions & 0 deletions data/stubs/migration.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Eloquent\Migrations\Migrations\Migration;

return new class extends Migration
{
/**
* Enables, if supported, wrapping the migration within a transaction.
*/
//public bool $withinTransaction = false;
// Un comment if you want the migration not to run inside a transaction

/**
* Run the migrations.
*/
public function up(): void
{
//
}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
}
33 changes: 33 additions & 0 deletions data/stubs/migration.update.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Eloquent\Migrations\Migrations\Migration;

return new class extends Migration
{
/**
* Enables, if supported, wrapping the migration within a transaction.
*/
//public bool $withinTransaction = false;
// Un comment if you want the migration not to run inside a transaction

/**
* Run the migrations.
*/
public function up(): void
{
$this->schema()->table('{{ table }}', function (Blueprint $table) {
//
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
$this->schema()->table('{{ table }}', function (Blueprint $table) {
//
});
}
}
149 changes: 149 additions & 0 deletions src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace Eloquent\Migrations\Command;

use Closure;
use Illuminate\Database\DatabaseManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Input\ArrayInput;

abstract class AbstractCommand extends Command
{
protected string $configFile;
protected array $config;
protected InputInterface $input;
protected OutputInterface $output;
protected string $environment;
protected ?string $database;

protected function configure()
{
$this->addOption('config', '-c', InputOption::VALUE_REQUIRED, 'The configuration file to load', 'elmigrator.php');
$this->addOption('env', '-e', InputOption::VALUE_OPTIONAL, 'Choose an environment');
$this->addOption('database', '-d', InputOption::VALUE_OPTIONAL, 'The database connection to use');
}

protected function bootstrap(InputInterface $input, OutputInterface $output): void
{
$this->input = $input;
$this->output = $output;
$this->loadConfig($input);
}

protected function loadConfig(InputInterface $input): void
{
$this->configFile = (string)$input->getOption('config');
$this->config = config('plugin.eloquent.migrations.app');
$this->environment = $this->config['default_environment'];
$this->database = $this->config['database'] ?? null;

if ($this->configFile === null) {
$this->output->writeln('<danger>could not find nothing configuration a file. Set throught --config option or environment variable ELMIGRATOR_CONFIG</danger>');
}
}

protected function getMigrationPath(): string
{
return (string)$this->config['paths']['migrations'];
}

protected function getSeedPath(): string
{
return (string)$this->config['paths']['seeds'];
}

protected function getDb(): DatabaseManager
{
return $this->config['db'];
}

protected function environment(): string
{
return $this->environment;
}

protected function getMigrationTable(): string
{
return (string)$this->config['migration_table'];
}

protected function table(array $headers, array $contents)
{
$table = new Table($this->output);
$table->setHeaders($headers)
->setRows($contents)
->render();
}

public function confirm(string $message): bool
{
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion($message, false);

if (!$helper->ask($this->input, $this->output, $question)) {
return false;
}
return true;
}

/**
* Confirm before proceeding with the action.
*
* This method only asks for confirmation in production.
*
* @param string $warning
* @param Closure|bool|null $callback
* @return bool
*/
public function confirmToProceed(string $warning = 'Application In Production!', $callback = null): bool
{
$callback = is_null($callback) ? $this->getDefaultConfirmCallback() : $callback;
$shouldConfirm = $callback instanceof Closure ? call_user_func($callback) : $callback;
if ($shouldConfirm) {
if ($this->input->hasOption('force') && $this->input->getOption('force')) {
return true;
}
$this->output->writeln("<fg=yellow>$warning</>");
$confirmed = $this->confirm('Do you really wish to run this command?');
if (! $confirmed) {
$this->output->writeln('<comment>Command Cancelled!</comment>');
return false;
}
}
return true;
}

/**
* Get the default confirmation callback.
*
* @return Closure
*/
protected function getDefaultConfirmCallback(): Closure
{
return function () {
return $this->environment() === 'production';
};
}

/**
* Call another console command.
*
* @param string $command
* @param array $arguments
* @return int
*/
public function call(string $command, array $arguments = []): int
{
$arguments['command'] = $command;
$arguments['--config'] = $this->configFile;
return $this->getApplication()->find($command)->run(
new ArrayInput($arguments),
$this->output
);
}
}
30 changes: 30 additions & 0 deletions src/Command/CreateDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Eloquent\Migrations\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CreateDatabase extends AbstractCommand
{
protected static $defaultName = 'create:database';

protected function configure()
{
$this
->setDescription('Create a database')
->addArgument('name', InputArgument::REQUIRED, 'The database name')
->setHelp('Creates a database' . PHP_EOL);

parent::configure();
}

public function execute(InputInterface $input, OutputInterface $output)
{
$this->bootstrap($input, $output);
$this->getDb()->statement('CREATE DATABASE :database', ['database' => $input->getArgument('name')]);

return 0;
}
}
Loading

0 comments on commit fc72940

Please sign in to comment.