Travis-CI 上的 Rails 数据库设置

发布于 2025-01-06 05:13:09 字数 204 浏览 1 评论 0原文

我正在尝试在 Rails 项目上使用 Travis 持续集成。文档说,对于 SQLite3,测试数据库必须配置如下:

test:
  adapter: sqlite3
  database: ":memory:"
  timeout: 500

但我想保留本地测试的默认配置。是否可以同时保留我的本地设置和 Travis 要求?

I'm trying to use Travis Continuous Integration on a Rails project. The documentation says that the test db must be configured as following for SQLite3:

test:
  adapter: sqlite3
  database: ":memory:"
  timeout: 500

But I'd like to keep my default configuration for local tests. Is it possible to keep both my local settings and the Travis requirements?

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

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

发布评论

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

评论(3

π浅易 2025-01-13 05:13:09

我对这个问题的解决方案完全基于 一篇博客文章,有一些差异:

  1. config/database.travis.yml 中的 Travis CI 特定设置;
  2. cp config/database.travis.yml config/database.yml 在.travis.yml之前的脚本部分;
  3. 我在源代码树中没有 config/database.yml 。

以下是这两个文件的完整列表:

# .travis.yml
language: ruby
rvm:
  - 1.9.3
env:
  - DB=sqlite
  - DB=mysql
  - DB=postgresql
script:
  - RAILS_ENV=test bundle exec rake db:migrate --trace
  - bundle exec rake db:test:prepare
  - bundle exec rake
before_script:
  - cp config/database.travis.yml config/database.yml
  - mysql -e 'create database strano_test'
  - psql -c 'create database strano_test' -U postgres


# config/database.travis.yml
sqlite: &sqlite
  adapter: sqlite3
  database: db/<%= Rails.env %>.sqlite3

mysql: &mysql
  adapter: mysql2
  username: root
  password:
  database: strano_<%= Rails.env %>

postgresql: &postgresql
  adapter: postgresql
  username: postgres
  password:
  database: strano_<%= Rails.env %>
  min_messages: ERROR

defaults: &defaults
  pool: 5
  timeout: 5000
  host: localhost
  <<: *<%= ENV['DB'] || "postgresql" %>

development:
  <<: *defaults

test:
  <<: *defaults

production:
  <<: *defaults

My solution for this problem is fully based on a blog post with a few differences:

  1. Travis CI specific settings in config/database.travis.yml;
  2. cp config/database.travis.yml config/database.yml in before script section of .travis.yml;
  3. I don't have config/database.yml in source tree.

Here is full listing for both files:

# .travis.yml
language: ruby
rvm:
  - 1.9.3
env:
  - DB=sqlite
  - DB=mysql
  - DB=postgresql
script:
  - RAILS_ENV=test bundle exec rake db:migrate --trace
  - bundle exec rake db:test:prepare
  - bundle exec rake
before_script:
  - cp config/database.travis.yml config/database.yml
  - mysql -e 'create database strano_test'
  - psql -c 'create database strano_test' -U postgres


# config/database.travis.yml
sqlite: &sqlite
  adapter: sqlite3
  database: db/<%= Rails.env %>.sqlite3

mysql: &mysql
  adapter: mysql2
  username: root
  password:
  database: strano_<%= Rails.env %>

postgresql: &postgresql
  adapter: postgresql
  username: postgres
  password:
  database: strano_<%= Rails.env %>
  min_messages: ERROR

defaults: &defaults
  pool: 5
  timeout: 5000
  host: localhost
  <<: *<%= ENV['DB'] || "postgresql" %>

development:
  <<: *defaults

test:
  <<: *defaults

production:
  <<: *defaults
奈何桥上唱咆哮 2025-01-13 05:13:09

@mrm 的博客文章没有回答您的问题。
我遇到了同样的问题,我的本地计算机上的 postgreql 凭据与 travis 默认凭据不同。这是我想出的最简单的解决方案:

# config/database.yml
test:
  adapter: postgresql
  database: medscraper_test
  username: <%= ENV['TRAVIS'] ? 'postgres' : 'MY_TEST_USERNAME' %>
  password: <%= ENV['TRAVIS'] ? '' : 'MY_TEST_PASSWORD' %>

请注意,Travis CI 自动设置 TRAVIS 环境变量。
你的解决方案是:

# config/database.yml
test:
  adapter: sqlite3
  database: <%= ENV['TRAVIS'] ? '":memory:"' : 'db/test.sqlite3' %>
  timeout: 500

@mrm's blog post doesn't say anything about answering your question.
I faced the same problem where my postgreql credentials are different on my local machine than travis default. This is the simplest solution I came up with:

# config/database.yml
test:
  adapter: postgresql
  database: medscraper_test
  username: <%= ENV['TRAVIS'] ? 'postgres' : 'MY_TEST_USERNAME' %>
  password: <%= ENV['TRAVIS'] ? '' : 'MY_TEST_PASSWORD' %>

Note that Travis CI automatically sets TRAVIS environment variable.
Your solution would be:

# config/database.yml
test:
  adapter: sqlite3
  database: <%= ENV['TRAVIS'] ? '":memory:"' : 'db/test.sqlite3' %>
  timeout: 500
忆梦 2025-01-13 05:13:09

我刚刚写了 一篇博客文章描述了如何执行此操作。

I just wrote a blog post describing how to do this.

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