<?php
use think\migration\Migrator;
use Phinx\Db\Adapter\AdapterFactory;
class CreateRulesTable extends Migrator
{
protected function init()
{
$options = $this->getDbConfig();
$adapter = AdapterFactory::instance()->getAdapter($options['adapter'], $options);
if ($adapter->hasOption('table_prefix') || $adapter->hasOption('table_suffix')) {
$adapter = AdapterFactory::instance()->getWrapper('prefix', $adapter);
}
$this->setAdapter( $adapter);
}
protected function getDbConfig(): array
{
$default = config('tauthz.database.connection') ?: config('database.default');
$config = config("database.connections.{$default}");
if (0 == $config['deploy']) {
$dbConfig = [
'adapter' => $config['type'],
'host' => $config['hostname'],
'name' => $config['database'],
'user' => $config['username'],
'pass' => $config['password'],
'port' => $config['hostport'],
'charset' => $config['charset'],
'table_prefix' => $config['prefix'],
];
} else {
$dbConfig = [
'adapter' => explode(',', $config['type'])[0],
'host' => explode(',', $config['hostname'])[0],
'name' => explode(',', $config['database'])[0],
'user' => explode(',', $config['username'])[0],
'pass' => explode(',', $config['password'])[0],
'port' => explode(',', $config['hostport'])[0],
'charset' => explode(',', $config['charset'])[0],
'table_prefix' => explode(',', $config['prefix'])[0],
];
}
$table = config('database.migration_table', 'migrations');
$dbConfig['default_migration_table'] = $dbConfig['table_prefix'] . $table;
return $dbConfig;
}
public function up()
{
$default = config('tauthz.default');
$table = $this->table(config('tauthz.enforcers.'.$default.'.database.rules_name'));
$table->addColumn('ptype', 'string', ['null' => true])
->addColumn('v0', 'string', ['null' => true])
->addColumn('v1', 'string', ['null' => true])
->addColumn('v2', 'string', ['null' => true])
->addColumn('v3', 'string', ['null' => true])
->addColumn('v4', 'string', ['null' => true])
->addColumn('v5', 'string', ['null' => true])
->create();
}
public function down()
{
$default = config('tauthz.default');
$table = $this->table(config('tauthz.enforcers.'.$default.'.database.rules_name'));
$table->drop();
}
}