Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/lib/database/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -33,11 +34,15 @@ class PrimaryDatabase {
/// the secret backend, connects to it and registers the primary database
/// service in the current scope.
static Future<void> 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
Expand Down
2 changes: 2 additions & 0 deletions app/lib/frontend/handlers/pubapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -457,6 +458,7 @@ class PubApi {
Future<Response> debug(Request request) async => debugResponse({
'package': packageDebugStats(),
'search': searchDebugStats(),
'database': primaryDatabase != null,
});

@EndPoint.get('/packages.json')
Expand Down
12 changes: 11 additions & 1 deletion app/lib/service/services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -251,7 +252,16 @@ Future<R> _withPubServices<R>(FutureOr<R> 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());
Expand Down