-- permission-v2 stage 1 finalize for MySQL 8.0
-- purpose:
-- 1. verify required user-position data is complete
-- 2. convert system_users.position_id to NOT NULL
--
-- execute this script only after:
-- 1. 01/02/03 have completed successfully
-- 2. every existing user has a valid position_id

SELECT COUNT(*) AS missing_position_count
FROM system_users
WHERE position_id IS NULL;

SET @missing_position_count := (
  SELECT COUNT(*) FROM system_users WHERE position_id IS NULL
);

SET @finalize_sql := IF(
  @missing_position_count = 0,
  'ALTER TABLE system_users MODIFY COLUMN position_id BIGINT NOT NULL COMMENT ''主岗位ID''',
  'SELECT ''skip finalize: system_users.position_id still has NULL rows'' AS message'
);

PREPARE stmt FROM @finalize_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- verification:
-- SHOW CREATE TABLE system_users;
