Skip to content

Commit

Permalink
feat: add user_space table to collect user's votes and proposals coun…
Browse files Browse the repository at this point in the history
…t per space
  • Loading branch information
wa0x6e committed Aug 24, 2023
1 parent cfd461c commit 9c55475
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
31 changes: 31 additions & 0 deletions scripts/generate_user_space_counters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'dotenv/config';
import db from '../src/helpers/mysql';

// Usage: yarn ts-node scripts/generate_user_space_counters.ts
async function main() {
console.log('Inserting/Updating proposals_count');
const proposalsCountRes = await db.queryAsync(
`INSERT INTO user_space (proposals_count, user_id, space_id)
(select * from (select count(id) as proposals_count, author, space from proposals group by author, space) as t)
ON DUPLICATE KEY UPDATE proposals_count = t.proposals_count`
);
console.log(proposalsCountRes);

console.log('Inserting/Updating votes_count');
const votesCountRes = await db.queryAsync(
`INSERT INTO user_space (votes_count, user_id, space_id)
(select * from (select count(id) as votes_count, voter, space from votes group by voter, space) as t)
ON DUPLICATE KEY UPDATE votes_count = t.votes_count`
);
console.log(votesCountRes);
}

(async () => {
try {
await main();
process.exit(0);
} catch (e) {
console.error(e);
process.exit(1);
}
})();
11 changes: 11 additions & 0 deletions src/helpers/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,14 @@ CREATE TABLE statements (
INDEX created (created),
INDEX updated (updated)
);

CREATE TABLE user_space (
user_id varchar(64) NOT NULL,
space_id varchar(64) NOT NULL,
votes_count smallint unsigned NOT NULL DEFAULT '0',
proposals_count smallint unsigned NOT NULL DEFAULT '0',
UNIQUE KEY user_id_space_id (user_id,space_id),
INDEX space_id (space_id),
INDEX votes_count (votes_count),
INDEX proposals_count (proposals_count)
);

0 comments on commit 9c55475

Please sign in to comment.