-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fc72940
Showing
25 changed files
with
1,379 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 生成数据填充文件 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
// | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.