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

Refactor version history (PART 1/2) #8041

Merged
merged 32 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d45f5d8
Initial version history refactor
kurtisassad May 24, 2024
d02bab4
Merge branch 'master' into ka.versionHistoryRefactor
kurtisassad Jun 3, 2024
d747cf8
Merge branch 'master' into ka.versionHistoryRefactor
kurtisassad Jun 4, 2024
0fa5074
Fixed version histories
kurtisassad Jun 4, 2024
fbaddfa
Added migration script for version history
kurtisassad Jun 5, 2024
52ee065
Finished migration scripts for version histories
kurtisassad Jun 11, 2024
6202b92
Fixed linter errors
kurtisassad Jun 11, 2024
0370eee
Fixed eslint errors
kurtisassad Jun 17, 2024
84e3794
Fixed eslint errors
kurtisassad Jun 17, 2024
bddd6d1
Fixed tests
kurtisassad Jun 17, 2024
d7955b0
Fixed tests
kurtisassad Jun 17, 2024
2f7f69e
Fixed test
kurtisassad Jun 17, 2024
13e8d0b
Merge branch 'master' into ka.versionHistoryRefactor
kurtisassad Jun 17, 2024
5ea6f0a
Fixed tests
kurtisassad Jun 18, 2024
6679341
Fixed tests
kurtisassad Jun 18, 2024
0483feb
Fixed tests
kurtisassad Jun 18, 2024
b144c31
Fixed tests
kurtisassad Jun 18, 2024
1a1735f
Fixed test
kurtisassad Jun 18, 2024
6a5aa78
fixed linter
kurtisassad Jun 18, 2024
d7d2926
Fixed PR comments
kurtisassad Jun 21, 2024
fba797b
Fixed linter errors
kurtisassad Jun 21, 2024
b8cb3f7
Fixed thread migration
kurtisassad Jun 24, 2024
7b3ebc8
Merge branch 'master' into ka.versionHistoryRefactor
kurtisassad Jul 1, 2024
71ec0ac
Fixed lint error
kurtisassad Jul 1, 2024
b41ae39
Fixed thread version history, as well as stopped recording edits with…
kurtisassad Jul 11, 2024
43ac9ed
Fixed eslint error
kurtisassad Jul 11, 2024
a38fd07
Updated version history migration creation date.
kurtisassad Jul 11, 2024
ed561d3
use associations
Jul 12, 2024
455ca37
Merge branch 'master' into ka.versionHistoryRefactor
kurtisassad Jul 15, 2024
f5f1f07
Fixed PR comment
kurtisassad Jul 15, 2024
2aa9e7b
Fixed migration scripts
kurtisassad Jul 15, 2024
ddbf380
Fixed PR comments
kurtisassad Jul 16, 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
6 changes: 6 additions & 0 deletions libs/model/src/models/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type CommentAttributes = {
deleted_at?: Date;
marked_as_spam_at?: Date;
discord_meta?: IDiscordMeta;
version_history_updated?: boolean;

// associations
Community?: CommunityAttributes;
Expand Down Expand Up @@ -97,6 +98,11 @@ export default (
allowNull: false,
defaultValue: 0,
},
version_history_updated: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false,
},
},
{
hooks: {
Expand Down
40 changes: 40 additions & 0 deletions libs/model/src/models/comment_version_history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { CommentVersionHistory } from '@hicommonwealth/schemas';
import Sequelize from 'sequelize';
import { z } from 'zod';
import { CommentAttributes } from './comment';
import type { ModelInstance } from './types';

export type CommentVersionHistoryAttributes = z.infer<
typeof CommentVersionHistory
> & {
// associations
Comment?: CommentAttributes;
};

export type CommentVersionHistoryInstance =
ModelInstance<CommentVersionHistoryAttributes>;

export default (
sequelize: Sequelize.Sequelize,
): Sequelize.ModelStatic<CommentVersionHistoryInstance> =>
sequelize.define<CommentVersionHistoryInstance>(
'CommentVersionHistory',
{
id: { type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true },
comment_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'Comments',
key: 'id',
},
},
text: { type: Sequelize.TEXT, allowNull: false },
timestamp: { type: Sequelize.DATE, allowNull: false },
},
{
tableName: 'CommentVersionHistories',
timestamps: false,
indexes: [{ fields: ['comment_id'] }],
},
);
4 changes: 4 additions & 0 deletions libs/model/src/models/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ChainNode from './chain_node';
import Collaboration from './collaboration';
import Comment from './comment';
import CommentSubscription from './comment_subscriptions';
import CommentVersionHistory from './comment_version_history';
import Community from './community';
import CommunityAlert from './community_alerts';
import CommunityBanner from './community_banner';
Expand Down Expand Up @@ -45,6 +46,7 @@ import Tags from './tags';
import Template from './template';
import Thread from './thread';
import ThreadSubscription from './thread_subscriptions';
import ThreadVersionHistory from './thread_version_history';
import Topic from './topic';
import User from './user';
import Vote from './vote';
Expand All @@ -56,6 +58,7 @@ export const Factories = {
ChainNode,
Collaboration,
Comment,
CommentVersionHistory,
CommentSubscription,
Community,
CommunityAlert,
Expand Down Expand Up @@ -94,6 +97,7 @@ export const Factories = {
SubscriptionPreference,
Template,
Thread,
ThreadVersionHistory,
ThreadSubscription,
Topic,
User,
Expand Down
2 changes: 2 additions & 0 deletions libs/model/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export * from './ban';
export * from './chain_node';
export * from './collaboration';
export * from './comment';
export * from './comment_version_history';
export * from './community';
export * from './community_banner';
export * from './community_contract';
Expand Down Expand Up @@ -79,6 +80,7 @@ export * from './subscription';
export * from './tags';
export * from './template';
export * from './thread';
export * from './thread_version_history';
export * from './topic';
export * from './types';
export * from './user';
Expand Down
7 changes: 7 additions & 0 deletions libs/model/src/models/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { ModelInstance } from './types';

export type ThreadAttributes = z.infer<typeof Thread> & {
// associations
version_history_updated?: boolean;
Community?: CommunityAttributes;
collaborators?: AddressAttributes[];
topic?: TopicAttributes;
Expand Down Expand Up @@ -109,6 +110,12 @@ export default (
allowNull: false,
defaultValue: 0,
},

version_history_updated: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false,
},
},
{
timestamps: true,
Expand Down
41 changes: 41 additions & 0 deletions libs/model/src/models/thread_version_history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ThreadVersionHistory } from '@hicommonwealth/schemas';
import Sequelize from 'sequelize';
import { z } from 'zod';
import { ThreadAttributes } from './thread';
import type { ModelInstance } from './types';

export type ThreadVersionHistoryAttributes = z.infer<
typeof ThreadVersionHistory
> & {
// associations
Thread?: ThreadAttributes;
};

export type ThreadVersionHistoryInstance =
ModelInstance<ThreadVersionHistoryAttributes>;

export default (
sequelize: Sequelize.Sequelize,
): Sequelize.ModelStatic<ThreadVersionHistoryInstance> =>
sequelize.define<ThreadVersionHistoryInstance>(
'ThreadVersionHistory',
{
id: { type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true },
thread_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'Threads',
key: 'id',
},
},
address: { type: Sequelize.STRING, allowNull: false },
body: { type: Sequelize.TEXT, allowNull: false },
timestamp: { type: Sequelize.DATE, allowNull: false },
},
{
tableName: 'ThreadVersionHistories',
timestamps: false,
indexes: [{ fields: ['thread_id'] }],
},
);
9 changes: 9 additions & 0 deletions libs/schemas/src/entities/comment.schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { z } from 'zod';
import { PG_INT } from '../utils';

