Skip to content

Commit

Permalink
Merge pull request #4 from giagara/main
Browse files Browse the repository at this point in the history
Fix error in PR #3 and refactoring
  • Loading branch information
Sairahcaz authored Jul 19, 2023
2 parents 449e8ff + 4da639e commit 710208f
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 136 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ php artisan schema:generate-rules persons -cf --file Api\\V1\\StorePersonRequest
To always skip columns add it in the config file, under `skip_columns` parameter.

```php
'skip_columns' => ['created_at', 'updated_at', 'deleted_at', 'whatever'],
'skip_columns' => ['whatever', 'some_other_column'],
```


Expand Down
4 changes: 2 additions & 2 deletions config/schema-rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
'string_min_length' => env('SCHEMA_RULES_STRING_MIN_LENGTH', 1),

/**
* Always skip this columns (usefule for created_at, updated_at, deleted_at)
* Always skip this columns
*/
'skip_columns' => []
'skip_columns' => ['created_at', 'updated_at', 'deleted_at'],

];
2 changes: 1 addition & 1 deletion src/Commands/GenerateRulesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use LaracraftTech\LaravelSchemaRules\Exceptions\FailedToCreateRequestClassException;
use LaracraftTech\LaravelSchemaRules\Exceptions\MultipleTablesSuppliedException;
use LaracraftTech\LaravelSchemaRules\Exceptions\TableDoesNotExistException;
use LaracraftTech\LaravelSchemaRules\Resolvers\SchemaRulesResolverInterface;
use LaracraftTech\LaravelSchemaRules\Contracts\SchemaRulesResolverInterface;

class GenerateRulesCommand extends Command
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace LaracraftTech\LaravelSchemaRules\Resolvers;
namespace LaracraftTech\LaravelSchemaRules\Contracts;

