Merge pull request #17443 from bforbis/knex_application_name

Knex application name
This commit is contained in:
Johan Haals
2023-04-20 10:18:52 +02:00
committed by GitHub
3 changed files with 90 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-common': patch
---
Updated the `DatabaseManager` to include the plugin id in the Postgres application name of the database connections created for each plugin.
@@ -165,6 +165,7 @@ describe('DatabaseManager', () => {
user: 'foo',
password: 'bar',
port: '5432',
application_name: 'backstage_plugin_pluginwithoutconfig',
},
});
@@ -741,5 +742,83 @@ describe('DatabaseManager', () => {
expect(baseConfig.data.role).toEqual('backstage-plugin');
});
it('Defaults the application_name for postgres clients', async () => {
const testManager = DatabaseManager.fromConfig(
new ConfigReader({
backend: {
database: {
client: 'pg',
connection: {},
},
},
}),
);
await testManager.forPlugin('testplugin').getClient();
const mockCalls = mocked(createDatabaseClient).mock.calls.splice(-1);
const [baseConfig, _] = mockCalls[0];
expect(baseConfig.get()).toMatchObject({
connection: {
application_name: 'backstage_plugin_testplugin',
},
});
});
it('Allows manually setting the application_name for postgres clients', async () => {
const testManager = DatabaseManager.fromConfig(
new ConfigReader({
backend: {
database: {
client: 'pg',
connection: {
application_name: 'backstage_custom_app_name',
},
},
},
}),
);
await testManager.forPlugin('testplugin').getClient();
const mockCalls = mocked(createDatabaseClient).mock.calls.splice(-1);
const [baseConfig, overrides] = mockCalls[0];
expect(baseConfig.get().connection.application_name).toBe(
'backstage_custom_app_name',
);
expect(overrides.connection.application_name).toBeUndefined();
});
it('Allows manually setting the application_name for individual plugin client', async () => {
const testManager = DatabaseManager.fromConfig(
new ConfigReader({
backend: {
database: {
client: 'pg',
connection: {
host: 'localhost',
user: 'foo',
password: 'bar',
database: 'foodb',
application_name: 'backstage_custom_app_name',
},
plugin: {
overrideplugin: {
connection: {
application_name: 'custom_plugin',
},
},
},
},
},
}),
);
await testManager.forPlugin('overrideplugin').getClient();
const mockCalls = mocked(createDatabaseClient).mock.calls.splice(-1);
const [baseConfig, _] = mockCalls[0];
expect(baseConfig.get().connection.application_name).toBe(
'custom_plugin',
);
});
});
});
@@ -268,6 +268,12 @@ export class DatabaseManager {
client,
);
if (client === 'pg') {
(
baseConnection as Knex.PgConnectionConfig
).application_name ||= `backstage_plugin_${pluginId}`;
}
return {
// include base connection if client type has not been overridden
...(overridden ? {} : baseConnection),