Skip to content

Commit

Permalink
Updating per NP feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
deasydoesit committed Oct 20, 2023
1 parent d9fe121 commit 892ff03
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"configs": [
"CREATE UNIQUE INDEX IF NOT EXISTS key_to_asset_asset_index ON key_to_assets(asset);",
"CREATE UNIQUE INDEX IF NOT EXISTS iot_hotspot_infos_asset_index ON iot_hotspot_infos(asset);",
"CREATE UNIQUE INDEX IF NOT EXISTS mobile_hotspot_infos_asset_index ON mobile_hotspot_infos(asset);"
]
}
2 changes: 2 additions & 0 deletions packages/account-postgres-sink-service/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const PROGRAM_ACCOUNT_CONFIGS =
process.env.PROGRAM_ACCOUNT_CONFIGS ||
`${__dirname}/../program_account_configs_example.json`;

export const INDEX_CONFIGS = process.env.INDEX_CONFIGS;

export const HELIUS_AUTH_SECRET = process.env.HELIUS_AUTH_SECRET;

export const RUN_JOBS_AT_STARTUP = process.env.RUN_JOBS_AT_STARTUP === 'true';
13 changes: 12 additions & 1 deletion packages/account-postgres-sink-service/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PublicKey } from "@solana/web3.js";
import {
HELIUS_AUTH_SECRET,
PROGRAM_ACCOUNT_CONFIGS,
INDEX_CONFIGS,
RUN_JOBS_AT_STARTUP,
} from "./env";
import database from "./utils/database";
Expand All @@ -33,6 +34,16 @@ if (!HELIUS_AUTH_SECRET) {
return accountConfigs ? accountConfigs.configs : [];
})();

const indexConfigs = (() => {
let configs: null | { configs: string[] } = null;

if (INDEX_CONFIGS) {
configs = JSON.parse(fs.readFileSync(INDEX_CONFIGS, "utf8"));
}

return configs ? configs.configs : [];
})();

const customJobs = configs
.filter((x) => !!x.crons)
.flatMap(({ programId, crons = [] }) =>
Expand Down Expand Up @@ -209,7 +220,7 @@ if (!HELIUS_AUTH_SECRET) {
// models are defined on boot, and updated in refresh-accounts
await database.sync();
await defineAllIdlModels({ configs, sequelize: database });
await createPgIndexes({ sequelize: database });
await createPgIndexes({ indexConfigs, sequelize: database });
await server.listen({ port: 3000, host: "0.0.0.0" });
const address = server.server.address();
const port = typeof address === "string" ? address : address?.port;
Expand Down
20 changes: 10 additions & 10 deletions packages/account-postgres-sink-service/src/utils/createPgIndexes.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Sequelize } from "sequelize";

export const createPgIndexes = async ({
sequelize,
indexConfigs,
sequelize
}: {
indexConfigs: string[],
sequelize: Sequelize;
}) => {
try {
await sequelize.query(`
CREATE UNIQUE INDEX IF NOT EXISTS key_to_asset_asset_index ON key_to_assets(asset);
`);
await sequelize.query(`
CREATE UNIQUE INDEX IF NOT EXISTS iot_hotspot_infos_asset_index ON iot_hotspot_infos(asset);
`);
await sequelize.query(`
CREATE UNIQUE INDEX IF NOT EXISTS mobile_hotspot_infos_asset_index ON mobile_hotspot_infos(asset);
`);
const indexPromises = indexConfigs.map((config) => {
return sequelize.query(config);
});

await Promise.all(indexPromises);

console.log("createPgIndexes: Indexes created!");
} catch (err) {
console.error("createPgIndexes: Index creation failed");
console.error(err);
Expand Down

0 comments on commit 892ff03

Please sign in to comment.