diff --git a/packages/account-postgres-sink-service/program_account_configs_example.json b/packages/account-postgres-sink-service/program_account_configs_example.json index f84671ad4..0d540d950 100644 --- a/packages/account-postgres-sink-service/program_account_configs_example.json +++ b/packages/account-postgres-sink-service/program_account_configs_example.json @@ -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);" ] } diff --git a/packages/account-postgres-sink-service/src/server.ts b/packages/account-postgres-sink-service/src/server.ts index 35f37bafc..6a8956d58 100644 --- a/packages/account-postgres-sink-service/src/server.ts +++ b/packages/account-postgres-sink-service/src/server.ts @@ -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"; @@ -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 @@ -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; diff --git a/packages/account-postgres-sink-service/src/utils/createPgIndexes.ts b/packages/account-postgres-sink-service/src/utils/createPgIndexes.ts new file mode 100644 index 000000000..28efaeea5 --- /dev/null +++ b/packages/account-postgres-sink-service/src/utils/createPgIndexes.ts @@ -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); + } +}; \ No newline at end of file diff --git a/packages/metadata-service/src/index.ts b/packages/metadata-service/src/index.ts index 4f821b0b0..6d6bacafd 100644 --- a/packages/metadata-service/src/index.ts +++ b/packages/metadata-service/src/index.ts @@ -15,7 +15,6 @@ import { IotHotspotInfo, KeyToAsset, MobileHotspotInfo, - sequelize, } from "./model"; import { provider } from "./solana"; @@ -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);