-
Notifications
You must be signed in to change notification settings - Fork 17
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
feat: Add virtual third parties #772
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dfd9bf4
feat: Add virtual third parties
LautaroPetaccio 9fedb89
fix: Imports
LautaroPetaccio a0300bb
fix: Test
LautaroPetaccio be15759
Merge branch 'master' of github.com:decentraland/builder-server into …
LautaroPetaccio 369a7ec
feat: Add virtual third party deletion
LautaroPetaccio 7a44429
fix: Comments
LautaroPetaccio b3de8f2
Merge branch 'master' of github.com:decentraland/builder-server into …
LautaroPetaccio 1c3bf41
fix: Some small fixes
LautaroPetaccio 81a4197
fix: Tests
LautaroPetaccio a6ba41f
fix: Test
LautaroPetaccio 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
This file contains 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,110 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { MigrationBuilder } from 'node-pg-migrate' | ||
import { VirtualThirdParty } from '../src/ThirdParty/VirtualThirdParty.model' | ||
import { Collection } from '../src/Collection/Collection.model' | ||
|
||
const update_updated_at_column_function_name = 'update_updated_at_column' | ||
const update_updated_at_trigger_name = 'update_updated_at' | ||
const delete_virtual_third_party_function_name = | ||
'delete_virtual_third_party_function' | ||
const delete_virtual_third_party_trigger_name = | ||
'delete_virtual_third_party_trigger' | ||
|
||
export async function up(pgm: MigrationBuilder): Promise<void> { | ||
pgm.createTable(VirtualThirdParty.tableName, { | ||
id: { | ||
type: 'TEXT', | ||
primaryKey: true, | ||
notNull: true, | ||
}, | ||
managers: { | ||
type: 'TEXT', | ||
notNull: true, | ||
}, | ||
raw_metadata: { | ||
type: 'TEXT', | ||
notNull: true, | ||
}, | ||
created_at: { | ||
type: 'TIMESTAMP', | ||
notNull: true, | ||
default: pgm.func('current_timestamp'), | ||
}, | ||
updated_at: { | ||
type: 'TIMESTAMP', | ||
notNull: true, | ||
default: pgm.func('current_timestamp'), | ||
}, | ||
}) | ||
|
||
// Create the trigger function to automatically update the updated_at column | ||
pgm.createFunction( | ||
update_updated_at_column_function_name, | ||
[], | ||
{ | ||
returns: 'TRIGGER', | ||
language: 'plpgsql', | ||
}, | ||
` | ||
BEGIN | ||
NEW.updated_at = NOW(); | ||
RETURN NEW; | ||
END; | ||
` | ||
) | ||
|
||
// Create the trigger to update the updated_at column | ||
pgm.createTrigger( | ||
VirtualThirdParty.tableName, | ||
update_updated_at_trigger_name, | ||
{ | ||
when: 'BEFORE', | ||
operation: 'UPDATE', | ||
level: 'ROW', | ||
function: update_updated_at_column_function_name, | ||
condition: 'OLD.* IS DISTINCT FROM NEW.*', | ||
} | ||
) | ||
|
||
// Create the trigger function to delete a virtual third party if no collections are assigned | ||
pgm.createFunction( | ||
delete_virtual_third_party_function_name, | ||
[], | ||
{ | ||
returns: 'TRIGGER', | ||
language: 'plpgsql', | ||
}, | ||
` | ||
BEGIN | ||
IF NOT EXISTS (SELECT 1 FROM ${Collection.tableName} WHERE third_party_id = OLD.third_party_id) THEN | ||
DELETE FROM ${VirtualThirdParty.tableName} WHERE id = OLD.third_party_id; | ||
END IF; | ||
RETURN OLD; | ||
END; | ||
` | ||
) | ||
|
||
// Create the trigger to delete a virtual third party if no collections are assigned | ||
pgm.createTrigger( | ||
Collection.tableName, | ||
delete_virtual_third_party_trigger_name, | ||
{ | ||
when: 'AFTER', | ||
operation: 'DELETE', | ||
level: 'ROW', | ||
function: delete_virtual_third_party_function_name, | ||
} | ||
) | ||
} | ||
|
||
export async function down(pgm: MigrationBuilder): Promise<void> { | ||
// Drop the triggers | ||
pgm.dropTrigger(VirtualThirdParty.tableName, update_updated_at_trigger_name) | ||
pgm.dropTrigger(Collection.tableName, delete_virtual_third_party_trigger_name) | ||
|
||
// Drop the trigger functions | ||
pgm.dropFunction(update_updated_at_column_function_name, []) | ||
pgm.dropFunction(delete_virtual_third_party_function_name, []) | ||
|
||
pgm.dropTable(VirtualThirdParty.tableName) | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't the updated_at field be updated with each update? or should it meet a specific condition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
updated_at
column will get updated only if any column in the row gets updated to something new. If it doesn't, we'll be saving a write operation by not updating theupdated_at
.