Initial: Laravel 13 setup with multi-tenant schema
This commit is contained in:
commit
a78a50ef0c
69 changed files with 10805 additions and 0 deletions
1
database/.gitignore
vendored
Normal file
1
database/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
*.sqlite*
|
||||
45
database/factories/UserFactory.php
Normal file
45
database/factories/UserFactory.php
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @extends Factory<User>
|
||||
*/
|
||||
class UserFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* The current password being used by the factory.
|
||||
*/
|
||||
protected static ?string $password;
|
||||
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'name' => fake()->name(),
|
||||
'email' => fake()->unique()->safeEmail(),
|
||||
'email_verified_at' => now(),
|
||||
'password' => static::$password ??= Hash::make('password'),
|
||||
'remember_token' => Str::random(10),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the model's email address should be unverified.
|
||||
*/
|
||||
public function unverified(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'email_verified_at' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
49
database/migrations/0001_01_01_000000_create_users_table.php
Normal file
49
database/migrations/0001_01_01_000000_create_users_table.php
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('users', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->string('email')->unique();
|
||||
$table->timestamp('email_verified_at')->nullable();
|
||||
$table->string('password');
|
||||
$table->rememberToken();
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
Schema::create('password_reset_tokens', function (Blueprint $table) {
|
||||
$table->string('email')->primary();
|
||||
$table->string('token');
|
||||
$table->timestamp('created_at')->nullable();
|
||||
});
|
||||
|
||||
Schema::create('sessions', function (Blueprint $table) {
|
||||
$table->string('id')->primary();
|
||||
$table->foreignId('user_id')->nullable()->index();
|
||||
$table->string('ip_address', 45)->nullable();
|
||||
$table->text('user_agent')->nullable();
|
||||
$table->longText('payload');
|
||||
$table->integer('last_activity')->index();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('users');
|
||||
Schema::dropIfExists('password_reset_tokens');
|
||||
Schema::dropIfExists('sessions');
|
||||
}
|
||||
};
|
||||
35
database/migrations/0001_01_01_000001_create_cache_table.php
Normal file
35
database/migrations/0001_01_01_000001_create_cache_table.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('cache', function (Blueprint $table) {
|
||||
$table->string('key')->primary();
|
||||
$table->mediumText('value');
|
||||
$table->bigInteger('expiration')->index();
|
||||
});
|
||||
|
||||
Schema::create('cache_locks', function (Blueprint $table) {
|
||||
$table->string('key')->primary();
|
||||
$table->string('owner');
|
||||
$table->bigInteger('expiration')->index();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('cache');
|
||||
Schema::dropIfExists('cache_locks');
|
||||
}
|
||||
};
|
||||
57
database/migrations/0001_01_01_000002_create_jobs_table.php
Normal file
57
database/migrations/0001_01_01_000002_create_jobs_table.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('jobs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('queue')->index();
|
||||
$table->longText('payload');
|
||||
$table->unsignedTinyInteger('attempts');
|
||||
$table->unsignedInteger('reserved_at')->nullable();
|
||||
$table->unsignedInteger('available_at');
|
||||
$table->unsignedInteger('created_at');
|
||||
});
|
||||
|
||||
Schema::create('job_batches', function (Blueprint $table) {
|
||||
$table->string('id')->primary();
|
||||
$table->string('name');
|
||||
$table->integer('total_jobs');
|
||||
$table->integer('pending_jobs');
|
||||
$table->integer('failed_jobs');
|
||||
$table->longText('failed_job_ids');
|
||||
$table->mediumText('options')->nullable();
|
||||
$table->integer('cancelled_at')->nullable();
|
||||
$table->integer('created_at');
|
||||
$table->integer('finished_at')->nullable();
|
||||
});
|
||||
|
||||
Schema::create('failed_jobs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('uuid')->unique();
|
||||
$table->text('connection');
|
||||
$table->text('queue');
|
||||
$table->longText('payload');
|
||||
$table->longText('exception');
|
||||
$table->timestamp('failed_at')->useCurrent();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('jobs');
|
||||
Schema::dropIfExists('job_batches');
|
||||
Schema::dropIfExists('failed_jobs');
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
return new class extends Migration {
|
||||
public function up(): void {
|
||||
Schema::create('tenants', function (Blueprint $t) {
|
||||
$t->id();
|
||||
$t->string('name', 100);
|
||||
$t->string('slug', 50)->unique();
|
||||
$t->string('plan', 20)->default('free');
|
||||
$t->timestamps();
|
||||
});
|
||||
}
|
||||
public function down(): void { Schema::dropIfExists('tenants'); }
|
||||
};
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
return new class extends Migration {
|
||||
public function up(): void {
|
||||
Schema::table('users', function (Blueprint $t) {
|
||||
$t->foreignId('tenant_id')->nullable()->after('id')->constrained('tenants')->nullOnDelete();
|
||||
$t->enum('role', ['admin','user','viewer'])->default('user')->after('password');
|
||||
$t->index('tenant_id');
|
||||
});
|
||||
}
|
||||
public function down(): void {
|
||||
Schema::table('users', function (Blueprint $t) {
|
||||
$t->dropForeign(['tenant_id']);
|
||||
$t->dropColumn(['tenant_id','role']);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
return new class extends Migration {
|
||||
public function up(): void {
|
||||
Schema::create('devices', function (Blueprint $t) {
|
||||
$t->id();
|
||||
$t->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
|
||||
$t->string('name',100);
|
||||
$t->string('slug',50);
|
||||
$t->string('type',30)->default('ems-esp');
|
||||
$t->string('mqtt_topic_prefix',100);
|
||||
$t->string('mqtt_username',50)->nullable();
|
||||
$t->string('timezone',50)->default('Europe/Berlin');
|
||||
$t->json('settings')->nullable();
|
||||
$t->timestamp('last_seen_at')->nullable();
|
||||
$t->timestamps();
|
||||
$t->unique(['tenant_id','slug']);
|
||||
$t->index('tenant_id');
|
||||
});
|
||||
}
|
||||
public function down(): void { Schema::dropIfExists('devices'); }
|
||||
};
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
return new class extends Migration {
|
||||
public function up(): void {
|
||||
Schema::create('readings', function (Blueprint $t) {
|
||||
$t->bigIncrements('id');
|
||||
$t->foreignId('device_id')->constrained('devices')->cascadeOnDelete();
|
||||
$t->string('metric',60);
|
||||
$t->decimal('value',10,3)->nullable();
|
||||
$t->string('value_text',50)->nullable();
|
||||
$t->timestamp('recorded_at',3);
|
||||
$t->index(['device_id','metric','recorded_at'],'idx_dmt');
|
||||
$t->index('recorded_at','idx_rec');
|
||||
});
|
||||
}
|
||||
public function down(): void { Schema::dropIfExists('readings'); }
|
||||
};
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
return new class extends Migration {
|
||||
public function up(): void {
|
||||
Schema::create('events', function (Blueprint $t) {
|
||||
$t->bigIncrements('id');
|
||||
$t->foreignId('device_id')->constrained('devices')->cascadeOnDelete();
|
||||
$t->enum('event_type',['burner_on','burner_off','error','warning','info','connection']);
|
||||
$t->enum('severity',['debug','info','warning','error','critical'])->default('info');
|
||||
$t->json('payload')->nullable();
|
||||
$t->timestamp('occurred_at',3);
|
||||
$t->index(['device_id','occurred_at'],'idx_dt');
|
||||
$t->index(['severity','occurred_at'],'idx_sev');
|
||||
});
|
||||
}
|
||||
public function down(): void { Schema::dropIfExists('events'); }
|
||||
};
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
return new class extends Migration {
|
||||
public function up(): void {
|
||||
Schema::create('audit_log', function (Blueprint $t) {
|
||||
$t->bigIncrements('id');
|
||||
$t->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
|
||||
$t->foreignId('device_id')->constrained('devices')->cascadeOnDelete();
|
||||
$t->string('action',60);
|
||||
$t->string('old_value',100)->nullable();
|
||||
$t->string('new_value',100)->nullable();
|
||||
$t->string('ip_address',45)->nullable();
|
||||
$t->timestamp('created_at',3);
|
||||
$t->index(['device_id','created_at'],'idx_adt');
|
||||
$t->index(['user_id','created_at'],'idx_aut');
|
||||
});
|
||||
}
|
||||
public function down(): void { Schema::dropIfExists('audit_log'); }
|
||||
};
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
return new class extends Migration {
|
||||
public function up(): void {
|
||||
Schema::create('readings_hourly', function (Blueprint $t) {
|
||||
$t->bigIncrements('id');
|
||||
$t->foreignId('device_id')->constrained('devices')->cascadeOnDelete();
|
||||
$t->string('metric',60);
|
||||
$t->timestamp('hour');
|
||||
$t->decimal('value_avg',10,3)->nullable();
|
||||
$t->decimal('value_min',10,3)->nullable();
|
||||
$t->decimal('value_max',10,3)->nullable();
|
||||
$t->unsignedInteger('sample_count');
|
||||
$t->unique(['device_id','metric','hour'],'uq_dmh');
|
||||
});
|
||||
}
|
||||
public function down(): void { Schema::dropIfExists('readings_hourly'); }
|
||||
};
|
||||
27
database/seeders/DatabaseSeeder.php
Normal file
27
database/seeders/DatabaseSeeder.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
namespace Database\Seeders;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
class DatabaseSeeder extends Seeder {
|
||||
public function run(): void {
|
||||
$tid = DB::table('tenants')->insertGetId([
|
||||
'name'=>'Privat','slug'=>'privat','plan'=>'free',
|
||||
'created_at'=>now(),'updated_at'=>now(),
|
||||
]);
|
||||
DB::table('users')->insert([
|
||||
'tenant_id'=>$tid,
|
||||
'name'=>'Admin','email'=>'admin@heizung.local',
|
||||
'password'=>Hash::make('change-me-now'),
|
||||
'role'=>'admin',
|
||||
'created_at'=>now(),'updated_at'=>now(),
|
||||
]);
|
||||
DB::table('devices')->insert([
|
||||
'tenant_id'=>$tid,
|
||||
'name'=>'Haus Eltern','slug'=>'eltern','type'=>'ems-esp',
|
||||
'mqtt_topic_prefix'=>'ems-esp/eltern',
|
||||
'timezone'=>'Europe/Berlin',
|
||||
'created_at'=>now(),'updated_at'=>now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue