diff --git a/app/lib/database/database.dart b/app/lib/database/database.dart index c947d4f6f9..ff045235b3 100644 --- a/app/lib/database/database.dart +++ b/app/lib/database/database.dart @@ -11,6 +11,7 @@ import 'package:gcloud/service_scope.dart' as ss; import 'package:meta/meta.dart'; import 'package:postgres/postgres.dart'; import 'package:pub_dev/service/secret/backend.dart'; +import 'package:pub_dev/shared/configuration.dart'; import 'package:pub_dev/shared/env_config.dart'; final _random = Random.secure(); @@ -33,11 +34,15 @@ class PrimaryDatabase { /// the secret backend, connects to it and registers the primary database /// service in the current scope. static Future tryRegisterInScope() async { + if (activeConfiguration.isProduction) { + // Production is not configured for postgresql yet. + return; + } var connectionString = envConfig.pubPostgresUrl ?? (await secretBackend.lookup(SecretKey.postgresConnectionString)); - if (connectionString == null && envConfig.isRunningInAppengine) { - // ignore for now, must throw once we have the environment setup ready + if (connectionString == null && activeConfiguration.isStaging) { + // Staging may not have the connection string set yet. return; } // The scope-specific custom database. We are creating a custom database for diff --git a/app/lib/frontend/handlers/pubapi.dart b/app/lib/frontend/handlers/pubapi.dart index 20e05481b2..c9ecd00402 100644 --- a/app/lib/frontend/handlers/pubapi.dart +++ b/app/lib/frontend/handlers/pubapi.dart @@ -9,6 +9,7 @@ import 'package:_pub_shared/data/package_api.dart'; import 'package:_pub_shared/data/publisher_api.dart'; import 'package:_pub_shared/data/task_api.dart'; import 'package:api_builder/api_builder.dart'; +import 'package:pub_dev/database/database.dart'; import 'package:pub_dev/frontend/handlers/atom_feed.dart'; import 'package:shelf/shelf.dart'; import 'package:shelf_router/shelf_router.dart'; @@ -457,6 +458,7 @@ class PubApi { Future debug(Request request) async => debugResponse({ 'package': packageDebugStats(), 'search': searchDebugStats(), + 'database': primaryDatabase != null, }); @EndPoint.get('/packages.json') diff --git a/app/lib/service/services.dart b/app/lib/service/services.dart index af9863a9ae..99ecefc7ca 100644 --- a/app/lib/service/services.dart +++ b/app/lib/service/services.dart @@ -14,6 +14,7 @@ import 'package:gcloud/service_scope.dart'; import 'package:gcloud/storage.dart'; import 'package:googleapis_auth/auth_io.dart' as auth; import 'package:logging/logging.dart'; +import 'package:postgres/postgres.dart'; import 'package:pub_dev/database/database.dart'; import 'package:pub_dev/package/api_export/api_exporter.dart'; import 'package:pub_dev/search/handlers.dart'; @@ -251,7 +252,16 @@ Future _withPubServices(FutureOr Function() fn) async { await storageService.verifyBucketExistenceAndAccess(bucketName); } - await PrimaryDatabase.tryRegisterInScope(); + try { + await PrimaryDatabase.tryRegisterInScope(); + } on PgException catch (e, st) { + if (envConfig.isRunningInAppengine) { + // ignore setup issues for now + _logger.warning('Could not connect to Postgresql database.', e, st); + } else { + rethrow; + } + } registerAccountBackend(AccountBackend(dbService)); registerAdminBackend(AdminBackend(dbService)); registerAnnouncementBackend(AnnouncementBackend());