Add repo hygiene rules and ignore secrets
This commit is contained in:
29
server/db/migrate.php
Normal file
29
server/db/migrate.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Database\ConnectionFactory;
|
||||
use Dotenv\Dotenv;
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
$repoRoot = dirname(__DIR__, 2);
|
||||
if (file_exists($repoRoot . '/.env')) {
|
||||
$dotenv = Dotenv::createImmutable($repoRoot);
|
||||
$dotenv->safeLoad();
|
||||
}
|
||||
|
||||
$pdo = ConnectionFactory::create();
|
||||
|
||||
$migrationsDir = __DIR__ . '/migrations';
|
||||
$files = glob($migrationsDir . '/*.sql');
|
||||
sort($files);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$sql = file_get_contents($file);
|
||||
if ($sql === false) {
|
||||
throw new RuntimeException("Migration nicht lesbar: {$file}");
|
||||
}
|
||||
$pdo->exec($sql);
|
||||
echo "OK: " . basename($file) . PHP_EOL;
|
||||
}
|
||||
72
server/db/migrations/001_init.sql
Normal file
72
server/db/migrations/001_init.sql
Normal file
@@ -0,0 +1,72 @@
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
race_key TEXT NOT NULL DEFAULT 'human',
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS planets (
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
class_key TEXT NOT NULL,
|
||||
planet_seed INT NOT NULL DEFAULT 0,
|
||||
temperature_c INT NOT NULL DEFAULT 0,
|
||||
modifiers TEXT NOT NULL,
|
||||
resources TEXT NOT NULL,
|
||||
last_resource_update_at TIMESTAMP NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS planet_buildings (
|
||||
id SERIAL PRIMARY KEY,
|
||||
planet_id INT NOT NULL REFERENCES planets(id) ON DELETE CASCADE,
|
||||
building_key TEXT NOT NULL,
|
||||
count INT NOT NULL DEFAULT 0,
|
||||
level INT NOT NULL DEFAULT 0,
|
||||
UNIQUE (planet_id, building_key)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS build_jobs (
|
||||
id SERIAL PRIMARY KEY,
|
||||
planet_id INT NOT NULL REFERENCES planets(id) ON DELETE CASCADE,
|
||||
building_key TEXT NOT NULL,
|
||||
mode TEXT NOT NULL,
|
||||
delta_count INT,
|
||||
target_level INT,
|
||||
started_at TIMESTAMP NOT NULL,
|
||||
finish_at TIMESTAMP NOT NULL,
|
||||
slot_index INT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS roles (
|
||||
id SERIAL PRIMARY KEY,
|
||||
key TEXT NOT NULL UNIQUE,
|
||||
name TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS permissions (
|
||||
id SERIAL PRIMARY KEY,
|
||||
key TEXT NOT NULL UNIQUE,
|
||||
module TEXT NOT NULL,
|
||||
description TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS role_permissions (
|
||||
role_id INT NOT NULL REFERENCES roles(id) ON DELETE CASCADE,
|
||||
permission_id INT NOT NULL REFERENCES permissions(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (role_id, permission_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS user_roles (
|
||||
user_id INT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
role_id INT NOT NULL REFERENCES roles(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (user_id, role_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS user_permission_overrides (
|
||||
user_id INT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
permission_id INT NOT NULL REFERENCES permissions(id) ON DELETE CASCADE,
|
||||
effect TEXT NOT NULL CHECK (effect IN ('allow', 'deny')),
|
||||
PRIMARY KEY (user_id, permission_id)
|
||||
);
|
||||
109
server/db/seed.php
Normal file
109
server/db/seed.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Config\ConfigLoader;
|
||||
use App\Config\ConfigValidator;
|
||||
use App\Database\ConnectionFactory;
|
||||
use App\Module\Permissions\Permissions;
|
||||
use App\Module\PlanetGenerator\Service\PlanetGenerator;
|
||||
use Dotenv\Dotenv;
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
$repoRoot = dirname(__DIR__, 2);
|
||||
if (file_exists($repoRoot . '/.env')) {
|
||||
$dotenv = Dotenv::createImmutable($repoRoot);
|
||||
$dotenv->safeLoad();
|
||||
}
|
||||
|
||||
$pdo = ConnectionFactory::create();
|
||||
$pdo->beginTransaction();
|
||||
|
||||
try {
|
||||
$stmt = $pdo->prepare("INSERT INTO roles (key, name) VALUES (:key, :name) ON CONFLICT (key) DO NOTHING");
|
||||
$stmt->execute(['key' => 'player', 'name' => 'Spieler']);
|
||||
$stmt->execute(['key' => 'admin', 'name' => 'Admin']);
|
||||
|
||||
$permStmt = $pdo->prepare(
|
||||
'INSERT INTO permissions (key, module, description) VALUES (:key, :module, :description)
|
||||
ON CONFLICT (key) DO NOTHING'
|
||||
);
|
||||
foreach (Permissions::definitions() as $perm) {
|
||||
$permStmt->execute($perm);
|
||||
}
|
||||
|
||||
$pdo->exec("INSERT INTO role_permissions (role_id, permission_id)
|
||||
SELECT r.id, p.id
|
||||
FROM roles r
|
||||
JOIN permissions p ON p.key = 'planet.public.view'
|
||||
WHERE r.key = 'player'
|
||||
ON CONFLICT DO NOTHING");
|
||||
|
||||
$pdo->exec("INSERT INTO role_permissions (role_id, permission_id)
|
||||
SELECT r.id, p.id
|
||||
FROM roles r
|
||||
JOIN permissions p ON p.key = 'planet.admin.generate'
|
||||
WHERE r.key = 'admin'
|
||||
ON CONFLICT DO NOTHING");
|
||||
|
||||
$stmt = $pdo->prepare("INSERT INTO users (username, race_key) VALUES (:username, :race_key) ON CONFLICT (username) DO NOTHING");
|
||||
$stmt->execute(['username' => 'dev', 'race_key' => 'human']);
|
||||
|
||||
$userId = (int)$pdo->query("SELECT id FROM users WHERE username = 'dev'")->fetchColumn();
|
||||
|
||||
$pdo->exec("INSERT INTO user_roles (user_id, role_id)
|
||||
SELECT {$userId}, r.id
|
||||
FROM roles r
|
||||
WHERE r.key = 'player'
|
||||
ON CONFLICT DO NOTHING");
|
||||
|
||||
$planetExists = $pdo->prepare('SELECT id FROM planets WHERE user_id = :user_id LIMIT 1');
|
||||
$planetExists->execute(['user_id' => $userId]);
|
||||
$planetId = $planetExists->fetchColumn();
|
||||
|
||||
if (!$planetId) {
|
||||
$configLoader = new ConfigLoader(new ConfigValidator(), $repoRoot . '/config');
|
||||
$generator = new PlanetGenerator($configLoader);
|
||||
$generated = $generator->generate('temperate', 'normal', 42);
|
||||
|
||||
$resources = [];
|
||||
foreach ($configLoader->planetClasses()['resources'] as $res) {
|
||||
$resources[$res] = 500.0;
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare(
|
||||
'INSERT INTO planets (user_id, name, class_key, planet_seed, temperature_c, modifiers, resources, last_resource_update_at)
|
||||
VALUES (:user_id, :name, :class_key, :planet_seed, :temperature_c, :modifiers, :resources, :last_update)
|
||||
RETURNING id'
|
||||
);
|
||||
$stmt->execute([
|
||||
'user_id' => $userId,
|
||||
'name' => 'Earth Prime',
|
||||
'class_key' => $generated['class_key'],
|
||||
'planet_seed' => 42,
|
||||
'temperature_c' => (int)$generated['temperature_c'],
|
||||
'modifiers' => json_encode($generated['modifiers'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
|
||||
'resources' => json_encode($resources, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
|
||||
'last_update' => (new DateTimeImmutable('now'))->format('Y-m-d H:i:s'),
|
||||
]);
|
||||
$planetId = (int)$stmt->fetchColumn();
|
||||
|
||||
$stmt = $pdo->prepare(
|
||||
'INSERT INTO planet_buildings (planet_id, building_key, count, level)
|
||||
VALUES (:planet_id, :building_key, :count, 0)
|
||||
ON CONFLICT (planet_id, building_key) DO NOTHING'
|
||||
);
|
||||
$stmt->execute([
|
||||
'planet_id' => $planetId,
|
||||
'building_key' => 'build_center',
|
||||
'count' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
echo "Seed abgeschlossen.\n";
|
||||
} catch (Throwable $e) {
|
||||
$pdo->rollBack();
|
||||
throw $e;
|
||||
}
|
||||
Reference in New Issue
Block a user