Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rethinkdb): OrganizationUser: Phase 2 #9953

Merged
merged 69 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
bfa5c21
chore: read ReflectionGroups from PG
mattkrick May 29, 2024
7ac086e
Merge branch 'master' into chore/retrogroups3
mattkrick May 29, 2024
9844960
Merge branch 'master' into chore/retrogroups3
mattkrick May 30, 2024
62e123c
chore: add PG RetroReflection table
mattkrick May 30, 2024
c38f86c
chore: write to PG RetroReflection table
mattkrick Jun 3, 2024
d318560
fixup: inserts and updates
mattkrick Jun 4, 2024
16843dc
Merge branch 'master' into retroReflection-phase1e
mattkrick Jun 4, 2024
abb3bdf
fix: move to record literal types
mattkrick Jun 4, 2024
9911448
fix types
mattkrick Jun 4, 2024
af03b31
chore: migrate reflections to PG
mattkrick Jun 6, 2024
2a3c0ca
Merge branch 'master' into retroReflection-phase1e
mattkrick Jun 6, 2024
8dfa870
fix: remove hard delete of inactive groups
mattkrick Jun 6, 2024
3b48d22
Merge branch 'retroReflection-phase1e' into chore/retroReflection-phase2
mattkrick Jun 6, 2024
24da04b
fixup: remove unused var
mattkrick Jun 6, 2024
c60e502
Merge branch 'retroReflection-phase1e' into chore/retroReflection-phase2
mattkrick Jun 6, 2024
0d2c7d7
handle escape chars and commas
mattkrick Jun 6, 2024
9ef9346
chore: begin building equality checker
mattkrick Jun 6, 2024
76aeff9
fixup: account for lots of formatting in content
mattkrick Jun 7, 2024
0830c8e
Merge branch 'retroReflection-phase1e' into chore/retroReflection-phase2
mattkrick Jun 24, 2024
a12b08a
fix: rename extra spaces for conversion from plaintext to content
mattkrick Jun 24, 2024
a732657
fix: rename Reactji composite type attributes
mattkrick Jun 24, 2024
954503d
fix: constructor prop in lookup table
mattkrick Jun 25, 2024
c7775ad
handle reactji migration
mattkrick Jun 25, 2024
d53aca4
change dataloaders to pg
mattkrick Jun 25, 2024
6689e39
fix: self-review
mattkrick Jun 25, 2024
a6fd0d3
Merge branch 'master' into chore/retroReflection-phase2
mattkrick Jun 25, 2024
00964a8
Merge branch 'chore/retroReflection-phase2' into chore/retroReflectio…
mattkrick Jun 25, 2024
50ca56b
fix: migration order rename
mattkrick Jun 25, 2024
b220f22
Merge branch 'chore/retroReflection-phase2' into chore/retroReflectio…
mattkrick Jun 25, 2024
22ca4f3
chore: write to PG
mattkrick Jun 25, 2024
9c55acd
chore: migrate old records to PG
mattkrick Jun 25, 2024
9dfd35c
fix: add teamid FK
mattkrick Jun 25, 2024
654aa42
Merge branch 'chore/timeline-phase1' into chore/timeline-phase2
mattkrick Jun 25, 2024
ebbea29
chore: read from pg
mattkrick Jun 25, 2024
7dbea0d
Merge branch 'master' into chore/timeline-phase2
mattkrick Jun 26, 2024
3fd2236
Merge branch 'chore/timeline-phase2' into chore/timeline-phase3
mattkrick Jun 26, 2024
c552ea6
fix bad merge
mattkrick Jun 26, 2024
50d9e77
Merge branch 'chore/timeline-phase2' into chore/timeline-phase3
mattkrick Jun 26, 2024
b7c092d
add migration
mattkrick Jun 26, 2024
fe9efe9
write to pg
mattkrick Jun 27, 2024
4d838f3
Merge branch 'master' into chore/organization-phase1
mattkrick Jun 27, 2024
34b05bf
Merge branch 'master' into chore/organization-phase1
mattkrick Jul 1, 2024
02aa23d
Merge branch 'master' into chore/organization-phase1
mattkrick Jul 1, 2024
dfba81e
Merge branch 'master' into chore/organization-phase1
mattkrick Jul 1, 2024
81d68b7
Merge branch 'master' into chore/organization-phase1
mattkrick Jul 3, 2024
f4f5ede
fix: array append/remove
mattkrick Jul 3, 2024
dcbcd78
chore: migrate existing orgs to PG
mattkrick Jul 3, 2024
b329fd3
remove RethinkDB.Organization
mattkrick Jul 4, 2024
0a549d6
Merge branch 'master' into chore/organization-phase2
mattkrick Jul 4, 2024
5f6bdfc
Merge branch 'chore/organization-phase2' into chore/organization-phase3
mattkrick Jul 4, 2024
21876d9
Merge branch 'master' into chore/organization-phase3
mattkrick Jul 4, 2024
af856f2
fix: isRequestToJoin tests
mattkrick Jul 8, 2024
129d1a9
fix: isOrgVerified
mattkrick Jul 8, 2024
6e368a4
fix deleteUser test
mattkrick Jul 8, 2024
27b0cf2
fix: comment
mattkrick Jul 9, 2024
b5422dd
Merge branch 'master' into chore/organization-phase3
mattkrick Jul 9, 2024
3c061e2
fix: remove newUserUntil
mattkrick Jul 9, 2024
1cdf979
chore: replace half of OrganizationUser instances
mattkrick Jul 9, 2024
473c0c5
remove newUserUntil on client
mattkrick Jul 10, 2024
49be1bc
fix: update all instances of OrganizationUser
mattkrick Jul 10, 2024
65614c6
Merge branch 'master' into chore/organizationUser-phase1
mattkrick Jul 10, 2024
ca01882
fix: bad merge
mattkrick Jul 10, 2024
44d153e
re-add rethinkdb to server tests
mattkrick Jul 10, 2024
303f493
chore: migrate existing data
mattkrick Jul 10, 2024
3c3b46c
fix tests
mattkrick Jul 10, 2024
3e3634c
fix: use test schema
mattkrick Jul 10, 2024
8d33999
fix: safer queries
mattkrick Jul 10, 2024
78dcb46
Merge branch 'chore/organizationUser-phase1' into chore/organizationU…
mattkrick Jul 10, 2024
24a2698
Merge branch 'master' into chore/organizationUser-phase2
mattkrick Jul 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 16 additions & 46 deletions packages/server/graphql/private/mutations/checkRethinkPgEquality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@ import getRethink from '../../../database/rethinkDriver'
import getFileStoreManager from '../../../fileStorage/getFileStoreManager'
import getKysely from '../../../postgres/getKysely'
import {checkRowCount, checkTableEq} from '../../../postgres/utils/checkEqBase'
import {
compareDateAlmostEqual,
compareRValOptionalPluckedObject,
compareRValStringAsNumber,
compareRValUndefinedAsEmptyArray,
compareRValUndefinedAsFalse,
compareRValUndefinedAsNull,
compareRValUndefinedAsNullAndTruncateRVal,
compareRValUndefinedAsZero,
defaultEqFn
} from '../../../postgres/utils/rethinkEqualityFns'
import {compareRValUndefinedAsNull, defaultEqFn} from '../../../postgres/utils/rethinkEqualityFns'
import {MutationResolvers} from '../resolverTypes'

