ActiveRecord::ConnectionAdapters::SQLite3Adapter does not support skipping duplicates - ruby-on-rails

I'm having this problem when I'm trying to run rspec on Github actions. This is my github action workflow.
name: Ruby
on:
push:
branches: [ master]
pull_request:
branches: [ master]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout#v2
- name: Setup System
run: |
sudo apt-get install sqlite3 libsqlite3-dev
- name: Set up Ruby 2.7.2
uses: actions/setup-ruby#v1
with:
ruby-version: 2.7.2
- name: Cache Ruby Gems
uses: actions/cache#v2
with:
path: vendor/bundle
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
restore-keys: |
${{ runner.os }}-gems-
- name: Bundle Install
run: |
bundle config path vendor/bundle
bundle install --jobs 4 --retry 3 --without=production
- name: Setup DB
env:
RAILS_ENV: test
DISABLE_SPRING: 1
run: |
bin/rails db:setup db:migrate db:seed
- name: Run rspec
env:
RAILS_ENV: test
DISABLE_SPRING: 1
run: bundle exec rspec
My code is calling insert_all.
I was able to track the error down to https://github.com/rails/rails/blob/main/activerecord/lib/active_record/insert_all.rb#L97
From what I can see, it has nothing to do with the data being insert but the connection itself
For some reason the (sqlite3) connection doesn't support skipping duplicates
I do not get this error on my local macbook air

tl;dr - I was using an old version of SQLite3
Okay, tracing the rail's code, I find that for the sqlite3 adapter it calls
https://github.com/rails/rails/blob/da418dc2508eb8af94ba88b70034021cd17b1abd/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L152
which calls
https://github.com/rails/rails/blob/da418dc2508eb8af94ba88b70034021cd17b1abd/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L149
which specifies that sqlite3 must be version 3.24.0 or great.
At the same time, Github's ubuntu-latest currently (02-2021) uses Ubuntu 18 as "latest".
And the sqlite3 package on Ubuntu installs sqlite 3.22 by default. source: https://packages.ubuntu.com/search?keywords=sqlite3
So after changing to runs-on: ubuntu-20.04 I no longer get this problem!

Related

Github-actions: Temporary failure in name resolution (PG::ConnectionBad)

I'm trying to configure a ruby.yml with github-actions but I'm getting this error at Setup database
PG::ConnectionBad: could not translate host name "db" to address: Temporary failure in name resolution
my yml file looks like this
name: CI
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: postgres
ports: ['5432:5432']
steps:
- uses: actions/checkout#v2
- name: Setup Ruby
uses: ruby/setup-ruby#v1
with:
bundler-cache: true
- name: Install Dependencies
run: |
sudo apt install -yqq libpq-dev
gem install bundler
- name: Install Gems
run: |
bundle install
- name: Setup database
env:
PG_DATABASE: postgres
PG_HOST: localhost
PG_USER: postgres
PG_PASSWORD: password
RAILS_ENV: test
WITH_COVERAGE: true
DISABLE_SPRING: 1
run: |
bundle exec rails db:setup
- name: Run Tests
env:
PGHOST: localhost
PGUSER: postgres
PGPASSWORD: postgres
RAILS_ENV: test
run: |
bundle exec rake test
I tried following this tutorial https://dev.to/buildwithallan/how-to-set-up-a-ci-workflow-using-github-actions-4818 but nothing it still doesn’t work. I'm not sure how I can fix this, would appreciate some help
double check this segment:
env:
PG_DATABASE: postgres
PG_HOST: localhost
PG_USER: postgres
PG_PASSWORD: password
RAILS_ENV: test
WITH_COVERAGE: true
DISABLE_SPRING: 1
check db in your config file. seems github action complaints it can not connect to db

Unable to give correct URI for postgresql database in yml file for GitHub Actions

