多个数据库设置GitHub操作
我已经在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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(2)
通过匹配
ci.yml
和database.yml
之间的用户名/密码字段来解决问题。The problem was solved by matching the username/password fields between both
ci.yml
anddatabase.yml
.我认为当我在支持多个数据库的同时,在CircleCi进行测试时,我遇到了同样的问题。因为Circleci认为您只需要1个数据库。就我而言,我正在使用MySQL,但我认为同样的方法也会为您提供帮助。首先,我安装了MySQL客户端,以便从CircleCi上的图像中进行SQL查询。然后,我给了数据库中的用户权限。希望下面的代码更好。
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.