Skip to content

Commit 75b2c2c

Browse files
update
1 parent dcef55a commit 75b2c2c

File tree

6 files changed

+84
-23
lines changed

6 files changed

+84
-23
lines changed

src/Dummy/dummyFactory.dum

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Tamedevelopers\Database\Factories;
4+
5+
use Tamedevelopers\Database\Factory;
6+
7+
class {{ class }} extends Factory
8+
{
9+
/**
10+
* Define the model's default state.
11+
*
12+
* @return array<string, mixed>
13+
*/
14+
public function definition()
15+
{
16+
return [
17+
18+
];
19+
}
20+
}

src/Dummy/dummySeeder.dum

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Tamedevelopers\Database\Seeders;
4+
5+
use Tamedevelopers\Database\Seeder;
6+
7+
class {{ class }} extends Seeder
8+
{
9+
/**
10+
* Run the database seeds.
11+
*/
12+
public function run(): void
13+
{
14+
15+
}
16+
}

src/Factory.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tamedevelopers\Database;
6+
7+
class Factory{
8+
9+
10+
}

src/Migrations/Traits/MigrationTrait.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ trait MigrationTrait{
1919

2020
private static $database;
2121
private static $migrations;
22+
private static $factories;
2223
private static $seeders;
2324
private static $error;
2425
private static $message;
@@ -35,6 +36,7 @@ private static function normalizeFolderStructure()
3536
// collection of migration and seeders path
3637
self::$database = Env::getServers('server') . "database/";
3738
self::$migrations = self::$database . "migrations/";
39+
self::$factories = self::$database . "factories/";
3840
self::$seeders = self::$database . "seeders/";
3941
}
4042

@@ -130,21 +132,19 @@ private static function initBaseDirectory()
130132

131133
// check if database folder not exist
132134
if(!File::isDirectory(self::$database)){
133-
@File::makeDirectory(self::$database, 0777);
134-
135-
// gitignore fle path
136-
$gitignore = sprintf("%s.gitignore", self::$database);
137-
138-
// create file if not exist
139-
if (!File::exists($gitignore) && !is_dir($gitignore)) {
140-
// Write the contents to the new file
141-
File::put($gitignore, preg_replace(
142-
'/^[ \t]+|[ \t]+$/m', '',
143-
".
144-
/database
145-
.env"
146-
));
147-
}
135+
File::makeDirectory(self::$database, 0777);
136+
}
137+
138+
// gitignore fle path
139+
$gitignore = sprintf("%s.gitignore", self::$database);
140+
141+
// create file if not exist
142+
if (!File::exists($gitignore)) {
143+
File::put($gitignore, preg_replace(
144+
'/^[ \t]+|[ \t]+$/m', '',
145+
".env
146+
*.sqlite*"
147+
));
148148
}
149149

150150
// if migrations folder not found
@@ -157,6 +157,11 @@ private static function initBaseDirectory()
157157
File::makeDirectory(self::$seeders, 0777);
158158
}
159159

160+
// if factories folder not found
161+
if(!File::isDirectory(self::$factories)){
162+
File::makeDirectory(self::$factories, 0777);
163+
}
164+
160165
if(!File::isDirectory(self::$migrations)){
161166
throw new \Exception(
162167
sprintf("Path to dabatase[dir] not found ---> `%s`", self::$migrations)

src/Seeder.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tamedevelopers\Database;
6+
7+
class Seeder{
8+
9+
10+
}

src/Traits/DBSchemaExportTrait.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ protected function generateTableSchema(string $dbName, string $table): string
9898
$colName = $ix->Column_name ?? $ix->column_name ?? null;
9999
$nonUnique = (int) ($ix->Non_unique ?? $ix->non_unique ?? 1);
100100
if (!$idxName || !$colName) { continue; }
101-
if (Str::lower((string) $idxName) === 'primary') { continue; }
101+
if (Str::lower($idxName) === 'primary') { continue; }
102102
if (!isset($byIndexName[$idxName])) {
103103
$byIndexName[$idxName] = ['cols' => [], 'unique' => ($nonUnique === 0)];
104104
}
@@ -135,10 +135,10 @@ protected function generateTableSchema(string $dbName, string $table): string
135135

136136
$field = $col?->Field ?? $col?->field;
137137
$type = Str::lower($col?->Type ?? $col?->type);
138-
$null = Str::lower((string) $col?->Null ?? $col?->null) === 'yes';
138+
$null = Str::lower($col?->Null ?? $col?->null) === 'yes';
139139
// Key type should come from Key column not Null
140-
$key = Str::lower((string) ($col?->Key ?? $col?->key ?? ''));
141-
$extra = Str::lower((string) ($col?->Extra ?? $col?->extra ?? ''));
140+
$key = Str::lower(($col?->Key ?? $col?->key ?? ''));
141+
$extra = Str::lower(($col?->Extra ?? $col?->extra ?? ''));
142142
$default = $col?->Default ?? $col?->default;
143143

144144
// Skip the primary auto column since id() already added it
@@ -377,7 +377,7 @@ protected function toBlueprintLine(string $field, string $type, bool $nullable,
377377
}
378378

379379
// primary (non auto-increment) should be reflected inline
380-
if ($key === 'pri' && !str_contains(Str::lower((string)$extra), 'auto_increment')) {
380+
if ($key === 'pri' && !str_contains(Str::lower($extra), 'auto_increment')) {
381381
$line .= "->primary()";
382382
}
383383

@@ -426,7 +426,7 @@ protected function buildIndexLines(array $indexes, bool $primaryAuto): array
426426
$col = $idx->Column_name ?? $idx->column_name ?? null;
427427
$nonUnique = (int) ($idx->Non_unique ?? $idx->non_unique ?? 1);
428428
if (!$idxName || !$col) { continue; }
429-
if (Str::lower((string) $idxName) === 'primary') { continue; }
429+
if (Str::lower($idxName) === 'primary') { continue; }
430430
if (!isset($byIndexName[$idxName])) {
431431
$byIndexName[$idxName] = ['cols' => [], 'unique' => ($nonUnique === 0)];
432432
}
@@ -451,12 +451,12 @@ protected function buildIndexLines(array $indexes, bool $primaryAuto): array
451451
/** Detect if table has a single auto-increment primary key named 'id'. */
452452
protected function detectPrimaryAutoIncrement(array $columns): bool
453453
{
454-
$pkCols = array_values(array_filter($columns, fn($c) => Str::lower((string)($c->Key ?? '')) === 'pri'));
454+
$pkCols = array_values(array_filter($columns, fn($c) => Str::lower($c->Key ?? '') === 'pri'));
455455
if (count($pkCols) !== 1) {
456456
return false;
457457
}
458458
$pk = $pkCols[0];
459-
$isAuto = str_contains(Str::lower((string)$pk->Extra), 'auto_increment');
459+
$isAuto = str_contains(Str::lower($pk->Extra), 'auto_increment');
460460
return $isAuto && Str::lower($pk->Field) === 'id';
461461
}
462462

0 commit comments

Comments
 (0)