diff --git a/docker-compose.db-dev.yml b/docker-compose.db-dev.yml index fa92c84988..cb4ca4c581 100644 --- a/docker-compose.db-dev.yml +++ b/docker-compose.db-dev.yml @@ -15,7 +15,6 @@ # SLAVE_POSTGRES_USER=joplin # SLAVE_POSTGRES_PORT=5433 # SLAVE_POSTGRES_HOST=localhost -# USERS_WITH_REPLICATION=ID1,ID2,... version: '2' diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index 5ecd24f960..8f60a2f183 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -336,7 +336,6 @@ async function main() { if (slaveConnectionCheck) { printConnectionCheckInfo(slaveConnectionCheck); - appLogger().info(`Users with replication: ${config().USERS_WITH_REPLICATION}`); } else { appLogger().info('Not using database replication...'); } diff --git a/packages/server/src/db.replication.test.ts b/packages/server/src/db.replication.test.ts index 56713b0cfe..b8a09a4d16 100644 --- a/packages/server/src/db.replication.test.ts +++ b/packages/server/src/db.replication.test.ts @@ -100,7 +100,6 @@ describe('db.replication', () => { const { session, user } = await createUserAndSession(1, true); const changeModel = models().change(); - changeModel.usersWithReplication_ = [user.id]; const folder = { id: '000000000000000000000000000000F1', diff --git a/packages/server/src/env.ts b/packages/server/src/env.ts index 0b1c653957..31b24eaff4 100644 --- a/packages/server/src/env.ts +++ b/packages/server/src/env.ts @@ -22,7 +22,6 @@ const defaultEnvValues: EnvVariables = { ERROR_STACK_TRACES: false, COOKIES_SECURE: false, RUNNING_IN_DOCKER: false, - USERS_WITH_REPLICATION: '', // Temporary HEARTBEAT_MESSAGE_SCHEDULE: '* * * * *', // The admin panel is accessible only if this is an admin instance. @@ -152,7 +151,6 @@ export interface EnvVariables { ERROR_STACK_TRACES: boolean; COOKIES_SECURE: boolean; RUNNING_IN_DOCKER: boolean; - USERS_WITH_REPLICATION: string; HEARTBEAT_MESSAGE_SCHEDULE: string; MAX_TIME_DRIFT: number; diff --git a/packages/server/src/models/BaseModel.test.ts b/packages/server/src/models/BaseModel.test.ts deleted file mode 100644 index 33d0616860..0000000000 --- a/packages/server/src/models/BaseModel.test.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { beforeAllDb, afterAllTests, beforeEachDb, models } from '../utils/testing/testUtils'; - -describe('BaseModel', () => { - - beforeAll(async () => { - await beforeAllDb('BaseModel'); - }); - - afterAll(async () => { - await afterAllTests(); - }); - - beforeEach(async () => { - await beforeEachDb(); - }); - - test('should check if a user has replication', async () => { - // cSpell:disable - const itemModel = models().item(); - itemModel.usersWithReplication_ = ['A', 'B', 'EYE1m66mGmA01sDDDDKE19']; - expect(itemModel.isUserWithReplication('AAAAAAAAAAAA')).toBe(true); - expect(itemModel.isUserWithReplication('AAAAAAAAAAAAEEEEEEE')).toBe(true); - expect(itemModel.isUserWithReplication('bbbbbbbb')).toBe(false); - expect(itemModel.isUserWithReplication('BBBBBBBBBB')).toBe(true); - expect(itemModel.isUserWithReplication('')).toBe(false); - expect(itemModel.isUserWithReplication('EYE1m66mGmA01sDDDDKE19')).toBe(true); - // cSpell:enable - }); - -}); diff --git a/packages/server/src/models/BaseModel.ts b/packages/server/src/models/BaseModel.ts index 25bc18c6db..9c8a9d494c 100644 --- a/packages/server/src/models/BaseModel.ts +++ b/packages/server/src/models/BaseModel.ts @@ -69,14 +69,12 @@ export default abstract class BaseModel { private modelFactory_: NewModelFactoryHandler; private config_: Config; private savePoints_: SavePoint[] = []; - public usersWithReplication_: string[] = []; public constructor(db: DbConnection, dbSlave: DbConnection, modelFactory: NewModelFactoryHandler, config: Config) { this.db_ = db; this.dbSlave_ = dbSlave; this.modelFactory_ = modelFactory; this.config_ = config; - this.usersWithReplication_ = config.USERS_WITH_REPLICATION ? config.USERS_WITH_REPLICATION.split(',') : []; this.transactionHandler_ = new TransactionHandler(db); } @@ -117,24 +115,8 @@ export default abstract class BaseModel { return this.db_; } - public isUserWithReplication(userId: Uuid) { - if (!userId) return false; - - for (const id of this.usersWithReplication_) { - if (id === userId) return true; - if (id.length < 22 && userId.startsWith(id)) return true; - } - - return false; - } - - public dbSlave(userId: Uuid = ''): DbConnection { - if (this.isUserWithReplication(userId)) { - logger.info(`Using slave database for user: ${userId}`); - return this.dbSlave_; - } - - return this.db_; + public get dbSlave(): DbConnection { + return this.dbSlave_; } protected get defaultFields(): string[] { diff --git a/packages/server/src/models/ChangeModel.ts b/packages/server/src/models/ChangeModel.ts index b98636088a..4ce0b4290d 100644 --- a/packages/server/src/models/ChangeModel.ts +++ b/packages/server/src/models/ChangeModel.ts @@ -199,8 +199,8 @@ export default class ChangeModel extends BaseModel { if (!doCountQuery) { finalParams.push(limit); - if (isPostgres(this.dbSlave(userId))) { - query = this.dbSlave(userId).raw(` + if (isPostgres(this.dbSlave)) { + query = this.dbSlave.raw(` WITH cte1 AS MATERIALIZED ( ${subQuery1} ) @@ -214,7 +214,7 @@ export default class ChangeModel extends BaseModel { LIMIT ? `, finalParams); } else { - query = this.dbSlave(userId).raw(` + query = this.dbSlave.raw(` SELECT ${fieldsSql} FROM (${subQuery1}) as sub1 UNION ALL SELECT ${fieldsSql} FROM (${subQuery2}) as sub2 @@ -223,7 +223,7 @@ export default class ChangeModel extends BaseModel { `, finalParams); } } else { - query = this.dbSlave(userId).raw(` + query = this.dbSlave.raw(` SELECT count(*) as total FROM ( (${subQuery1}) diff --git a/packages/server/src/models/ItemModel.ts b/packages/server/src/models/ItemModel.ts index fa69e87d43..d767cf25e1 100644 --- a/packages/server/src/models/ItemModel.ts +++ b/packages/server/src/models/ItemModel.ts @@ -102,7 +102,7 @@ export default class ItemModel extends BaseModel { let driver = ItemModel.storageDrivers_.get(config); if (!driver) { - driver = await loadStorageDriver(config, this.db, this.dbSlave()); + driver = await loadStorageDriver(config, this.db, this.dbSlave); ItemModel.storageDrivers_.set(config, driver); } @@ -331,7 +331,7 @@ export default class ItemModel extends BaseModel { let fromDriver: StorageDriverBase = drivers[item.content_storage_id]; if (!fromDriver) { - fromDriver = await loadStorageDriver(item.content_storage_id, this.db, this.dbSlave()); + fromDriver = await loadStorageDriver(item.content_storage_id, this.db, this.dbSlave); drivers[item.content_storage_id] = fromDriver; }