Skip to content

Commit 42de216

Browse files
authored
Do not fail Postgres init on staging or prod. (#9024)
1 parent ed88f93 commit 42de216

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

app/lib/database/database.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:gcloud/service_scope.dart' as ss;
1111
import 'package:meta/meta.dart';
1212
import 'package:postgres/postgres.dart';
1313
import 'package:pub_dev/service/secret/backend.dart';
14+
import 'package:pub_dev/shared/configuration.dart';
1415
import 'package:pub_dev/shared/env_config.dart';
1516

1617
final _random = Random.secure();
@@ -33,11 +34,15 @@ class PrimaryDatabase {
3334
/// the secret backend, connects to it and registers the primary database
3435
/// service in the current scope.
3536
static Future<void> tryRegisterInScope() async {
37+
if (activeConfiguration.isProduction) {
38+
// Production is not configured for postgresql yet.
39+
return;
40+
}
3641
var connectionString =
3742
envConfig.pubPostgresUrl ??
3843
(await secretBackend.lookup(SecretKey.postgresConnectionString));
39-
if (connectionString == null && envConfig.isRunningInAppengine) {
40-
// ignore for now, must throw once we have the environment setup ready
44+
if (connectionString == null && activeConfiguration.isStaging) {
45+
// Staging may not have the connection string set yet.
4146
return;
4247
}
4348
// The scope-specific custom database. We are creating a custom database for

app/lib/frontend/handlers/pubapi.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:_pub_shared/data/package_api.dart';
99
import 'package:_pub_shared/data/publisher_api.dart';
1010
import 'package:_pub_shared/data/task_api.dart';
1111
import 'package:api_builder/api_builder.dart';
12+
import 'package:pub_dev/database/database.dart';
1213
import 'package:pub_dev/frontend/handlers/atom_feed.dart';
1314
import 'package:shelf/shelf.dart';
1415
import 'package:shelf_router/shelf_router.dart';
@@ -457,6 +458,7 @@ class PubApi {
457458
Future<Response> debug(Request request) async => debugResponse({
458459
'package': packageDebugStats(),
459460
'search': searchDebugStats(),
461+
'database': primaryDatabase != null,
460462
});
461463

462464
@EndPoint.get('/packages.json')

app/lib/service/services.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'package:gcloud/service_scope.dart';
1414
import 'package:gcloud/storage.dart';
1515
import 'package:googleapis_auth/auth_io.dart' as auth;
1616
import 'package:logging/logging.dart';
17+
import 'package:postgres/postgres.dart';
1718
import 'package:pub_dev/database/database.dart';
1819
import 'package:pub_dev/package/api_export/api_exporter.dart';
1920
import 'package:pub_dev/search/handlers.dart';
@@ -251,7 +252,16 @@ Future<R> _withPubServices<R>(FutureOr<R> Function() fn) async {
251252
await storageService.verifyBucketExistenceAndAccess(bucketName);
252253
}
253254

254-
await PrimaryDatabase.tryRegisterInScope();
255+
try {
256+
await PrimaryDatabase.tryRegisterInScope();
257+
} on PgException catch (e, st) {
258+
if (envConfig.isRunningInAppengine) {
259+
// ignore setup issues for now
260+
_logger.warning('Could not connect to Postgresql database.', e, st);
261+
} else {
262+
rethrow;
263+
}
264+
}
255265
registerAccountBackend(AccountBackend(dbService));
256266
registerAdminBackend(AdminBackend(dbService));
257267
registerAnnouncementBackend(AnnouncementBackend());

0 commit comments

Comments
 (0)