diff --git a/CHANGELOG.md b/CHANGELOG.md index ed45dd3..1616dd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to `laravel-schema-rules` will be documented in this file. +## v1.3.3 - 2023-10-19 + +### What's changed + +- output generated rules info text only in console mode + ## v1.3.2 - 2023-08-21 ### What's Changed diff --git a/config/schema-rules.php b/config/schema-rules.php index a9ba9b2..da0df50 100644 --- a/config/schema-rules.php +++ b/config/schema-rules.php @@ -19,5 +19,5 @@ * Always skip this columns */ 'skip_columns' => ['created_at', 'updated_at', 'deleted_at'], - + ]; diff --git a/src/Contracts/SchemaRulesResolverInterface.php b/src/Contracts/SchemaRulesResolverInterface.php index 2928f0a..59d211e 100644 --- a/src/Contracts/SchemaRulesResolverInterface.php +++ b/src/Contracts/SchemaRulesResolverInterface.php @@ -5,16 +5,12 @@ interface SchemaRulesResolverInterface { /** - * @param string $table - * @param array $columns * @return array */ public function __construct(string $table, array $columns = []); /** * Generate the rules of the provided tables schema. - * - * @return array */ public function generate(): array; } diff --git a/src/LaravelSchemaRulesServiceProvider.php b/src/LaravelSchemaRulesServiceProvider.php index c7fdbde..3aafe88 100644 --- a/src/LaravelSchemaRulesServiceProvider.php +++ b/src/LaravelSchemaRulesServiceProvider.php @@ -49,7 +49,7 @@ public function register() break; default: throw new UnsupportedDbDriverException('This db driver is not supported: '.$driver); - }; + } return new $class(...array_values($parameters)); }); diff --git a/src/Resolvers/BaseSchemaRulesResolver.php b/src/Resolvers/BaseSchemaRulesResolver.php index fdc43db..1e5ea5b 100644 --- a/src/Resolvers/BaseSchemaRulesResolver.php +++ b/src/Resolvers/BaseSchemaRulesResolver.php @@ -8,6 +8,7 @@ abstract class BaseSchemaRulesResolver implements SchemaRulesResolverInterface { private string $table; + private array $columns; public function __construct(string $table, array $columns = []) diff --git a/src/Resolvers/SchemaRulesResolverMySql.php b/src/Resolvers/SchemaRulesResolverMySql.php index eec33c4..faa99e8 100644 --- a/src/Resolvers/SchemaRulesResolverMySql.php +++ b/src/Resolvers/SchemaRulesResolverMySql.php @@ -37,7 +37,7 @@ protected function getColumnsDefinitionsFromTable() $databaseName = config('database.connections.mysql.database'); $tableName = $this->table(); - $tableColumns = collect(DB::select('SHOW COLUMNS FROM ' . $tableName))->keyBy('Field')->toArray(); + $tableColumns = collect(DB::select('SHOW COLUMNS FROM '.$tableName))->keyBy('Field')->toArray(); $foreignKeys = DB::select(" SELECT k.COLUMN_NAME, k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME @@ -61,10 +61,10 @@ protected function getColumnsDefinitionsFromTable() protected function generateColumnRules(stdClass $column): array { $columnRules = []; - $columnRules[] = $column->Null === "YES" ? 'nullable' : 'required' ; + $columnRules[] = $column->Null === 'YES' ? 'nullable' : 'required'; if (! empty($column->Foreign)) { - $columnRules[] = "exists:".implode(',', $column->Foreign); + $columnRules[] = 'exists:'.implode(',', $column->Foreign); return $columnRules; } @@ -72,34 +72,34 @@ protected function generateColumnRules(stdClass $column): array $type = Str::of($column->Type); switch (true) { case $type == 'tinyint(1)' && config('schema-rules.tinyint1_to_bool'): - $columnRules[] = "boolean"; + $columnRules[] = 'boolean'; break; case $type->contains('char'): - $columnRules[] = "string"; - $columnRules[] = "min:".config('schema-rules.string_min_length'); - $columnRules[] = "max:".filter_var($type, FILTER_SANITIZE_NUMBER_INT); + $columnRules[] = 'string'; + $columnRules[] = 'min:'.config('schema-rules.string_min_length'); + $columnRules[] = 'max:'.filter_var($type, FILTER_SANITIZE_NUMBER_INT); break; case $type == 'text': - $columnRules[] = "string"; - $columnRules[] = "min:".config('schema-rules.string_min_length'); + $columnRules[] = 'string'; + $columnRules[] = 'min:'.config('schema-rules.string_min_length'); break; case $type->contains('int'): - $columnRules[] = "integer"; - $sign = ($type->contains('unsigned')) ? 'unsigned' : 'signed' ; + $columnRules[] = 'integer'; + $sign = ($type->contains('unsigned')) ? 'unsigned' : 'signed'; $intType = $type->before(' unsigned')->__toString(); // prevent int(xx) for mysql - $intType = preg_replace("/\([^)]+\)/", "", $intType); + $intType = preg_replace("/\([^)]+\)/", '', $intType); - if(! array_key_exists($intType, self::$integerTypes)) { - $intType = "int"; + if (! array_key_exists($intType, self::$integerTypes)) { + $intType = 'int'; } - $columnRules[] = "min:".self::$integerTypes[$intType][$sign][0]; - $columnRules[] = "max:".self::$integerTypes[$intType][$sign][1]; + $columnRules[] = 'min:'.self::$integerTypes[$intType][$sign][0]; + $columnRules[] = 'max:'.self::$integerTypes[$intType][$sign][1]; break; case $type->contains('double') || @@ -108,7 +108,7 @@ protected function generateColumnRules(stdClass $column): array $type->contains('float'): // should we do more specific here? // some kind of regex validation for double, double unsigned, double(8, 2), decimal etc...? - $columnRules[] = "numeric"; + $columnRules[] = 'numeric'; break; case $type->contains('enum') || $type->contains('set'): diff --git a/src/Resolvers/SchemaRulesResolverPgSql.php b/src/Resolvers/SchemaRulesResolverPgSql.php index dfe519a..02c736e 100644 --- a/src/Resolvers/SchemaRulesResolverPgSql.php +++ b/src/Resolvers/SchemaRulesResolverPgSql.php @@ -21,10 +21,10 @@ protected function getColumnsDefinitionsFromTable() $tableName = $this->table(); $tableColumns = collect(DB::select( - " + ' SELECT column_name, data_type, character_maximum_length, is_nullable, column_default FROM INFORMATION_SCHEMA.COLUMNS - WHERE table_name = :table", + WHERE table_name = :table', ['table' => $tableName] ))->keyBy('column_name')->toArray(); @@ -57,10 +57,10 @@ protected function getColumnsDefinitionsFromTable() protected function generateColumnRules(stdClass $column): array { $columnRules = []; - $columnRules[] = $column->is_nullable === "YES" ? 'nullable' : 'required' ; + $columnRules[] = $column->is_nullable === 'YES' ? 'nullable' : 'required'; if (! empty($column->Foreign)) { - $columnRules[] = "exists:".implode(',', $column->Foreign); + $columnRules[] = 'exists:'.implode(',', $column->Foreign); return $columnRules; } @@ -68,24 +68,24 @@ protected function generateColumnRules(stdClass $column): array $type = Str::of($column->data_type); switch (true) { case $type == 'boolean': - $columnRules[] = "boolean"; + $columnRules[] = 'boolean'; break; case $type->contains('char'): - $columnRules[] = "string"; - $columnRules[] = "min:".config('schema-rules.string_min_length'); - $columnRules[] = "max:".$column->character_maximum_length; + $columnRules[] = 'string'; + $columnRules[] = 'min:'.config('schema-rules.string_min_length'); + $columnRules[] = 'max:'.$column->character_maximum_length; break; case $type == 'text': - $columnRules[] = "string"; - $columnRules[] = "min:".config('schema-rules.string_min_length'); + $columnRules[] = 'string'; + $columnRules[] = 'min:'.config('schema-rules.string_min_length'); break; case $type->contains('int'): - $columnRules[] = "integer"; - $columnRules[] = "min:" . self::$integerTypes[$type->__toString()][0]; - $columnRules[] = "max:" . self::$integerTypes[$type->__toString()][1]; + $columnRules[] = 'integer'; + $columnRules[] = 'min:'.self::$integerTypes[$type->__toString()][0]; + $columnRules[] = 'max:'.self::$integerTypes[$type->__toString()][1]; break; case $type->contains('double') || @@ -94,7 +94,7 @@ protected function generateColumnRules(stdClass $column): array $type->contains('real'): // should we do more specific here? // some kind of regex validation for double, double unsigned, double(8, 2), decimal etc...? - $columnRules[] = "numeric"; + $columnRules[] = 'numeric'; break; // unfortunately, it's not so easy in pgsql to find out if a column is an enum diff --git a/src/Resolvers/SchemaRulesResolverSqlite.php b/src/Resolvers/SchemaRulesResolverSqlite.php index 3d8df40..6b58c84 100644 --- a/src/Resolvers/SchemaRulesResolverSqlite.php +++ b/src/Resolvers/SchemaRulesResolverSqlite.php @@ -29,10 +29,10 @@ protected function getColumnsDefinitionsFromTable() protected function generateColumnRules(stdClass $column): array { $columnRules = []; - $columnRules[] = $column->notnull ? 'required' : 'nullable' ; + $columnRules[] = $column->notnull ? 'required' : 'nullable'; if (! empty($column->Foreign)) { - $columnRules[] = "exists:".implode(',', $column->Foreign); + $columnRules[] = 'exists:'.implode(',', $column->Foreign); return $columnRules; } @@ -40,24 +40,24 @@ protected function generateColumnRules(stdClass $column): array $type = Str::of($column->type); switch (true) { case $type == 'tinyint(1)' && config('schema-rules.tinyint1_to_bool'): - $columnRules[] = "boolean"; + $columnRules[] = 'boolean'; break; case $type == 'varchar' || $type == 'text': - $columnRules[] = "string"; - $columnRules[] = "min:".config('schema-rules.string_min_length'); + $columnRules[] = 'string'; + $columnRules[] = 'min:'.config('schema-rules.string_min_length'); break; case $type == 'integer': - $columnRules[] = "integer"; - $columnRules[] = "min:-9223372036854775808"; - $columnRules[] = "max:9223372036854775807"; + $columnRules[] = 'integer'; + $columnRules[] = 'min:-9223372036854775808'; + $columnRules[] = 'max:9223372036854775807'; break; case $type->contains('numeric') || $type->contains('float'): // should we do more specific here? // some kind of regex validation for double, double unsigned, double(8, 2), decimal etc...? - $columnRules[] = "numeric"; + $columnRules[] = 'numeric'; break; case $type == 'date' || $type == 'time' || $type == 'datetime': diff --git a/tests/SchemaRulesTest.php b/tests/SchemaRulesTest.php index 0f0b29c..2370fe4 100644 --- a/tests/SchemaRulesTest.php +++ b/tests/SchemaRulesTest.php @@ -30,7 +30,7 @@ $this->expectException(\Symfony\Component\Console\Exception\InvalidArgumentException::class); - $this->artisan("schema:generate-rules", [ + $this->artisan('schema:generate-rules', [ 'foo' => $this->tableName, ]); }); @@ -42,7 +42,7 @@ $this->expectException(\Symfony\Component\Console\Exception\InvalidOptionException::class); - $this->artisan("schema:generate-rules", [ + $this->artisan('schema:generate-rules', [ 'table' => $this->tableName, '--foo' => 'test_bool', ]); @@ -55,7 +55,7 @@ $this->expectException(TableDoesNotExistException::class); - $this->artisan("schema:generate-rules", [ + $this->artisan('schema:generate-rules', [ 'table' => $this->tableName.'1', ]); }); @@ -67,7 +67,7 @@ $this->expectException(MultipleTablesSuppliedException::class); - $this->artisan("schema:generate-rules", [ + $this->artisan('schema:generate-rules', [ 'table' => $this->tableName.',tests2', ]); }); @@ -79,7 +79,7 @@ $this->expectException(ColumnDoesNotExistException::class); - $this->artisan("schema:generate-rules", [ + $this->artisan('schema:generate-rules', [ 'table' => $this->tableName, '--columns' => 'foo', ]); @@ -106,7 +106,7 @@ $stringNullableColumnName => ['nullable', 'string', 'min:1', 'max:255'], ]); - $this->artisan("schema:generate-rules", [ + $this->artisan('schema:generate-rules', [ 'table' => $this->tableName, ])->assertSuccessful(); }); @@ -132,7 +132,7 @@ $boolNullableColumnName => ['nullable', 'boolean'], ]); - $this->artisan("schema:generate-rules", [ + $this->artisan('schema:generate-rules', [ 'table' => $this->tableName, ])->assertSuccessful(); }); @@ -170,7 +170,7 @@ $textColumnName => ['required', 'string', 'min:1'], ]); - $this->artisan("schema:generate-rules", [ + $this->artisan('schema:generate-rules', [ 'table' => $this->tableName, ])->assertSuccessful(); }); @@ -234,7 +234,7 @@ $bigintNullableColumnName => ['nullable', 'integer', 'min:'.$integerTypes['bigint']['signed'][0], 'max:'.$integerTypes['bigint']['signed'][1]], ]); - $this->artisan("schema:generate-rules", [ + $this->artisan('schema:generate-rules', [ 'table' => $this->tableName, ])->assertSuccessful(); }); @@ -282,7 +282,7 @@ $decimalNullableColumnName => ['nullable', 'numeric'], ]); - $this->artisan("schema:generate-rules", [ + $this->artisan('schema:generate-rules', [ 'table' => $this->tableName, ])->assertSuccessful(); }); @@ -310,7 +310,7 @@ $setColumnName => ['required', 'string', 'in:'.implode(',', $allowed)], ]); - $this->artisan("schema:generate-rules", [ + $this->artisan('schema:generate-rules', [ 'table' => $this->tableName, ])->assertSuccessful(); }); @@ -344,7 +344,7 @@ $timestampColumnName => ['required', 'date', 'after_or_equal:1970-01-01 00:00:01', 'before_or_equal:2038-01-19 03:14:07'], ]); - $this->artisan("schema:generate-rules", [ + $this->artisan('schema:generate-rules', [ 'table' => $this->tableName, ])->assertSuccessful(); }); @@ -364,7 +364,7 @@ $jsonColumnName => ['required', 'json'], ]); - $this->artisan("schema:generate-rules", [ + $this->artisan('schema:generate-rules', [ 'table' => $this->tableName, ])->assertSuccessful(); }); @@ -396,7 +396,7 @@ $foreignKeyColumnName => ['required', 'exists:'.implode(',', $constrained)], ]); - $this->artisan("schema:generate-rules", [ + $this->artisan('schema:generate-rules', [ 'table' => $foreignTable, ])->assertSuccessful(); @@ -426,7 +426,7 @@ $stringColumnName => ['required', 'string', 'min:1', 'max:255'], ]); - $this->artisan("schema:generate-rules", [ + $this->artisan('schema:generate-rules', [ 'table' => $this->tableName, ])->assertSuccessful();