多个数据库设置GitHub操作

发布于 2025-02-10 23:59:34 字数 3173 浏览 2 评论 0原文

我已经在Rails应用程序中定义了一个辅助外部数据库,以进行仅阅读目的。因此,为了进行测试,我设置了一个本地数据库,并计划在测试示例中模拟数据。连接到数据库并在本地运行的测试很棒。但是,在运行CI测试时,由于以下错误,辅助数据库无法设置:

“ https://i.sstatic.net/fywwq.png” alt =“在此处输入图像说明”>

我相信这是ci.yml文件中的配置设置问题并且不确定如何正确配置。

# ci.yml

name: Continuous Integration
on:
  pull_request:
    branches: [ main ]
jobs:
  test:
    name: Testing
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:12
        ports:
          - "5432:5432"
        env:
          POSTGRES_USER: rails
          POSTGRES_PASSWORD: password
    env:
      RAILS_ENV: test
      RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Set up Chromedriver
        uses: nanasess/setup-chromedriver@v1
        # with:
          # Optional: do not specify to match Chrome's version
          # chromedriver-version: '88.0.4324.96'
      # Add or replace dependency steps here
      - name: Install Ruby and gems
        uses: ruby/setup-ruby@1a68550f2e3309e13c8ccb91ac6b8786f59ee147
        with:
          bundler-cache: true
      # Add or replace database setup steps here
      - name: Set up primary database
        env:
          POSTGRES_DB: calendarize_test
          DATABASE_URL: "postgres://rails:password@localhost:5432/calendarize_test"
        run: bin/rails db:create:primary db:migrate:primary
      - name: Set up warehouse database
        env:
          POSTGRES_DB: warehouse_test
          DATABASE_URL: "postgres://rails:password@localhost:5432/warehouse_test"
        run: bin/rails db:create:warehouse db:migrate:warehouse
      # Add or replace test runners here
      - name: Start Chromedriver
        run: |
          export DISPLAY=:99
          chromedriver --url-base=/wd/hub --disable-dev-shm-usage &
          sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & # optional
      - name: Run tests
        run: bundle exec rspec --color
# database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

calendarize: &calendarize
  <<: *default
  host: localhost
  username: <%= ENV["CALENDARIZE_DATABASE_USERNAME"] %>
  password: <%= ENV["CALENDARIZE_DATABASE_PASSWORD"] %>

test:
  primary:
    <<: *calendarize
    database: calendarize_test
  warehouse:
    <<: *calendarize
    database: warehouse_test
    migrations_paths: db/warehouse_migrate

development:
  primary:
    <<: *calendarize
    database: calendarize_development
  warehouse:
    <<: *calendarize
    database: warehouse_development
    migrations_paths: db/warehouse_migrate

production:
  primary:
    <<: *calendarize
    database: <%= ENV["CALENDARIZE_DATABASE_NAME"] %>
  warehouse:
    <<: *default
    url: <%= ENV["WAREHOUSE_DATABASE_URL"] %>
    database_tasks: false

I have defined a secondary external database in my Rails application for read-only purposes. Thus for testing, I setup a local database and plan to mock data within my test examples. Connecting to the database and running tests locally work great. However, when running the CI tests, the secondary database fails to setup due to the following error:

enter image description here

I believe this to be a configuration setup issue within the ci.yml file, and am not sure how to configure this properly.

# ci.yml

name: Continuous Integration
on:
  pull_request:
    branches: [ main ]
jobs:
  test:
    name: Testing
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:12
        ports:
          - "5432:5432"
        env:
          POSTGRES_USER: rails
          POSTGRES_PASSWORD: password
    env:
      RAILS_ENV: test
      RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Set up Chromedriver
        uses: nanasess/setup-chromedriver@v1
        # with:
          # Optional: do not specify to match Chrome's version
          # chromedriver-version: '88.0.4324.96'
      # Add or replace dependency steps here
      - name: Install Ruby and gems
        uses: ruby/setup-ruby@1a68550f2e3309e13c8ccb91ac6b8786f59ee147
        with:
          bundler-cache: true
      # Add or replace database setup steps here
      - name: Set up primary database
        env:
          POSTGRES_DB: calendarize_test
          DATABASE_URL: "postgres://rails:password@localhost:5432/calendarize_test"
        run: bin/rails db:create:primary db:migrate:primary
      - name: Set up warehouse database
        env:
          POSTGRES_DB: warehouse_test
          DATABASE_URL: "postgres://rails:password@localhost:5432/warehouse_test"
        run: bin/rails db:create:warehouse db:migrate:warehouse
      # Add or replace test runners here
      - name: Start Chromedriver
        run: |
          export DISPLAY=:99
          chromedriver --url-base=/wd/hub --disable-dev-shm-usage &
          sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & # optional
      - name: Run tests
        run: bundle exec rspec --color
