πŸ—„οΈ PHPStrap Database Structure Guide

This guide explains the core tables, relationships, and best practices for working with the PhPstrap database.

πŸ” Overview

PhPstrap uses a lightweight MySQL schema centered on users, settings, and optional feature tables (modules, invites, tokens, purchases, logs, affiliates). The schema is designed to be modular: you can run a minimal core or enable related features as needed.

🧩 Relationships (Text ERD)

users (id) ─┬─< affiliate_clicks.user_id
            β”œβ”€< affiliate_signups.user_id
            β”œβ”€< affiliate_signups.referred_user_id
            β”œβ”€< invites.generated_by
            β”œβ”€  invites.used_by (nullable)
            β”œβ”€< password_resets.user_id
            β”œβ”€< token_purchases.user_id
            β”œβ”€< user_tokens.user_id
            └─< withdrawals.user_id
withdrawals.processed_by (nullable) ──> users.id

settings.updated_by (nullable) ──> users.id
modules (no FKs)
admin_logs (optional; often includes actor_id -> users.id)
  

πŸ›οΈ Core Tables

users

The primary identity table. Tracks authentication, verification, membership, balances, and API access.

ColumnTypeNotes
id (PK)INT AUTO_INCREMENTPrimary key
nameVARCHAR(100)Display name
EmailVARCHAR(191)Unique; indexed
passwordVARCHAR(255)BCrypt/Password_hash()
is_adminTINYINT(1)Admin flag
verifiedTINYINT(1)Email verified
verified_atDATETIME NULLVerification timestamp
membership_statusENUM/VARe.g., free/premium
api_tokenVARCHAR(64)For API access
created_at / updated_atDATETIMETimestamps

settings

Key-value configuration store. Also used for app_version as the canonical version flag.

ColumnTypeNotes
key (PK)VARCHAR(191)Unique setting key
valueTEXTSerialized/JSON/string
updated_byINT NULLFK β†’ users.id (nullable)
updated_atDATETIMELast change

modules

Registry for optional features (hcaptcha/smtp/analytics). Stores versions, enablement, and module-specific JSON.

ColumnTypeNotes
name (PK)VARCHAR(100)Machine name (e.g., "smtp")
titleVARCHAR(150)Display title
descriptionTEXTModule purpose
versionVARCHAR(20)Module version
enabledTINYINT(1)Feature toggle
settingsJSON/TEXTModule config
hooksJSON/TEXTEvent hooks

πŸ’³ Accounts, Tokens & Access

user_tokens

Tracks user token balances/credits (for quotas or billing).

ColumnTypeNotes
id (PK)INT AUTO_INCREMENTPrimary key
user_idINTFK β†’ users.id
balanceINTCurrent token count
updated_atDATETIMELast update

token_purchases

Token purchase ledger (auditing/accounting).

ColumnTypeNotes
id (PK)INT AUTO_INCREMENTPrimary key
user_idINTFK β†’ users.id
amountINTTokens purchased
referenceVARCHAR(255)Payment ref/txn id
created_atDATETIMEPurchase time

withdrawals

Tracks payout/withdrawal requests and processing.

ColumnTypeNotes
id (PK)INT AUTO_INCREMENTPrimary key
user_idINTFK β†’ users.id
amountDECIMAL(10,2)Payout amount
statusENUM/VARpending/approved/paid/rejected
processed_byINT NULLFK β†’ users.id (admin, nullable)
created_at / updated_atDATETIMETimestamps

πŸ” Access & Security

password_resets

One-time reset tokens with expirations.

ColumnTypeNotes
id (PK)INT AUTO_INCREMENTPrimary key
user_idINTFK β†’ users.id
tokenVARCHAR(64)Unique; indexed
expires_atDATETIMEExpiry time
created_atDATETIMEIssued time

invites

Invitation codes and their usage (referral/onboarding).

ColumnTypeNotes
id (PK)INT AUTO_INCREMENTPrimary key
codeVARCHAR(32)Unique; indexed
generated_byINTFK β†’ users.id
used_byINT NULLFK β†’ users.id (nullable)
used_atDATETIME NULLRedemption time
created_atDATETIMEIssued time

🀝 Affiliate (Optional)

affiliate_clicks

Click events attributed to an affiliate user.

ColumnTypeNotes
id (PK)INT AUTO_INCREMENTPrimary key
user_idINTFK β†’ users.id (affiliate)
ipVARCHAR(45)IPv4/IPv6
uaVARCHAR(255)User agent
created_atDATETIMEEvent time

affiliate_signups

New signups attributed to an affiliate, including referred user.

ColumnTypeNotes
id (PK)INT AUTO_INCREMENTPrimary key
user_idINTFK β†’ users.id (affiliate)
referred_user_idINTFK β†’ users.id (new user)
created_atDATETIMEEvent time

πŸ“ Admin & Observability (Optional)

admin_logs

Administrative activity trail (actions performed in the admin UI or background jobs).

ColumnTypeNotes
id (PK)INT AUTO_INCREMENTPrimary key
actionVARCHAR(100)Event name/key
actor_idINT NULLFK β†’ users.id (nullable)
contextJSON/TEXTExtra metadata
created_atDATETIMEWhen it happened

βš™οΈ Indexing & Performance

  • users.email should be UNIQUE INDEX.
  • invites.code, password_resets.token should be indexed/unique.
  • Foreign keys (where enabled) should have indexes on the child columns: user_tokens.user_id, withdrawals.user_id, etc.
  • Consider pruning logs and old resetsβ€”see retention below.

🏷️ Versioning (Source of Truth)

The canonical application version is stored in settings.app_version. A code-side fallback exists in /config/version.php for cold-starts and installer flows.

πŸ“¦ Migrations & Retention

  • Installer: The installer creates tables and seeds defaults. Re-running safely detects existing tables.
  • Updater: The admin updater writes the new app_version to settings after a successful file update.
  • Backups: Always back up DB before structural changes or major upgrades.
  • Retention: Periodically delete expired password_resets, old admin_logs, and stale affiliate_clicks as policy allows.

For questions or contributions, open an issue on GitHub.