diff --git a/database/migrations/2024_07_13_093048_create_mothers_table.php b/database/migrations/2024_07_13_093048_create_mothers_table.php index 8d4ba81..a3ba5ec 100644 --- a/database/migrations/2024_07_13_093048_create_mothers_table.php +++ b/database/migrations/2024_07_13_093048_create_mothers_table.php @@ -3,6 +3,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\App; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; use WatheqAlshowaiter\ModelRequiredFields\Constants; @@ -19,7 +20,7 @@ public function up(): void $table->enum('types', ['one', 'two'])->default('one'); // default => ignored - if ((float) App::version() >= Constants::VERSION_AFTER_UUID_SUPPORT) { + if ((float) App::version() >= Constants::VERSION_AFTER_UUID_SUPPORT && DB::connection()->getDriverName() !== 'mariadb') { $table->uuid('uuid'); // required } else { $table->string('uuid'); @@ -29,7 +30,12 @@ public function up(): void } else { $table->string('ulid'); // required } - $table->json('description')->nullable(); + + if (DB::connection()->getDriverName() === 'mariadb') { + $table->json('description')->nullable(); + } else { + $table->text('description')->nullable(); + } }); } diff --git a/database/migrations/2024_07_13_100247_create_sons_table.php b/database/migrations/2024_07_13_100247_create_sons_table.php index eedd167..ab8f4ca 100644 --- a/database/migrations/2024_07_13_100247_create_sons_table.php +++ b/database/migrations/2024_07_13_100247_create_sons_table.php @@ -5,6 +5,7 @@ use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Schema; use WatheqAlshowaiter\ModelRequiredFields\Constants; +use Illuminate\Support\Facades\DB; class CreateSonsTable extends Migration { @@ -12,7 +13,7 @@ public function up(): void { Schema::create('sons', function (Blueprint $table) { - if ((float) App::version() >= Constants::VERSION_AFTER_UUID_SUPPORT) { + if ((float) App::version() >= Constants::VERSION_AFTER_UUID_SUPPORT && DB::connection()->getDriverName() !== 'mariadb') { $table->uuid('id')->primary(); // primary key => ignored } else { $table->bigIncrements('id'); // primary key => ignored diff --git a/phpunit.xml b/phpunit.xml index ef1f819..57e785f 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -26,12 +26,11 @@ - - + + - diff --git a/src/RequiredFields.php b/src/RequiredFields.php index 3ec292a..9298acf 100644 --- a/src/RequiredFields.php +++ b/src/RequiredFields.php @@ -37,19 +37,21 @@ public static function getRequiredFields( ->toArray(); return collect(Schema::getColumns((new self())->getTable())) - ->reject(function ($column) use ($primaryIndex, $withNullables, $withDefaults, $withPrimaryKey) { + ->reject(function ($column) use ($primaryIndex, $withNullables, $withDefaults) { return - $column['nullable'] && ! $withNullables - || $column['default'] != null && ! $withDefaults - || in_array($column['name'], $primaryIndex) && ! $withPrimaryKey; + $column['nullable'] && !$withNullables || + $column['default'] != null && !$withDefaults || + (in_array($column['name'], $primaryIndex)); }) ->pluck('name') + ->when($withPrimaryKey, function ($collection) use ($primaryIndex) { + return $collection->prepend(...$primaryIndex); + }) + ->unique() ->toArray(); } /** - * @todo convert this method to private after testing - * * @return array|string */ public static function getRequiredFieldsForOlderVersions( @@ -107,11 +109,12 @@ private static function getRequiredFieldsForSqlite( return (array) $column; }, $queryResult); + return collect($queryResult) ->reject(function ($column) use ($withNullables, $withDefaults, $withPrimaryKey) { - return $column['pk'] && ! $withPrimaryKey - || $column['dflt_value'] != null && ! $withDefaults - || ! $column['notnull'] && ! $withNullables; + return $column['pk'] && !$withPrimaryKey + || $column['dflt_value'] != null && !$withDefaults + || !$column['notnull'] && !$withNullables; }) ->pluck('name') ->toArray(); @@ -152,9 +155,9 @@ private static function getRequiredFieldsForMysqlAndMariaDb( return collect($queryResult) ->reject(function ($column) use ($withNullables, $withDefaults, $withPrimaryKey) { - return $column['primary'] && ! $withPrimaryKey - || $column['default'] != null && ! $withDefaults - || $column['nullable'] && ! $withNullables; + return $column['primary'] && !$withPrimaryKey + || $column['default'] != null && !$withDefaults + || $column['nullable'] && !$withNullables; }) ->pluck('name') ->toArray(); @@ -168,18 +171,12 @@ private static function getRequiredFieldsForPostgres( $withDefaults = false, $withPrimaryKey = false ) { - $table = self::getTableFromThisModel(); $primaryIndex = DB::select(" - SELECT + SELECT ic.relname AS name, - string_agg( - a.attname, - ',' - ORDER BY - indseq.ord - ) AS columns, + string_agg(a.attname, ',' ORDER BY indseq.ord) AS columns, am.amname AS type, i.indisunique AS unique, i.indisprimary AS primary @@ -193,14 +190,14 @@ private static function getRequiredFieldsForPostgres( LEFT JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = indseq.num WHERE - tc.relname = 'users' + tc.relname = ? AND tn.nspname = CURRENT_SCHEMA GROUP BY ic.relname, am.amname, i.indisunique, i.indisprimary; - "); + ", [$table]); $primaryIndex = array_map(function ($column) { return (array) $column; @@ -216,35 +213,41 @@ private static function getRequiredFieldsForPostgres( $queryResult = DB::select( ' - SELECT - is_nullable as nullable, - column_name as name, - column_default as default - FROM - information_schema.columns - WHERE - TABLE_NAME = ? - ORDER BY - ORDINAL_POSITION ASC', + SELECT + is_nullable AS nullable, + column_name AS name, + column_default AS default + FROM + information_schema.columns + WHERE + table_name = ? + ORDER BY + ordinal_position ASC', [$table] ); - // convert stdClass object children to array $queryResult = array_map(function ($column) { return (array) $column; }, $queryResult); - return collect($queryResult) + $result = collect($queryResult) ->reject(function ($column) use ($primaryIndex, $withPrimaryKey, $withDefaults, $withNullables) { - return - $column['default'] && ! $withDefaults - || $column['nullable'] == 'YES' && ! $withNullables - || in_array($column['name'], $primaryIndex) && ! $withPrimaryKey; + return ($column['default'] && !$withDefaults) || + ($column['nullable'] == 'YES' && !$withNullables) || + (in_array($column['name'], $primaryIndex)); }) ->pluck('name') ->toArray(); + + // Add primary key to the result if $withPrimaryKey is true + if ($withPrimaryKey) { + $result = array_unique(array_merge($primaryIndex, $result)); + } + + return $result; } + /** * Not tested yet in machine with SQL SERVER * @@ -283,9 +286,9 @@ private static function getRequiredFieldsForSqlServer( return collect($queryResult) ->reject(function ($column) use ($withDefaults, $withNullables, $withPrimaryKey) { - return $column['primary'] && ! $withPrimaryKey - || $column['default'] != null && ! $withDefaults - || $column['nullable'] && ! $withNullables; + return $column['primary'] && !$withPrimaryKey + || $column['default'] != null && !$withDefaults + || $column['nullable'] && !$withNullables; }) ->pluck('name') ->toArray(); diff --git a/tests/TestCase.php b/tests/TestCase.php index 4e3bb84..738e2ad 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -21,6 +21,72 @@ protected function getPackageProviders($app) public function getEnvironmentSetUp($app) { - config()->set('database.default', 'testing'); + $dbConnection = env('DB_CONNECTION', 'sqlite'); + + $app['config']->set('database.default', $dbConnection); + + if ($dbConnection === 'mysql') { + $app['config']->set('database.connections.mysql', [ + 'driver' => 'mysql', + 'host' => env('DB_HOST', '127.0.0.1'), + 'port' => env('DB_PORT', '3306'), + 'database' => env('DB_DATABASE', 'laravel'), + 'username' => env('DB_USERNAME', 'root'), + 'password' => env('DB_PASSWORD', ''), + ]); + } + + if ($dbConnection === 'mariadb') { + $app['config']->set('database.connections.mariadb', [ + 'driver' => 'mariadb', + 'host' => env('DB_HOST', '127.0.0.1'), + 'port' => env('DB_PORT', '3306'), + 'database' => env('DB_DATABASE', 'laravel'), + 'username' => env('DB_USERNAME', 'root'), + 'password' => env('DB_PASSWORD', ''), + 'unix_socket' => env('DB_SOCKET', ''), + 'charset' => env('DB_CHARSET', 'utf8mb4'), + 'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'), + 'prefix' => '', + 'prefix_indexes' => true, + 'strict' => true, + 'engine' => null, + ]); + } + + if ($dbConnection === 'pgsql') { + $app['config']->set('database.connections.pgsql', [ + 'driver' => 'pgsql', + 'host' => env('PGSQL_DB_HOST', '127.0.0.1'), + 'port' => env('PGSQL_DB_PORT', '5432'), + 'database' => env('PGSQL_DB_DATABASE', 'laravel'), + 'username' => env('PGSQL_DB_USERNAME', 'postgres'), + 'password' => env('PGSQL_DB_PASSWORD', ''), + ]); + } + + if ($dbConnection === 'sqlite') { + $app['config']->set('database.connections.sqlite', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + ]); + } + + if ($dbConnection === 'sqlsrv') { + $app['config']->set('database.connections.sqlsrv', [ + 'driver' => 'sqlsrv', + 'host' => env('SQLSRV_DB_HOST', 'localhost'), + 'port' => env('SQLSRV_DB_PORT', '1433'), + 'database' => env('SQLSRV_DB_DATABASE', 'laravel'), + 'username' => env('SQLSRV_DB_USERNAME', 'root'), + 'password' => env('SQLSRV_DB_PASSWORD', ''), + ]); + } + + // if not supported above then stop the test + if (!in_array($dbConnection, ['mysql', 'mariadb', 'pgsql', 'sqlite', 'sqlsrv'])) { + echo "database {$dbConnection} is not supported"; + exit; + } } }