# database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

calendarize: &calendarize
  <<: *default
  host: localhost
  username: <%= ENV["CALENDARIZE_DATABASE_USERNAME"] %>
  password: <%= ENV["CALENDARIZE_DATABASE_PASSWORD"] %>

test:
  primary:
    <<: *calendarize
    database: calendarize_test
  warehouse:
    <<: *calendarize
    database: warehouse_test
    migrations_paths: db/warehouse_migrate

development:
  primary:
    <<: *calendarize
    database: calendarize_development
  warehouse:
    <<: *calendarize
    database: warehouse_development
    migrations_paths: db/warehouse_migrate

production:
  primary:
    <<: *calendarize
    database: <%= ENV["CALENDARIZE_DATABASE_NAME"] %>
  warehouse:
    <<: *default
    url: <%= ENV["WAREHOUSE_DATABASE_URL"] %>
    database_tasks: false

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽心 2025-02-17 23:59:34

通过匹配ci.ymldatabase.yml之间的用户名/密码字段来解决问题。

# database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  host: localhost
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

test:
  primary:
    <<: *default
    database: calendarize_test
    username: postgres
    password: postgres
  warehouse:
    <<: *default
    database: warehouse_test
    username: postgres
    password: postgres
    migrations_paths: db/warehouse_migrate

development:
  primary:
    <<: *default
    database: calendarize_development
  warehouse:
    <<: *default
    database: warehouse_development
    migrations_paths: db/warehouse_migrate

production:
  primary:
    <<: *default
    database: <%= ENV["CALENDARIZE_DATABASE_NAME"] %>
    username: <%= ENV["CALENDARIZE_DATABASE_USERNAME"] %>
    password: <%= ENV["CALENDARIZE_DATABASE_PASSWORD"] %>
  warehouse:
    <<: *default
    url: <%= ENV["WAREHOUSE_DATABASE_URL"] %>
    database_tasks: false
# ci.yml

name: Continuous Integration
on:
  pull_request:
    branches: [ main ]
jobs:
  test:
    name: Testing
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres
        ports:
          - "5432:5432"
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    env:
      RAILS_ENV: test
      RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Set up Chromedriver
        uses: nanasess/setup-chromedriver@v1
        # with:
          # Optional: do not specify to match Chrome's version
          # chromedriver-version: '88.0.4324.96'
      # Add or replace dependency steps here
      - name: Install Ruby and gems
        uses: ruby/setup-ruby@1a68550f2e3309e13c8ccb91ac6b8786f59ee147
        with:
          bundler-cache: true
      # Add or replace database setup steps here
      - name: Set up primary database
        env:
          POSTGRES_DB: calendarize_test
          DATABASE_URL: "postgresql://postgres:postgres@localhost:5432/calendarize_test"
        run: bin/rails db:create:primary db:migrate:primary
      - name: Set up warehouse database
        env:
          POSTGRES_DB: warehouse_test
          DATABASE_URL: "postgresql://postgres:postgres@localhost:5432/warehouse_test"
        run: bin/rails db:create:warehouse db:migrate:warehouse
      # Add or replace test runners here
      - name: Start Chromedriver
        run: |
          export DISPLAY=:99
          chromedriver --url-base=/wd/hub --disable-dev-shm-usage &
          sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & # optional
      - name: Run tests
        env:
          PG_USER: postgres
          PG_PASSWORD: postgres
        run: bundle exec rspec --color

The problem was solved by matching the username/password fields between both ci.yml and database.yml.

# database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  host: localhost
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

test:
  primary:
    <<: *default
    database: calendarize_test
    username: postgres
    password: postgres
  warehouse:
    <<: *default
    database: warehouse_test
    username: postgres
    password: postgres
    migrations_paths: db/warehouse_migrate

development:
  primary:
    <<: *default
    database: calendarize_development
  warehouse:
    <<: *default
    database: warehouse_development
    migrations_paths: db/warehouse_migrate

production:
  primary:
    <<: *default
    database: <%= ENV["CALENDARIZE_DATABASE_NAME"] %>
    username: <%= ENV["CALENDARIZE_DATABASE_USERNAME"] %>
    password: <%= ENV["CALENDARIZE_DATABASE_PASSWORD"] %>
  warehouse:
    <<: *default
    url: <%= ENV["WAREHOUSE_DATABASE_URL"] %>
    database_tasks: false
