Skip to content

Commit

Permalink
feat(database): add basic replica recognition to stop system schema s…
Browse files Browse the repository at this point in the history
…yncs from replicas

fix(database): manually modify "sync" variable of sequelize models when on instance syncs
  • Loading branch information
kkopanidis committed Jan 18, 2024
1 parent cef3d78 commit 20aefaf
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
15 changes: 9 additions & 6 deletions modules/database/src/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,23 @@ export default class DatabaseModule extends ManagedModule<void> {
}

async onServerStart() {
await this._activeAdapter.registerSystemSchema(models.DeclaredSchema);
await this._activeAdapter.registerSystemSchema(models.MigratedSchemas);
const isReplica = this.grpcSdk.isAvailable('database');
await this._activeAdapter.registerSystemSchema(models.DeclaredSchema, isReplica);
await this._activeAdapter.registerSystemSchema(models.MigratedSchemas, isReplica);
let modelPromises = Object.values(models).flatMap((model: ConduitSchema) => {
if (['_DeclaredSchema', 'MigratedSchemas'].includes(model.name)) return [];
return this._activeAdapter.registerSystemSchema(model);
return this._activeAdapter.registerSystemSchema(model, isReplica);
});
await Promise.all(modelPromises);
await this._activeAdapter.retrieveForeignSchemas();
await this._activeAdapter.recoverSchemasFromDatabase();
await this._activeAdapter.recoverViewsFromDatabase();
await runMigrations(this._activeAdapter);
if (!isReplica) {
await runMigrations(this._activeAdapter);
}
modelPromises = Object.values(models).flatMap((model: ConduitSchema) => {
return this._activeAdapter.registerSystemSchema(model).then(() => {
if (this._activeAdapter.getDatabaseType() !== 'MongoDB') {
return this._activeAdapter.registerSystemSchema(model, isReplica).then(() => {
if (this._activeAdapter.getDatabaseType() !== 'MongoDB' && !isReplica) {
return this._activeAdapter.syncSchema(model.name);
}
});
Expand Down
6 changes: 3 additions & 3 deletions modules/database/src/adapters/DatabaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export abstract class DatabaseAdapter<T extends Schema> {
this.legacyDeployment = await this.hasLegacyCollections();
}

async registerSystemSchema(schema: ConduitSchema) {
async registerSystemSchema(schema: ConduitSchema, isReplica: boolean) {
// @dirty-type-cast
await this.createSchemaFromAdapter(schema);
await this.createSchemaFromAdapter(schema, false, false, isReplica);
this._systemSchemas.add(schema.name);
}

Expand Down Expand Up @@ -282,7 +282,7 @@ export abstract class DatabaseAdapter<T extends Schema> {
model,
!!model.modelOptions.conduit?.imported,
true,
false,
true,
);
});

Expand Down
2 changes: 2 additions & 0 deletions modules/database/src/adapters/sequelize-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ export abstract class SequelizeAdapter extends DatabaseAdapter<SequelizeSchema>
// do not sync extracted schemas
if (isNil(noSync) || !noSync) {
await this.models[schema.name].sync();
} else {
this.models[schema.name].synced = true;
}
// do not store extracted schemas to db
if (saveToDb && !isInstanceSync) {
Expand Down

0 comments on commit 20aefaf

Please sign in to comment.