softdelete users and migration script

This commit is contained in:
2026-08-01 14:02:44 +05:00
parent 4a7de76155
commit 46f130150a
4 changed files with 233 additions and 24 deletions
+31 -1
View File
@@ -12,17 +12,47 @@ CREATE TABLE IF NOT EXISTS customers (
PRIMARY KEY (id)
);
-- Soft delete: deleting a client hard-removes its radcheck/radusergroup/customers
-- rows but only SETS deleted_at here, so the human metadata survives and can be
-- restored/referenced. deleted_at IS NULL means "live". Uniqueness on mac_address
-- is enforced only among live rows via the generated active_mac column (NULL for
-- soft-deleted rows — MySQL/MariaDB allows many NULLs in a UNIQUE index), so a
-- re-added client coexists with any number of old soft-deleted copies.
-- groupname/status mirror the client's current group (radusergroup) and billing
-- status (customers): populated on add and kept in sync on edit. They stay on the
-- row when the client is soft-deleted, so a deleted client's group/status aren't
-- lost along with its hard-deleted RADIUS/billing rows.
CREATE TABLE IF NOT EXISTS radadmin_clients (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
mac_address VARCHAR(17) NOT NULL,
name VARCHAR(128) NULL,
phone VARCHAR(32) NULL,
alias VARCHAR(64) NULL,
groupname VARCHAR(64) NULL,
status VARCHAR(10) NULL,
created_at DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at DATETIME NULL DEFAULT NULL,
active_mac VARCHAR(17) AS (IF(deleted_at IS NULL, mac_address, NULL)) STORED,
PRIMARY KEY (id),
UNIQUE KEY uq_radadmin_clients_mac (mac_address)
UNIQUE KEY uq_radadmin_clients_active_mac (active_mac)
);
-- Soft-delete migration for radadmin_clients tables created before this change
-- (MariaDB syntax; idempotent, and a no-op on the fresh schema created above).
ALTER TABLE radadmin_clients
ADD COLUMN IF NOT EXISTS groupname VARCHAR(64) NULL AFTER alias;
ALTER TABLE radadmin_clients
ADD COLUMN IF NOT EXISTS status VARCHAR(10) NULL AFTER groupname;
ALTER TABLE radadmin_clients
ADD COLUMN IF NOT EXISTS deleted_at DATETIME NULL DEFAULT NULL AFTER created_at;
ALTER TABLE radadmin_clients
ADD COLUMN IF NOT EXISTS active_mac VARCHAR(17)
AS (IF(deleted_at IS NULL, mac_address, NULL)) STORED;
ALTER TABLE radadmin_clients
DROP INDEX IF EXISTS uq_radadmin_clients_mac;
ALTER TABLE radadmin_clients
ADD UNIQUE KEY IF NOT EXISTS uq_radadmin_clients_active_mac (active_mac);
CREATE TABLE IF NOT EXISTS radadmin_devices (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
mac_address VARCHAR(17) NOT NULL,