42association / 42ActivityAPI

1 stars 1 forks source link

Add a new endpoint to add shift data #62

Closed ShotaTanemura closed 3 months ago

ShotaTanemura commented 3 months ago

Yes, I can help you set up seeding for a Go server using GitHub Actions. The goal is to ensure that when your server runs in the CI/CD pipeline, it seeds the necessary data into your database. Here’s a step-by-step guide:

Step 1: Write Your Seed Script

First, create a Go script to seed your database. For example, you can create a file named seed.go:

package main

import (
    "database/sql"
    "log"

    _ "github.com/lib/pq" // Import the PostgreSQL driver
)

func main() {
    // Database connection string
    connStr := "user=username dbname=mydb sslmode=disable password=password host=localhost"
    db, err := sql.Open("postgres", connStr)
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    // Example seed data
    _, err = db.Exec("INSERT INTO users (name, email) VALUES ($1, $2)", "John Doe", "john@example.com")
    if err != nil {
        log.Fatal(err)
    }

    log.Println("Seeding completed successfully!")
}

Make sure to replace the connection string and the SQL statements with those relevant to your database and schema.

Step 2: Add Your Seed Script to the Workflow

Next, you need to create a GitHub Actions workflow to run your seed script. Here’s an example .github/workflows/ci.yml file:

name: CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:

    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:latest
        ports:
          - 5432:5432
        env:
          POSTGRES_USER: username
          POSTGRES_DB: mydb
          POSTGRES_PASSWORD: password
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up Go
      uses: actions/setup-go@v3
      with:
        go-version: '1.18'

    - name: Build
      run: go build -v ./...

    - name: Wait for Postgres
      run: |
        until pg_isready -h localhost -p 5432; do
          echo "Waiting for postgres to be ready..."
          sleep 1
        done

    - name: Run Seed Script
      run: go run seed.go
      env:
        DATABASE_URL: postgres://username:password@localhost:5432/mydb?sslmode=disable

    - name: Run Tests
      run: go test -v ./...
      env:
        DATABASE_URL: postgres://username:password@localhost:5432/mydb?sslmode=disable

Explanation

  1. Checkout code: This step checks out your code from the repository.
  2. Set up Go: This step sets up the Go environment.
  3. Build: This step builds your Go application.
  4. Wait for Postgres: This step ensures that PostgreSQL is up and running before running your seed script.
  5. Run Seed Script: This step runs the seed.go script to seed your database.
  6. Run Tests: This step runs your Go tests, ensuring that they use the seeded data.

Step 3: Configure Your Seed Script and Tests

Ensure that your seed script and tests are configured to use the DATABASE_URL environment variable. This makes it easier to manage the connection string across different environments (local, CI, production, etc.).

Step 4: Commit and Push

Commit your changes and push them to your repository:

git add .
git commit -m "Add database seeding to GitHub Actions workflow"
git push origin main

This setup ensures that your database is seeded with the necessary data before running tests in the CI/CD pipeline. Modify the seeding logic and the SQL statements in the seed.go script as per your actual requirements.