-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add migration record tracking (#16)
* feat: add migration record tracking * chore: update default db settings
- Loading branch information
1 parent
606b453
commit 0ec07d0
Showing
11 changed files
with
753 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,9 @@ | ||
export const NODE_ENV = process.env.NODE_ENV || "local"; | ||
export const DB_DATABASE = process.env.DB_DATABASE || "gas-tank"; | ||
export const DB_HOST = process.env.DB_HOST || "host.docker.internal"; | ||
export const DB_PASSWORD = process.env.DB_PASSWORD || ""; | ||
export const DB_PORT = Number(process.env.DB_PORT) || 5432; | ||
export const DB_USERNAME = process.env.DB_USERNAME || "postgres"; | ||
export const DB_SSL_MODE = process.env.DB_SSL_MODE | ||
? (process.env.DB_SSL_MODE || "").toLowerCase() === "true" | ||
: NODE_ENV !== "local"; |
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,26 @@ | ||
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from "typeorm"; | ||
|
||
@Entity({ | ||
name: "migration_tracking", | ||
}) | ||
export class MigrationTracking extends BaseEntity { | ||
@PrimaryGeneratedColumn("increment") | ||
id!: number; | ||
|
||
@Column({ name: "transaction_hash", type: "text" }) | ||
transactionHash!: string; | ||
|
||
@Column({ name: "wallet_address", type: "text" }) | ||
walletAddress!: string; | ||
|
||
@Column({ | ||
nullable: true, | ||
type: "text", | ||
}) | ||
token!: string; | ||
|
||
@Column({ | ||
type: "text", | ||
}) | ||
chain!: string; | ||
} |
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,43 @@ | ||
import { DataSource } from "typeorm"; | ||
import { | ||
DB_DATABASE, | ||
DB_HOST, | ||
DB_PASSWORD, | ||
DB_PORT, | ||
DB_USERNAME, | ||
DB_SSL_MODE, | ||
NODE_ENV, | ||
} from "../config/db"; | ||
import { MigrationTracking } from "./entity/MigrationTracking"; | ||
|
||
const PaymasterDataSource = new DataSource({ | ||
type: "postgres", | ||
host: DB_HOST, | ||
port: DB_PORT, | ||
username: DB_USERNAME, | ||
password: DB_PASSWORD, | ||
database: DB_DATABASE, | ||
synchronize: true, | ||
ssl: DB_SSL_MODE | ||
? process.env.CA_CERT | ||
? { | ||
rejectUnauthorized: true, | ||
ca: process.env.CA_CERT, | ||
} | ||
: { | ||
rejectUnauthorized: false, | ||
} | ||
: false, | ||
logging: false, | ||
entities: [MigrationTracking], | ||
migrations: | ||
NODE_ENV === "local" | ||
? ["src/migrations/**/*.ts"] | ||
: ["dist/migrations/**/*.js"], | ||
subscribers: | ||
NODE_ENV === "local" | ||
? ["src/subscriber/**/*.ts"] | ||
: ["dist/subscriber/**/*.js"], | ||
}); | ||
|
||
export { PaymasterDataSource }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { QueryRunner } from "typeorm"; | ||
import { MigrationTracking } from "../db/entity/MigrationTracking"; | ||
|
||
export type SaveMigrationRecordParams = { | ||
user: string; | ||
tokenAddress: string; | ||
txHash: string; | ||
}; | ||
export async function saveMigrationRecord( | ||
queryRunner: QueryRunner, | ||
{ user, tokenAddress, txHash }: SaveMigrationRecordParams | ||
) { | ||
const trackRecord = queryRunner.manager.create(MigrationTracking, { | ||
chain: "ethereum", | ||
token: tokenAddress.toLowerCase(), | ||
transactionHash: txHash, | ||
walletAddress: user.toLowerCase(), | ||
}); | ||
|
||
await queryRunner.manager.save(trackRecord); | ||
} |
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,19 @@ | ||
import { QueryRunner } from "typeorm"; | ||
import { MigrationTracking } from "../../db/entity/MigrationTracking"; | ||
|
||
export type IsAlreadyMigratedParams = { | ||
user: string; | ||
tokenAddress: string; | ||
}; | ||
export async function isAlreadyMigrated( | ||
queryRunner: QueryRunner, | ||
{ user, tokenAddress }: IsAlreadyMigratedParams | ||
) { | ||
const trackRecord = await queryRunner.manager.findOneBy(MigrationTracking, { | ||
chain: "ethereum", | ||
token: tokenAddress.toLowerCase(), | ||
walletAddress: user.toLowerCase(), | ||
}); | ||
|
||
return Boolean(trackRecord); | ||
} |
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
Oops, something went wrong.