interface SchemaRulesResolverInterface
{
Expand Down
2 changes: 1 addition & 1 deletion src/LaravelSchemaRulesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use LaracraftTech\LaravelSchemaRules\Commands\GenerateRulesCommand;
use LaracraftTech\LaravelSchemaRules\Exceptions\UnsupportedDbDriverException;
use LaracraftTech\LaravelSchemaRules\Resolvers\SchemaRulesResolverInterface;
use LaracraftTech\LaravelSchemaRules\Contracts\SchemaRulesResolverInterface;
use LaracraftTech\LaravelSchemaRules\Resolvers\SchemaRulesResolverMySql;
use LaracraftTech\LaravelSchemaRules\Resolvers\SchemaRulesResolverPgSql;
use LaracraftTech\LaravelSchemaRules\Resolvers\SchemaRulesResolverSqlite;
Expand Down
69 changes: 69 additions & 0 deletions src/Resolvers/BaseSchemaRulesResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace LaracraftTech\LaravelSchemaRules\Resolvers;

use LaracraftTech\LaravelSchemaRules\Contracts\SchemaRulesResolverInterface;
use stdClass;

abstract class BaseSchemaRulesResolver implements SchemaRulesResolverInterface
{

private string $table;
private array $columns;

public function __construct(string $table, array $columns = [])
{
$this->table = $table;
$this->columns = $columns;
}

public function generate(): array
{
$tableColumns = $this->getColumnsDefinitionsFromTable();

$skip_columns = config('schema-rules.skip_columns');

$tableRules = [];
foreach ($tableColumns as $column) {
$field = $this->getField($column);

// If specific columns where supplied only process those...
if (! empty($this->columns()) && ! in_array($field, $this->columns())) {
continue;
}

// If column should be skipped
if (in_array($field, $skip_columns)) {
continue;
}

// We do not need a rule for auto increments
if ($this->isAutoIncrement($column)) {
continue;
}

$tableRules[$field] = $this->generateColumnRules($column);
}

return $tableRules;
}

protected function table()
{
return $this->table;
}

protected function columns()
{
return $this->columns;
}

abstract protected function isAutoIncrement($column) : bool;

abstract protected function getField($column) : string;

abstract protected function getColumnsDefinitionsFromTable();

abstract protected function generateColumnRules(stdClass $column): array;

}
59 changes: 16 additions & 43 deletions src/Resolvers/SchemaRulesResolverMySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use LaracraftTech\LaravelSchemaRules\Contracts\SchemaRulesResolverInterface;
use stdClass;

class SchemaRulesResolverMySql implements SchemaRulesResolverInterface
class SchemaRulesResolverMySql extends BaseSchemaRulesResolver implements SchemaRulesResolverInterface
{
private string $table;
private array $columns;

public static array $integerTypes = [
'tinyint' => [
Expand All @@ -34,47 +33,10 @@ class SchemaRulesResolverMySql implements SchemaRulesResolverInterface
],
];

public function __construct(string $table, array $columns = [])
{
$this->table = $table;
$this->columns = $columns;
}

public function generate(): array
{
$tableColumns = $this->getColumnsDefinitionsFromTable();

$skip_columns = config('schema-rules.skip_columns');

$tableRules = [];
foreach ($tableColumns as $column) {
$field = $column->Field;

// If specific columns where supplied only process those...
if (! empty($this->columns) && ! in_array($field, $this->columns)) {
continue;
}

// If column should be skipped
if (in_array($column, $skip_columns)) {
continue;
}

// We do not need a rule for auto increments
if ($column->Extra === 'auto_increment') {
continue;
}

$tableRules[$field] = $this->generateColumnRules($column);
}

return $tableRules;
}

private function getColumnsDefinitionsFromTable()
protected function getColumnsDefinitionsFromTable()
{
$databaseName = config('database.connections.mysql.database');
$tableName = $this->table;
$tableName = $this->table();

$tableColumns = collect(DB::select('SHOW COLUMNS FROM ' . $tableName))->keyBy('Field')->toArray();

Expand All @@ -97,7 +59,7 @@ private function getColumnsDefinitionsFromTable()
return $tableColumns;
}

private function generateColumnRules(stdClass $column): array
protected function generateColumnRules(stdClass $column): array
{
$columnRules = [];
$columnRules[] = $column->Null === "YES" ? 'nullable' : 'required' ;
Expand Down Expand Up @@ -177,4 +139,15 @@ private function generateColumnRules(stdClass $column): array

return $columnRules;
}

protected function isAutoIncrement($column) : bool
{
return $column->Extra === 'auto_increment';
}

protected function getField($column) : string
{
return $column->Field;
}

}
59 changes: 16 additions & 43 deletions src/Resolvers/SchemaRulesResolverPgSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,22 @@

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use LaracraftTech\LaravelSchemaRules\Contracts\SchemaRulesResolverInterface;
use stdClass;

class SchemaRulesResolverPgSql implements SchemaRulesResolverInterface
class SchemaRulesResolverPgSql extends BaseSchemaRulesResolver implements SchemaRulesResolverInterface
{
private string $table;
private array $columns;

public static array $integerTypes = [
'smallint' => ['-32768', '32767'],
'integer' => ['-2147483648', '2147483647'],
'bigint' => ['-9223372036854775808', '9223372036854775807'],
];

public function __construct(string $table, array $columns = [])
{
$this->table = $table;
$this->columns = $columns;
}

public function generate(): array
{
$tableColumns = $this->getColumnsDefinitionsFromTable();

$skip_columns = config('schema-rules.skip_columns');

$tableRules = [];
foreach ($tableColumns as $column) {
$field = $column->column_name;

// If specific columns where supplied only process those...
if (! empty($this->columns) && ! in_array($field, $this->columns)) {
continue;
}

// If column should be skipped
if (in_array($column, $skip_columns)) {
continue;
}

// We do not need a rule for auto increments
if (Str::contains($column->column_default, 'nextval')) {
continue;
}

$tableRules[$field] = $this->generateColumnRules($column);
}

return $tableRules;
}

private function getColumnsDefinitionsFromTable()
protected function getColumnsDefinitionsFromTable()
{
$databaseName = config('database.connections.mysql.database');
$tableName = $this->table;
$tableName = $this->table();

$tableColumns = collect(DB::select(
"
Expand Down Expand Up @@ -93,7 +55,7 @@ private function getColumnsDefinitionsFromTable()
return $tableColumns;
}

private function generateColumnRules(stdClass $column): array
protected function generateColumnRules(stdClass $column): array
{
$columnRules = [];
$columnRules[] = $column->is_nullable === "YES" ? 'nullable' : 'required' ;
Expand Down Expand Up @@ -157,4 +119,15 @@ private function generateColumnRules(stdClass $column): array

return $columnRules;
}

protected function isAutoIncrement($column) : bool
{
return Str::contains($column->column_default, 'nextval');
}

protected function getField($column) : string
{
return $column->column_name;
}

}
59 changes: 16 additions & 43 deletions src/Resolvers/SchemaRulesResolverSqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,18 @@

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use LaracraftTech\LaravelSchemaRules\Contracts\SchemaRulesResolverInterface;
use stdClass;

class SchemaRulesResolverSqlite implements SchemaRulesResolverInterface
class SchemaRulesResolverSqlite extends BaseSchemaRulesResolver implements SchemaRulesResolverInterface
{
private string $table;
private array $columns;

public function __construct(string $table, array $columns = [])
protected function getColumnsDefinitionsFromTable()
{
$this->table = $table;
$this->columns = $columns;
}

public function generate(): array
{
$tableColumns = $this->getColumnsDefinitionsFromTable();

$skip_columns = config('schema-rules.skip_columns');

$tableRules = [];
foreach ($tableColumns as $column) {
$field = $column->name;

// If specific columns where supplied only process those...
if (! empty($this->columns) && ! in_array($field, $this->columns)) {
continue;
}

// If column should be skipped
if (in_array($column, $skip_columns)) {
continue;
}

// We do not need a rule for auto increments
if ($column->pk) {
continue;
}

$tableRules[$field] = $this->generateColumnRules($column);
}
//dd($tableColumns);
return $tableRules;
}

private function getColumnsDefinitionsFromTable()
{
$tableColumns = collect(DB::select('PRAGMA table_info(' . $this->table . ')'))->keyBy('name')->toArray();
$tableColumns = collect(DB::select("PRAGMA table_info('{$this->table()}')"))->keyBy('name')->toArray();

$foreignKeys = DB::select("PRAGMA foreign_key_list($this->table)");
$foreignKeys = DB::select("PRAGMA foreign_key_list({$this->table()})");

foreach ($foreignKeys as $foreignKey) {
$tableColumns[$foreignKey->from]->Foreign = [
Expand All @@ -64,7 +27,7 @@ private function getColumnsDefinitionsFromTable()
return $tableColumns;
}

private function generateColumnRules(stdClass $column): array
protected function generateColumnRules(stdClass $column): array
{
$columnRules = [];
$columnRules[] = $column->notnull ? 'required' : 'nullable' ;
Expand Down Expand Up @@ -109,4 +72,14 @@ private function generateColumnRules(stdClass $column): array

return $columnRules;
}

protected function isAutoIncrement($column) : bool
{
return $column->pk;
}

protected function getField($column) : string
{
return $column->name;
}
}
2 changes: 1 addition & 1 deletion tests/SchemaRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use LaracraftTech\LaravelSchemaRules\Exceptions\ColumnDoesNotExistException;
use LaracraftTech\LaravelSchemaRules\Exceptions\MultipleTablesSuppliedException;
use LaracraftTech\LaravelSchemaRules\Exceptions\TableDoesNotExistException;
use LaracraftTech\LaravelSchemaRules\Resolvers\SchemaRulesResolverInterface;
use LaracraftTech\LaravelSchemaRules\Contracts\SchemaRulesResolverInterface;
use LaracraftTech\LaravelSchemaRules\Resolvers\SchemaRulesResolverMySql;

beforeEach(function () {
Expand Down

0 comments on commit 710208f

Please sign in to comment.