Procházet zdrojové kódy

feat: use nix for psgl (#1378)

tsk před 1 měsícem
rodič
revize
491165b6e7

+ 3 - 5
.github/workflows/ci.yml

@@ -220,7 +220,7 @@ jobs:
       fail-fast: true
       matrix:
         build-args: [-p cdk-integration-tests]
-        database: [SQLITE]
+        database: [SQLITE, POSTGRES]
     steps:
       - name: checkout
         uses: actions/checkout@v4
@@ -291,10 +291,8 @@ jobs:
           shared-key: "stable-${{ steps.flake-hash.outputs.hash }}"
       - name: Test fake mint
         run: nix develop -i -L .#stable --command just test-pure ${{ matrix.database }}
-      - name: Install Postgres
-        run: bash -x crates/cdk-postgres/start_db_for_test.sh
-      - name: Test mint
-        run: nix develop -i -L .#stable --command just test
+      - name: Test mint with PostgreSQL
+        run: nix develop -i -L .#stable --command bash -c "start-postgres && just test"
 
   payment-processor-itests:
     name: "Payment processor tests"

+ 1 - 0
.gitignore

@@ -11,6 +11,7 @@ result
 Cargo.lock
 .aider*
 **/postgres_data/
+.pg_data/
 **/.env
 
 # Mutation testing artifacts

+ 17 - 2
crates/cdk-integration-tests/src/shared.rs

@@ -178,6 +178,21 @@ pub fn create_fake_wallet_settings(
     signatory_config: Option<(String, String)>, // (url, certs_dir)
     fake_wallet_config: Option<cdk_mintd::config::FakeWallet>,
 ) -> cdk_mintd::config::Settings {
+    let engine = DatabaseEngine::from_str(database).expect("valid database");
+
+    // If using PostgreSQL, get the connection URL from the environment or use default
+    let postgres_config = if engine == DatabaseEngine::Postgres {
+        let url = std::env::var("CDK_MINTD_DATABASE_URL").unwrap_or_else(|_| {
+            "postgresql://cdk_user:cdk_password@localhost:5432/cdk_mint".to_string()
+        });
+        Some(cdk_mintd::config::PostgresConfig {
+            url,
+            ..Default::default()
+        })
+    } else {
+        None
+    };
+
     cdk_mintd::config::Settings {
         info: cdk_mintd::config::Info {
             url: format!("http://127.0.0.1:{port}"),
@@ -216,8 +231,8 @@ pub fn create_fake_wallet_settings(
         fake_wallet: fake_wallet_config,
         grpc_processor: None,
         database: Database {
-            engine: DatabaseEngine::from_str(database).expect("valid database"),
-            postgres: None,
+            engine,
+            postgres: postgres_config,
         },
         auth_database: None,
         mint_management_rpc: None,

+ 0 - 37
crates/cdk-postgres/start_db_for_test.sh

@@ -1,37 +0,0 @@
-#!/usr/bin/env bash
-set -euo pipefail
-
-CONTAINER_NAME="rust-test-pg"
-DB_USER="test"
-DB_PASS="test"
-DB_NAME="testdb"
-DB_PORT="5433"
-
-echo "Starting fresh PostgreSQL container..."
-docker run -d --rm \
-  --name "${CONTAINER_NAME}" \
-  -e POSTGRES_USER="${DB_USER}" \
-  -e POSTGRES_PASSWORD="${DB_PASS}" \
-  -e POSTGRES_DB="${DB_NAME}" \
-  -p ${DB_PORT}:5432 \
-  postgres:16
-
-echo "Waiting for PostgreSQL to be ready and database '${DB_NAME}' to exist..."
-until docker exec -e PGPASSWORD="${DB_PASS}" "${CONTAINER_NAME}" \
-    psql -U "${DB_USER}" -d "${DB_NAME}" -c "SELECT 1;" >/dev/null 2>&1; do
-  sleep 0.5
-done
-
-docker exec -e PGPASSWORD="${DB_PASS}" "${CONTAINER_NAME}" \
-  psql -U "${DB_USER}" -d "${DB_NAME}" -c "CREATE DATABASE mintdb;"
-docker exec -e PGPASSWORD="${DB_PASS}" "${CONTAINER_NAME}" \
-  psql -U "${DB_USER}" -d "${DB_NAME}" -c "CREATE DATABASE mintdb_auth;"
-
-# Export environment variables for both main and auth databases
-export DATABASE_URL="host=localhost user=${DB_USER} password=${DB_PASS} dbname=${DB_NAME} port=${DB_PORT}"
-export CDK_MINTD_POSTGRES_URL="postgresql://${DB_USER}:${DB_PASS}@localhost:${DB_PORT}/mintdb"
-export CDK_MINTD_AUTH_POSTGRES_URL="postgresql://${DB_USER}:${DB_PASS}@localhost:${DB_PORT}/mintdb_auth"
-
-echo "Database URLs configured:"
-echo "Main database: ${CDK_MINTD_POSTGRES_URL}"
-echo "Auth database: ${CDK_MINTD_AUTH_POSTGRES_URL}"

+ 95 - 1
flake.nix

@@ -119,6 +119,82 @@
           ]
           ++ libsDarwin;
 
+        # PostgreSQL configuration
+        postgresConf = {
+          pgUser = "cdk_user";
+          pgPassword = "cdk_password";
+          pgDatabase = "cdk_mint";
+          pgPort = "5432";
+        };
+
+        # Script to start PostgreSQL
+        startPostgres = pkgs.writeShellScriptBin "start-postgres" ''
+          set -e
+          PGDATA="$PWD/.pg_data"
+          PGPORT="${postgresConf.pgPort}"
+          PGUSER="${postgresConf.pgUser}"
+          PGPASSWORD="${postgresConf.pgPassword}"
+          PGDATABASE="${postgresConf.pgDatabase}"
+
+          # Stop any existing instance first
+          if [ -d "$PGDATA" ] && ${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" status > /dev/null 2>&1; then
+            echo "Stopping existing PostgreSQL instance..."
+            ${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" stop > /dev/null 2>&1
+          fi
+
+          if [ ! -d "$PGDATA" ]; then
+            echo "Initializing PostgreSQL database..."
+            ${pkgs.postgresql_16}/bin/initdb -D "$PGDATA" --auth=trust --no-locale --encoding=UTF8
+
+            # Configure PostgreSQL
+            echo "listen_addresses = 'localhost'" >> "$PGDATA/postgresql.conf"
+            echo "port = $PGPORT" >> "$PGDATA/postgresql.conf"
+            echo "unix_socket_directories = '$PGDATA'" >> "$PGDATA/postgresql.conf"
+
+            # Start temporarily to create user and database
+            ${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" -l "$PGDATA/logfile" start
+            sleep 2
+
+            # Create user and database
+            ${pkgs.postgresql_16}/bin/createuser -h localhost -p $PGPORT -s "$PGUSER" || true
+            ${pkgs.postgresql_16}/bin/psql -h localhost -p $PGPORT -c "ALTER USER $PGUSER WITH PASSWORD '$PGPASSWORD';" postgres
+            ${pkgs.postgresql_16}/bin/createdb -h localhost -p $PGPORT -O "$PGUSER" "$PGDATABASE" || true
+
+            ${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" stop
+            echo "PostgreSQL initialized."
+          fi
+
+          echo "Starting PostgreSQL on port $PGPORT..."
+          ${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" -l "$PGDATA/logfile" start
+          echo "PostgreSQL started. Connection URL: postgresql://$PGUSER:$PGPASSWORD@localhost:$PGPORT/$PGDATABASE"
+        '';
+
+        # Script to stop PostgreSQL
+        stopPostgres = pkgs.writeShellScriptBin "stop-postgres" ''
+          PGDATA="$PWD/.pg_data"
+          if [ -d "$PGDATA" ]; then
+            echo "Stopping PostgreSQL..."
+            ${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" stop || echo "PostgreSQL was not running."
+          else
+            echo "No PostgreSQL data directory found."
+          fi
+        '';
+
+        # Script to check PostgreSQL status
+        pgStatus = pkgs.writeShellScriptBin "pg-status" ''
+          PGDATA="$PWD/.pg_data"
+          if [ -d "$PGDATA" ]; then
+            ${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" status
+          else
+            echo "No PostgreSQL data directory found. Run 'start-postgres' first."
+          fi
+        '';
+
+        # Script to connect to PostgreSQL
+        pgConnect = pkgs.writeShellScriptBin "pg-connect" ''
+          ${pkgs.postgresql_16}/bin/psql "postgresql://${postgresConf.pgUser}:${postgresConf.pgPassword}@localhost:${postgresConf.pgPort}/${postgresConf.pgDatabase}"
+        '';
+
         # Common arguments can be set here to avoid repeating them later
         nativeBuildInputs = [
           #Add additional build inputs here
@@ -194,8 +270,26 @@
                       pkgs.zlib
                     ]
                   }:$LD_LIBRARY_PATH
+
+                  # PostgreSQL environment variables
+                  export CDK_MINTD_DATABASE_URL="postgresql://${postgresConf.pgUser}:${postgresConf.pgPassword}@localhost:${postgresConf.pgPort}/${postgresConf.pgDatabase}"
+
+                  echo ""
+                  echo "PostgreSQL commands available:"
+                  echo "  start-postgres  - Initialize and start PostgreSQL"
+                  echo "  stop-postgres   - Stop PostgreSQL (run before exiting)"
+                  echo "  pg-status       - Check PostgreSQL status"
+                  echo "  pg-connect      - Connect to PostgreSQL with psql"
+                  echo ""
                 '';
-                buildInputs = buildInputs ++ [ stable_toolchain ];
+                buildInputs = buildInputs ++ [
+                  stable_toolchain
+                  pkgs.postgresql_16
+                  startPostgres
+                  stopPostgres
+                  pgStatus
+                  pgConnect
+                ];
                 inherit nativeBuildInputs;
 
               }

+ 6 - 23
misc/fake_itests.sh

@@ -28,8 +28,9 @@ cleanup() {
         echo "Temp directory removed: $CDK_ITESTS_DIR"
     fi
 
-    if [ -n "$CONTAINER_NAME" ]; then
-        docker rm "${CONTAINER_NAME}" -f
+    # Stop PostgreSQL if it was started
+    if [ -d "$PWD/.pg_data" ]; then
+        stop-postgres 2>/dev/null || true
     fi
 
     # Unset all environment variables
@@ -62,27 +63,9 @@ cargo build -p cdk-integration-tests
 echo "Starting fake mint using Rust binary..."
 
 if [ "${CDK_MINTD_DATABASE}" = "POSTGRES" ]; then
-    export CONTAINER_NAME="rust-fake-test-pg"
-    DB_USER="test"
-    DB_PASS="test"
-    DB_NAME="testdb"
-    DB_PORT="15433"
-
-    docker run -d --rm \
-      --name "${CONTAINER_NAME}" \
-      -e POSTGRES_USER="${DB_USER}" \
-      -e POSTGRES_PASSWORD="${DB_PASS}" \
-      -e POSTGRES_DB="${DB_NAME}" \
-      -p ${DB_PORT}:5432 \
-      postgres:16
-    export CDK_MINTD_DATABASE_URL="postgresql://${DB_USER}:${DB_PASS}@localhost:${DB_PORT}/${DB_NAME}"
-
-    echo "Waiting for PostgreSQL to be ready and database '${DB_NAME}' to exist..."
-    until docker exec -e PGPASSWORD="${DB_PASS}" "${CONTAINER_NAME}" \
-        psql -U "${DB_USER}" -d "${DB_NAME}" -c "SELECT 1;" >/dev/null 2>&1; do
-      sleep 0.5
-    done
-    echo "PostgreSQL container is ready"
+    echo "Starting PostgreSQL via nix..."
+    start-postgres
+    echo "PostgreSQL is ready"
 fi
 
 if [ "$2" = "external_signatory" ]; then

+ 13 - 1
misc/itests.sh

@@ -27,7 +27,12 @@ cleanup() {
     #     rm -rf "$CDK_ITESTS_DIR"
     #     echo "Temp directory removed: $CDK_ITESTS_DIR"
     # fi
-    
+
+    # Stop PostgreSQL if it was started
+    if [ -d "$PWD/.pg_data" ]; then
+        stop-postgres 2>/dev/null || true
+    fi
+
     # Unset all environment variables
     unset CDK_ITESTS_DIR
     unset CDK_ITESTS_MINT_ADDR
@@ -62,6 +67,13 @@ fi
 echo "Temp directory created: $CDK_ITESTS_DIR"
 export CDK_MINTD_DATABASE="$1"
 
+# Start PostgreSQL if needed
+if [ "${CDK_MINTD_DATABASE}" = "POSTGRES" ]; then
+    echo "Starting PostgreSQL via nix..."
+    start-postgres
+    echo "PostgreSQL is ready"
+fi
+
 cargo build --bin start_regtest_mints 
 
 echo "Starting regtest and mints"