I am implementing GitHub actions for the first time and I am unable to pass the test successfully for my Ruby On Rails project. The reason is that I am unable to provide the correct URI for the PostgreSQL server.
When I push the code to the GitHub via git push, a test begins successfully as expected, but it fails to pass with this error:
URI::InvalidURIError: bad URI(is not URI?): postgres:***:***#localhost:5432/github_actions_test
I have a username and password set in my GitHub secrets.
I have provided a constant to hold the value of the URI in my yml file as:
DATABASE_URL = "postgres://username:password#localhost:5432/github_actions_test"
Now replacing username & password with mine, since my DB credentials are already available in secrets:
DATABASE_URL = "postgres://${{secrets.DATABASE_USERNAME}}:${{secrets.DATABASE_PASSWORD}}#localhost:5432/github_actions_test"
this doesn't work and it gave same error as mentioned above. Then I tried as this:
DATABASE_URL = "postgres://<%=secrets.DATABASE_USERNAME%>:<%=secrets.DATABASE_PASSWORD%>#localhost:5432/github_actions_test"
this also resulted in the same error.
Then to test whether the DATABASE_URL really works, I explicitly gave DB credentials in the URI. I know it's not recommended but I had to, to test things out and that also resulted in the same error.
I also tried to save the DB URL in my secrets and then call in my yml file as I did with my DB credentials but that didn't work too.
So my question is how do I make the PostgreSQL URI work in GitHub actions? Is there some other way to set it up or may be my string interpolation is wrong? Or may be I am doing something else wrong?
Here's the code inside of my yml file:
# This workflow uses actions that are not certified by GitHub. They are
# provided by a third-party and are governed by separate terms of service,
# privacy policy, and support documentation.
#
# This workflow will install a prebuilt Ruby version, install dependencies, and
# run tests and linters.
name: "Ruby on Rails CI"
on:
push:
branches: ["rspec"]
pull_request:
branches: ["rspec"]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:11-alpine
ports:
- "5432:5432"
env:
POSTGRES_DB: github_actions_test
POSTGRES_USER: ${{ secrets.DATABASE_USERNAME }}
POSTGRES_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
env:
RAILS_ENV: test
DATABASE_URL: "postgres://username:password#localhost:5432/github_actions_test"
steps:
- name: Checkout code
uses: actions/checkout#v3
# Add or replace dependency steps here
- name: Install Ruby and gems
uses: ruby/setup-ruby#0a29871fe2b0200a17a4497bae54fe5df0d973aa # v1.115.3
with:
bundler-cache: true
ruby-version: 3.1.2
# Add or replace database setup steps here
- name: Setup Ruby
run: sudo apt-get -yqq install libpq-dev
- name: Setup bundler and gems
run: |
gem install bundler
bundle install --jobs 4 --retry 3
- name: Set up database schema
run: bin/rails db:prepare
# Add or replace test runners here
- name: Run tests
run: bundle/exec rspec
# lint:
# runs-on: ubuntu-latest
# steps:
# - name: Checkout code
# uses: actions/checkout#v3
# - name: Install Ruby and gems
# uses: ruby/setup-ruby#0a29871fe2b0200a17a4497bae54fe5df0d973aa # v1.115.3
# with:
# bundler-cache: true
# # Add or replace any other lints here
# - name: Security audit dependencies
# run: bin/bundler-audit --update
# - name: Security audit application code
# run: bin/brakeman -q -w2
# - name: Lint Ruby files
# run: bin/rubocop --parallel

GitHub Actions: rubocop errors out, but the code is not from my repo

When running the workflow in GitHub actions, rubocop errors out, but the code that it complains about is not present in my repo. How can I fix this?
Error:
Run bin/rubocop --parallel
vendor/bundle/ruby/2.7.0/gems/activerecord-import-1.1.0/.rubocop.yml: Lint/EndAlignment has the wrong namespace - should be Layout
vendor/bundle/ruby/2.7.0/gems/activerecord-import-1.1.0/.rubocop.yml: Metrics/LineLength has the wrong namespace - should be Layout
vendor/bundle/ruby/2.7.0/gems/activerecord-import-1.1.0/.rubocop.yml: Style/ElseAlignment has the wrong namespace - should be Layout
vendor/bundle/ruby/2.7.0/gems/activerecord-import-1.1.0/.rubocop.yml: Style/SpaceInsideParens has the wrong namespace - should be Layout
Error: The `Lint/HandleExceptions` cop has been renamed to `Lint/SuppressedException`.
(obsolete configuration found in vendor/bundle/ruby/2.7.0/gems/activerecord-import-1.1.0/.rubocop_todo.yml, please update it)
Error: Process completed with exit code 2.
GitHub Actions workflow yml file:
name: Verify
on: [push]
jobs:
linters:
name: Linters
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout#v2
- name: Setup Ruby and install gems
uses: ruby/setup-ruby#v1
with:
bundler-cache: true
- name: Setup Node
uses: actions/setup-node#v1
with:
node-version: 10.13.0
- name: Find yarn cache location
id: yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: JS package cache
uses: actions/cache#v1
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install packages
run: |
yarn install --pure-lockfile
sudo apt-get -yqq install libpq-dev
gem install bundler
bundle install --jobs 4 --retry 3
- name: Run linters
run: |
bin/rubocop --parallel
tests:
name: Tests
runs-on: ubuntu-latest
services:
postgres:
image: postgres:11
env:
POSTGRES_USER: myapp
POSTGRES_DB: myapp_test
POSTGRES_PASSWORD: ""
ports: ["5432:5432"]
steps:
- name: Checkout code
uses: actions/checkout#v2
- name: Setup Ruby and install gems
uses: ruby/setup-ruby#v1
with:
bundler-cache: true
- name: Setup Node
uses: actions/setup-node#v1
with:
node-version: 10.13.0
- name: Find yarn cache location
id: yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: JS package cache
uses: actions/cache#v1
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install packages
run: |
gem install bundler
bundle install --jobs 4 --retry 3
- name: Setup test database
env:
RAILS_ENV: test
PGHOST: localhost
PGUSER: myapp
run: |
bundle exec rails db:create
bundle exec rails db:migrate
- name: Run tests
run: |
bundle exec rails test
As you already noticed there is not really a benefit in running RuboCop against third-party code and external gems because they are not really under your control and you certainly do not want to "fix" them.
Therefore I suggest excluding folders with external code, for example, gems in the vendor/bundle folder. This can be done by adding the following lines to your project's .rubycop.yml configuration file:
AllCops:
Exclude:
- 'vendor/bundle/**/*'
See the RuboCop docs about excluding files and folders.

