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

Transfer ownership script #2698

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
132 changes: 101 additions & 31 deletions backend/scripts/transfer-contract-ownership.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,107 @@
import { SafeBulkWriter } from 'shared/safe-bulk-writer'
import { createSupabaseDirectClient, pgp } from 'shared/supabase/init'
import { runScript } from './run-script'
import { convertUser } from 'common/supabase/users'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import { getContractFromSlugSupabase, getUser } from 'shared/utils'

interface Args {
slugs: string[]
toUserId: string
}

const argv = yargs(hideBin(process.argv))
.option('slugs', {
alias: 's',
type: 'array',
description: 'List of slugs to transfer',
demandOption: true,
coerce: (arg) => {
return Array.isArray(arg) ? arg : arg.split(',')
},
})
.option('toUserId', {
alias: 'u',
type: 'string',
description: 'User ID to transfer contracts to',
demandOption: true,
})
.parseSync() as Args

const BATCH_SIZE = 20

if (require.main === module) {
runScript(async ({ pg, firestore }) => {
const fromUserId = 'zgCIqq8AmRUYVu6AdQ9vVEJN8On1'
const contractIdsToTransfer = await pg.map(
`select id from contracts where creator_id = $1`,
[fromUserId],
(row) => row.id
)
const toUserId = '4juQfJkFnwX9nws3dFOpz4gc1mi2'
const toUser = await pg.one(
`select * from users where id = $1`,
[toUserId],
(row) => convertUser(row)
)
console.log(
`Transferring ${contractIdsToTransfer.length} contracts from ${fromUserId} to ${toUser.name}`
)
const writer = new SafeBulkWriter()
for (const id of contractIdsToTransfer) {
const doc = firestore.collection('contracts').doc(id)
writer.update(doc, {
creatorId: toUser.id,
creatorAvatarUrl: toUser.avatarUrl,
creatorUsername: toUser.username,
creatorName: toUser.name,
creatorCreatedTime: toUser.createdTime,
})
}
await writer.close()
runScript(async ({ pg }) => {
const { slugs: slugsToTransfer, toUserId } = argv

try {
const toUser = await getUser(toUserId, pg)

if (!toUser) {
console.error('User not found')
return
}

console.log('done.')
const contractsToTransfer = (
await Promise.all(
slugsToTransfer.map((slug) => getContractFromSlugSupabase(slug))
)
).filter((contract) => contract !== null)

if (contractsToTransfer.length === 0) {
console.error('No contracts found')
return
}

if (contractsToTransfer.length !== slugsToTransfer.length) {
console.error(
'Some contracts could not be found. Please check the slugs.'
)
return
}

console.log(
`Transferring ${contractsToTransfer.length} contracts to ${toUser.name}`
)

for (let i = 0; i < contractsToTransfer.length; i += BATCH_SIZE) {
const batch = contractsToTransfer.slice(i, i + BATCH_SIZE)

for (const contract of batch) {
const { id } = contract!

await pg.none(
`UPDATE contracts
SET creator_id = $1,
data = jsonb_set(
jsonb_set(
jsonb_set(
jsonb_set(
data,
'{creatorId}',
to_jsonb($1::text),
true
),
'{creatorUsername}',
to_jsonb($2::text),
true
),
'{creatorName}',
to_jsonb($3::text),
true
),
'{creatorAvatarUrl}',
to_jsonb($4::text),
true
)
WHERE id = $5`,
[toUser.id, toUser.username, toUser.name, toUser.avatarUrl, id]
)
}
}

console.log('done.')
} catch (error) {
console.error('Error transferring contracts:', error)
}
})
}
Loading