added writing initial state history
This commit is contained in:
@@ -6,7 +6,6 @@ CREATE TABLE IF NOT EXISTS account_states (
|
|||||||
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||||
UNIQUE KEY uq_account_states_name (name)
|
UNIQUE KEY uq_account_states_name (name)
|
||||||
);
|
);
|
||||||
|
|
||||||
INSERT IGNORE INTO account_states (name)
|
INSERT IGNORE INTO account_states (name)
|
||||||
VALUES ('guest'),
|
VALUES ('guest'),
|
||||||
('applicant'),
|
('applicant'),
|
||||||
@@ -16,15 +15,12 @@ VALUES ('guest'),
|
|||||||
('suspended'),
|
('suspended'),
|
||||||
('banned'),
|
('banned'),
|
||||||
('denied');
|
('denied');
|
||||||
|
|
||||||
ALTER TABLE members
|
ALTER TABLE members
|
||||||
RENAME COLUMN state TO state_legacy;
|
RENAME COLUMN state TO state_legacy;
|
||||||
|
|
||||||
ALTER TABLE members
|
ALTER TABLE members
|
||||||
ADD COLUMN state INT NOT NULL DEFAULT 1,
|
ADD COLUMN state INT NOT NULL DEFAULT 1,
|
||||||
ADD INDEX idx_members_state (state),
|
ADD INDEX idx_members_state (state),
|
||||||
ADD CONSTRAINT fk_members_state_id FOREIGN KEY (state) REFERENCES account_states(id);
|
ADD CONSTRAINT fk_members_state_id FOREIGN KEY (state) REFERENCES account_states(id);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS member_state_history (
|
CREATE TABLE IF NOT EXISTS member_state_history (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
member_id INT NOT NULL,
|
member_id INT NOT NULL,
|
||||||
@@ -40,8 +36,22 @@ CREATE TABLE IF NOT EXISTS member_state_history (
|
|||||||
CONSTRAINT fk_member_state_type FOREIGN KEY (state_id) REFERENCES account_states(id),
|
CONSTRAINT fk_member_state_type FOREIGN KEY (state_id) REFERENCES account_states(id),
|
||||||
CONSTRAINT fk_member_state_history_created_by FOREIGN KEY (created_by_id) REFERENCES members(id)
|
CONSTRAINT fk_member_state_history_created_by FOREIGN KEY (created_by_id) REFERENCES members(id)
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Convert member states to new system
|
-- Convert member states to new system
|
||||||
UPDATE members m
|
UPDATE members m
|
||||||
JOIN account_states s ON m.state_legacy = s.name
|
JOIN account_states s ON m.state_legacy = s.name
|
||||||
SET m.state = s.id;
|
SET m.state = s.id;
|
||||||
|
|
||||||
|
-- Initial history population
|
||||||
|
INSERT INTO member_state_history (
|
||||||
|
member_id,
|
||||||
|
state_id,
|
||||||
|
reason,
|
||||||
|
start_date,
|
||||||
|
created_at
|
||||||
|
)
|
||||||
|
SELECT id,
|
||||||
|
state,
|
||||||
|
'history start',
|
||||||
|
CURDATE(),
|
||||||
|
NOW()
|
||||||
|
FROM members;
|
||||||
Reference in New Issue
Block a user