const handleResult = async (
Expand All @@ -37,55 +27,35 @@ const checkRethinkPgEquality: MutationResolvers['checkRethinkPgEquality'] = asyn
) => {
const r = await getRethink()

if (tableName === 'Organization') {
if (tableName === 'OrganizationUser') {
const rowCountResult = await checkRowCount(tableName)
const rethinkQuery = (updatedAt: Date, id: string | number) => {
const rethinkQuery = (joinedAt: Date, id: string | number) => {
return r
.table('Organization' as any)
.between([updatedAt, id], [r.maxval, r.maxval], {
index: 'updatedAtId',
.table('OrganizationUser' as any)
.between([joinedAt, id], [r.maxval, r.maxval], {
index: 'joinedAtId',
leftBound: 'open',
rightBound: 'closed'
})
.orderBy({index: 'updatedAtId'}) as any
.orderBy({index: 'joinedAtId'}) as any
}
const pgQuery = async (ids: string[]) => {
return getKysely()
.selectFrom('Organization')
.selectAll()
.select(({fn}) => [fn('to_json', ['creditCard']).as('creditCard')])
.where('id', 'in', ids)
.execute()
return getKysely().selectFrom('OrganizationUser').selectAll().where('id', 'in', ids).execute()
}
const errors = await checkTableEq(
rethinkQuery,
pgQuery,
{
id: defaultEqFn,
activeDomain: compareRValUndefinedAsNullAndTruncateRVal(100),
isActiveDomainTouched: compareRValUndefinedAsFalse,
creditCard: compareRValOptionalPluckedObject({
brand: compareRValUndefinedAsNull,
expiry: compareRValUndefinedAsNull,
last4: compareRValStringAsNumber
}),
createdAt: defaultEqFn,
name: compareRValUndefinedAsNullAndTruncateRVal(100),
payLaterClickCount: compareRValUndefinedAsZero,
periodEnd: compareRValUndefinedAsNull,
periodStart: compareRValUndefinedAsNull,
picture: compareRValUndefinedAsNull,
showConversionModal: compareRValUndefinedAsFalse,
stripeId: compareRValUndefinedAsNull,
stripeSubscriptionId: compareRValUndefinedAsNull,
upcomingInvoiceEmailSentAt: compareRValUndefinedAsNull,
suggestedTier: compareRValUndefinedAsNull,
inactive: defaultEqFn,
joinedAt: defaultEqFn,
orgId: defaultEqFn,
removedAt: defaultEqFn,
role: compareRValUndefinedAsNull,
userId: defaultEqFn,
tier: defaultEqFn,
tierLimitExceededAt: compareRValUndefinedAsNull,
trialStartDate: compareRValUndefinedAsNull,
scheduledLockAt: compareRValUndefinedAsNull,
lockedAt: compareRValUndefinedAsNull,
updatedAt: compareDateAlmostEqual,
featureFlags: compareRValUndefinedAsEmptyArray
trialStartDate: compareRValUndefinedAsNull
},
maxErrors
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import {Kysely, PostgresDialect} from 'kysely'
import {r} from 'rethinkdb-ts'
import connectRethinkDB from '../../database/connectRethinkDB'
import getPg from '../getPg'

export async function up() {
await connectRethinkDB()
const pg = new Kysely<any>({
dialect: new PostgresDialect({
pool: getPg()
})
})
try {
console.log('Adding index')
await r
.table('OrganizationUser')
.indexCreate('joinedAtId', (row: any) => [row('joinedAt'), row('id')])
.run()
await r.table('OrganizationUser').indexWait().run()
} catch {
// index already exists
}
await r.table('OrganizationUser').get('aGhostOrganizationUser').update({tier: 'enterprise'}).run()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove hardcoded ghost organization user

The hardcoded update for aGhostOrganizationUser should be documented or removed if not necessary.

- await r.table('OrganizationUser').get('aGhostOrganizationUser').update({tier: 'enterprise'}).run()
+ // Consider removing or documenting the reason for this update

Committable suggestion was skipped due to low confidence.

await console.log('Adding index complete')
const MAX_PG_PARAMS = 65545
const PG_COLS = [
'id',
'suggestedTier',
'inactive',
'joinedAt',
'orgId',
'removedAt',
'role',
'userId',
'tier',
'trialStartDate'
] as const
type OrganizationUser = {
[K in (typeof PG_COLS)[number]]: any
}
const BATCH_SIZE = Math.trunc(MAX_PG_PARAMS / PG_COLS.length)

let curjoinedAt = r.minval
let curId = r.minval
for (let i = 0; i < 1e6; i++) {
console.log('inserting row', i * BATCH_SIZE, curjoinedAt, curId)
const rawRowsToInsert = (await r
.table('OrganizationUser')
.between([curjoinedAt, curId], [r.maxval, r.maxval], {
index: 'joinedAtId',
leftBound: 'open',
rightBound: 'closed'
})
.orderBy({index: 'joinedAtId'})
.limit(BATCH_SIZE)
.pluck(...PG_COLS)
.run()) as OrganizationUser[]

const rowsToInsert = rawRowsToInsert.map((row) => {
const {newUserUntil, ...rest} = row as any
return {
...rest
}
})
if (rowsToInsert.length === 0) break
const lastRow = rowsToInsert[rowsToInsert.length - 1]
curjoinedAt = lastRow.joinedAt
curId = lastRow.id
try {
await pg
.insertInto('OrganizationUser')
.values(rowsToInsert)
.onConflict((oc) => oc.doNothing())
.execute()
} catch (e) {
await Promise.all(
rowsToInsert.map(async (row) => {
try {
await pg
.insertInto('OrganizationUser')
.values(row)
.onConflict((oc) => oc.doNothing())
.execute()
} catch (e) {
if (e.constraint === 'fk_userId' || e.constraint === 'fk_orgId') {
console.log(`Skipping ${row.id} because it has no user/org`)
return
}
console.log(e, row)
}
})
)
}
}
}

export async function down() {
await connectRethinkDB()
try {
await r.table('OrganizationUser').indexDrop('joinedAtId').run()
} catch {
// index already dropped
}
const pg = new Kysely<any>({
dialect: new PostgresDialect({
pool: getPg()
})
})
await pg.deleteFrom('OrganizationUser').execute()
}
4 changes: 2 additions & 2 deletions packages/server/postgres/utils/checkEqBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const checkRowCount = async (tableName: string) => {
}

export async function checkTableEq(
rethinkQuery: (updatedAt: Date, id: string | number) => RSelection,
rethinkQuery: (joinedAt: Date, id: string | number) => RSelection,
pgQuery: (ids: string[]) => Promise<PGDoc[] | null>,
equalityMap: Record<string, (a: unknown, b: unknown) => boolean>,
maxErrors: number | null | undefined
Expand All @@ -51,7 +51,7 @@ export async function checkTableEq(
.run()) as RethinkDoc[]
if (rethinkRows.length === 0) break
const lastRow = rethinkRows[rethinkRows.length - 1]!
curUpdatedDate = lastRow.updatedAt
curUpdatedDate = lastRow.joinedAt
Copy link
Contributor

@coderabbitai coderabbitai bot Jul 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimise the loop condition.

The loop condition for (let i = 0; i < 1e6; i++) is arbitrary. Consider optimising it based on actual data size.

-  for (let i = 0; i < 1e6; i++) {
+  while (true) {

Committable suggestion was skipped due to low confidence.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using a while true loop would open us up to a threat of an infinite loop, right? isn't that worse than an arbitrarily big for loop?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattkrick, you are correct that using a while (true) loop without a proper exit condition can lead to an infinite loop, which is indeed worse than an arbitrarily large for loop. A more robust approach would involve setting a logical exit condition based on the actual data size or another relevant criterion. This way, we can avoid the risk of an infinite loop while also not relying on an arbitrary large number.

Would you like me to propose a specific solution or gather more information on the data size to determine a better exit condition?

curId = lastRow.id
const ids = rethinkRows.map((t) => t.id)
const pgRows = (await pgQuery(ids)) ?? []
Expand Down
Loading