export const CommentVersionHistory = z.object({
id: PG_INT.optional(),
comment_id: PG_INT,
text: z.string(),
timestamp: z.date().optional(),
rbennettcw marked this conversation as resolved.
Show resolved Hide resolved
});
1 change: 1 addition & 0 deletions libs/schemas/src/entities/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './chain.schemas';
export * from './comment.schemas';
export * from './community.schemas';
export * from './contest-manager.schemas';
export * from './contract.schemas';
Expand Down
12 changes: 11 additions & 1 deletion libs/schemas/src/entities/thread.schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod';
import { PG_INT, discordMetaSchema, linksSchema, zDate } from '../utils';
import { discordMetaSchema, linksSchema, PG_INT, zDate } from '../utils';
import { Address } from './user.schemas';

export const Thread = z.object({
Expand Down Expand Up @@ -81,3 +81,13 @@ export const Comment = z.object({

Address: Address.optional(),
});

export const ThreadVersionHistory = z.object({
id: PG_INT.optional(),
thread_id: PG_INT,
address: z
.string()
.describe('Address of the creator of the post or the collaborator'),
body: z.string(),
timestamp: z.date().optional(),
rbennettcw marked this conversation as resolved.
Show resolved Hide resolved
});
2 changes: 2 additions & 0 deletions libs/schemas/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ export type Aggregates = Extract<
Entities,
| 'ChainNode'
| 'Comment'
| 'CommentVersionHistory'
| 'Community'
| 'NotificationCategory'
| 'Subscription'
| 'Thread'
| 'ThreadVersionHistory'
| 'Reaction'
| 'User'
| 'StakeTransaction'
Expand Down
2 changes: 2 additions & 0 deletions packages/commonwealth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
},
"type": "module",
"scripts": {
"async-migrate-comment-version-history": "tsx ./scripts/async-migrate-comment-version-history.ts",
"async-migrate-thread-version-history": "tsx ./scripts/async-migrate-thread-version-history.ts",
"add-component-showcase": "tsx ./scripts/add-component-showcase.ts",
"archive-outbox": "node --import=extensionless/register --max-old-space-size=$(../../scripts/get-max-old-space-size.sh) build/scripts/archive-outbox.js",
"build": "tsc -b ./tsconfig.build.json && tsc-alias -p ./tsconfig.build.json",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//TODO: This should be deleted after comment version histories are fixed
import { dispose } from '@hicommonwealth/core';
import { CommentVersionHistoryInstance, models } from '@hicommonwealth/model';
import { QueryTypes } from 'sequelize';

async function run() {
const commentCount = (
await models.sequelize.query(
`SELECT COUNT(*) FROM "Comments" WHERE version_history_updated = false`,
{
raw: true,
type: QueryTypes.SELECT,
},
)
)[0];

const count = parseInt(commentCount['count']);
let i = 0;
while (i < count) {
try {
const commentVersionHistory: {
id: number;
versionHistories: { timestamp: string; body: string }[];
}[] = (
await models.sequelize.query(
`SELECT id, version_history FROM "Comments" where version_history_updated = false LIMIT 10`,
{
raw: true,
type: QueryTypes.SELECT,
},
)
).map((c) => ({
id: parseInt(c['id']),
versionHistories: c['version_history'].map((v) => JSON.parse(v)),
}));

if (commentVersionHistory.length === 0) {
break;
}

for (const versionHistory of commentVersionHistory) {
console.log(
`${i}/${count} Updating comment version_histories for id ${versionHistory.id}`,
);

const formattedValues = versionHistory.versionHistories.map((v) => {
const { body, ...rest } = v;
return {
comment_id: versionHistory.id,
...rest,
text: body,
};
}) as unknown as CommentVersionHistoryInstance[];

await models.sequelize.transaction(async (transaction) => {
await models.sequelize.query(
`UPDATE "Comments" SET version_history_updated = true WHERE id = $id`,
{
bind: { id: versionHistory.id },
transaction,
},
);
return await models.CommentVersionHistory.bulkCreate(
formattedValues,
{
transaction,
},
);
});
}
} catch (error) {
console.error('Error:', error.message);
throw error;
}

i += 1;
}

console.log('Finished migration');
}

run()
.then(() => {
void dispose()('EXIT', true);
})
.catch((error) => {
console.error('Failed to migrate community counts:', error);
});
Loading
Loading