Skip to content

Commit

Permalink
Merge pull request #455 from helium/dz/mv-pg-index-creation
Browse files Browse the repository at this point in the history
Moving pg index creation
  • Loading branch information
deasydoesit authored Oct 23, 2023
2 parents 52e6919 + 7a5cd52 commit cc95cfd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,10 @@
{ "type": "RecipientV0", "table": "recipients", "schema": "public" }
]
}
],
"indexConfigs": [
"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);"
]
}
12 changes: 9 additions & 3 deletions packages/account-postgres-sink-service/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "./env";
import database from "./utils/database";
import { defineAllIdlModels } from "./utils/defineIdlModels";
import { createPgIndexes } from "./utils/createPgIndexes";
import { truthy, upsertProgramAccounts } from "./utils/upsertProgramAccounts";
import { integrityCheckProgramAccounts } from "./utils/integrityCheckProgramAccounts";
import { handleAccountWebhook } from "./utils/handleAccountWebhook";
Expand All @@ -24,12 +25,16 @@ if (!HELIUS_AUTH_SECRET) {
}

(async () => {
const configs = (() => {
const accountConfigs: null | {
const { configs, indexConfigs } = (() => {
const dbConfigs: null | {
configs: IConfig[];
indexConfigs?: string[]
} = JSON.parse(fs.readFileSync(PROGRAM_ACCOUNT_CONFIGS, "utf8"));

return accountConfigs ? accountConfigs.configs : [];
return {
configs: dbConfigs && dbConfigs.configs ? dbConfigs.configs : [],
indexConfigs: dbConfigs && dbConfigs.indexConfigs ? dbConfigs.indexConfigs : [],
}
})();

const customJobs = configs
Expand Down Expand Up @@ -208,6 +213,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({ 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Sequelize } from "sequelize";

export const createPgIndexes = async ({
indexConfigs,
sequelize
}: {
indexConfigs: string[],
sequelize: Sequelize;
}) => {
try {
if (indexConfigs.length === 0) {
console.log("createPgIndexes: No indexes created!");
return;
}

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);
}
};
11 changes: 1 addition & 10 deletions packages/metadata-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
IotHotspotInfo,
KeyToAsset,
MobileHotspotInfo,
sequelize,
} from "./model";
import { provider } from "./solana";

Expand Down Expand Up @@ -160,19 +159,11 @@ function locationAttributes(

const start = async () => {
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);
`);
await server.listen({ port: 8081, host: "0.0.0.0" });

const address = server.server.address();
const port = typeof address === "string" ? address : address?.port;
console.log(`Running on 0.0.0.0:${port}`);
} catch (err) {
server.log.error(err);
process.exit(1);
Expand Down

0 comments on commit cc95cfd

Please sign in to comment.