Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(database): high-availability issues especially on SQL dbs #899

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 9 additions & 4 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 @@ -85,7 +85,11 @@ export abstract class DatabaseAdapter<T extends Schema> {
await this.addExtensionsFromSchemaModel(schema, gRPC);
stitchSchema(schema as ConduitDatabaseSchema); // @dirty-type-cast
const schemaUpdate = this.registeredSchemas.has(schema.name);
const createdSchema = await this._createSchemaFromAdapter(schema, !instanceSync);
const createdSchema = await this._createSchemaFromAdapter(
schema,
!instanceSync,
instanceSync,
);
this.hashSchemaFields(schema as ConduitDatabaseSchema); // @dirty-type-cast
if (!instanceSync && !schemaUpdate) {
ConduitGrpcSdk.Metrics?.increment('registered_schemas_total', 1, {
Expand Down Expand Up @@ -278,7 +282,7 @@ export abstract class DatabaseAdapter<T extends Schema> {
model,
!!model.modelOptions.conduit?.imported,
true,
false,
true,
);
});

Expand Down Expand Up @@ -377,6 +381,7 @@ export abstract class DatabaseAdapter<T extends Schema> {
protected abstract _createSchemaFromAdapter(
schema: ConduitSchema,
saveToDb: boolean,
instanceSync: boolean,
): Promise<Schema>;

protected async saveSchemaToDatabase(schema: ConduitSchema) {
Expand Down
3 changes: 2 additions & 1 deletion modules/database/src/adapters/mongoose-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ export class MongooseAdapter extends DatabaseAdapter<MongooseSchema> {
protected async _createSchemaFromAdapter(
schema: ConduitDatabaseSchema,
saveToDb: boolean = true,
isInstanceSync: boolean = false,
): Promise<MongooseSchema> {
let compiledSchema = JSON.parse(JSON.stringify(schema));
validateFieldConstraints(compiledSchema, 'mongodb');
Expand Down Expand Up @@ -424,7 +425,7 @@ export class MongooseAdapter extends DatabaseAdapter<MongooseSchema> {
await this.saveSchemaToDatabase(schema);
}

if (indexes) {
if (indexes && !isInstanceSync) {
await this.createIndexes(schema.name, indexes, schema.ownerModule);
}
return this.models[schema.name];
Expand Down
81 changes: 9 additions & 72 deletions modules/database/src/adapters/sequelize-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from '../../interfaces';
import { sqlSchemaConverter } from './sql-adapter/SqlSchemaConverter';
import { pgSchemaConverter } from './postgres-adapter/PgSchemaConverter';
import { isNil, merge } from 'lodash';
import { isNil } from 'lodash';

const sqlSchemaName = process.env.SQL_SCHEMA ?? 'public';

Expand Down Expand Up @@ -203,77 +203,10 @@ export abstract class SequelizeAdapter extends DatabaseAdapter<SequelizeSchema>
: schema.name;
}

private async processExtractedSchemas(
schema: ConduitDatabaseSchema,
extractedSchemas: Indexable,
associatedSchemas: { [key: string]: SequelizeSchema | SequelizeSchema[] },
) {
for (const extractedSchema in extractedSchemas) {
const modelOptions = merge({}, schema.modelOptions, {
conduit: {
cms: {
enabled: false,
crudOperations: {
read: {
enabled: false,
},
create: {
enabled: false,
},
delete: {
enabled: false,
},
update: {
enabled: false,
},
},
},

permissions: {
extendable: false,
canCreate: false,
canModify: 'Nothing',
canDelete: false,
},
},
});
let modeledSchema;
let isArray = false;
if (Array.isArray(extractedSchemas[extractedSchema])) {
isArray = true;
modeledSchema = new ConduitSchema(
`${schema.name}_${extractedSchema}`,
extractedSchemas[extractedSchema][0],
modelOptions,
`${schema.collectionName}_${extractedSchema}`,
);
} else {
modeledSchema = new ConduitSchema(
`${schema.name}_${extractedSchema}`,
extractedSchemas[extractedSchema],
modelOptions,
`${schema.collectionName}_${extractedSchema}`,
);
}

modeledSchema.ownerModule = schema.ownerModule;
(modeledSchema as ConduitDatabaseSchema).compiledFields = modeledSchema.fields;
// check index compatibility
const sequelizeSchema = await this._createSchemaFromAdapter(
modeledSchema as ConduitDatabaseSchema,
false,
{
parentSchema: schema.name,
},
);
associatedSchemas[extractedSchema] = isArray ? [sequelizeSchema] : sequelizeSchema;
}
}

protected async _createSchemaFromAdapter(
schema: ConduitDatabaseSchema,
saveToDb: boolean = true,
options?: { parentSchema: string },
isInstanceSync: boolean = false,
): Promise<SequelizeSchema> {
for (const [key, value] of Object.entries(this.views)) {
if (value.originalSchema.name === schema.name) {
Expand Down Expand Up @@ -309,13 +242,17 @@ export abstract class SequelizeAdapter extends DatabaseAdapter<SequelizeSchema>
objectPaths,
);

const noSync = this.models[schema.name].originalSchema.modelOptions.conduit!.noSync;
const noSync =
this.models[schema.name].originalSchema.modelOptions.conduit!.noSync ||
isInstanceSync;
// do not sync extracted schemas
if ((isNil(noSync) || !noSync) && !options) {
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 (!options && saveToDb) {
if (saveToDb && !isInstanceSync) {
await this.compareAndStoreMigratedSchema(schema);
await this.saveSchemaToDatabase(schema);
}
Expand Down
Loading