MCPcopy
hub / github.com/CapSoftware/Cap / validateBackfill

Function validateBackfill

packages/database/migrations/orgid_backfill.ts:76–131  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

74}
75
76async function validateBackfill(): Promise<void> {
77 console.log("🔍 Validating backfill results...");
78
79 // Count videos still missing orgId
80 const videosWithoutOrgId = await db()
81 .select({ count: sql<number>`count(*)` })
82 .from(videos)
83 .where(isNull(videos.orgId));
84
85 // Count users still missing defaultOrgId where they have activeOrganizationId
86 const usersWithoutDefaultOrgId = await db()
87 .select({ count: sql<number>`count(*)` })
88 .from(users)
89 .where(
90 and(isNull(users.defaultOrgId), isNotNull(users.activeOrganizationId)),
91 );
92
93 // Count videos with mismatched orgId (different from owner's activeOrganizationId)
94 const videosWithMismatchedOrgId = await db()
95 .select({ count: sql<number>`count(*)` })
96 .from(videos)
97 .innerJoin(users, eq(videos.ownerId, users.id))
98 .where(
99 and(
100 isNotNull(videos.orgId),
101 isNotNull(users.activeOrganizationId),
102 sql`videos.orgId != users.activeOrganizationId`,
103 ),
104 );
105
106 // Count users with mismatched defaultOrgId (different from activeOrganizationId)
107 const usersWithMismatchedDefaultOrgId = await db()
108 .select({ count: sql<number>`count(*)` })
109 .from(users)
110 .where(
111 and(
112 isNotNull(users.defaultOrgId),
113 isNotNull(users.activeOrganizationId),
114 sql`users.defaultOrgId != users.activeOrganizationId`,
115 ),
116 );
117
118 console.log("📊 Validation results:");
119 console.log(
120 ` Videos still missing orgId: ${videosWithoutOrgId[0]?.count || 0}`,
121 );
122 console.log(
123 ` Users still missing defaultOrgId: ${usersWithoutDefaultOrgId[0]?.count || 0}`,
124 );
125 console.log(
126 ` Videos with mismatched orgId: ${videosWithMismatchedOrgId[0]?.count || 0}`,
127 );
128 console.log(
129 ` Users with mismatched defaultOrgId: ${usersWithMismatchedDefaultOrgId[0]?.count || 0}`,
130 );
131}
132
133async function getInitialStats(): Promise<void> {

Callers 1

runOrgIdBackfillFunction · 0.85

Calls 1

dbFunction · 0.90

Tested by

no test coverage detected