diff --git a/.github/workflows/all-test.yml b/.github/workflows/all-test.yml index 053837221..66fdc9ea0 100644 --- a/.github/workflows/all-test.yml +++ b/.github/workflows/all-test.yml @@ -46,6 +46,19 @@ jobs: matrix: shard: [0, 1, 2, 3, 4, 5, 6, 7] runs-on: ubuntu-latest + services: + postgres: + image: postgres + env: + POSTGRES_PASSWORD: postgres + # Set health checks to wait until postgres has started + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 5432:5432 steps: - name: Cache PUB_CACHE uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 @@ -63,6 +76,8 @@ jobs: run: sudo apt-get update -yq && sudo apt-get install webp - run: dart test -P presubmit --shard-index ${{matrix.shard}} --total-shards 8 working-directory: app/ + env: + PUB_POSTGRES_URL: postgresql://postgres:postgres@localhost:5432/postgres?sslmode=disable define_pkg_list: runs-on: ubuntu-latest diff --git a/app/lib/shared/env_config.dart b/app/lib/shared/env_config.dart index 033073792..660f529cf 100644 --- a/app/lib/shared/env_config.dart +++ b/app/lib/shared/env_config.dart @@ -50,6 +50,9 @@ class _EnvConfig { late final fakeEmailSenderOutputDir = Platform.environment['FAKE_EMAIL_SENDER_OUTPUT_DIR']; + /// When specified, the server will connect to this URL for postgres database connections. + late final pubPostgresUrl = Platform.environment['PUB_POSTGRES_URL']; + /// True, if running inside AppEngine. bool get isRunningInAppengine => _gaeService != null && _gaeVersion != null; diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 6ed50000a..7830e4ef9 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -49,6 +49,7 @@ dependencies: pana: '0.22.23' # 3rd-party packages with pinned versions mailer: '6.5.0' + postgres: '3.5.8' ulid: '2.0.1' tar: '2.0.0' api_builder: diff --git a/app/test/shared/postgresql_ci_test.dart b/app/test/shared/postgresql_ci_test.dart new file mode 100644 index 000000000..d5df7fa44 --- /dev/null +++ b/app/test/shared/postgresql_ci_test.dart @@ -0,0 +1,36 @@ +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:clock/clock.dart'; +import 'package:postgres/postgres.dart'; +import 'package:pub_dev/shared/env_config.dart'; +import 'package:test/test.dart'; + +void main() { + group('Postgresql connection on CI', () { + test('connects to CI instance', () async { + final pubPostgresUrl = envConfig.pubPostgresUrl; + if (pubPostgresUrl == null) { + markTestSkipped('PUB_POSTGRES_URL was not specified.'); + return; + } + try { + await Connection.open( + Endpoint(host: 'localhost', database: 'postgres'), + ); + } catch (e, st) { + print(e); + print(st); + } + final conn = await Connection.openFromUrl(pubPostgresUrl); + final rs1 = await conn.execute('SELECT 1;'); + expect(rs1[0][0], 1); + + final dbName = + 'pubtemp_${clock.now().millisecondsSinceEpoch.toRadixString(36)}'; + await conn.execute('CREATE DATABASE $dbName'); + await conn.execute('DROP DATABASE $dbName'); + }); + }); +} diff --git a/pubspec.lock b/pubspec.lock index d52fad8a2..47ae927b3 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -73,6 +73,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.2" + buffer: + dependency: transitive + description: + name: buffer + sha256: "389da2ec2c16283c8787e0adaede82b1842102f8c8aae2f49003a766c5c6b3d1" + url: "https://pub.dev" + source: hosted + version: "1.2.3" build: dependency: transitive description: @@ -649,6 +657,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.5.1" + postgres: + dependency: transitive + description: + name: postgres + sha256: "96d7f40e079b96cdbfed13d71c64668cfd79c35bc17223c6b3ac0ca63bff72f0" + url: "https://pub.dev" + source: hosted + version: "3.5.8" protobuf: dependency: transitive description: