Skip to content

Commit

Permalink
Fix database migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
fatemeh-ra committed Aug 5, 2024
1 parent 1234224 commit e82b354
Show file tree
Hide file tree
Showing 5 changed files with 431 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/database/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import { WatcherMigration1700710099334 } from './postgres/1700710099334-watcherM
import { WatcherMigration1703244656364 } from './postgres/1703244656364-watcherMigration';
import { WatcherMigration1704105342269 } from './postgres/1704105342269-watcherMigration';
import { WatcherMigration1720424860524 } from './postgres/1720424860524-watcherMigration';
import { WatcherMigration1722549850233 } from './postgres/1722549850233-watcherMigration';
import { WatcherMigration1722867363628 } from './postgres/1722867363628-watcherMigration';
import { WatcherMigration1700641198429 } from './sqlite/1700641198429-watcherMigration';
import { WatcherMigration1703244614956 } from './sqlite/1703244614956-watcherMigration';
import { WatcherMigration1704105040303 } from './sqlite/1704105040303-watcherMigration';
import { WatcherMigration1706610773175 } from './sqlite/1706610773175-watcherMigration';
import { WatcherMigration1706610773177 } from './sqlite/1706610773177-watcherMigration';
import { WatcherMigration1720425345411 } from './sqlite/1720425345411-watcherMigration';
import { WatcherMigration1722597111974 } from './sqlite/1722597111974-watcherMigration';
import { WatcherMigration1722866480590 } from './sqlite/1722866480590-watcherMigration';

