Skip to content

Commit

Permalink
Merge pull request #1334 from Giveth/3752_add_unique_index_for_sybil_…
Browse files Browse the repository at this point in the history
…table

Add unique index for sybil table
  • Loading branch information
jainkrati authored Feb 18, 2024
2 parents a06616e + f6f1ead commit 4b1a67f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 7 deletions.
29 changes: 29 additions & 0 deletions migration/1708256080054-add_unique_index_for_sybil_table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddUniqueIndexForSybilTable1708256080054
implements MigrationInterface
{
async up(queryRunner: QueryRunner): Promise<void> {
// Step 1: Identify and Resolve Duplicates
// Example strategy: Keep the first record and delete the rest
await queryRunner.query(`
DELETE FROM sybil
WHERE id NOT IN (
SELECT MIN(id)
FROM sybil
GROUP BY "userId", "qfRoundId"
);
`);

// Step 2: Create Unique Index
await queryRunner.query(`
CREATE UNIQUE INDEX "IDX_USERID_QFROUNDID" ON "sybil" ("userId", "qfRoundId");
`);
}

async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
DROP INDEX "IDX_USERID_QFROUNDID";
`);
}
}
2 changes: 2 additions & 0 deletions src/entities/sybil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import {
ManyToOne,
RelationId,
BaseEntity,
Unique,
} from 'typeorm';
import { User } from './user';
import { QfRound } from './qfRound';

@ObjectType()
@Entity()
@Unique(['userId', 'qfRoundId'])
export class Sybil extends BaseEntity {
@Field(type => ID)
@PrimaryGeneratedColumn()
Expand Down
49 changes: 49 additions & 0 deletions src/entities/userPassportScore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Field, ID, ObjectType } from 'type-graphql';
import {
PrimaryGeneratedColumn,
Column,
Entity,
ManyToOne,
RelationId,
BaseEntity,
UpdateDateColumn,
CreateDateColumn,
} from 'typeorm';
import { User } from './user';
import { QfRound } from './qfRound';

@ObjectType()
@Entity()
export class UserPassportScore extends BaseEntity {
@Field(type => ID)
@PrimaryGeneratedColumn()
readonly id: number;

@Field({ nullable: false })
@Column('number')
passportScore: number;

@Field(type => User)
@ManyToOne(type => User, { eager: true })
user: User;

@RelationId((userPassportScore: UserPassportScore) => userPassportScore.user)
@Column()
userId: number;

@Field(type => QfRound)
@ManyToOne(type => QfRound, { eager: true })
qfRound: QfRound;

@RelationId(
(userPassportScore: UserPassportScore) => userPassportScore.qfRound,
)
@Column()
qfRoundId: number;

@UpdateDateColumn()
updatedAt: Date;

@CreateDateColumn()
createdAt: Date;
}
16 changes: 9 additions & 7 deletions src/server/adminJs/tabs/sybilTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,16 @@ export const createSybil = async (
throw new Error('No valid entries to insert');
}

// Insert query
const query = `
INSERT INTO sybil ("confirmedSybil", "userId", "qfRoundId")
VALUES ${values};
`;

// Upsert query
const upsertQuery = `
INSERT INTO sybil ("confirmedSybil", "userId", "qfRoundId")
VALUES ${values}
ON CONFLICT ("userId", "qfRoundId")
DO UPDATE SET
"confirmedSybil" = EXCLUDED."confirmedSybil";
`;
// Execute the query
await Sybil.query(query);
await Sybil.query(upsertQuery);
} else {
const sybil = new Sybil();
sybil.confirmedSybil = true;
Expand Down

0 comments on commit 4b1a67f

Please sign in to comment.