Skip to content

Database Schema

LoyaltyBay extends the default WordPress database with custom tables to log transactions, cache balances, and track background jobs.


  • Prefix: All tables inherit the active WordPress database prefix (e.g. wp_).
  • Engine: Custom tables are created using the InnoDB storage engine.

[!CAUTION] The InnoDB engine is mandatory for transaction processing. LoyaltyBay relies on row-level locking (SELECT ... FOR UPDATE) during checkout processing to guarantee race-condition safety. If converted to MyISAM, table locks will fail, allowing double-spending of points.


Upon plugin activation, three custom tables are created via the WordPress dbDelta() system:

This table is an append-only log of every individual point transaction (credits and debits) in the system.

Column NameData TypeNullKeyDefaultDescription
idbigint(20) unsignedNoPRIAuto-incrementUnique ledger entry ID.
user_idbigint(20) unsignedNoMULWordPress User ID.
transaction_typevarchar(20)NoEither credit or debit.
amountdecimal(10,2)NoPoint value (fractional support).
reference_typevarchar(50)NoEvent source: order, refund, registration, review, referral, admin, expiry.
reference_idbigint(20) unsignedNoEvent source database ID.
descriptionvarchar(255)YesNULLPlain-text description.
expires_atdatetimeYesNULLExpiration timestamp (planned).
created_atdatetimeNoCURRENT_TIMESTAMPTransaction timestamp.
  • Idempotency Index: A unique index unique_transaction is defined on (user_id, reference_type, reference_id, transaction_type). This DB-level restriction prevents duplicate credits or debits for the same event (such as refreshing a checkout page).
  • Performance Index: Index idx_user_created is defined on (user_id, created_at) to optimize customer dashboard history queries.

This table caches the calculated point balance for each customer to avoid executing expensive sum queries across the entire ledger.

Column NameData TypeNullKeyDefaultDescription
user_idbigint(20) unsignedNoPRIWordPress User ID.
balancedecimal(10,2)No0.00Current available points balance.
last_updateddatetimeNoCURRENT_TIMESTAMPLast cache update timestamp.
  • Cache Updates: When a points transaction is committed, LedgerService updates this table’s balance row. In addition, user meta loyaltybay_balance is denormalized in parallel for sub-20ms reads.

Logs the execution history and status of all scheduled background jobs.

Column NameData TypeNullKeyDefaultDescription
idbigint(20) unsignedNoPRIAuto-incrementLog ID.
job_namevarchar(100)NoRegistered cron job handle.
statusvarchar(20)NoMULJob state: started, completed, failed.
payloadtextYesNULLJSON string of job arguments.
error_messagetextYesNULLPHP error message string if failed.
retry_counttinyint(4)No0Number of automated retries executed.
started_atdatetimeNoJob start timestamp.
completed_atdatetimeYesNULLJob completion timestamp.

LoyaltyBay stores user-specific state in the default WordPress wp_usermeta table:

  • loyaltybay_balance: Denormalized balance cache (float) for fast read operations.
  • loyaltybay_vip_tier: The slug of the customer’s active VIP loyalty level (e.g. gold).
  • loyaltybay_referral_code: Unique alpha-numeric string assigned to the user for referral links.
  • loyaltybay_referred_by: The User ID of the customer who referred this user.
  • loyaltybay_referral_rewarded: Boolean check tracking if the referrer has been rewarded for this signup.

Stored in wp_options:

  • loyaltybay: Serialized array containing all active settings.
  • loyaltybay_version: The database version string (used by Activator to trigger database migrations on upgrade).

  • Deactivation: Keeps all database tables, options, and user meta intact. Deactivation only clears scheduled cron jobs and removes customer rewrite endpoints.
  • Uninstallation: Deletes all data only if Delete All on Uninstall (advanced_deleteAllOnUninstall) is checked in settings. If enabled, deleting the plugin will:
    1. Execute DROP TABLE on the three custom loyalty tables.
    2. Delete options loyaltybay and loyaltybay_version.
    3. Clear all associated user meta.
    4. Remove the manage_loyaltybay capability from all WordPress user roles.