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

Increase typescript-eslint coverage #5622

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

Conversation

flozia
Copy link
Collaborator

@flozia flozia commented Feb 12, 2025

Description

Increases TSLint coverage to include the following files:

  • src/app/global-error.js
  • src/db/migrations/*.js
  • src/scripts/build/*.js
  • src/scripts/loadtest/*.js

How to test

Running npm run lint should be successful.

@flozia flozia mentioned this pull request Feb 12, 2025
@flozia flozia requested review from Vinnl, rhelmer and mansaj February 12, 2025 18:16
Copy link

Copy link
Collaborator

@rhelmer rhelmer left a comment

Choose a reason for hiding this comment

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

This is great, thanks! A few questions and suggestions. It's nice that we can use JSDoc-style comments to shore up the older code like migrations.

table.string("title").default("");
// @ts-ignore TODO: Determine if the following line should be changed to
// `defaultTo` or remain unchanged as initially deployed.
table.string("domain").default("");
Copy link
Collaborator

Choose a reason for hiding this comment

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

@flozia was this renamed on the knex side? I only see defaultTo in their docs, I don't see any reference to default

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don’t think so — at least I couldn’t find any reference. I was just being cautious about updating existing migration files. Sounds like maybe we should.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Unless we're nuking our prod database (please don't :P) I think it's better to have the migration files accurately reflect how the prod DB got to its current state.

Copy link
Collaborator Author

@flozia flozia Feb 13, 2025

Choose a reason for hiding this comment

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

Yes, that is why I’ve just added the ignores. What do you think @rhelmer and @mansaj about leaving those as is vs. updating them? The ones I’ve applied would be:

  1. table.default() -> table.defaultTo()
  2. table.dropUnique("") -> table.dropUnique([""])

… and table.varchar() we may just also leave as is.

Copy link
Collaborator

@rhelmer rhelmer Feb 13, 2025

Choose a reason for hiding this comment

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

I agree that these modifications to the migration files shouldn't mutate the database, mostly I'm just wondering wtf .default() does because it doesn't seem to be documented :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

FWIW, if we want to ensure that changes to existing migration files don't change the schema, we could test it locally:

  1. create new db, run "before" migrations, dump schema (pgdump)
  2. apply changes to migration scripts
  3. repeat step 1 and diff the "before"/"after" schema

It's possible for migration scripts to insert/delete data too but I don't think we tend to do that.

export function up(knex) {
return knex.schema.createTable("onerep_profiles", (table) => {
table.increments("id").primary();
table
.integer("onerep_profile_id")
.references("subscribers.onerep_profile_id")
.nullable();
// @ts-ignore TODO: Determine if the following line should be changed
Copy link
Collaborator

Choose a reason for hiding this comment

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

Curious why this doesn't work, varchar should be valid no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It looks like varchar is implemented, but missing from the type TableBuilder. I think it is recommended to use .string().

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah OK, yeah "under the hood" in Postgres I believe TEXT and VARCHAR types are implemented the same way so .text() is probably what you want https://knexjs.org/guide/schema-builder.html#text

Comment on lines +21 to 22
// @ts-ignore If body is `null` we are already throwing an error above.
await finished(Readable.fromWeb(body).pipe(stream));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// @ts-ignore If body is `null` we are already throwing an error above.
await finished(Readable.fromWeb(body).pipe(stream));
await finished(Readable.fromWeb(body!).pipe(stream));

Copy link
Collaborator

@rhelmer rhelmer Feb 12, 2025

Choose a reason for hiding this comment

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

This is cleaner than ts-ignore and will result in the same thing at runtime I think, but I think the real potential problem it's pointing out is that body may be non-null but still not a ReadableStream. I'm not sure there's much we can do to handle it in this case though, my suggestion should throw a TypeError at runtime which is probably the best we can do.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since this is a .js file non-null assertion won’t work here. :/

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh shoot right sorry I forgot, yeah that's fine then. Unfortunate to have to turn it off for the whole line that's all. I guess if you care you could explicitly check this and throw.

Copy link
Collaborator

@Vinnl Vinnl left a comment

Choose a reason for hiding this comment

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

Thanks for being so thorough!

@@ -293,10 +337,30 @@ try {
return false;
}

return locationDataPopulated.some((location) => {
return locationDataRows.some((location) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this the bug you were referring to? I don't quite understand why we're suddenly looking at locationDataRows instead of at locationDataPopulated like before, just from this diff.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, that’s the one. locationDataRows is the final data we write to the resulting JSON file. For safe storage, it is as small as possible and missing the feature class (which we can look up in locationDataPopulated). This was the lowest-hanging fix to make without having to touch more of the file.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, gotcha, thanks! Only remaining thing I don't follow is the impact of the bug: wouldn't it cause no locations to be returned?

if (result.success !== true) {
throw new Error(`Non-success result: ${JSON.stringify(result)}`);
}
} catch {
// @ts-ignore TODO: Add type for result.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This line doesn't refer to result (just to res, but I assume that that one is typed)?

@flozia flozia changed the title Increase TSLint coverage Increase typescript-eslint coverage Feb 13, 2025
@flozia flozia self-assigned this Feb 13, 2025
@flozia flozia requested a review from rhelmer February 13, 2025 15:28
Copy link
Collaborator

@rhelmer rhelmer left a comment

Choose a reason for hiding this comment

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

lgtm, thanks for slogging through that! I'd say in general you've erred on the safe side rather than changing existing migration code (and risk mutating the schema for new vs. existing DB instances) which seems appropriate.

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.

3 participants