add user migration
This commit is contained in:
parent
50814ffd60
commit
6fbc1fb4f5
5 changed files with 113 additions and 1 deletions
46
backend/bin/migrate
Executable file
46
backend/bin/migrate
Executable file
|
|
@ -0,0 +1,46 @@
|
|||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
use App\Database\Migration;
|
||||
|
||||
(static function (): void {
|
||||
$root = dirname(__DIR__);
|
||||
|
||||
require $root . '/vendor/autoload.php';
|
||||
|
||||
$dotenv = Dotenv\Dotenv::createImmutable($root);
|
||||
$dotenv->safeLoad();
|
||||
|
||||
require $root . '/config/database.php';
|
||||
|
||||
$migrationsDir = $root . '/database/migrations';
|
||||
$files = glob($migrationsDir . '/*.php');
|
||||
|
||||
if ($files === false || $files === []) {
|
||||
echo "No migration files found.\n";
|
||||
exit(0);
|
||||
}
|
||||
|
||||
sort($files);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$className = require $file;
|
||||
|
||||
if (! class_exists($className)) {
|
||||
echo "Migration file {$file} did not define class {$className}\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$migration = new $className();
|
||||
|
||||
if (! $migration instanceof Migration) {
|
||||
echo "Class {$className} must extend " . Migration::class . "\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
echo "Running {$className}...\n";
|
||||
$migration->up();
|
||||
}
|
||||
|
||||
echo "Done.\n";
|
||||
})();
|
||||
|
|
@ -2,7 +2,8 @@
|
|||
"name": "pilzno/rabbigerzi",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"App\\": "app/"
|
||||
"App\\": "app/",
|
||||
"App\\Database\\": "database/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
|
|
|
|||
18
backend/database/Migration.php
Normal file
18
backend/database/Migration.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace App\Database;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
|
||||
abstract class Migration
|
||||
{
|
||||
abstract public function up(): void;
|
||||
|
||||
abstract public function down(): void;
|
||||
|
||||
protected function schema(): Builder
|
||||
{
|
||||
return Capsule::schema();
|
||||
}
|
||||
}
|
||||
21
backend/database/UserModel.php
Normal file
21
backend/database/UserModel.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace App\Database;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class UserModel extends Model
|
||||
{
|
||||
protected $table = 'users';
|
||||
|
||||
public $timestamps = true;
|
||||
|
||||
protected $fillable = [
|
||||
'email',
|
||||
'password_hash',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'int',
|
||||
];
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace App\Database\Migrations;
|
||||
|
||||
use App\Database\Migration;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
|
||||
class CreateUsersTable extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Capsule::schema()->create('users', function ($table) {
|
||||
$table->increments('id');
|
||||
$table->string('email', 255)->unique();
|
||||
$table->string('password_hash', 255);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Capsule::schema()->dropIfExists('users');
|
||||
}
|
||||
}
|
||||
|
||||
return CreateUsersTable::class;
|
||||
Loading…
Add table
Add a link
Reference in a new issue