简体   繁体   English

github 操作 - 在 postgres 服务中运行 sql 脚本

[英]github actions - run sql script in postgres service

I want to run a script in the postgres service in github actions that creates a table and adds an extension.我想在 github 操作中的 postgres 服务中运行一个脚本,该操作创建一个表并添加一个扩展。 How can I do that?我怎样才能做到这一点? Do I need to make a shell script or can I do right in the yaml file?我需要制作 shell 脚本还是可以在 yaml 文件中做正确的事?

sql script sql 脚本

drop database mydb;
create database mydb;
\c mydb;
CREATE EXTENSION "pgcrypto";

workflow工作流程

name: API Integration Tests

on: 
    pull_request:
    push:
        branches:
            -master

env:
    DB_HOST: localhost
    DB_USERNAME: postgres
    DB_PASSWORD: rt

jobs:
    build:
        runs-on: ubuntu-latest

        strategy:
          matrix:
            node-version: [10.x, 12.x, 13.x]


    services:
        postgres:
          image: postgres:latest
          env:
            POSTGRES_DB: mydb        
            POSTGRES_PASSWORD: helloworl
            POSTGRES_USER: postgres
          ports:
            - 5433:5432
          # Set health checks to wait until postgres has started
          options: >-
            --health-cmd pg_isready
            --health-interval 10s
            --health-timeout 5s
            --health-retries 5
    steps:
        - uses: actions/checkout@v1
        - name: Use Node.js ${{ matrix.node-version }}
            uses: actions/setup-node@v1
            with:
            node-version: ${{ matrix.node-version }}
        - name: npm install
            run: npm ci
        - name: npm test
            run: npm run test

You can add a step that uses PSQL commands.您可以添加使用 PSQL 命令的步骤。

Here's an example step that creates your database:这是创建数据库的示例步骤:

- name: Create database
  run: |
     PGPASSWORD=helloworl psql -U postgres -tc "SELECT 'CREATE DATABASE mydb' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'mydb')"

By the way, I note that the next command you wanted was: CREATE EXTENSION "pgcrypto";顺便说一句,我注意到您想要的下一个命令是: CREATE EXTENSION "pgcrypto"; , which I assume is because you want to generate UUIDs (Common use case). ,我认为这是因为您想生成 UUID(常见用例)。 Please note that you do not need this for get_random_uuid() as this is natively support in Postgres from v13 onwards.请注意, get_random_uuid()不需要这个,因为从 v13 开始,Postgres 原生支持它。

However if you really, really, really wanted to add pgcrypto, you can use this step:但是,如果你真的,真的,真的想添加 pgcrypto,你可以使用这个步骤:

- name: Enable pgcrypto extension
  run: |
     PGPASSWORD=helloworl psql -U postgres -tc "CREATE EXTENSION 'pgcrypto';"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM