ci: Fix Postgres and MySQL tests (no-changelog) (#8106)

This role query works for sqlite but [fails for Postgres and
MySQL](https://github.com/n8n-io/n8n/actions/runs/7269009778/job/19805986017),
so generalize by adding alias and accounting for count possibly being
`string` in the resulting rows.

Run in progress: https://github.com/n8n-io/n8n/actions/runs/7275986797
This commit is contained in:
Iván Ovejero 2023-12-20 15:14:31 +01:00 committed by GitHub
parent 8df49e134d
commit 97aa38e783
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 8 deletions

View file

@ -6,7 +6,7 @@ on:
workflow_dispatch: workflow_dispatch:
pull_request: pull_request:
paths: paths:
- packages/cli/src/databases/migrations/** - packages/cli/src/databases/**
concurrency: concurrency:
group: db-${{ github.event.pull_request.number || github.ref }} group: db-${{ github.event.pull_request.number || github.ref }}
@ -65,7 +65,7 @@ jobs:
- name: Test MySQL - name: Test MySQL
working-directory: packages/cli working-directory: packages/cli
run: DB_TABLE_PREFIX=test_ pnpm test:mysql --runInBand run: pnpm test:mysql
postgres: postgres:
name: Postgres name: Postgres
@ -98,7 +98,7 @@ jobs:
- name: Test Postgres - name: Test Postgres
working-directory: packages/cli working-directory: packages/cli
run: DB_POSTGRESDB_SCHEMA=alt_schema DB_TABLE_PREFIX=test_ pnpm test:postgres --runInBand run: pnpm test:postgres
notify-on-failure: notify-on-failure:
name: Notify Slack on failure name: Notify Slack on failure

View file

@ -37,8 +37,8 @@
"swagger": "swagger-cli", "swagger": "swagger-cli",
"test": "pnpm test:sqlite", "test": "pnpm test:sqlite",
"test:sqlite": "N8N_LOG_LEVEL=silent DB_TYPE=sqlite jest", "test:sqlite": "N8N_LOG_LEVEL=silent DB_TYPE=sqlite jest",
"test:postgres": "N8N_LOG_LEVEL=silent DB_TYPE=postgresdb jest --no-coverage", "test:postgres": "N8N_LOG_LEVEL=silent DB_TYPE=postgresdb DB_POSTGRESDB_SCHEMA=alt_schema DB_TABLE_PREFIX=test_ jest --no-coverage",
"test:mysql": "N8N_LOG_LEVEL=silent DB_TYPE=mysqldb jest --no-coverage", "test:mysql": "N8N_LOG_LEVEL=silent DB_TYPE=mysqldb DB_TABLE_PREFIX=test_ jest --no-coverage",
"watch": "concurrently \"tsc -w -p tsconfig.build.json\" \"tsc-alias -w -p tsconfig.build.json\"", "watch": "concurrently \"tsc -w -p tsconfig.build.json\" \"tsc-alias -w -p tsconfig.build.json\"",
"typeorm": "ts-node -T ../../node_modules/typeorm/cli.js" "typeorm": "ts-node -T ../../node_modules/typeorm/cli.js"
}, },

View file

@ -2,6 +2,7 @@ import { Service } from 'typedi';
import { DataSource, Repository } from 'typeorm'; import { DataSource, Repository } from 'typeorm';
import type { RoleNames, RoleScopes } from '../entities/Role'; import type { RoleNames, RoleScopes } from '../entities/Role';
import { Role } from '../entities/Role'; import { Role } from '../entities/Role';
import { User } from '../entities/User';
@Service() @Service()
export class RoleRepository extends Repository<Role> { export class RoleRepository extends Repository<Role> {
@ -17,17 +18,17 @@ export class RoleRepository extends Repository<Role> {
* Counts the number of users in each role, e.g. `{ admin: 2, member: 6, owner: 1 }` * Counts the number of users in each role, e.g. `{ admin: 2, member: 6, owner: 1 }`
*/ */
async countUsersByRole() { async countUsersByRole() {
type Row = { role_name: string; count: number }; type Row = { role_name: string; count: number | string };
const rows: Row[] = await this.createQueryBuilder('role') const rows: Row[] = await this.createQueryBuilder('role')
.select('role.name') .select('role.name')
.addSelect('COUNT(user.id)', 'count') .addSelect('COUNT(user.id)', 'count')
.innerJoin('user', 'user', 'role.id = user.globalRoleId') .innerJoin(User, 'user', 'role.id = user.globalRoleId')
.groupBy('role.name') .groupBy('role.name')
.getRawMany(); .getRawMany();
return rows.reduce<Record<string, number>>((acc, item) => { return rows.reduce<Record<string, number>>((acc, item) => {
acc[item.role_name] = item.count; acc[item.role_name] = typeof item.count === 'number' ? item.count : parseInt(item.count, 10);
return acc; return acc;
}, {}); }, {});
} }