Merge pull request #27956 from backstage/blam/small-changes
postgres: set the `connection.type` to `default` when not provided
This commit is contained in:
@@ -220,6 +220,41 @@ describe('postgres', () => {
|
||||
useNullAsDefault: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('throws an error when the connection type is not supported', async () => {
|
||||
await expect(
|
||||
buildPgDatabaseConfig(
|
||||
new ConfigReader({
|
||||
client: 'pg',
|
||||
connection: {
|
||||
type: 'not-supported',
|
||||
},
|
||||
}),
|
||||
),
|
||||
).rejects.toThrow('Unknown connection type: not-supported');
|
||||
});
|
||||
|
||||
it('supports default as the default connection type', async () => {
|
||||
await expect(
|
||||
buildPgDatabaseConfig(
|
||||
new ConfigReader({
|
||||
client: 'pg',
|
||||
connection: {
|
||||
type: 'default',
|
||||
port: '5432',
|
||||
database: 'other_db',
|
||||
},
|
||||
}),
|
||||
),
|
||||
).resolves.toEqual({
|
||||
client: 'pg',
|
||||
connection: {
|
||||
port: '5432',
|
||||
database: 'other_db',
|
||||
},
|
||||
useNullAsDefault: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getPgConnectionConfig', () => {
|
||||
|
||||
@@ -77,44 +77,48 @@ export async function buildPgDatabaseConfig(
|
||||
overrides,
|
||||
);
|
||||
|
||||
if (config.connection?.type === 'cloudsql') {
|
||||
if (config.client !== 'pg') {
|
||||
throw new Error('Cloud SQL only supports the pg client');
|
||||
}
|
||||
const sanitizedConfig = JSON.parse(JSON.stringify(config));
|
||||
|
||||
if (!config.connection.instance) {
|
||||
throw new Error('Missing instance connection name for Cloud SQL');
|
||||
}
|
||||
// Trim additional properties from the connection object passed to knex
|
||||
delete sanitizedConfig.connection.type;
|
||||
delete sanitizedConfig.connection.instance;
|
||||
|
||||
const {
|
||||
Connector: CloudSqlConnector,
|
||||
IpAddressTypes,
|
||||
AuthTypes,
|
||||
} = await import('@google-cloud/cloud-sql-connector');
|
||||
const connector = new CloudSqlConnector();
|
||||
const clientOpts = await connector.getOptions({
|
||||
instanceConnectionName: config.connection.instance,
|
||||
ipType: IpAddressTypes.PUBLIC,
|
||||
authType: AuthTypes.IAM,
|
||||
});
|
||||
|
||||
const cloudsqlConfig = {
|
||||
...config,
|
||||
client: 'pg',
|
||||
connection: {
|
||||
...config.connection,
|
||||
...clientOpts,
|
||||
},
|
||||
};
|
||||
|
||||
// Trim additional properties from the connection object passed to knex
|
||||
delete cloudsqlConfig.connection.type;
|
||||
delete cloudsqlConfig.connection.instance;
|
||||
|
||||
return cloudsqlConfig;
|
||||
if (config.connection.type === 'default' || !config.connection.type) {
|
||||
return sanitizedConfig;
|
||||
}
|
||||
|
||||
return config;
|
||||
if (config.connection.type !== 'cloudsql') {
|
||||
throw new Error(`Unknown connection type: ${config.connection.type}`);
|
||||
}
|
||||
|
||||
if (config.client !== 'pg') {
|
||||
throw new Error('Cloud SQL only supports the pg client');
|
||||
}
|
||||
|
||||
if (!config.connection.instance) {
|
||||
throw new Error('Missing instance connection name for Cloud SQL');
|
||||
}
|
||||
|
||||
const {
|
||||
Connector: CloudSqlConnector,
|
||||
IpAddressTypes,
|
||||
AuthTypes,
|
||||
} = await import('@google-cloud/cloud-sql-connector');
|
||||
const connector = new CloudSqlConnector();
|
||||
const clientOpts = await connector.getOptions({
|
||||
instanceConnectionName: config.connection.instance,
|
||||
ipType: IpAddressTypes.PUBLIC,
|
||||
authType: AuthTypes.IAM,
|
||||
});
|
||||
|
||||
return {
|
||||
...sanitizedConfig,
|
||||
client: 'pg',
|
||||
connection: {
|
||||
...sanitizedConfig.connection,
|
||||
...clientOpts,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user