Files
laravel-manager-packagist/src/Support/Tables.php
T
2026-12-04 17:15:56 +00:00

86 lines
5.3 KiB
PHP

<?php
namespace IriksIT\Manager\Support;
use Closure;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* Creates the tables of a module, but leaves a table alone that is already there.
*
* A site that had its own manager before has tables with the same job (groups, pages, leads…).
* Point `manager.tables` at them and the migrations of the package skip what exists, so the
* data stays where it is. `manager:doctor` reports columns the package needs and the table lacks.
*/
final class Tables
{
/** @var array<string, list<string>> The columns the package reads and writes, per table (by the name the package uses). */
public const COLUMNS = [
'audit_logs' => ['id', 'event', 'action', 'summary', 'subject_type', 'subject_id', 'subject_label', 'details', 'user_id', 'user_name', 'email', 'ip', 'user_agent', 'created_at'],
'audit_settings' => ['id', 'auto_ban', 'max_failures', 'window_minutes', 'ban_minutes', 'keep_days', 'created_at', 'updated_at'],
'block_templates' => ['id', 'resource', 'blocks', 'created_at', 'updated_at'],
'bot_hits' => ['id', 'day', 'bot', 'kind', 'path', 'hits'],
'group_user' => ['group_id', 'user_id', 'is_primary'],
'groups' => ['id', 'name', 'description', 'permissions', 'created_at', 'updated_at', 'start', 'require_two_factor'],
'ip_bans' => ['id', 'ip', 'reason', 'automatic', 'whole_site', 'user_id', 'user_name', 'hits', 'last_hit_at', 'expires_at', 'created_at'],
'ip_notes' => ['id', 'ip', 'group_name', 'note', 'user_id', 'user_name', 'created_at', 'updated_at'],
'lead_blocks' => ['id', 'type', 'value', 'reason', 'expires_at', 'user_id', 'user_name', 'created_at', 'updated_at'],
'lead_taps' => ['id', 'day', 'kind', 'path', 'taps'],
'leads' => ['id', 'kind', 'folder', 'reasons', 'name', 'phone', 'email', 'prefers', 'about', 'message', 'message_hash', 'answers', 'location', 'subject', 'subject_id', 'page', 'channel', 'source', 'ip', 'opened_at', 'handled_at', 'outcome', 'notes', 'notified_at', 'notify_failed', 'created_at', 'updated_at'],
'media' => ['id', 'disk', 'location', 'name', 'filename', 'mime_type', 'size', 'checksum', 'width', 'height', 'alt', 'focus_x', 'focus_y', 'uploaded_by', 'created_at', 'updated_at'],
'page_views' => ['id', 'token', 'visit_id', 'path', 'post_id', 'viewed_at', 'day', 'hour', 'seconds'],
'pages' => ['id', 'slug', 'title', 'body', 'is_published', 'created_at', 'updated_at', 'meta_description', 'content', 'blocks'],
'post_shares' => ['id', 'post_id', 'platform', 'status', 'text', 'external_id', 'external_url', 'error', 'dry_run', 'shared_at', 'created_at', 'updated_at'],
'social_connections' => ['name', 'access_token', 'expires_at', 'refresh_token', 'refresh_token_expires_at', 'account_id', 'account_name', 'details', 'created_at', 'updated_at'],
'post_labels' => ['id', 'type', 'name', 'created_at', 'updated_at'],
'posts' => ['id', 'title', 'slug', 'excerpt', 'body', 'cover_image', 'category', 'tags', 'is_featured', 'published_at', 'created_at', 'updated_at'],
'records' => ['id', 'resource', 'data', 'created_at', 'updated_at'],
'reviews' => ['id', 'author', 'place', 'rating', 'body', 'is_visible', 'created_at', 'updated_at'],
'revisions' => ['id', 'type', 'subject_id', 'user_id', 'user_name', 'data', 'changed', 'created_at'],
'settings' => ['id', 'key', 'value', 'is_secret', 'updated_by', 'created_at', 'updated_at'],
'storage_files' => ['id', 'path', 'remote_id'],
'two_factor' => ['id', 'user_id', 'secret', 'recovery_codes', 'confirmed_at', 'last_step', 'required_since', 'created_at', 'updated_at'],
'user_settings' => ['id', 'user_id', 'theme', 'created_at', 'updated_at', 'view', 'locale', 'tutorial'],
'work_costs' => ['id', 'fingerprint', 'date', 'name', 'version', 'model', 'usd', 'eur', 'usd_per_eur', 'priced_at', 'created_at', 'updated_at'],
'visitor_salts' => ['day', 'salt'],
'visits' => ['id', 'visitor_hash', 'started_at', 'last_seen_at', 'day', 'hour', 'weekday', 'entry_path', 'exit_path', 'channel', 'source', 'referrer_host', 'country', 'device', 'browser', 'os', 'page_views', 'seconds'],
];
public static function create(string $table, Closure $callback): bool
{
if (Schema::hasTable($table)) {
return false;
}
Schema::create($table, $callback);
return true;
}
/** Adds a column only when it is not there yet. */
public static function addColumn(string $table, string $column, Closure $callback): void
{
if (Schema::hasTable($table) && ! Schema::hasColumn($table, $column)) {
Schema::table($table, fn (Blueprint $blueprint) => $callback($blueprint));
}
}
/**
* The columns a table lacks for the package, by the name the package uses for it (`groups`, not `site_groups`).
* A table the package does not know, or that does not exist, lacks nothing.
*
* @return list<string>
*/
public static function missingColumns(string $name): array
{
$table = Names::table($name);
if (! isset(self::COLUMNS[$name]) || ! Schema::hasTable($table)) {
return [];
}
return array_values(array_filter(self::COLUMNS[$name], fn (string $column) => ! Schema::hasColumn($table, $column)));
}
}