This repository has been archived by the owner on May 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
234 additions
and
64 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
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
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
154 changes: 154 additions & 0 deletions
154
modules/node/migrations/1597957131361-store-latest-action.ts
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,154 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class storeLatestAction1597957131361 implements MigrationInterface { | ||
name = "storeLatestAction1597957131361"; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
// Update remove app instance to set the latest state of the app | ||
await queryRunner.query( | ||
`DROP FUNCTION IF EXISTS remove_app_instance(jsonb,jsonb,jsonb);`, | ||
undefined, | ||
); | ||
|
||
await queryRunner.query( | ||
` | ||
CREATE OR REPLACE FUNCTION remove_app_instance( | ||
removed_app JSONB, | ||
free_balance_app_instance JSONB, | ||
signed_free_balance_update JSONB | ||
) RETURNS TEXT AS $$ | ||
DECLARE | ||
remove_app_result TEXT; | ||
remove_set_state_result TEXT; | ||
remove_conditional_result TEXT; | ||
update_free_balance_result TEXT; | ||
update_set_state_result TEXT; | ||
BEGIN | ||
UPDATE "app_instance" SET | ||
"type" = 'UNINSTALLED', | ||
"channelMultisigAddress" = NULL, | ||
"updatedAt" = CURRENT_TIMESTAMP, | ||
"latestState" = removed_app->'latestState', | ||
"latestAction" = removed_app->'latestAction', | ||
"stateTimeout" = removed_app->>'stateTimeout', | ||
"latestVersionNumber" = (removed_app->>'latestVersionNumber')::INTEGER | ||
WHERE "identityHash" = removed_app->>'identityHash' | ||
RETURNING "identityHash" INTO remove_app_result; | ||
DELETE FROM "set_state_commitment" | ||
WHERE "appIdentityHash" = removed_app->>'identityHash'; | ||
DELETE FROM "conditional_transaction_commitment" | ||
WHERE "appIdentityHash" = removed_app->>'identityHash'; | ||
UPDATE "app_instance" SET | ||
"latestState" = free_balance_app_instance->'latestState', | ||
"stateTimeout" = free_balance_app_instance->>'stateTimeout', | ||
"latestVersionNumber" = (free_balance_app_instance->>'latestVersionNumber')::INTEGER, | ||
"updatedAt" = CURRENT_TIMESTAMP | ||
WHERE "identityHash" = free_balance_app_instance->>'identityHash' | ||
RETURNING "identityHash" INTO update_free_balance_result; | ||
UPDATE "set_state_commitment" SET | ||
"appIdentity" = signed_free_balance_update->'appIdentity', | ||
"appStateHash" = signed_free_balance_update->>'appStateHash', | ||
"challengeRegistryAddress" = signed_free_balance_update->>'challengeRegistryAddress', | ||
"signatures" = signed_free_balance_update->'signatures', | ||
"stateTimeout" = signed_free_balance_update->>'stateTimeout', | ||
"versionNumber" = (signed_free_balance_update->>'versionNumber')::INTEGER, | ||
"transactionData" = signed_free_balance_update->>'transactionData', | ||
"updatedAt" = CURRENT_TIMESTAMP | ||
WHERE "appIdentityHash" = free_balance_app_instance->>'identityHash' | ||
RETURNING "appIdentityHash" INTO update_set_state_result; | ||
IF remove_app_result IS NULL OR update_free_balance_result IS NULL OR update_set_state_result IS NULL | ||
THEN | ||
RAISE EXCEPTION | ||
'Operation could not be completed: remove_app_result -> %, update_free_balance_result -> %, update_set_state_result -> %', | ||
remove_app_result, | ||
update_free_balance_result, | ||
update_set_state_result; | ||
END IF; | ||
RETURN remove_app_result; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
`, | ||
undefined, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
// Return procedure to previous migration (remove-app-commitments) | ||
await queryRunner.query( | ||
`DROP FUNCTION IF EXISTS remove_app_instance(jsonb,jsonb,jsonb);`, | ||
undefined, | ||
); | ||
|
||
await queryRunner.query( | ||
` | ||
CREATE OR REPLACE FUNCTION remove_app_instance( | ||
removed_app JSONB, | ||
free_balance_app_instance JSONB, | ||
signed_free_balance_update JSONB | ||
) RETURNS TEXT AS $$ | ||
DECLARE | ||
remove_app_result TEXT; | ||
remove_set_state_result TEXT; | ||
remove_conditional_result TEXT; | ||
update_free_balance_result TEXT; | ||
update_set_state_result TEXT; | ||
BEGIN | ||
UPDATE "app_instance" SET | ||
"type" = 'UNINSTALLED', | ||
"channelMultisigAddress" = NULL, | ||
"updatedAt" = CURRENT_TIMESTAMP, | ||
"latestState" = removed_app->'latestState', | ||
"stateTimeout" = removed_app->>'stateTimeout', | ||
"latestVersionNumber" = (removed_app->>'latestVersionNumber')::INTEGER | ||
WHERE "identityHash" = removed_app->>'identityHash' | ||
RETURNING "identityHash" INTO remove_app_result; | ||
DELETE FROM "set_state_commitment" | ||
WHERE "appIdentityHash" = removed_app->>'identityHash'; | ||
DELETE FROM "conditional_transaction_commitment" | ||
WHERE "appIdentityHash" = removed_app->>'identityHash'; | ||
UPDATE "app_instance" SET | ||
"latestState" = free_balance_app_instance->'latestState', | ||
"stateTimeout" = free_balance_app_instance->>'stateTimeout', | ||
"latestVersionNumber" = (free_balance_app_instance->>'latestVersionNumber')::INTEGER, | ||
"updatedAt" = CURRENT_TIMESTAMP | ||
WHERE "identityHash" = free_balance_app_instance->>'identityHash' | ||
RETURNING "identityHash" INTO update_free_balance_result; | ||
UPDATE "set_state_commitment" SET | ||
"appIdentity" = signed_free_balance_update->'appIdentity', | ||
"appStateHash" = signed_free_balance_update->>'appStateHash', | ||
"challengeRegistryAddress" = signed_free_balance_update->>'challengeRegistryAddress', | ||
"signatures" = signed_free_balance_update->'signatures', | ||
"stateTimeout" = signed_free_balance_update->>'stateTimeout', | ||
"versionNumber" = (signed_free_balance_update->>'versionNumber')::INTEGER, | ||
"transactionData" = signed_free_balance_update->>'transactionData', | ||
"updatedAt" = CURRENT_TIMESTAMP | ||
WHERE "appIdentityHash" = free_balance_app_instance->>'identityHash' | ||
RETURNING "appIdentityHash" INTO update_set_state_result; | ||
IF remove_app_result IS NULL OR update_free_balance_result IS NULL OR update_set_state_result IS NULL | ||
THEN | ||
RAISE EXCEPTION | ||
'Operation could not be completed: remove_app_result -> %, update_free_balance_result -> %, update_set_state_result -> %', | ||
remove_app_result, | ||
update_free_balance_result, | ||
update_set_state_result; | ||
END IF; | ||
RETURN remove_app_result; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
`, | ||
undefined, | ||
); | ||
} | ||
} |
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.