Commit 6e54309c authored by abkrim's avatar abkrim 😀
Browse files

0.1.0 version for Laravel 6

parent 59d86265
......@@ -2,6 +2,7 @@
All notable changes to `db-schema` will be documented in this file
## 1.0.0 - 201X-XX-XX
## 0.1.0 - 2019-09-09
- initial release for Laravel 6 without tests. ALl commands tested in brute. Several changes over original for compatibilty with Laravel 6
- initial release
......@@ -5,21 +5,21 @@
[![coverage report](https://gitlab.castris.com/root/db-schema/badges/master/coverage.svg)](https://gitlab.castris.com/root/db-schema/commits/master)
[![Total Downloads](https://img.shields.io/packagist/dt/abkrim/db-schema.svg?style=flat-square)](https://packagist.org/packages/abkrim/db-schema)
This package will help to display database schema information from terminal or save in file
This package will help to display database dbSchema information from terminal or save in file
## Installation
You can install the package via composer:
```bash
composer require --dev abkrim/db-schema
composer require --dev abkrim/db-dbSchema
```
## Usage
**Show Schema information in tabular form**
```bash
php artisan schema:show
php artisan db-dbSchema:show
```
Example output: Schema information in tabular form
......@@ -28,7 +28,7 @@ Example output: Schema information in tabular form
If you want to see a specific table then pass table name or Namespace\\\Model
```bash
php artisan schema:show --t=tableName or --t=Namespace\\Model
php artisan db-dbSchema:show --t=tableName or --t=Namespace\\Model
```
![database info commandline](./screenshots/tabular-single.png)
......@@ -38,7 +38,7 @@ _Note: Same condition will be applied for tables **list** view_
**Show Schema information in List**
```bash
php artisan schema:list
php artisan db-dbSchema:list
```
Example output: Schema information in list
......@@ -54,20 +54,20 @@ Avaliable Options in **show** and **list**:
**Show Table names and total rows**
```bash
php artisan schema:simple
php artisan db-dbSchema:simple
```
Example output: Tables name with rows count
![Tables name with rows count](./screenshots/simple.png)
_Note: You may pass `--c=connectionName` or `-c connectionName` to display a specific connection schema info_
_Note: You may pass `--c=connectionName` or `-c connectionName` to display a specific connection dbSchema info_
**Show table definition**
```bash
php artisan schema:table --t=tableName or --t=Namespace\\Model
php artisan db-dbSchema:table --t=tableName or --t=Namespace\\Model
```
Example output: Table definition with default page and limit
......@@ -86,14 +86,14 @@ Avaliable Options in **table**:
```bash
php artisan schema:table --t=countries --p=4 --o=id:desc --l=25
php artisan db-dbSchema:table --t=countries --p=4 --o=id:desc --l=25
```
**Perform raw sql query**
```bash
php artisan schema:query --r="your sql query"
php artisan db-dbSchema:query --r="your sql query"
```
Example output: Query result will be dumped in console
......@@ -109,7 +109,7 @@ Avaliable Options in **query**:
**Monitor database server**
```bash
php artisan schema:monitor
php artisan db-dbSchema:monitor
```
Example output: Showing the database status
......@@ -118,7 +118,7 @@ Example output: Showing the database status
You can pass --i=integerNumber as refresh time interval and --c=ConnectionName as well
```bash
php artisan schema:monitor --i=3 --c=secondaryDatabase
php artisan db-dbSchema:monitor --i=3 --c=secondaryDatabase
```
......@@ -130,7 +130,7 @@ composer test
### Laravel versions
This package is only tested in Laravel 6.0 and is possible that work in all version since 5.4
This package is only tested in Laravel 6.0 (not test at now, but are coming soon). For older versión try use [original package](https://github.com/thedevsaddam/laravel-schema)
For older versions that do not have auto discover providers you may need to add service provider.
......@@ -150,7 +150,8 @@ If you discover any security related issues, please email abdelkarim.mateos@cast
## Credits
- [AbdelKarim Mateos](https://github.com/abkrim)
- [Saddam H](thedevsaddam@gmail.com) [Original package](https://github.com/thedevsaddam/laravel-schema)
- [AbdelKarim Mateos](https://castris.com)
- [All Contributors](../../contributors)
## License
......
To Do
=====
1. MonitorSchema need method refresh time & a config value
......@@ -3,9 +3,11 @@
"description": "A fork of thedevsaddam/laravel-schema of Saddam H thedevsaddam@gmail.com with some improvements and maintained",
"keywords": [
"abkrim",
"db-schema"
"db-schema",
"schema",
"database"
],
"homepage": "https://github.com/abkrim/db-schema",
"homepage": "https://gitlab.castris.com/root/db-schema",
"license": "MIT",
"type": "library",
"authors": [
......@@ -17,7 +19,8 @@
],
"require": {
"php": "^7.1",
"illuminate/support": "5.8.*"
"laravel/framework": "^6.0",
"ext-json": "*"
},
"require-dev": {
"orchestra/testbench": "3.8.*",
......
<?php
namespace Abkrim\DbSchema\Console\Commands;
use DB;
use Illuminate\Console\Command;
class HelpSchema extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'db-dbSchema:help';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Display help message for this package.';
/**
* Schema package version
* @var
*/
protected $version;
/**
* Help table headers
* @var array
*/
protected $headers = ["Method Name", "Usage"];
/**
* Help table body
* @var array
*/
protected $body = [];
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->showHelp();
}
/**
* Display dbSchema help information in a table
* @return bool
*/
public function showHelp()
{
$this->generateBody();
$this->info('Laravel DbSchema version: ' . $this->version);
$this->table($this->headers, $this->body);
$this->comment('Visit https://packagist.org/packages/thedevsaddam/laravel-dbSchema for more details.');
}
/**
* Generate the table body
*/
private function generateBody()
{
$data = $this->readHelpGuide();
$this->version = $data['version'];
foreach ($data['help'] as $key => $value) {
$data = [$key, $value];
array_push($this->body, $data);
}
}
/**
* Read help.json file
* @return mixed
*/
private function readHelpGuide()
{
try {
$help = file_get_contents(realpath(__DIR__ . "/../../Schema/help.json"));
return json_decode($help, true);
} catch (\Exception $e) {
$this->error($e->getMessage());
}
}
}
<?php
namespace Abkrim\DbSchema\Console\Commands;
use Illuminate\Console\Command;
use Abkrim\DbSchema\Schema\Helper;
use Abkrim\DbSchema\DbSchema;
use Symfony\Component\Console\Input\InputOption;
class ListSchema extends Command
{
use Helper;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'db-dbSchema:list';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Display connected database db-dbSchema information in list';
private $dbschema;
public function __construct(DbSchema $dbschema)
{
parent::__construct();
$this->dbschema = $dbschema;
}
/**
* Execute the console command.
*
* @return mixed
* @throws \Exception
*/
public function handle()
{
$this->showSchemaInList();
}
/**
* @return bool
* @throws \Exception
*/
public function showSchemaInList()
{
//change connection if provide
if ($this->option('c')) {
$this->dbschema->setConnection($this->option('c'));
$this->dbschema->switchWrapper();
}
$tables = $this->dbschema->databaseWrapper->getTables();
if (!count($tables)) {
$this->warn('Database does not contain any table');
}
$tableName = $this->option('t');
if (!empty($tableName)) {
if ($this->isNamespaceModel($tableName)) {
$tableName = $this->tableNameFromModel($tableName);
}
if (!in_array($tableName, $tables)) {
$this->warn('Table name is not correct!');
return false;
}
$attributes = $this->dbschema->databaseWrapper->getColumns($tableName);
$rowsCount = $this->dbschema->getTableRowCount($tableName);
$this->info($tableName . ' (rows: ' . $rowsCount . ')');
foreach ($attributes as $attribute) {
$this->line(' ' . $attribute['Field'] . ' ' . $attribute['Type']);
}
return true;
}
foreach ($this->dbschema->databaseWrapper->getSchema() as $key => $value) {
$this->info($key . ' (rows: ' . $value['rowsCount'] . ')');
foreach ($value['attributes'] as $attribute) {
$this->line(' ' . $attribute['Field'] . ' ' . $attribute['Type']);
}
$this->line('');
}
}
protected function getOptions()
{
return [
['t', 't', InputOption::VALUE_OPTIONAL, 'Table name'],
['c', 'c', InputOption::VALUE_OPTIONAL, 'Connection name'],
];
}
}
<?php
namespace Abkrim\DbSchema\Console\Commands;
use Abkrim\DbSchema\DbSchema;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
class MonitorSchema extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'db-dbSchema:monitor';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Display database performance status (read/write/connection etc)';
/**
* Dbschema package version
* @var
*/
protected $version;
/**
* Help table headers
* @var array
*/
protected $headers = ["Query", "Value", "Create", "Value", "Alter", "Value", "Drop", "Value"];
/**
* Help table body
* @var array
*/
protected $body = [];
private $dbschema;
public function __construct(DbSchema $dbschema)
{
parent::__construct();
$this->dbschema = $dbschema;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//change connection if provide
if ($this->option('c')) {
$this->dbschema->setConnection($this->option('c'));
$this->dbschema->switchWrapper();
if ($this->dbschema->connection !== 'mysql') {
$this->warn('Monitoring in currently supporting for mysql and updating.');
return false;
}
}
$timeInterval = 2;
if (null !== $this->option('i') & is_integer($this->option('i'))) {
$timeInterval = $this->option('i');
}
for ($i = 1; ; $i++) {
$this->info("Fetching ($i):");
$this->table($this->headers, $this->dbschema->databaseWrapper->showDatabaseStatus($timeInterval));
$this->line('');
}
}
protected function getOptions()
{
return [
['i', 'i', InputOption::VALUE_OPTIONAL, 'Interval time'],
['c', 'c', InputOption::VALUE_OPTIONAL, 'Connection name']
];
}
}
<?php
namespace Abkrim\DbSchema\Console\Commands;
use Abkrim\DbSchema\DbSchema;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
class QuerySchema extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'db-dbSchema:query';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Raw sql query to database';
private $dbSchema;
public function __construct(DbSchema $dbDbSchema)
{
parent::__construct();
$this->dbSchema = $dbDbSchema;
}
public function handle()
{
$this->performQuery();
}
/**
* Perform raw sql query
* @return bool
*/
public function performQuery()
{
//change connection if provide
if ($this->option('c')) {
$this->dbSchema->setConnection($this->option('c'));
$this->dbSchema->switchWrapper();
}
$rawQuery = $this->option('r');
if (empty($rawQuery)) {
$this->warn('Please provide raw sql query as string (in single/double quote)!');
return false;
}
$result = $this->dbSchema->rawQuery($rawQuery);
if (!!$result) {
dump($result);
}
$this->info('Query executed successfully!');
}
protected function getOptions()
{
return [
['r', 'r', InputOption::VALUE_OPTIONAL, 'Raw sql query'],
['c', 'c', InputOption::VALUE_OPTIONAL, 'Connection name'],
];
}
}
<?php
namespace Abkrim\DbSchema\Console\Commands;
use Abkrim\DbSchema\DbSchema;
use Abkrim\DbSchema\Schema\Helper;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
class ShowSchema extends Command
{
use Helper;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'db-dbSchema:show';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Display connected database dbSchema information in tabular form';
private $dbschema;
public function __construct(DbSchema $dbSchema)
{
parent::__construct();
$this->dbschema = $dbSchema;
}
/**
* @throws \Exception
*/
public function handle()
{
$this->showSchemaInTable();
}
/**
* @return bool
* @throws \Exception
*/
public function showSchemaInTable()
{
//change connection if provide
if ($this->option('c')) {
$this->dbschema->setConnection($this->option('c'));
$this->dbschema->switchWrapper();
}
$tables = $this->dbschema->databaseWrapper->getTables();
$headers = $this->dbschema->headers;
if (!count($tables)) {
$this->warn('Database does not contain any table');
}
$tableName = $this->option('t');
if ($tableName) {
if ($this->isNamespaceModel($tableName)) {
$tableName = $this->tableNameFromModel($tableName);
}
if (!in_array($tableName, $tables)) {
$this->warn('Table name is not correct!');
return false;
}
$body = $this->dbschema->databaseWrapper->getColumns($tableName);
$rowsCount = $this->dbschema->getTableRowCount($tableName);
$this->info($tableName . ' (rows: ' . $rowsCount . ')');
$this->table($headers, $body);
return true;
}
foreach ($this->dbschema->databaseWrapper->getSchema() as $key => $value) {
$this->info($key . ' (rows: ' . $value['rowsCount'] . ')');
$this->table($headers, $value['attributes']);
$this->line('');
}
}
protected function getOptions()
{
return [
['t', 't', InputOption::VALUE_OPTIONAL, 'Table name'],
['c', 'c', InputOption::VALUE_OPTIONAL, 'Connection name'],