DB設計(mikyun_cloud)
1. DB基本情報
項目内容
DB名mikyun_cloud
エンジンMariaDB
配置R640(host 127.0.0.1:3306)
ユーザーmikyun
2. テーブル一覧
種別名前用途
TABLEcontracts契約本体
TABLEcustomer_devices顧客端末 / WireGuard端末
TABLEadmin_logs管理操作ログ
TABLEcontract_notes顧客メモ・通話記録
VIEWv_parent_conflicts親端末重複監査
3. contracts(契約テーブル)

契約本体。顧客ID、サブネット、容量、上限、状態を持つ。

カラム名説明
idbigint PK auto内部ID
contract_numberint unsigned UNI連番(表示用)
contract_idvarchar(32) UNI契約ID(例: 0001, M010)永久不変
vpn_subnetvarchar(32)割当VPNサブネット(現在は固定 10.251.0.0/24)
next_child_ipvarchar(32)次割当IP候補(現在はDBスキャンで決定するため未使用)
customer_namevarchar(128)顧客名
customer_emailvarchar(255) MULメールアドレス
customer_phonevarchar(64)電話番号
plan_namevarchar(64)プラン名(例: standard)
storage_quota_gbint DEFAULT 200契約総容量 (GB)
max_devicesint DEFAULT 3最大端末数
max_filesint DEFAULT 10000最大ファイル数
bandwidth_profilevarchar(64)帯域プロファイル(例: standard)
contract_statusvarchar(16) MULactive / paused / terminated / deleted
identity_passphrasetext顧客ポータルログイン用パスフレーズ
nc_passwordvarchar(255)Nextcloud親アカウントPW
nc_quota_gbint DEFAULT 200NC割当容量 (GB)
opt_video_calltinyint DEFAULT 0ビデオ通話オプション
created_at / updated_atdatetime作成・更新日時
4. customer_devices(端末テーブル)

契約配下の端末管理テーブル。WireGuardの鍵・IP・状態・統計を持つ中核テーブル。

カラム名説明
idbigint PK autoレコードID
contract_idvarchar(32) MUL所属契約ID
device_idvarchar(64) UNI端末識別ID(例: 0001-DEV-001)
wg_ifacevarchar(32)WGインターフェース名(wg0 / wg1)
wg_ipvarchar(64) UNI割当WG IP(10.200.0.x / 10.251.0.x)
public_keytext UNIWGクライアント公開鍵
private_keytextWGクライアント秘密鍵(DB保存)
preshared_keytextWG事前共有鍵
device_rolevarchar(16)parent / child
is_parenttinyint DEFAULT 0親端末フラグ(1=親)
statusvarchar(16) MULissued / active / paused / revoked / expired / deleted
device_namevarchar(128)端末名
platformvarchar(64)iPhone / Android / Windows 等
app_versionvarchar(64)アプリバージョン
issued_atdatetimeQR発行日時
qr_expires_atdatetimeQR失効日時(発行+24h)
first_handshake_atdatetime MUL初回WG接続日時
last_handshake_atdatetime MUL最終WG接続日時
activated_atdatetime有効化日時
parent_changed_atdatetime親変更日時
parent_changed_byvarchar(64)customer / admin
parent_change_reasonvarchar(255)変更理由
paused_atdatetime一時停止日時
pause_reasonvarchar(255)一時停止理由
rx_bytes_total / tx_bytes_totalbigint DEFAULT 0累計受信/送信バイト数
rx_bytes_24h / tx_bytes_24hbigint DEFAULT 024h受信/送信バイト数
rx_bytes_month / tx_bytes_monthbigint DEFAULT 0月次受信/送信バイト数
storage_quota_gbint DEFAULT 0端末個別割当容量 (GB)
nc_user / nc_passwordvarcharNextcloudユーザー名/PW
nc_quota_gbintNC容量
nc_enabledtinyint DEFAULT 1NC有効フラグ
created_at / updated_atdatetime作成・更新日時
5. admin_logs(管理操作ログ)
カラム名説明
idbigint PK auto内部ID
action_typevarchar(64)操作種別
target_typevarchar(64)対象種別(contract / device)
target_idvarchar(64)対象ID
messagetext操作内容テキスト
created_atdatetime auto記録日時
6. v_parent_conflicts(VIEWテーブル)

1契約に親端末が複数存在する異常状態を検出するためのビュー。定期確認推奨。

SELECT contract_id, COUNT(*) AS parent_count
FROM customer_devices
WHERE is_parent = 1 AND deleted_at IS NULL
GROUP BY contract_id
HAVING COUNT(*) > 1