CircleCi有时无法在构建中连接到Postgres

时间:2018-08-16 14:06:15

标签: javascript node.js postgresql knex.js circleci

基本上,我有一个Nodejs项目,该项目通过knex连接到postgres数据库,并且我使用CircleCI进行测试。它可以正常工作,但是有时由于“错误:连接ECONNREFUSED 127.0.0.1:5432”而导致构建失败,但我不知道为什么;它可以在我的机器上正常工作,在版本上必须有所不同。

为创建测试数据库而运行的文件如下:

'use strict';

require('dotenv').config();

const connectionOptions = require(`${process.cwd()}/knexfile.js`)[`${process.env.NODE_ENV}`];

const knex = require('knex')(connectionOptions);
console.log('New connection to default postgres database made');

// Remove all other connections to test database
knex.raw(`select pg_terminate_backend(pid) from pg_stat_activity where datname = '${process.env.PG_TEST_DATABASE}'`)
	.then(() => {
		console.log('Removed all other connections to the test database');
		// Drop test database if it exists
		return knex.raw(`DROP DATABASE IF EXISTS ${process.env.PG_TEST_DATABASE};`);
	})
	.then(() => {
		console.log('Dropped test database (if it existed)');
		// Create test database
		return knex.raw(`CREATE DATABASE ${process.env.PG_TEST_DATABASE};`);
	})
	.then(() => {
		console.log('Test database created');
		return process.exit();
	});

发生这种情况时,无论是尝试删除db,删除其他连接等,在连接后首次尝试对postgres执行任何操作时,都会发生该错误。当前,当我尝试执行“选择”时会发生此错误。 pg_terminate_backend'行。

我对Postgres的设置是:

const connectionOptions = {
	client: 'pg',
	version: '7.4.1',
	connection: {
		host: '127.0.0.1',
		user: process.env.PG_USER,
		password: process.env.PG_PASSWORD,
		database: process.env.PG_DATABASE,
		port: parseInt(process.env.PG_PORT) || 5432
	},
	pool: {
		min: 2,
		max: 10
	},
	migrations: {
		tableName: '_migrations',
		directory: './migrations',
	},
	seeds: {
		directory: './seeds/development'
	}
};

我的circleci yml文件如下:

version: 2
jobs:
  build:
    working_directory: ~/project
    docker:
      - image: circleci/node:8.9.4
      # The below environemnt is where you set the .env variables used throughout node
        environment:
          NODE_ENV: test
          PG_USER: jerrodq2
          PG_DATABASE: freelancing_project
          PG_TEST_DATABASE: freelancing_project_test
          PG_PORT: 5432

      - image: postgres:10.3
        environment:
          POSTGRES_USER: jerrodq2
          POSTGRES_DB: freelancing_project

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "package.json" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-

      - run:
          name: Install local dependencies
          command: npm install

      - run:
          name: Create database
          command: npm run db:reset:test



      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}

      # run tests!
      - run:
          name: Running Tests
          command: npm run test

我有cross-posted this question to CircleCI Discourse

1 个答案:

答案 0 :(得分:1)

对于任何感兴趣的人,我都能找到解决方案。按照上面评论中Linas的建议,我找到了Circleci的适当代码,该代码等待postgres启动,然后再进行下一步,我只是添加了一条命令,等待5432端口打开,然后再创建数据库,如下所示:

- run:
    name: Install local dependencies
    command: npm install

- run:
    name: Wait for Postgres to start
    command: dockerize -wait tcp://localhost:5432 -timeout 1m

- run:
    name: Create database
    command: npm run db:reset:test