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

fix: Improve Scope of Table Name Regex to Resolve Errors #188

Merged
merged 4 commits into from Sep 6, 2023

Conversation

darunrs
Copy link
Collaborator

@darunrs darunrs commented Aug 29, 2023

The previous regex was not able to support DDL where the table names were not encapsulated in quotes. Through more research, I learned as well that putting the table name in quotes causes the table name it be case sensitive and also allow special characters. I also omitted checking for "IF NOT EXISTS". So, I updated the regex to allow optional white space and "IF NOT EXISTS". Then, I skip a SINGLE optional double quote, match anything until another optional double quote or white space. Everything between the white space following either TABLE or EXISTS and potentially two double quotes is now the table name. The name can have special characters so I ensure that any such names have double quotes added back in. I also support table names defined by schema.tableName. Finally, for testing I did the following:

Tested generating table names in front end by opening all indexers and running debug mode to generate methods. All indexers succeeded, including notable ones like bos_quests, social_feed, and widget_testing.

I also added a stress test schema which is a valid DDL schema (verified using format button) which contains all kinds of edge cases, and ensure the generated method names correctly sanitize the special characters to underscores. There might be other ways to more cleanly implement this transition of special characters to function name but I think this is fine for now, and we hopefully will have autocomplete for the context object soon.

Finally, I also did an integration test using the existing insert function to confirm functionality has remained unimpacted.

@darunrs darunrs requested a review from a team as a code owner August 29, 2023 17:58
Copy link
Collaborator

@morgsmccauley morgsmccauley left a comment

Choose a reason for hiding this comment

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

Mostly looks good, but for now I think we should remove the errors that are thrown when trying to create context.db. Instead we should just log the errors and not add it to the context. This avoids us breaking existing indexers unintentionally. We can be more strict once we have the final solution.

Comment on lines 68 to 69
CREATE TABLE
creator_quest (
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we want to test same line too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's a good idea. I'll make this one one line to validate that. And I agree with the overall comment. I'll make it return an empty object if it detects a failure for now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was a great catch. Turns out the regex would have \s+ twice. So, the space between TABLE and the name would only satisfy one and it would be skipped.

Comment on lines 528 to 529
'insert__contractor___quest_',
'select__contractor___quest_',
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is there a _ at the end?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is an artifact of some edge case I had considered from an earlier implementation. The outside _ are where the double quote symbol was replaced. The current implementation adds quotes after the table names are collected. I had an edge case in mind where there might be "tableName" and tableName and we would want to distinguish between them but I think the current code will fetch only tableName as it does not require double quotes since its a valid name on its own. If someone wanted "tableName" as the actual name, it'd have to be ""tableName"" or something. In any case, I can strip the outer quotes before doing the replacement.

await this.writeLog(`context.db.insert_${tableName}`, blockHeight, `Calling context.db.insert_${tableName}.`, `Inserting object ${JSON.stringify(objects)} into table ${tableName} on schema ${schemaName}`);
[`insert_${tableName.replace(/[^a-zA-Z0-9_]/g, '_')}`]: async (objects: any) => {
await this.writeLog(`context.db.insert_${tableName.replace(/[^a-zA-Z0-9_]/g, '_')}`, blockHeight,
`Calling context.db.insert_${tableName.replace(/[^a-zA-Z0-9_]/g, '_')}.`,
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
`Calling context.db.insert_${tableName.replace(/[^a-zA-Z0-9_]/g, '_')}.`,
`Calling context.db.insert_${this.sanitizeTableName(tableName)}.`,

This is used all over the place - maybe extract to its own method?

Copy link
Collaborator Author

@darunrs darunrs Aug 29, 2023

Choose a reason for hiding this comment

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

Will do!

@darunrs darunrs merged commit b58a0fc into main Sep 6, 2023
3 checks passed
@darunrs darunrs deleted the fixTableRegex branch September 6, 2023 17:54
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.

None yet

2 participants