Skip to content

Commit

Permalink
ci: Fix Postgres and MySQL tests (no-changelog) (n8n-io#8106)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ivov authored Dec 20, 2023
1 parent 8df49e1 commit 97aa38e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci-postgres-mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
workflow_dispatch:
pull_request:
paths:
- packages/cli/src/databases/migrations/**
- packages/cli/src/databases/**

concurrency:
group: db-${{ github.event.pull_request.number || github.ref }}
Expand Down Expand Up @@ -65,7 +65,7 @@ jobs:
- name: Test MySQL
working-directory: packages/cli
run: DB_TABLE_PREFIX=test_ pnpm test:mysql --runInBand
run: pnpm test:mysql

postgres:
name: Postgres
Expand Down Expand Up @@ -98,7 +98,7 @@ jobs:
- name: Test Postgres
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:
name: Notify Slack on failure
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"swagger": "swagger-cli",
"test": "pnpm test:sqlite",
"test:sqlite": "N8N_LOG_LEVEL=silent DB_TYPE=sqlite jest",
"test:postgres": "N8N_LOG_LEVEL=silent DB_TYPE=postgresdb jest --no-coverage",
"test:mysql": "N8N_LOG_LEVEL=silent DB_TYPE=mysqldb 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 DB_TABLE_PREFIX=test_ jest --no-coverage",
"watch": "concurrently \"tsc -w -p tsconfig.build.json\" \"tsc-alias -w -p tsconfig.build.json\"",
"typeorm": "ts-node -T ../../node_modules/typeorm/cli.js"
},
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/src/databases/repositories/role.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Service } from 'typedi';
import { DataSource, Repository } from 'typeorm';
import type { RoleNames, RoleScopes } from '../entities/Role';
import { Role } from '../entities/Role';
import { User } from '../entities/User';

@Service()
export class RoleRepository extends Repository<Role> {
Expand All @@ -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 }`
*/
async countUsersByRole() {
type Row = { role_name: string; count: number };
type Row = { role_name: string; count: number | string };

const rows: Row[] = await this.createQueryBuilder('role')
.select('role.name')
.addSelect('COUNT(user.id)', 'count')
.innerJoin('user', 'user', 'role.id = user.globalRoleId')
.innerJoin(User, 'user', 'role.id = user.globalRoleId')
.groupBy('role.name')
.getRawMany();

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;
}, {});
}
Expand Down

0 comments on commit 97aa38e

Please sign in to comment.