| 190 | } |
| 191 | |
| 192 | async init(schema: string, tx: Transaction): Promise<void> { |
| 193 | try { |
| 194 | if (this.historical) { |
| 195 | const [results] = await this.sequelize.query(BTREE_GIST_EXTENSION_EXIST_QUERY); |
| 196 | if (results.length === 0) { |
| 197 | throw new Error('Btree_gist extension is required to enable historical data, contact DB admin for support'); |
| 198 | } |
| 199 | } |
| 200 | /* |
| 201 | On SyncSchema, if no schema migration is introduced, it would consider current schema to be null, and go all db operations again |
| 202 | every start up is a migration |
| 203 | */ |
| 204 | const schemaMigrationService = new SchemaMigrationService(this.sequelize, this, schema, this.config); |
| 205 | |
| 206 | await schemaMigrationService.run(null, this.subqueryProject.schema, tx); |
| 207 | |
| 208 | const deploymentsRaw = await this.metadataModel.find('deployments'); |
| 209 | const deployments = deploymentsRaw ? JSON.parse(deploymentsRaw) : {}; |
| 210 | |
| 211 | // Check if the deployment change or a local project is running |
| 212 | // WARNING:This assumes that the root is the same as the id for local project, there are no checks for this and it could change at any time |
| 213 | if ( |
| 214 | this.subqueryProject.id === this.subqueryProject.root || |
| 215 | last(Object.values(deployments)) !== this.subqueryProject.id |
| 216 | ) { |
| 217 | await this.metadataModel.setIncrement('schemaMigrationCount', undefined, tx); |
| 218 | } |
| 219 | } catch (e: any) { |
| 220 | exitWithError(new Error(`Having a problem when syncing schema`, {cause: e}), logger); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | private async initHotSchemaReloadQueries(schema: string): Promise<void> { |
| 225 | /* These SQL queries are to allow hot-schema reload on query service */ |