Test connection string now uses the currently typed in connection string rather than the last saved one

This commit is contained in:
Isaac Abadi
2021-07-26 20:31:35 -07:00
parent fb5c13db27
commit 258d5ff495
5 changed files with 15 additions and 10 deletions

View File

@@ -1600,9 +1600,10 @@ app.post('/api/transferDB', optionalJwt, async (req, res) => {
}); });
app.post('/api/testConnectionString', optionalJwt, async (req, res) => { app.post('/api/testConnectionString', optionalJwt, async (req, res) => {
const connection_string = req.body.connection_string;
let success = null; let success = null;
let error = ''; let error = '';
success = await db_api.connectToDB(0, true); success = await db_api.connectToDB(0, true, connection_string);
if (!success) error = 'Connection string failed.'; if (!success) error = 'Connection string failed.';
res.send({success: success, error: error}); res.send({success: success, error: error});

View File

@@ -74,9 +74,9 @@ exports.initialize = (input_db, input_users_db, input_logger) => {
using_local_db = config_api.getConfigItem('ytdl_use_local_db'); using_local_db = config_api.getConfigItem('ytdl_use_local_db');
} }
exports.connectToDB = async (retries = 5, no_fallback = false) => { exports.connectToDB = async (retries = 5, no_fallback = false, custom_connection_string = null) => {
if (using_local_db) return; if (using_local_db) return;
const success = await exports._connectToDB(); const success = await exports._connectToDB(custom_connection_string);
if (success) return true; if (success) return true;
if (retries) { if (retries) {
@@ -108,8 +108,8 @@ exports.connectToDB = async (retries = 5, no_fallback = false) => {
return true; return true;
} }
exports._connectToDB = async () => { exports._connectToDB = async (custom_connection_string = null) => {
const uri = config_api.getConfigItem('ytdl_mongodb_connection_string'); // "mongodb://127.0.0.1:27017/?compressors=zlib&gssapiServiceName=mongodb"; const uri = !custom_connection_string ? config_api.getConfigItem('ytdl_mongodb_connection_string') : custom_connection_string; // "mongodb://127.0.0.1:27017/?compressors=zlib&gssapiServiceName=mongodb";
const client = new MongoClient(uri, { const client = new MongoClient(uri, {
useNewUrlParser: true, useNewUrlParser: true,
useUnifiedTopology: true, useUnifiedTopology: true,
@@ -118,6 +118,10 @@ exports._connectToDB = async () => {
try { try {
await client.connect(); await client.connect();
database = client.db('ytdl_material'); database = client.db('ytdl_material');
// avoid doing anything else if it's just a test
if (custom_connection_string) return true;
const existing_collections = (await database.listCollections({}, { nameOnly: true }).toArray()).map(collection => collection.name); const existing_collections = (await database.listCollections({}, { nameOnly: true }).toArray()).map(collection => collection.name);
const missing_tables = tables_list.filter(table => !(existing_collections.includes(table))); const missing_tables = tables_list.filter(table => !(existing_collections.includes(table)));

View File

@@ -195,8 +195,8 @@ export class PostsService implements CanActivate {
return this.http.post(this.path + 'transferDB', {local_to_remote: local_to_remote}, this.httpOptions); return this.http.post(this.path + 'transferDB', {local_to_remote: local_to_remote}, this.httpOptions);
} }
testConnectionString() { testConnectionString(connection_string) {
return this.http.post(this.path + 'testConnectionString', {}, this.httpOptions); return this.http.post(this.path + 'testConnectionString', {connection_string: connection_string}, this.httpOptions);
} }
killAllDownloads() { killAllDownloads() {

View File

@@ -301,7 +301,7 @@
</mat-form-field> </mat-form-field>
<div class="test-connection-div"> <div class="test-connection-div">
<button (click)="testConnectionString()" [disabled]="testing_connection_string" mat-flat-button color="accent"><ng-container i18n="Test connection string button">Test connection string</ng-container></button> <button (click)="testConnectionString(new_config['Database']['mongodb_connection_string'])" [disabled]="testing_connection_string" mat-flat-button color="accent"><ng-container i18n="Test connection string button">Test connection string</ng-container></button>
</div> </div>
<div class="transfer-db-div"> <div class="transfer-db-div">

View File

@@ -307,9 +307,9 @@ export class SettingsComponent implements OnInit {
}); });
} }
testConnectionString() { testConnectionString(connection_string) {
this.testing_connection_string = true; this.testing_connection_string = true;
this.postsService.testConnectionString().subscribe(res => { this.postsService.testConnectionString(connection_string).subscribe(res => {
this.testing_connection_string = false; this.testing_connection_string = false;
if (res['success']) { if (res['success']) {
this.postsService.openSnackBar('Connection successful!'); this.postsService.openSnackBar('Connection successful!');