-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add ip and altIps to searchIndex #139
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
155 changes: 155 additions & 0 deletions
155
src/extensions/migrations/20251005GP-add-ip-altIps-to-searchIndex.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| export async function up (knex) { | ||
| await knex.transaction(async (trx) => { | ||
| await trx.raw(`DROP FUNCTION IF EXISTS generate_search_index`); | ||
| await trx.raw(`DROP TRIGGER IF EXISTS gp_probes_searchIndex_before_insert`); | ||
| await trx.raw(`DROP TRIGGER IF EXISTS gp_probes_searchIndex_before_update`); | ||
|
|
||
| await trx.raw(` | ||
| CREATE FUNCTION generate_search_index( | ||
| userId VARCHAR(255), | ||
| name VARCHAR(255), | ||
| city VARCHAR(255), | ||
| country VARCHAR(255), | ||
| countryName VARCHAR(255), | ||
| state VARCHAR(255), | ||
| stateName VARCHAR(255), | ||
| asn INT, | ||
| network VARCHAR(255), | ||
| continent VARCHAR(255), | ||
| continentName VARCHAR(255), | ||
| region VARCHAR(255), | ||
| tags LONGTEXT, | ||
| systemTags LONGTEXT, | ||
| ip VARCHAR(255), | ||
| altIps LONGTEXT | ||
| ) RETURNS TEXT | ||
| DETERMINISTIC | ||
| BEGIN | ||
| DECLARE tagsText TEXT; | ||
| DECLARE systemTagsText TEXT; | ||
| DECLARE githubUsername TEXT; | ||
| DECLARE altIpsText TEXT; | ||
|
|
||
| SELECT GROUP_CONCAT(CONCAT('u-', t.prefix, ':', t.value) SEPARATOR '\n') | ||
| INTO tagsText | ||
| FROM JSON_TABLE( | ||
| tags, | ||
| '$[*]' COLUMNS ( | ||
| prefix VARCHAR(255) PATH '$.prefix', | ||
| value VARCHAR(255) PATH '$.value' | ||
| ) | ||
| ) AS t; | ||
|
|
||
| SELECT GROUP_CONCAT(st.value SEPARATOR '\n') | ||
| INTO systemTagsText | ||
| FROM JSON_TABLE( | ||
| systemTags, | ||
| '$[*]' COLUMNS ( | ||
| value VARCHAR(255) PATH '$' | ||
| ) | ||
| ) AS st; | ||
|
|
||
| IF userId IS NOT NULL THEN | ||
| SELECT CONCAT('u-', github_username) | ||
| INTO githubUsername | ||
| FROM directus_users | ||
| WHERE id = userId | ||
| LIMIT 1; | ||
| END IF; | ||
|
|
||
| SELECT GROUP_CONCAT(a.value SEPARATOR '\n') | ||
| INTO altIpsText | ||
| FROM JSON_TABLE( | ||
| altIps, | ||
| '$[*]' COLUMNS ( | ||
| value VARCHAR(255) PATH '$' | ||
| ) | ||
| ) AS a; | ||
|
|
||
| RETURN LOWER(CONCAT_WS('\n', | ||
| name, | ||
| city, | ||
| country, | ||
| countryName, | ||
| state, | ||
| stateName, | ||
| asn, | ||
| network, | ||
| continent, | ||
| continentName, | ||
| region, | ||
| tagsText, | ||
| systemTagsText, | ||
| githubUsername, | ||
| ip, | ||
| altIpsText | ||
| )); | ||
| END; | ||
| `); | ||
|
|
||
| await trx.raw(` | ||
| CREATE TRIGGER gp_probes_searchIndex_before_insert | ||
| BEFORE INSERT ON gp_probes | ||
| FOR EACH ROW | ||
| BEGIN | ||
| SET NEW.searchIndex = generate_search_index( | ||
| NEW.userId, | ||
| NEW.name, | ||
| NEW.city, | ||
| NEW.country, | ||
| NEW.countryName, | ||
| NEW.state, | ||
| NEW.stateName, | ||
| NEW.asn, | ||
| NEW.network, | ||
| NEW.continent, | ||
| NEW.continentName, | ||
| NEW.region, | ||
| NEW.tags, | ||
| NEW.systemTags, | ||
| NEW.ip, | ||
| NEW.altIps | ||
| ); | ||
| END; | ||
| `); | ||
|
|
||
| await trx.raw(` | ||
| CREATE TRIGGER gp_probes_searchIndex_before_update | ||
| BEFORE UPDATE ON gp_probes | ||
| FOR EACH ROW | ||
| BEGIN | ||
| SET NEW.searchIndex = generate_search_index( | ||
| NEW.userId, | ||
| NEW.name, | ||
| NEW.city, | ||
| NEW.country, | ||
| NEW.countryName, | ||
| NEW.state, | ||
| NEW.stateName, | ||
| NEW.asn, | ||
| NEW.network, | ||
| NEW.continent, | ||
| NEW.continentName, | ||
| NEW.region, | ||
| NEW.tags, | ||
| NEW.systemTags, | ||
| NEW.ip, | ||
| NEW.altIps | ||
| ); | ||
| END; | ||
| `); | ||
|
|
||
| await trx.raw(` | ||
| UPDATE gp_probes | ||
| SET searchIndex = generate_search_index( | ||
| userId, name, city, country, countryName, state, stateName, asn, network, continent, continentName, region, tags, systemTags, ip, altIps | ||
| ) | ||
| `); | ||
| }); | ||
|
|
||
| console.log('Triggers for searchIndex column of gp_probes updated'); | ||
| } | ||
|
|
||
| export async function down () { | ||
| console.log('There is no down operation for that migration.'); | ||
| } | ||
PavelKopecky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.