Run deployment workflow if tests workflow is passed

I have the following two workflows:
Workflow to run test suite
Workflow to deploy the code, using https://github.com/miloserdow/capistrano-deploy
Now when I push my code, both workflows started. I want deployment Work to only start once Test Suite gets passed.
How can I do this?
Workflow that runs tests:
name: CI
on:
push:
branches: [setup_github]
jobs:
test:
runs-on: ubuntu-18.04
services:
postgres:
image: postgres:10
steps:
- name: Checkout
uses: actions/checkout#v1
- name: Set up Ruby
uses: ruby/setup-ruby#v1
with:
ruby-version: 2.5.3
- uses: borales/actions-yarn#v2.0.2
with:
cmd: install
- name: Install Dependencies
run: |
sudo apt-get -yqq install libpq-dev
- name: Install Gems
run: |
gem install bundler
- name: prepare Database
- name: RSpec
run: |
bundle exec rspec specs
Workflow that deploys:
name: Deploy on server
on:
push:
branches:
- setup_github
jobs:
deploy:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout#v1
- uses: ruby/setup-ruby#v1
with:
ruby-version: 2.5.3
bundler-cache: true
- uses: miloserdow/capistrano-deploy#master
with:
target: staging
deploy_key: ${{ secrets.DEPLOY_ENC_KEY }}
Your goal should be achievable by ensuring the following are true:
The CI workflow has run
The CI workflow was success
name: Deploy on server
on:
workflow_run:
workflows: [CI]
branches: [setup_github]
types:
- completed
jobs:
deploy:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout#v1
- uses: ruby/setup-ruby#v1
with:
ruby-version: 2.5.3
bundler-cache: true
- uses: miloserdow/capistrano-deploy#master
with:
target: staging
deploy_key: ${{ secrets.DEPLOY_ENC_KEY }}
This is described in the Github Actions docs on workflow_run.

PG::ConnectionBad on Github Actions for Rails

I'm in a problem right now and don't know how to solve this in Github Actions context.
Side context: I'm following a tutorial on setting up Rails-Postgres with GH actions for CI.
Normally when I run into this problem in dev environment, I'd delete a postmaster.pid file. But here, because I'm in a test environment in GH actions, I'm not sure how to solve it. I tried already doing how I would do it in dev env, it unfortunately did not work.
The solutions I've tried:
rm /usr/local/var/postgres/postmaster.pid
Changing the ports within postgres entry in the workflow
My GH Actions Workflow:
name: Test
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:12.1
ports:
- 6543:6543
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Checkout repository
uses: actions/checkout#v2
- name: Install Ruby version specified in `.ruby-version`
uses: eregon/use-ruby-action#master
- name: Install required apt packages
run: |
sudo apt-get -y install libpq-dev
- name: Setup cache key and directory for gems cache
uses: actions/cache#v1
with:
path: vendor/bundle
key: ${{ runner.os }}-gem-use-ruby-${{ hashFiles('**/Gemfile.lock') }}
- name: Read Node.js version to install from `.nvmrc`
run: echo "##[set-output name=NVMRC;]$(cat .nvmrc)"
id: nvm
- name: Install required Node.js version
uses: actions/setup-node#v1
with:
node-version: "${{ steps.nvm.outputs.NVMRC }}"
- name: Get Yarn cache directory path
id: yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Setup cache key and directory for node_modules cache
uses: actions/cache#v1
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
- name: Bundle install
run: |
gem install bundler -v2.1.4
bundle config path vendor/bundle
bundle install --jobs 4 --retry 3
- name: Yarn install
run: yarn --frozen-lockfile
- name: Run RSpec // ERRORS HERE
run: |
RUBYOPT='-W:no-deprecated -W:no-experimental' bundle exec rails db:prepare
RUBYOPT='-W:no-deprecated -W:no-experimental' bundle exec rspec
It breaks on the Run RSpec task.
Error message:
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Thanks for your help everyone😁
Found a fix!
I needed to add the host, username, and password entry in config/database.yml for the test DB
Because you specified the port for 6543, so PostgreSQL cannot find its default port 5432.
To fix this, specify the port under
services:
postgres:
ports:
- 5432/tcp

Resources