# ci.yml

name: Continuous Integration
on:
  pull_request:
    branches: [ main ]
jobs:
  test:
    name: Testing
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres
        ports:
          - "5432:5432"
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    env:
      RAILS_ENV: test
      RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Set up Chromedriver
        uses: nanasess/setup-chromedriver@v1
        # with:
          # Optional: do not specify to match Chrome's version
          # chromedriver-version: '88.0.4324.96'
      # Add or replace dependency steps here
      - name: Install Ruby and gems
        uses: ruby/setup-ruby@1a68550f2e3309e13c8ccb91ac6b8786f59ee147
        with:
          bundler-cache: true
      # Add or replace database setup steps here
      - name: Set up primary database
        env:
          POSTGRES_DB: calendarize_test
          DATABASE_URL: "postgresql://postgres:postgres@localhost:5432/calendarize_test"
        run: bin/rails db:create:primary db:migrate:primary
      - name: Set up warehouse database
        env:
          POSTGRES_DB: warehouse_test
          DATABASE_URL: "postgresql://postgres:postgres@localhost:5432/warehouse_test"
        run: bin/rails db:create:warehouse db:migrate:warehouse
      # Add or replace test runners here
      - name: Start Chromedriver
        run: |
          export DISPLAY=:99
          chromedriver --url-base=/wd/hub --disable-dev-shm-usage &
          sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & # optional
      - name: Run tests
        env:
          PG_USER: postgres
          PG_PASSWORD: postgres
        run: bundle exec rspec --color
终难遇 2025-02-17 23:59:34

我认为当我在支持多个数据库的同时,在CircleCi进行测试时,我遇到了同样的问题。因为Circleci认为您只需要1个数据库。就我而言,我正在使用MySQL,但我认为同样的方法也会为您提供帮助。首先,我安装了MySQL客户端,以便从CircleCi上的图像中进行SQL查询。然后,我给了数据库中的用户权限。希望下面的代码更好。

version: 2.1
jobs:
  test:
    parallelism: 3
    docker:
      - image: cimg/ruby:3.0.3
      - image: cimg/redis:6.2.6
      - image: cimg/mysql:8.0
        environment:
          MYSQL_ROOT_PASSWORD: rootpw
          MYSQL_USER: myuser
          MYSQL_PASSWORD: wq1234
    steps:
      - checkout
      - run:
          name: Waiting for MySQL to be ready
          command: |
            for i in `seq 1 10`;
            do
              nc -z 127.0.0.1 3306 && echo Success && exit 0
              echo -n .
              sleep 1
            done
            echo Failed waiting for MySQL && exit 1
      - run: sudo apt-get update
      - run: sudo apt-get install qt5-default libqt5webkit5-dev gstreamer1.0-plugins-base gstreamer1.0-tools gstreamer1.0-x
      - run: bundle install
      - run: sudo apt-get install mysql-client
      - run: mysql -u root -prootpw -e "GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' WITH GRANT OPTION" --protocol=tcp
      - run: rails db:create
      - run: rails db:schema:load --trace
      - run:
          name: Test
          command: rspec

I think i encountered the same issue when i run tests in circleci while supporting multiple database. Because circleci assumes you will only need 1 database. In my case i was using mysql but i think same approach will also help you too. First i installed the mysql client in order to make sql queries from the image that we are on in circleci. Then i gave permissions to the user that i have in my database.yml and rest is just rails commands. Hope the code below demosntrate it better.

version: 2.1
jobs:
  test:
    parallelism: 3
    docker:
      - image: cimg/ruby:3.0.3
      - image: cimg/redis:6.2.6
      - image: cimg/mysql:8.0
        environment:
          MYSQL_ROOT_PASSWORD: rootpw
          MYSQL_USER: myuser
          MYSQL_PASSWORD: wq1234
    steps:
      - checkout
      - run:
          name: Waiting for MySQL to be ready
          command: |
            for i in `seq 1 10`;
            do
              nc -z 127.0.0.1 3306 && echo Success && exit 0
              echo -n .
              sleep 1
            done
            echo Failed waiting for MySQL && exit 1
      - run: sudo apt-get update
      - run: sudo apt-get install qt5-default libqt5webkit5-dev gstreamer1.0-plugins-base gstreamer1.0-tools gstreamer1.0-x
      - run: bundle install
      - run: sudo apt-get install mysql-client
      - run: mysql -u root -prootpw -e "GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' WITH GRANT OPTION" --protocol=tcp
      - run: rails db:create
      - run: rails db:schema:load --trace
      - run:
          name: Test
          command: rspec

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文