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

🚧 remove typeorm annotations from GDocs classes #3299

Merged
merged 1 commit into from
Mar 26, 2024
Merged

Conversation

danyx23
Copy link
Contributor

@danyx23 danyx23 commented Mar 6, 2024

This PR converts GDocBase and all related classes (including links, images etc) to be fetched via knex. For GDocs, the classes are kept for business logic but the database loading/saving is done via knex and objects.

The main functionality for loading and storing Gdocs has moved to GDocFactory as plain functions.

@danyx23 danyx23 force-pushed the db-remove-dead-code-round-one branch from 5d00c2b to b93390a Compare March 6, 2024 12:42
@danyx23 danyx23 force-pushed the db-migrate-gdocs branch from 47f72ea to b63272c Compare March 6, 2024 12:42
@danyx23 danyx23 force-pushed the db-remove-dead-code-round-one branch from b93390a to a02c68d Compare March 16, 2024 00:06
@danyx23 danyx23 marked this pull request as ready for review March 16, 2024 00:10
@danyx23 danyx23 force-pushed the db-remove-dead-code-round-one branch from a02c68d to 2124f46 Compare March 16, 2024 12:36
@danyx23 danyx23 force-pushed the db-remove-dead-code-round-one branch from 2124f46 to 146ea9e Compare March 16, 2024 13:46
@danyx23 danyx23 force-pushed the db-migrate-gdocs branch 2 times, most recently from 43f3e8f to f7d9953 Compare March 16, 2024 17:18
@danyx23 danyx23 force-pushed the db-remove-dead-code-round-one branch from b37a219 to 6358087 Compare March 18, 2024 18:59
@danyx23 danyx23 requested a review from ikesau March 18, 2024 19:00
Copy link
Member

@ikesau ikesau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phew! That took a while. Mostly nitpicks to be honest. Imagine there are a couple "🙂" at the end of basically every comment 😅

Will QA tomorrow 🙂🙂

@@ -93,6 +95,27 @@ export interface OwidGdocMinimalPostInterface {
"featured-image"?: string // used in prominent links and research & writing block
}

export type OwidGdocIndexItem = Pick<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prettier 🤮

Comment on lines +41 to +43
const indexItem = extractGdocIndexItem(gdoc)
const gdocToUpdateIndex = this.gdocs.findIndex((g) => g.id === gdoc.id)
if (gdocToUpdateIndex >= 0) this.gdocs[gdocToUpdateIndex] = indexItem
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely more correct, but is it ever actually used? Whenever you load the index page it refetches all gdocs anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah probably not but it just seems like the right thing to do - maybe we change the behaviour in the future and then it's nice if this part already does what you would expect.

@@ -66,6 +67,7 @@ export const checkIsLightningUpdate = (
published: false, // requires an update of the blog roll
publishedAt: false, // could require an update of the blog roll
slug: false, // requires updating any articles that link to it
markdown: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind putting this above all the falses ? Keeps it easier to scan I think.

return b.updatedAt.getTime() - a.updatedAt.getTime()
})
)
getRouteWithROTransaction(apiRouter, "/gdocs", async (req, res, trx) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't bother me, but adding async here is unnecessary, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jup, I mechanically replaced some things and the occasionally a superfluous async came in

Comment on lines 115 to 118
knex: KnexReadonlyTransaction,
id: string,
gdoc: OwidGdoc,
markdownContentSource: OwidEnrichedGdocBlock[]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a biggie, just floating it for your consideration, but my rule of thumb is once functions get more than 3 parameters (i.e. it becomes complex enough that the name alone probably doesn't fully describe what it does), I strongly consider changing them to a object so that the arguments get descriptive names in usage.

When first reading this code I had no idea what the flattened sources could be for. Jumping to the definition is easy, so whatever, but also sometimes there are arguments that are similar to one another and bugs can happen if you mistakenly put them in the wrong order.

function updateGdocContentOnly({
    knex: trx,
    id,
    gdoc,
    markdownContentSource: gdoc.enrichedBlockSources.flat()
})

It also comes in handy when you've got a series of boolean parameters where you might want to rely on defaults like in getGdocBaseObjectById below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah valid point - when looking at it now though I realized that we might as well just extract it inside the function and get rid of the last parameter.


if (!gdoc) {
if (!gdoc || !("details" in gdoc.content)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe update the error message here too?

Comment on lines 257 to 260
static async getListedGdocPosts(
knex: KnexReadonlyTransaction
): Promise<GdocPost[]> {
return getAndLoadListedGdocPosts(knex)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just remove this and call the function directly?

filename: string
): Promise<Image | undefined> {
const image = await knex
.table("images")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use a variable here if we're using one for all our other tables 🙂

db/model/Link.ts Outdated
Comment on lines 20 to 21
select posts_gdocs_links.*, posts_gdocs.slug as sourceSlug
from posts_gdocs_links
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not worth it to try given the lack of code formatter for SQL in TS, but should we try to maintain a consistent style for our SQL? Uppercase keywords, indentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There actually is one that works for selections - see the message in our slack in the #developers channel

@@ -61,5 +63,6 @@ export function serializePostsGdocsRow(row: DbEnrichedPostGdoc): DbRawPostGdoc {
...row,
content: serializePostGdocContent(row.content),
breadcrumbs: serializePostsGdocsBreadcrumbs(row.breadcrumbs),
published: row.published ? 1 : 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jsyk you can also do Number(row.published) 🙂

Again, the ternary is probably more explicit but I just like sharing little JS special recipes

@danyx23 danyx23 force-pushed the db-remove-dead-code-round-one branch from 2b680e3 to 887bc31 Compare March 21, 2024 22:22
@ikesau
Copy link
Member

ikesau commented Mar 22, 2024

Haven't found any remaining issues in QA on the tip of this 🙂

@danyx23 danyx23 force-pushed the db-remove-dead-code-round-one branch from 887bc31 to 1e899ed Compare March 23, 2024 15:34
@danyx23 danyx23 force-pushed the db-remove-dead-code-round-one branch from 1e899ed to 1a011bc Compare March 26, 2024 11:44
@danyx23 danyx23 force-pushed the db-migrate-gdocs branch 2 times, most recently from eb9cbd7 to 3a05c68 Compare March 26, 2024 13:08
Copy link
Contributor Author

danyx23 commented Mar 26, 2024

Merge activity

  • Mar 26, 10:04 AM EDT: @danyx23 started a stack merge that includes this pull request via Graphite.
  • Mar 26, 10:17 AM EDT: Graphite rebased this pull request as part of a merge.
  • Mar 26, 10:18 AM EDT: @danyx23 merged this pull request with Graphite.

@danyx23 danyx23 force-pushed the db-remove-dead-code-round-one branch from 1a011bc to 805d56f Compare March 26, 2024 14:14
Base automatically changed from db-remove-dead-code-round-one to master March 26, 2024 14:16
Also remove the `GdocXImage` and `GdocXTag` classes and switches
them to knex.

Also introduces OwidGdocIndexItem which is a more lightweight
representation of a Gdoc that is used in the overview page of
the admin.
@danyx23 danyx23 merged commit 395c86a into master Mar 26, 2024
15 of 20 checks passed
@danyx23 danyx23 deleted the db-migrate-gdocs branch March 26, 2024 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants