-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Link collections * feat: Link collections to contracts * fix: Use amoy in the third party id
- Loading branch information
1 parent
119f446
commit fe43e60
Showing
5 changed files
with
86 additions
and
5 deletions.
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,55 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { MigrationBuilder } from 'node-pg-migrate' | ||
import { Collection } from '../src/Collection' | ||
|
||
const linkedContractAddressColumn = 'linked_contract_address' | ||
const linkedContractNetworkColumn = 'linked_contract_network' | ||
const linkedContractUniqueConstraintName = 'linkedContract_linkedNetwork_unique' | ||
const unlinkContractTriggerName = | ||
'prevent_null_update_if_previous_not_null_trigger' | ||
const unlinkContractFunctionName = 'prevent_null_update_if_previous_not_null' | ||
|
||
export async function up(pgm: MigrationBuilder): Promise<void> { | ||
pgm.addColumn(Collection.tableName, { | ||
[linkedContractAddressColumn]: { type: 'string', notNull: false }, | ||
[linkedContractNetworkColumn]: { type: 'string', notNull: false }, | ||
}) | ||
pgm.addConstraint(Collection.tableName, linkedContractUniqueConstraintName, { | ||
unique: [linkedContractAddressColumn, linkedContractNetworkColumn], | ||
}) | ||
// Create the trigger function | ||
pgm.createFunction( | ||
unlinkContractFunctionName, | ||
[], | ||
{ | ||
returns: 'TRIGGER', | ||
language: 'plpgsql', | ||
}, | ||
` | ||
BEGIN | ||
IF (OLD.${linkedContractAddressColumn} IS NOT NULL AND NEW.${linkedContractAddressColumn} IS NULL) OR | ||
(OLD.${linkedContractNetworkColumn} IS NOT NULL AND NEW.${linkedContractNetworkColumn} IS NULL) THEN | ||
RAISE EXCEPTION 'Columns ${linkedContractAddressColumn}, ${linkedContractNetworkColumn} cannot be set to null if they were previously not null'; | ||
END IF; | ||
RETURN NEW; | ||
END; | ||
` | ||
) | ||
// Attach the trigger to the table | ||
pgm.createTrigger(Collection.tableName, unlinkContractTriggerName, { | ||
when: 'BEFORE', | ||
operation: 'UPDATE', | ||
level: 'ROW', | ||
function: unlinkContractFunctionName, | ||
}) | ||
} | ||
|
||
export async function down(pgm: MigrationBuilder): Promise<void> { | ||
pgm.dropConstraint(Collection.tableName, linkedContractUniqueConstraintName) | ||
pgm.dropColumn(Collection.tableName, [ | ||
linkedContractAddressColumn, | ||
linkedContractNetworkColumn, | ||
]) | ||
pgm.dropTrigger(Collection.tableName, unlinkContractTriggerName) | ||
pgm.dropFunction(unlinkContractFunctionName, []) | ||
} |
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
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