Commit a43c0feb authored by abkrim's avatar abkrim 😀
Browse files

Refactor db-schema:doctrine-list and correct fail with tables with not indexes

parent 3be50148
Pipeline #280 failed with stages
in 1 minute
......@@ -21,4 +21,8 @@ All notable changes to `db-schema` will be documented in this file
## [1.0.2] - 2019-09-24
- Refactor doctrine-list for get options with Laravel model instead Symfony model
- Add option **--C|connection** to support an array of options
- Add option **--C|connections** to support an array of options
## [1.0.3] - 2019-09-25
- Refactor doctrine-list correct errors in **--C|connections**
- Correct error when table has not indexes
......@@ -27,9 +27,9 @@ php artisan vendor:publish --tag=config-db-schema
## Usage
**View or save a connection with version and information of each table**
**View or save a connections with version and information of each table**
New command for see or show schema and indexes for any connection fo our Laravel project, using Doctrine instead use several wrappers.
New command for see or show schema and indexes for any connections fo our Laravel project, using Doctrine instead use several wrappers.
Only tested with MySQL (not testing phpunit, testing in production) but is possible that work fine with Postgresql, sqlite,...
On this version options for this command are in Laravel mode instead Symfony Model. Check with --help
......@@ -72,7 +72,7 @@ Example output: Schema information in list
Avaliable Options in **show** and **list**:
* `--t=tableName` or `-t tableName` to provide table name or Namespace\\Model
* `--c=connectionName` or `-c connectionName` to provide connection name
* `--c=connectionName` or `-c connectionName` to provide connections name
......@@ -85,7 +85,7 @@ Example output: Tables name with rows count
![Tables name with rows count](https://gitlab.castris.com/root/db-schema/raw/master/screenshots/simple.png)
_Note: You may pass `--c=connectionName` or `-c connectionName` to display a specific connection dbSchema info_
_Note: You may pass `--c=connectionName` or `-c connectionName` to display a specific connections dbSchema info_
......@@ -104,7 +104,7 @@ Avaliable Options in **table**:
* `--p=pageNumber` or `-p PageNumber` to provide current page which you want to see
* `--o=orderBy` or `-o orderBy` to provide orderBy clause against a column (e.g: --o=id:desc or --o=id:asc [default asc]).
* `--l=rowsLimitPerPage` or `-l rowsLimitPerPage` to provide number of rows you want to display (e.g: --l=20).
* `--c=connectionName` or `-c connectionName` to provide connection name
* `--c=connectionName` or `-c connectionName` to provide connections name
* `--w=widthOfTableCell` or `-w widthOfTableCell` to provide character length of cell to show in tables (numeric [default=10]).
* `--s=columnName` to provide the column to display, you can provide comma (,) separated names to display (e.g: --s=name or --s=id,name).
......@@ -127,7 +127,7 @@ Example output: Query result will be dumped in console
Avaliable Options in **query**:
* `--r=yourRawQuery` or `-r yourRawQuery` to provide your raw sql query (e.g: --r="select * from someTable limit 20").
* `--c=connectionName` or `-c connectionName` to provide connection name (e.g: --c=mysql or -c sqlite)
* `--c=connectionName` or `-c connectionName` to provide connections name (e.g: --c=mysql or -c sqlite)
......
......@@ -15,13 +15,13 @@ class CreateDbSchemasTable extends Migration
{
Schema::create('db_schemas', function (Blueprint $table) {
$table->increments('id');
$table->string('connection',100)->nullable(false)->index();
$table->string('connections',100)->nullable(false)->index();
$table->string('table',100)->nullable(false)->index();
$table->string('version', 25)->nullable(false)->index();
$table->text('schema');
$table->timestamps();
$table->unique(['connection', 'table', 'version']);
$table->unique(['connections', 'table', 'version']);
});
}
......
......@@ -19,13 +19,6 @@
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" />
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<php>
<env name="DB_CONNECTION" value="testing" />
</php>
......
......@@ -18,7 +18,7 @@ class DoctrineListSchema extends Command
*
* @var string
*/
protected $signature = 'db-schema:doctrine-list {--T|table= : Table name} {--C|connection=* : Connection name}
protected $signature = 'db-schema:doctrine-list {--C|connections=* : Connection name} {--T|table= : Table name}
{--N|number= : Version number} {--S|save : Save schema version} {--F|force : Force save schema}';
/**
......@@ -30,46 +30,57 @@ class DoctrineListSchema extends Command
private $table_name;
private $connection;
private $connections = array();
private $db_tables = array();
public function handle()
{
// Save
//dd("hello",$this->options());
//change connection if provide
$this->setConnection();
//dd($this->options());
if ((($this->option('save') || ($this->option('force'))) && (is_null($this->option('number'))))) {
$this->error('If option save or force is active must be a version number');
exit();
}
//dd($this->option('connections'));
$this->setConnections();
// Get all tables
$this->setDbTables();
if (!count($this->db_tables)) {
$this->warn('Database does not contain any table');
$this->error('Database connection does not contain any table');
exit();
}
if (is_null($this->option('t'))) {
$this->info('Show all tables on '.$this->connection);
$all_tables = collect($this->db_tables);
$tables = $this->db_tables;
foreach ($tables as $table) {
$this->getSchemaTable($table);
if (is_null($this->option('table'))) {
foreach ($all_tables as $conn => $tables) {
foreach ($tables as $table) {
$this->getSchemaTable($table, $conn);
}
}
} else {
$this->getSchemaTable($this->option('t'));
if (! in_array($this->option('table'), $this->db_tables[$this->connections[0]])) {
$this->error('Connection ' . $this->connections[0] . ' has not a table '. $this->option('table'));
exit();
}
$this->getSchemaTable($this->option('table'), $this->connections[0]);
}
}
private function getSchemaTable(string $table)
private function getSchemaTable(string $table, string $connection)
{
$table_schemas = array();
$schema_indexes = array();
$schema_columns = array();
$sm = Schema::connection($this->connection)->getConnection()->getDoctrineSchemaManager();
$sm = Schema::connection($connection)->getConnection()->getDoctrineSchemaManager();
$indexes = $sm->listTableIndexes($table);
foreach ($indexes as $index) {
......@@ -109,20 +120,21 @@ class DoctrineListSchema extends Command
'columns' => $schema_columns
];
if ($this->option('s') == true) {
if (! is_null($this->option('e'))) {
$this->saveSchema($table, $table_schemas);
if ($this->option('save') == true) {
if (! is_null($this->option('number'))) {
$this->saveSchema($table, $connection, $table_schemas);
} else {
$this->warn('If use option "s" you must use option "-e" with version for save');
$this->error('If use option "-S" you must use option "-N" with version for save');
exit();
}
} else {
$this->showSchema($table, $table_schemas);
$this->showSchema($table, $connection, $table_schemas);
}
}
private function showSchema(string $table, array $table_schemas): void
private function showSchema(string $table, string $connection, array $table_schemas): void
{
$this->info("Show table: ". $table);
$this->info("Show table: ". $table . ' of connection '. $connection);
// Columns
$rows = $table_schemas['columns'];
......@@ -133,45 +145,71 @@ class DoctrineListSchema extends Command
$this->info("Indexes: ". $table);
// Indexes
// Some tables has not indexes. Lol
$rows = $table_schemas['indexes'];
$headers = (array_keys($rows[0]));
if (! empty($rows)) {
$headers = (array_keys($rows[0]));
$this->table($headers, $rows);
$this->table($headers, $rows);
}
}
private function saveSchema(string $table, array $table_schemas): void
private function saveSchema(string $table, string $connection, array $table_schemas): void
{
$this->info('Save table: ' . $table);
if ($this->option('f')) {
$this->saveOrUpdateTableSchema($table, $table_schemas);
$this->info('Save table: ' . $table . ' of connections ' . $connection);
if ($this->option('force')) {
$this->saveOrUpdateTableSchema($table, $connection, $table_schemas);
} else {
$this->saveTableSchema($table, $table_schemas);
$this->saveTableSchema($table, $connection, $table_schemas);
}
}
private function setConnection(): void
private function setConnections(): void
{
if (! is_null($this->option('c'))) {
$this->connection = $this->option('c');
if (! empty($this->option('connections'))) {
$this->connections = $this->option('connections');
} else {
$this->connection = config('database.default');
$this->connections[] = config('database.default');
}
if (! Arr::has(config('database.connections'), $this->connection)) {
$this->error('Incorrect option database connection');
exit;
$this->checkConnections();
}
private function checkConnections(): void
{
$conns = $this->connections;
foreach ($conns as $conn) {
if (! Arr::has(config('database.connections'), $conn)) {
$this->error('Incorrect option database connections: '. $conn);
exit;
}
}
}
private function setDbTables(): void
{
$sm_tables = Schema::connection($this->connection)->getAllTables();
$conns = $this->connections;
foreach ($conns as $conn) {
$sm_tables = Schema::connection($conn)->getAllTables();
$sm_db = 'Tables_in_'.config('database.connections.' . $this->connection . '.database');
$sm_db = 'Tables_in_'.config('database.connections.' . $conn . '.database');
foreach ($sm_tables as $sm_table) {
$this->db_tables[] = $sm_table->$sm_db;
foreach ($sm_tables as $sm_table) {
$this->db_tables[$conn][] = $sm_table->$sm_db;
}
}
$this->checkTables();
}
private function checkTables(): void
{
if (empty($this->db_tables)) {
$this->error('Not table(s) for processing');
exit;
}
}
......@@ -189,36 +227,42 @@ class DoctrineListSchema extends Command
/**
* @param string $table
* @param string $connection
* @param array $table_schemas
*/
private function saveTableSchema(string $table, array $table_schemas): void
private function saveTableSchema(string $table, string $connection, array $table_schemas): void
{
$now = Carbon::now();
try {
DB::table(config('db-schema.table'))->insert([
'connection' => config('database.connections.' . $this->connection . '.database'),
'connection' => config('database.connections.' . $connection . '.database'),
'table' => $table,
'version' => $this->option('e'),
'version' => $this->option('number'),
'schema' => json_encode($table_schemas),
'created_at' => $now,
'updated_at' => $now
]);
} catch (\Exception $e) {
$this->warn("Some connection+table+version is already in system");
$this->warn("Some connections+table+version is already in system");
echo $e->getMessage().PHP_EOL; //Integrity constraint violation:
}
}
private function saveOrUpdateTableSchema(string $table, array $table_schemas): void
/**
* @param string $table
* @param string $connection
* @param array $table_schemas
*/
private function saveOrUpdateTableSchema(string $table, string $connection, array $table_schemas): void
{
$now = Carbon::now();
DB::table(config('db-schema.table'))->updateOrInsert(
[
'connection' => config('database.connections.' . $this->connection . '.database'),
'connection' => config('database.connections.' . $connection . '.database'),
'table' => $table,
'version' => $this->option('e')
'version' => $this->option('number')
],
[
'schema' => json_encode($table_schemas),
......
......@@ -63,7 +63,7 @@ class ListSchema extends Command
*/
public function showSchemaInList()
{
//change connection if provide
//change connections if provide
$this->setConnection();
$this->setDbTables();
......
......@@ -20,7 +20,7 @@ class MonitorSchema extends Command
*
* @var string
*/
protected $description = 'Display database performance status (read/write/connection etc)';
protected $description = 'Display database performance status (read/write/connections etc)';
/**
* Dbschema package version
......@@ -56,7 +56,7 @@ class MonitorSchema extends Command
*/
public function handle()
{
//change connection if provide
//change connections if provide
if ($this->option('c')) {
$this->dbschema->setConnection($this->option('c'));
$this->dbschema->switchWrapper();
......
......@@ -43,7 +43,7 @@ class QuerySchema extends Command
*/
public function performQuery()
{
//change connection if provide
//change connections if provide
if ($this->option('c')) {
$this->dbSchema->setConnection($this->option('c'));
$this->dbSchema->switchWrapper();
......
......@@ -47,7 +47,7 @@ class ShowSchema extends Command
*/
public function showSchemaInTable()
{
//change connection if provide
//change connections if provide
if ($this->option('c')) {
$this->dbschema->setConnection($this->option('c'));
$this->dbschema->switchWrapper();
......
......@@ -47,7 +47,7 @@ class SimpleSchema extends Command
*/
public function showSchemaInSimpleTable()
{
//change connection if provide
//change connections if provide
if ($this->option('c')) {
$this->dbSchema->setConnection($this->option('c'));
$this->dbSchema->switchWrapper();
......
......@@ -49,7 +49,7 @@ class TableSchema extends Command
*/
public function showSchemaDefinitionInTable()
{
//change connection if provide
//change connections if provide
if ($this->option('c')) {
$this->dbschema->setConnection($this->option('c'));
$this->dbschema->switchWrapper();
......
<?php
namespace Abkrim\DbSchema;
class DBSchemaDoctrine
{
public function setConnections($array)
{
}
}
......@@ -27,7 +27,8 @@ class DbSchemaServiceProvider extends ServiceProvider
MonitorSchema::class,
QuerySchema::class,
SimpleSchema::class,
TableSchema::class
TableSchema::class,
DoctrineListSchema::class
]);
$this->publishes([
......
......@@ -29,7 +29,7 @@ abstract class BaseDbSchema
}
/**
* Set connection
* Set connections
* @param $connection
* @return $this
*/
......
......@@ -7,6 +7,8 @@ use Abkrim\DbSchema\DbSchemaServiceProvider;
use Abkrim\DbSchema\DbSchemaFacade;
use Faker\Factory as Faker;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Orchestra\Testbench\TestCase;
......@@ -60,17 +62,40 @@ class DbSchemaTest extends TestCase
DB::table('db_schemas')->insert([
[
'connection' => 'mysql',
'connections' => 'mysql',
'table' => 'table1',
'version' => '0.9.1'
],
[
'connection' => 'mysql',
'connections' => 'mysql',
'table' => 'table1',
'version' => '0.9.1'
],
]);
}
/** @test */
function it_command_without_any_options_get_connection_default()
{
$this->artisan('db-schema:doctrine-list');
// $output = Artisan::all();
//
// dd($output);
$connections = [
'mysql' => [
'driver' => 'mysql',
],
'mysql_vmail' => [
'driver' => 'mysql',
],
];
Config::set('database.default', 'mysql');
Config::set('database.connections', $connections);
dd(config('database'));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment