You've already forked joplin
							
							
				mirror of
				https://github.com/laurent22/joplin.git
				synced 2025-10-31 00:07:48 +02:00 
			
		
		
		
	Server: Remove USERS_WITH_REPLICATION env variable
This commit is contained in:
		| @@ -15,7 +15,6 @@ | ||||
| # SLAVE_POSTGRES_USER=joplin | ||||
| # SLAVE_POSTGRES_PORT=5433 | ||||
| # SLAVE_POSTGRES_HOST=localhost | ||||
| # USERS_WITH_REPLICATION=ID1,ID2,... | ||||
|  | ||||
| version: '2' | ||||
|  | ||||
|   | ||||
| @@ -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...'); | ||||
| 		} | ||||
|   | ||||
| @@ -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', | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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 | ||||
| 	}); | ||||
|  | ||||
| }); | ||||
| @@ -69,14 +69,12 @@ export default abstract class BaseModel<T> { | ||||
| 	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,26 +115,10 @@ export default abstract class BaseModel<T> { | ||||
| 		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}`); | ||||
| 	public get dbSlave(): DbConnection { | ||||
| 		return this.dbSlave_; | ||||
| 	} | ||||
|  | ||||
| 		return this.db_; | ||||
| 	} | ||||
|  | ||||
| 	protected get defaultFields(): string[] { | ||||
| 		if (!this.defaultFields_.length) { | ||||
| 			this.defaultFields_ = Object.keys(databaseSchema[this.tableName]); | ||||
|   | ||||
| @@ -199,8 +199,8 @@ export default class ChangeModel extends BaseModel<Change> { | ||||
| 		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<Change> { | ||||
| 					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<Change> { | ||||
| 				`, finalParams); | ||||
| 			} | ||||
| 		} else { | ||||
| 			query = this.dbSlave(userId).raw(` | ||||
| 			query = this.dbSlave.raw(` | ||||
| 				SELECT count(*) as total | ||||
| 				FROM ( | ||||
| 					(${subQuery1}) | ||||
|   | ||||
| @@ -102,7 +102,7 @@ export default class ItemModel extends BaseModel<Item> { | ||||
| 		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<Item> { | ||||
| 			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; | ||||
| 			} | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user