CREATE TABLE perm_entity(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    platform TEXT NOT NULL,
    entity_type TEXT NOT NULL,
    external_id TEXT NOT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE UNIQUE INDEX idx_perm_entity_lookup
ON perm_entity(platform, entity_type, external_id);

CREATE TABLE perm_info(
    entity_id INTEGER NOT NULL,
    config_key TEXT NOT NULL,
    value BOOLEAN,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    -- 联合主键
    PRIMARY KEY (entity_id, config_key)
);

CREATE TRIGGER perm_entity_update AFTER UPDATE
ON perm_entity BEGIN
    UPDATE perm_entity SET updated_at=CURRENT_TIMESTAMP WHERE id=old.id;
END;
CREATE TRIGGER perm_info_update AFTER UPDATE
ON perm_info BEGIN
    UPDATE perm_info SET updated_at=CURRENT_TIMESTAMP WHERE entity_id=old.entity_id;
END;

