circleCI 的自动测试输出一个错误,指出该表不存在



我使用ruby on rails。我使用circleCI来运行自动化测试,当我实现了一个我以前没有实现的测试代码,并试图用circleCI运行它时,我得到了以下错误:表不存在。ActiveRecord::StatementInvalid: Mysql2::Error: Table 'saving_test-1.patienecs' doesn't exist.

配置文件按如下方式设置,但是我如何修改它以使自动化测试成功?

的形式

default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: password
host: 127.0.0.1
development:
<<: *default
database: saving_development
test:
database: saving_test
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
host: 127.0.0.1
production:
<<: *default
database: <%= ENV['DB_DATABASE'] %>
adapter: mysql2
encoding: utf8mb4
charset: utf8mb4
collation: utf8mb4_general_ci
host: <%= ENV['DB_HOST'] %>
username: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>

.circleci/config.yml

version: 2.1
orbs:
ruby: circleci/ruby@0.1.2
jobs:
build:
docker:
- image: circleci/ruby:3.0.0
environment:
RAILS_ENV: test
working_directory: ~/saving
steps:
- checkout
- restore_cache:
name: restore result bundle install
keys:
- v1-dependencies-{{ checksum "Gemfile.lock" }}
- v1-dependencies-
- run:
name: bundle install
command: bundle install --path=vendor/bundle --jobs 4 --retry 3
- run:
name: rubocop
command: bundle exec rubocop
- save_cache:
name: save cash result bundle install
paths:
- ./vendor/bundle
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
test:
docker:
- image: circleci/ruby:3.0.0
environment:
- BUNDLER_VERSION: 2.1.4
- RAILS_ENV: 'test'
- DB_HOST: 127.0.0.1
- image: circleci/mysql:5.7
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
- MYSQL_DB: saving_test
working_directory: ~/saving
steps:
- checkout
- run:
name: wait for database
command: dockerize -wait tcp://127.0.0.1:3306 -timeout 5m
- restore_cache:
name: restore cash result bundle install
keys:
- v1-dependencies-{{ checksum "Gemfile.lock" }}
- v1-dependencies-
- run:
name: Install dependencies
command: |
gem install bundler -v 2.1.4
bundle install --path=vendor/bundle --jobs 4 --retry 3
- run:
name: Database Setup
command: |
bundle exec rake db:create RAILS_ENV=test
bundle exec rake db:schema:load RAILS_ENV=test
bundle exec rake db:migrate RAILS_ENV=test
- run:
name: Run test
command: bundle exec rails test

deploy:
machine:
enabled: true
steps:
- checkout
- add_ssh_keys:
fingerprints:
- 34:98:98:c5:83:2a:c0:b1:94:a0:c2:56:2d:4c:0d:11
- 08:a1:85:27:e7:83:c5:ae:68:8c:d2:3b:05:dc:82:70
- run:
name: Reflect Code
command: ssh ${USER_NAME}@${HOST_NAME} 'cd /saving && sudo git pull origin main'
- run:
name: Docker task
command: ssh ${USER_NAME}@${HOST_NAME} 'cd /saving && docker-compose down && docker-compose build --no-cache && docker-compose up -d'
- run:
name: database setup
command: ssh ${USER_NAME}@${HOST_NAME} 'cd /saving && docker-compose exec -T app rails db:migrate RAILS_ENV=production'
workflows:
version: 2
build_test_and_deploy:
jobs:
- build
- test:
requires:
- build
- deploy:
requires:
- test
filters:
branches:
only: main

耐心
class Patience < ApplicationRecord
belongs_to :user
validates :user_id, presence: true
validates :memo,length: { maximum: 140 }
end

的补充数据库。Yml和circleci的配置。我没有指定任何像saving_test-1这样的表。有-1的病人,所以我甚至不知道为什么这个1出现。

您发布的ActiveRecord::StatementInvalid: Mysql2::Error: Table 'saving_test-1.patienecs' doesn't exist.错误可能是由于您的类名中的错字(看起来您已经修复了)。

Activerecord期望有一个名为activerecord_subclass.name.underscore.pluralize的数据库表。因此,Patience类需要一个名为patiences的表。saving_test-1是circle-ci创建的数据库的模式名。

假设你有一个包含patiences表的迁移/最新的模式文件,你所需要做的就是把代码推到你这里的圆圈-ci (class Patience而不是class Patienec),你应该不会看到这个错误。

最新更新