-- =============================================================================
-- 06-post-migration-verify.sql
-- Run after full upgrade. Compare counts to 00-preflight-checks.sql snapshot.
-- =============================================================================

SELECT COUNT(*) AS members_count FROM members;
SELECT COUNT(*) AS attendances_count FROM attendances;
SELECT COUNT(*) AS leaves_count FROM leaves;

SELECT id, timestamp, name FROM migrations ORDER BY timestamp;

SELECT slug, name, owner_id FROM access_roles ORDER BY owner_id, hierarchy DESC;

SELECT COUNT(*) AS role_permissions_count FROM role_permissions;

SELECT COUNT(*) AS members_missing_access_role_id
FROM members
WHERE access_role_id IS NULL;

SELECT
  m.role,
  COUNT(*) AS cnt,
  SUM(m.access_role_id IS NULL) AS missing_role
FROM members m
GROUP BY m.role;

-- Expected pending migrations for TypeORM (should return 0 rows if all applied):
-- SELECT name FROM migrations WHERE name IN (
--   'AccessControl1780900000000',
--   'MemberAccessRole1780910000000',
--   'UpgradeLegacyScopeValues1780920000000',
--   'OwnerScopeValuesAll1780930000000',
--   'OwnerScopeUseAllMembers1780940000000'
-- );