export default {
sqlite: [
Expand All @@ -17,11 +21,15 @@ export default {
WatcherMigration1706610773175,
WatcherMigration1706610773177,
WatcherMigration1720425345411,
WatcherMigration1722597111974,
WatcherMigration1722866480590,
],
postgres: [
WatcherMigration1700710099334,
WatcherMigration1703244656364,
WatcherMigration1704105342269,
WatcherMigration1720424860524,
WatcherMigration1722549850233,
WatcherMigration1722867363628,
],
};
108 changes: 108 additions & 0 deletions src/database/migrations/postgres/1722549850233-watcherMigration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class WatcherMigration1722549850233 implements MigrationInterface {
name = 'WatcherMigration1722549850233';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`
DELETE FROM "typeorm_metadata"
WHERE "type" = $1
AND "name" = $2
AND "schema" = $3
`,
['VIEW', 'revenue_view', 'public']
);
await queryRunner.query(`
DROP VIEW "revenue_view"
`);
await queryRunner.query(
`
DELETE FROM "typeorm_metadata"
WHERE "type" = $1
AND "name" = $2
AND "schema" = $3
`,
['VIEW', 'revenue_chart_data', 'public']
);
await queryRunner.query(`
DROP VIEW "revenue_chart_data"
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE VIEW "revenue_chart_data" AS
SELECT "pe"."WID" AS "wid",
"be"."year" AS "year",
"be"."month" AS "month",
"be"."day" AS "day",
re."tokenId" AS "tokenId",
re."amount" AS "amount",
be."timestamp" AS "timestamp"
FROM "revenue_entity" "re"
INNER JOIN "permit_entity" "pe" ON re."permitId" = "pe"."id"
INNER JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"
`);
await queryRunner.query(
`
INSERT INTO "typeorm_metadata"(
"database",
"schema",
"table",
"type",
"name",
"value"
)
VALUES (DEFAULT, $1, DEFAULT, $2, $3, $4)
`,
[
'public',
'VIEW',
'revenue_chart_data',
'SELECT "pe"."WID" AS "wid", "be"."year" AS "year", "be"."month" AS "month", "be"."day" AS "day", re."tokenId" AS "tokenId", re."amount" AS "amount", be."timestamp" AS "timestamp" FROM "revenue_entity" "re" INNER JOIN "permit_entity" "pe" ON re."permitId" = "pe"."id" INNER JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"',
]
);
await queryRunner.query(`
CREATE VIEW "revenue_view" AS
SELECT "pe"."id" AS "id",
"pe"."WID" AS "wid",
"ete"."sourceChainHeight" AS "lockHeight",
"be"."height" AS "height",
"be"."timestamp" AS "timestamp",
pe."txId" AS "permitTxId",
ete."eventId" AS "eventId",
ete."fromChain" AS "fromChain",
ete."toChain" AS "toChain",
ete."fromAddress" AS "fromAddress",
ete."toAddress" AS "toAddress",
ete."amount" AS "amount",
ete."bridgeFee" AS "bridgeFee",
ete."networkFee" AS "networkFee",
ete."sourceChainTokenId" AS "tokenId",
ete."sourceTxId" AS "lockTxId"
FROM "permit_entity" "pe"
INNER JOIN "event_trigger_entity" "ete" ON pe."txId" = ete."spendTxId"
LEFT JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"
`);
await queryRunner.query(
`
INSERT INTO "typeorm_metadata"(
"database",
"schema",
"table",
"type",
"name",
"value"
)
VALUES (DEFAULT, $1, DEFAULT, $2, $3, $4)
`,
[
'public',
'VIEW',
'revenue_view',
'SELECT "pe"."id" AS "id", "pe"."WID" AS "wid", "ete"."sourceChainHeight" AS "lockHeight", "be"."height" AS "height", "be"."timestamp" AS "timestamp", pe."txId" AS "permitTxId", ete."eventId" AS "eventId", ete."fromChain" AS "fromChain", ete."toChain" AS "toChain", ete."fromAddress" AS "fromAddress", ete."toAddress" AS "toAddress", ete."amount" AS "amount", ete."bridgeFee" AS "bridgeFee", ete."networkFee" AS "networkFee", ete."sourceChainTokenId" AS "tokenId", ete."sourceTxId" AS "lockTxId" FROM "permit_entity" "pe" INNER JOIN "event_trigger_entity" "ete" ON pe."txId" = ete."spendTxId" LEFT JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"',
]
);
}
}
108 changes: 108 additions & 0 deletions src/database/migrations/postgres/1722867363628-watcherMigration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class WatcherMigration1722867363628 implements MigrationInterface {
name = 'WatcherMigration1722867363628';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE VIEW "revenue_chart_data" AS
SELECT "pe"."WID" AS "wid",
"be"."year" AS "year",
"be"."month" AS "month",
"be"."day" AS "day",
re."tokenId" AS "tokenId",
re."amount" AS "amount",
be."timestamp" AS "timestamp"
FROM "revenue_entity" "re"
INNER JOIN "permit_entity" "pe" ON re."permitId" = "pe"."id"
INNER JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"
`);
await queryRunner.query(
`
INSERT INTO "typeorm_metadata"(
"database",
"schema",
"table",
"type",
"name",
"value"
)
VALUES (DEFAULT, $1, DEFAULT, $2, $3, $4)
`,
[
'public',
'VIEW',
'revenue_chart_data',
'SELECT "pe"."WID" AS "wid", "be"."year" AS "year", "be"."month" AS "month", "be"."day" AS "day", re."tokenId" AS "tokenId", re."amount" AS "amount", be."timestamp" AS "timestamp" FROM "revenue_entity" "re" INNER JOIN "permit_entity" "pe" ON re."permitId" = "pe"."id" INNER JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"',
]
);
await queryRunner.query(`
CREATE VIEW "revenue_view" AS
SELECT "pe"."id" AS "id",
"pe"."WID" AS "wid",
"ete"."sourceChainHeight" AS "lockHeight",
"be"."height" AS "height",
"be"."timestamp" AS "timestamp",
pe."txId" AS "permitTxId",
ete."eventId" AS "eventId",
ete."fromChain" AS "fromChain",
ete."toChain" AS "toChain",
ete."fromAddress" AS "fromAddress",
ete."toAddress" AS "toAddress",
ete."amount" AS "amount",
ete."bridgeFee" AS "bridgeFee",
ete."networkFee" AS "networkFee",
ete."sourceChainTokenId" AS "tokenId",
ete."sourceTxId" AS "lockTxId"
FROM "permit_entity" "pe"
INNER JOIN "event_trigger_entity" "ete" ON pe."txId" = ete."spendTxId"
LEFT JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"
`);
await queryRunner.query(
`
INSERT INTO "typeorm_metadata"(
"database",
"schema",
"table",
"type",
"name",
"value"
)
VALUES (DEFAULT, $1, DEFAULT, $2, $3, $4)
`,
[
'public',
'VIEW',
'revenue_view',
'SELECT "pe"."id" AS "id", "pe"."WID" AS "wid", "ete"."sourceChainHeight" AS "lockHeight", "be"."height" AS "height", "be"."timestamp" AS "timestamp", pe."txId" AS "permitTxId", ete."eventId" AS "eventId", ete."fromChain" AS "fromChain", ete."toChain" AS "toChain", ete."fromAddress" AS "fromAddress", ete."toAddress" AS "toAddress", ete."amount" AS "amount", ete."bridgeFee" AS "bridgeFee", ete."networkFee" AS "networkFee", ete."sourceChainTokenId" AS "tokenId", ete."sourceTxId" AS "lockTxId" FROM "permit_entity" "pe" INNER JOIN "event_trigger_entity" "ete" ON pe."txId" = ete."spendTxId" LEFT JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"',
]
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`
DELETE FROM "typeorm_metadata"
WHERE "type" = $1
AND "name" = $2
AND "schema" = $3
`,
['VIEW', 'revenue_view', 'public']
);
await queryRunner.query(`
DROP VIEW "revenue_view"
`);
await queryRunner.query(
`
DELETE FROM "typeorm_metadata"
WHERE "type" = $1
AND "name" = $2
AND "schema" = $3
`,
['VIEW', 'revenue_chart_data', 'public']
);
await queryRunner.query(`
DROP VIEW "revenue_chart_data"
`);
}
}
103 changes: 103 additions & 0 deletions src/database/migrations/sqlite/1722597111974-watcherMigration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class WatcherMigration1722597111974 implements MigrationInterface {
name = 'WatcherMigration1722597111974';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`
DELETE FROM "typeorm_metadata"
WHERE "type" = ?
AND "name" = ?
`,
['VIEW', 'revenue_chart_data']
);
await queryRunner.query(`
DROP VIEW "revenue_chart_data"
`);
await queryRunner.query(
`
DELETE FROM "typeorm_metadata"
WHERE "type" = ?
AND "name" = ?
`,
['VIEW', 'revenue_view']
);
await queryRunner.query(`
DROP VIEW "revenue_view"
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE VIEW "revenue_view" AS
SELECT "pe"."id" AS "id",
"pe"."WID" AS "wid",
"ete"."sourceChainHeight" AS "lockHeight",
"be"."height" AS "height",
"be"."timestamp" AS "timestamp",
pe."txId" AS "permitTxId",
ete."eventId" AS "eventId",
ete."fromChain" AS "fromChain",
ete."toChain" AS "toChain",
ete."fromAddress" AS "fromAddress",
ete."toAddress" AS "toAddress",
ete."amount" AS "amount",
ete."bridgeFee" AS "bridgeFee",
ete."networkFee" AS "networkFee",
ete."sourceChainTokenId" AS "tokenId",
ete."sourceTxId" AS "lockTxId"
FROM "permit_entity" "pe"
INNER JOIN "event_trigger_entity" "ete" ON pe."txId" = ete."spendTxId"
LEFT JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"
`);
await queryRunner.query(
`
INSERT INTO "typeorm_metadata"(
"database",
"schema",
"table",
"type",
"name",
"value"
)
VALUES (NULL, NULL, NULL, ?, ?, ?)
`,
[
'VIEW',
'revenue_view',
'SELECT "pe"."id" AS "id", "pe"."WID" AS "wid", "ete"."sourceChainHeight" AS "lockHeight", "be"."height" AS "height", "be"."timestamp" AS "timestamp", pe."txId" AS "permitTxId", ete."eventId" AS "eventId", ete."fromChain" AS "fromChain", ete."toChain" AS "toChain", ete."fromAddress" AS "fromAddress", ete."toAddress" AS "toAddress", ete."amount" AS "amount", ete."bridgeFee" AS "bridgeFee", ete."networkFee" AS "networkFee", ete."sourceChainTokenId" AS "tokenId", ete."sourceTxId" AS "lockTxId" FROM "permit_entity" "pe" INNER JOIN "event_trigger_entity" "ete" ON pe."txId" = ete."spendTxId" LEFT JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"',
]
);
await queryRunner.query(`
CREATE VIEW "revenue_chart_data" AS
SELECT "be"."year" AS "year",
"be"."month" AS "month",
"be"."day" AS "day",
re."tokenId" AS "tokenId",
re."amount" AS "amount",
be."timestamp" AS "timestamp"
FROM "revenue_entity" "re"
INNER JOIN "permit_entity" "pe" ON re."permitId" = "pe"."id"
INNER JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"
`);
await queryRunner.query(
`
INSERT INTO "typeorm_metadata"(
"database",
"schema",
"table",
"type",
"name",
"value"
)
VALUES (NULL, NULL, NULL, ?, ?, ?)
`,
[
'VIEW',
'revenue_chart_data',
'SELECT "be"."year" AS "year", "be"."month" AS "month", "be"."day" AS "day", re."tokenId" AS "tokenId", re."amount" AS "amount", be."timestamp" AS "timestamp" FROM "revenue_entity" "re" INNER JOIN "permit_entity" "pe" ON re."permitId" = "pe"."id" INNER JOIN "block_entity" "be" ON "pe"."block" = "be"."hash"',
]
);
}
}
Loading

0 comments on commit e82b354

Please sign in to comment.