-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matt Krick <[email protected]>
- Loading branch information
Showing
6 changed files
with
75 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/server/postgres/migrations/1724174924811_oneTeamLead.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {Kysely, PostgresDialect, sql} from 'kysely' | ||
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() | ||
}) | ||
}) | ||
const teamsWithout1Leader = await pg | ||
.selectFrom('TeamMember') | ||
.select('teamId') | ||
.select(({fn}) => fn.count('id').as('count')) | ||
.where('isNotRemoved', '=', true) | ||
.groupBy('teamId') | ||
.having(sql<boolean>`SUM(CASE WHEN "isLead" = true THEN 1 ELSE 0 END) != 1`) | ||
.execute() | ||
|
||
await Promise.all( | ||
teamsWithout1Leader.map(async (row) => { | ||
const {teamId} = row | ||
// remove all leads for the cases where we had more than 1 | ||
await pg.updateTable('TeamMember').set({isLead: false}).where('teamId', '=', teamId).execute() | ||
await pg | ||
.with('NextLead', (qb) => | ||
qb | ||
.selectFrom('TeamMember') | ||
.select('id') | ||
.where('teamId', '=', teamId) | ||
.where('isNotRemoved', '=', true) | ||
.orderBy('createdAt', 'asc') | ||
.limit(1) | ||
) | ||
.updateTable('TeamMember') | ||
.set({isLead: true}) | ||
.where(({eb, selectFrom}) => eb('id', '=', selectFrom('NextLead').select('id'))) | ||
.returning('id') | ||
.execute() | ||
}) | ||
) | ||
} | ||
|
||
export async function down() { | ||
// noop | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters