From 76c5f689ddfed948133835adb3f681231f59a2e6 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sun, 24 Nov 2024 19:41:40 +0800 Subject: [PATCH] add PostgreSQL ci action --- .github/workflows/nodejs.yml | 61 ++++++++++++++++- README.md | 2 +- app/port/config.ts | 9 +-- config/config.default.ts | 41 +++--------- config/config.unittest.ts | 3 +- config/database.ts | 43 ++++++++++++ package.json | 8 ++- prepare-database-postgresql.sh | 3 +- sql/ddl_postgresql.sql | 116 ++++++++++++++++----------------- sql/postgresql/3.68.0.sql | 116 ++++++++++++++++----------------- test/TestUtil.ts | 63 +++++++++--------- 11 files changed, 270 insertions(+), 195 deletions(-) create mode 100644 config/database.ts diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 624f0e98..85d4715a 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -10,6 +10,63 @@ on: branches: [ master ] jobs: + test-postgresql-fs-nfs: + runs-on: ${{ matrix.os }} + + services: + # https://docs.github.com/en/actions/use-cases-and-examples/using-containerized-services/creating-postgresql-service-containers + # Label used to access the service container + postgres: + # Docker Hub image + image: postgres + # Provide the password for postgres + env: + POSTGRES_PASSWORD: postgres + # Set health checks to wait until postgres has started + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + redis: + # https://docs.github.com/en/actions/using-containerized-services/about-service-containers#example-mapping-redis-ports + image: redis + ports: + # Opens tcp port 6379 on the host and service container + - 6379:6379 + + strategy: + fail-fast: false + matrix: + node-version: [18, 20, 22] + os: [ubuntu-latest] + + steps: + - name: Checkout Git Source + uses: actions/checkout@v3 + + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + + - name: Install Dependencies + run: npm i -g npminstall && npminstall + + - name: Continuous Integration + run: npm run ci + # Environment variables used by the `client.js` script to create a new PostgreSQL table. + env: + # The hostname used to communicate with the PostgreSQL service container + POSTGRES_HOST: postgres + # The default PostgreSQL port + POSTGRES_PORT: 5432 + + - name: Code Coverage + uses: codecov/codecov-action@v3 + with: + token: ${{ secrets.CODECOV_TOKEN }} + test-mysql57-fs-nfs: runs-on: ${{ matrix.os }} @@ -18,7 +75,7 @@ jobs: image: mysql:5.7 env: MYSQL_ALLOW_EMPTY_PASSWORD: true - CNPMCORE_DATABASE_NAME: cnpmcore_unittest + MYSQL_DATABASE: cnpmcore_unittest ports: - 3306:3306 options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5 @@ -68,7 +125,7 @@ jobs: image: mysql:5.7 env: MYSQL_ALLOW_EMPTY_PASSWORD: true - CNPMCORE_DATABASE_NAME: cnpmcore_unittest + MYSQL_DATABASE: cnpmcore_unittest ports: - 3306:3306 options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5 diff --git a/README.md b/README.md index a0512cc0..1c2bbcb7 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ [![CodeQL](https://github.com/cnpm/cnpmcore/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/cnpm/cnpmcore/actions/workflows/codeql-analysis.yml) [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fcnpm%2Fcnpmcore.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fcnpm%2Fcnpmcore?ref=badge_shield) -Refactor based on [cnpmjs.org](https://github.com/cnpm/cnpmjs.org) with TypeScript. +Reimplement based on [cnpmjs.org](https://github.com/cnpm/cnpmjs.org) with TypeScript. ## Registry HTTP API diff --git a/app/port/config.ts b/app/port/config.ts index 3e914388..f8872e90 100644 --- a/app/port/config.ts +++ b/app/port/config.ts @@ -1,13 +1,8 @@ import { SyncDeleteMode, SyncMode, ChangesStreamMode } from '../common/constants'; +import { DATABASE_TYPE } from '../../config/database'; export { cnpmcoreConfig } from '../../config/config.default'; -export enum DATABASE_TYPE { - MySQL = 'MySQL', - PostgreSQL = 'PostgreSQL', - SQLite = 'SQLite', -} - export type CnpmcoreConfig = { name: string, /** @@ -100,7 +95,7 @@ export type CnpmcoreConfig = { /** * white scope list */ - allowScopes: string [], + allowScopes: string[], /** * allow publish non-scope package, disable by default */ diff --git a/config/config.default.ts b/config/config.default.ts index e71e7b72..b1975aef 100644 --- a/config/config.default.ts +++ b/config/config.default.ts @@ -5,7 +5,8 @@ import { EggAppConfig, PowerPartial } from 'egg'; import OSSClient from 'oss-cnpm'; import { patchAjv } from '../app/port/typebox'; import { ChangesStreamMode, NOT_IMPLEMENTED_PATH, SyncDeleteMode, SyncMode } from '../app/common/constants'; -import { CnpmcoreConfig, DATABASE_TYPE } from '../app/port/config'; +import type { CnpmcoreConfig } from '../app/port/config'; +import { database } from './database'; export const cnpmcoreConfig: CnpmcoreConfig = { name: 'cnpm', @@ -61,7 +62,7 @@ export const cnpmcoreConfig: CnpmcoreConfig = { strictValidateTarballPkg: false, strictValidatePackageDeps: false, database: { - type: process.env.CNPMCORE_DATABASE_TYPE ?? DATABASE_TYPE.MySQL, + type: database.type, }, }; @@ -73,41 +74,17 @@ export default (appInfo: EggAppConfig) => { // override config from framework / plugin config.dataDir = process.env.CNPMCORE_DATA_DIR || join(appInfo.root, '.cnpmcore'); - - let dbName = process.env.CNPMCORE_DATABASE_NAME; - let dbHost = process.env.CNPMCORE_DATABASE_HOST; - let dbPort = process.env.CNPMCORE_DATABASE_PORT; - let dbUser = process.env.CNPMCORE_DATABASE_USER; - let dbPassword = process.env.CNPMCORE_DATABASE_PASSWORD; - let dialect = 'mysql'; - let dbClient = 'mysql2'; - if (config.cnpmcore.database?.type === DATABASE_TYPE.MySQL) { - // Compatible mysql configurations - dbName = dbName ?? process.env.CNPMCORE_MYSQL_DATABASE ?? process.env.MYSQL_DATABASE; - dbHost = dbHost ?? process.env.CNPMCORE_MYSQL_HOST ?? process.env.MYSQL_HOST ?? '127.0.0.1'; - dbPort = dbPort ?? process.env.CNPMCORE_MYSQL_PORT ?? process.env.MYSQL_PORT ?? '3306'; - dbUser = dbUser ?? process.env.CNPMCORE_MYSQL_USER ?? process.env.MYSQL_USER ?? 'root'; - dbPassword = dbPassword ?? process.env.CNPMCORE_MYSQL_PASSWORD ?? process.env.MYSQL_PASSWORD; - } else if (config.cnpmcore.database?.type === DATABASE_TYPE.PostgreSQL) { - dbClient = 'pg'; - dialect = 'postgres'; - } else if (config.cnpmcore.database?.type === DATABASE_TYPE.SQLite) { - dbClient = 'sqlite'; - dialect = 'sqlite'; - } config.orm = { - dialect, - client: dbClient, - database: dbName ?? 'cnpmcore', - host: dbHost, - port: dbPort, - user: dbUser, - password: dbPassword, + ...database, + database: database.name ?? 'cnpmcore', charset: 'utf8mb4', logger: { // https://github.com/cyjake/leoric/blob/master/docs/zh/logging.md#logqueryerror // ignore query error - logQueryError() {}, + // logQueryError() {}, + logQueryError(...args: any[]) { + console.log(args); + }, }, }; diff --git a/config/config.unittest.ts b/config/config.unittest.ts index 81425e4a..070884da 100644 --- a/config/config.unittest.ts +++ b/config/config.unittest.ts @@ -1,6 +1,7 @@ import { join } from 'path'; import { EggAppConfig, PowerPartial } from 'egg'; import Mock from '@elastic/elasticsearch-mock'; +import { database } from './database'; export const mockES = new Mock(); @@ -9,7 +10,7 @@ export default (appInfo: EggAppConfig) => { config.dataDir = join(appInfo.root, '.cnpmcore_unittest'); config.orm = { - database: process.env.MYSQL_DATABASE || 'cnpmcore_unittest', + database: database.name ?? 'cnpmcore_unittest', }; config.nfs = { diff --git a/config/database.ts b/config/database.ts new file mode 100644 index 00000000..83c2ef0e --- /dev/null +++ b/config/database.ts @@ -0,0 +1,43 @@ +export enum DATABASE_TYPE { + MySQL = 'MySQL', + PostgreSQL = 'PostgreSQL', + SQLite = 'SQLite', +} + +const dbType = process.env.CNPMCORE_DATABASE_TYPE ?? DATABASE_TYPE.MySQL; +let dbName = process.env.CNPMCORE_DATABASE_NAME; +let dbHost = process.env.CNPMCORE_DATABASE_HOST; +let dbPort = process.env.CNPMCORE_DATABASE_PORT; +let dbUser = process.env.CNPMCORE_DATABASE_USER; +let dbPassword = process.env.CNPMCORE_DATABASE_PASSWORD; +let dialect = 'mysql'; +let dbClient = 'mysql2'; +if (dbType === DATABASE_TYPE.MySQL) { + // Compatible mysql configurations + dbName = dbName ?? process.env.CNPMCORE_MYSQL_DATABASE ?? process.env.MYSQL_DATABASE; + dbHost = dbHost ?? process.env.CNPMCORE_MYSQL_HOST ?? process.env.MYSQL_HOST ?? '127.0.0.1'; + dbPort = dbPort ?? process.env.CNPMCORE_MYSQL_PORT ?? process.env.MYSQL_PORT ?? '3306'; + dbUser = dbUser ?? process.env.CNPMCORE_MYSQL_USER ?? process.env.MYSQL_USER ?? 'root'; + dbPassword = dbPassword ?? process.env.CNPMCORE_MYSQL_PASSWORD ?? process.env.MYSQL_PASSWORD; +} else if (dbType === DATABASE_TYPE.PostgreSQL) { + dbClient = 'pg'; + dialect = 'postgres'; + dbHost = dbHost ?? process.env.CNPMCORE_POSTGRES_HOST ?? process.env.POSTGRES_HOST; + dbPort = dbPort ?? process.env.CNPMCORE_POSTGRES_PORT ?? process.env.POSTGRES_PORT ?? '5432'; + dbPassword = dbPassword ?? process.env.CNPMCORE_POSTGRES_PASSWORD ?? process.env.POSTGRES_PASSWORD; +} else if (dbType === DATABASE_TYPE.SQLite) { + // TODO + dbClient = 'sqlite'; + dialect = 'sqlite'; +} + +export const database = { + type: dbType, + dialect, + client: dbClient, + name: dbName, + host: dbHost, + port: dbPort, + user: dbUser, + password: dbPassword, +}; diff --git a/package.json b/package.json index 88d68439..1674e32b 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "dev:postgresql": "CNPMCORE_DATABASE_TYPE=PostgreSQL egg-bin dev", "lint": "eslint --cache --ext .ts .", "lint:fix": "eslint --cache --ext .ts --fix .", - "test:postgresql": "npm run lint:fix && npm run test:local", + "test:postgresql": "npm run lint:fix && npm run test:local:postgresql", "pretest:local:postgresql": "bash prepare-database-postgresql.sh", "test:local:postgresql": "CNPMCORE_DATABASE_TYPE=PostgreSQL egg-bin test", "test": "npm run lint:fix && npm run test:local", @@ -52,6 +52,7 @@ "precov:postgresql": "bash prepare-database-postgresql.sh", "cov:postgresql": "CNPMCORE_DATABASE_TYPE=PostgreSQL egg-bin cov", "ci": "npm run lint && npm run cov && npm run tsc:prod", + "ci:postgresql": "npm run lint && npm run cov:postgresql && npm run tsc:prod", "clean": "tsc -b --clean && rm -rf dist", "tsc": "npm run clean && tsc -p ./tsconfig.json", "tsc:prod": "npm run clean && tsc -p ./tsconfig.prod.json", @@ -108,6 +109,7 @@ "lodash": "^4.17.21", "mime-types": "^2.1.35", "mysql2": "^3.9.4", + "pg": "^8.13.1", "node-rsa": "^1.1.1", "npm-package-arg": "^10.1.0", "oss-cnpm": "^5.0.1", @@ -120,8 +122,7 @@ "validate-npm-package-name": "^3.0.0" }, "optionalDependencies": { - "s3-cnpmcore": "^1.1.2", - "pg": "^8.13.1" + "s3-cnpmcore": "^1.1.2" }, "devDependencies": { "@cnpmjs/npm-cli-login": "^1.1.0", @@ -133,6 +134,7 @@ "@types/mysql": "^2.15.21", "@types/node-rsa": "^1.1.4", "@types/npm-package-arg": "^6.1.1", + "@types/pg": "^8.11.10", "@types/semver": "^7.3.12", "@types/tar": "^6.1.4", "@types/ua-parser-js": "^0.7.36", diff --git a/prepare-database-postgresql.sh b/prepare-database-postgresql.sh index a34ab64e..28d5597b 100644 --- a/prepare-database-postgresql.sh +++ b/prepare-database-postgresql.sh @@ -41,7 +41,8 @@ echo "🤖 Running the following SQL files:" # execute sql files for file in $sql_files; do echo "🔖 Running $file..." - psql $param --dbname=$db_name --file=$file --echo-all + # psql $param --dbname=$db_name --file=$file --echo-all + psql $param --dbname=$db_name --file=$file --quiet done echo "🎉 prepare database $db_name done" diff --git a/sql/ddl_postgresql.sql b/sql/ddl_postgresql.sql index a823811b..0922ec89 100644 --- a/sql/ddl_postgresql.sql +++ b/sql/ddl_postgresql.sql @@ -6,14 +6,14 @@ CREATE TABLE binaries ( category varchar(50) NOT NULL, parent varchar(500) NOT NULL, name varchar(200) NOT NULL, - is_dir smallint NOT NULL DEFAULT 0, + is_dir boolean NOT NULL DEFAULT false, size integer NOT NULL, date varchar(100) NOT NULL ); -CREATE UNIQUE INDEX uk_binary_id ON binaries (binary_id); -CREATE UNIQUE INDEX uk_category_parent_name ON binaries (category, parent, name); -CREATE INDEX idx_category_parent ON binaries (category, parent); +CREATE UNIQUE INDEX binaries_uk_binary_id ON binaries (binary_id); +CREATE UNIQUE INDEX binaries_uk_category_parent_name ON binaries (category, parent, name); +CREATE INDEX binaries_idx_category_parent ON binaries (category, parent); COMMENT ON TABLE binaries IS 'binary info'; COMMENT ON COLUMN binaries.id IS 'primary key'; @@ -38,7 +38,7 @@ CREATE TABLE changes ( data json DEFAULT NULL ); -CREATE UNIQUE INDEX uk_change_id ON changes (change_id); +CREATE UNIQUE INDEX changes_uk_change_id ON changes (change_id); COMMENT ON TABLE changes IS 'change info'; COMMENT ON COLUMN changes.id IS 'primary key'; @@ -62,7 +62,7 @@ CREATE TABLE dists ( integrity varchar(512) NOT NULL ); -CREATE UNIQUE INDEX uk_dist_id ON dists (dist_id); +CREATE UNIQUE INDEX dists_uk_dist_id ON dists (dist_id); COMMENT ON TABLE dists IS 'dist info'; COMMENT ON COLUMN dists.id IS 'primary key'; @@ -93,7 +93,7 @@ CREATE TABLE history_tasks ( error text ); -CREATE UNIQUE INDEX uk_task_id ON history_tasks (task_id); +CREATE UNIQUE INDEX history_tasks_uk_task_id ON history_tasks (task_id); COMMENT ON TABLE history_tasks IS 'history task info'; COMMENT ON COLUMN history_tasks.id IS 'primary key'; @@ -123,11 +123,11 @@ CREATE TABLE hooks ( endpoint varchar(2048) NOT NULL, secret varchar(200) NOT NULL, latest_task_id varchar(24) DEFAULT NULL, - enable smallint NOT NULL DEFAULT 0 + enable boolean NOT NULL DEFAULT false ); -CREATE UNIQUE INDEX uk_type_name_owner_id ON hooks (type, name, owner_id); -CREATE INDEX idx_type_name_id ON hooks (type, name, id); +CREATE UNIQUE INDEX hooks_uk_type_name_owner_id ON hooks (type, name, owner_id); +CREATE INDEX hooks_idx_type_name_id ON hooks (type, name, id); COMMENT ON TABLE hooks IS 'task info'; COMMENT ON COLUMN hooks.id IS 'primary key'; @@ -151,9 +151,9 @@ CREATE TABLE maintainers ( user_id varchar(24) NOT NULL ); -CREATE UNIQUE INDEX uk_package_id_user_id ON maintainers (package_id, user_id); -CREATE INDEX idx_package_id ON maintainers (package_id); -CREATE INDEX idx_user_id ON maintainers (user_id); +CREATE UNIQUE INDEX maintainers_uk_package_id_user_id ON maintainers (package_id, user_id); +CREATE INDEX maintainers_idx_package_id ON maintainers (package_id); +CREATE INDEX maintainers_idx_user_id ON maintainers (user_id); COMMENT ON TABLE maintainers IS 'package maintainers'; COMMENT ON COLUMN maintainers.id IS 'primary key'; @@ -174,8 +174,8 @@ CREATE TABLE package_deps ( spec varchar(100) NOT NULL ); -CREATE UNIQUE INDEX uk_package_dep_id ON package_deps (package_dep_id); -CREATE UNIQUE INDEX uk_package_version_id_scope_name ON package_deps (package_version_id, scope, name); +CREATE UNIQUE INDEX package_deps_uk_package_dep_id ON package_deps (package_dep_id); +CREATE UNIQUE INDEX package_deps_uk_package_version_id_scope_name ON package_deps (package_version_id, scope, name); COMMENT ON TABLE package_deps IS 'package dependency info'; COMMENT ON COLUMN package_deps.id IS 'primary key'; @@ -198,8 +198,8 @@ CREATE TABLE package_tags ( version varchar(256) NOT NULL ); -CREATE UNIQUE INDEX uk_package_tag_id ON package_tags (package_tag_id); -CREATE UNIQUE INDEX uk_package_tag ON package_tags (package_id, tag); +CREATE UNIQUE INDEX package_tags_uk_package_tag_id ON package_tags (package_tag_id); +CREATE UNIQUE INDEX package_tags_uk_package_tag ON package_tags (package_id, tag); COMMENT ON TABLE package_tags IS 'package tag info'; COMMENT ON COLUMN package_tags.id IS 'primary key'; @@ -221,8 +221,8 @@ CREATE TABLE package_version_blocks ( reason text NOT NULL ); -CREATE UNIQUE INDEX uk_package_version_block_id ON package_version_blocks (package_version_block_id); -CREATE UNIQUE INDEX uk_name_version ON package_version_blocks (package_id, version); +CREATE UNIQUE INDEX package_version_blocks_uk_package_version_block_id ON package_version_blocks (package_version_block_id); +CREATE UNIQUE INDEX package_version_blocks_uk_name_version ON package_version_blocks (package_id, version); COMMENT ON TABLE package_version_blocks IS 'blocklist package versions'; COMMENT ON COLUMN package_version_blocks.id IS 'primary key'; @@ -274,9 +274,9 @@ CREATE TABLE package_version_downloads ( d31 integer NOT NULL DEFAULT 0 ); -CREATE UNIQUE INDEX uk_year_month_package_id_version ON package_version_downloads (year_month, package_id, version); -CREATE INDEX idx_year_month ON package_version_downloads (year_month); -CREATE INDEX idx_packageid_yearmonth ON package_version_downloads (package_id, year_month); +CREATE UNIQUE INDEX package_version_downloads_uk_year_month_package_id_version ON package_version_downloads (year_month, package_id, version); +CREATE INDEX package_version_downloads_idx_year_month ON package_version_downloads (year_month); +CREATE INDEX package_version_downloads_idx_packageid_yearmonth ON package_version_downloads (package_id, year_month); COMMENT ON TABLE package_version_downloads IS 'package version download total info'; COMMENT ON COLUMN package_version_downloads.id IS 'primary key'; @@ -331,8 +331,8 @@ CREATE TABLE package_version_files ( mtime timestamp(3) NOT NULL ); -CREATE UNIQUE INDEX uk_package_version_file_id ON package_version_files (package_version_file_id); -CREATE UNIQUE INDEX ux_package_version_id_directory_name ON package_version_files (package_version_id, directory, name); +CREATE UNIQUE INDEX package_version_files_uk_package_version_file_id ON package_version_files (package_version_file_id); +CREATE UNIQUE INDEX package_version_files_ux_package_version_id_directory_name ON package_version_files (package_version_id, directory, name); COMMENT ON TABLE package_version_files IS 'package version file'; COMMENT ON COLUMN package_version_files.id IS 'primary key'; @@ -357,9 +357,9 @@ CREATE TABLE package_version_manifests ( manifest json NOT NULL ); -CREATE UNIQUE INDEX uk_package_version_manifest_id ON package_version_manifests (package_version_manifest_id); -CREATE UNIQUE INDEX uk_package_version_id ON package_version_manifests (package_version_id); -CREATE INDEX idx_package_id ON package_version_manifests (package_id); +CREATE UNIQUE INDEX package_version_manifests_uk_package_version_manifest_id ON package_version_manifests (package_version_manifest_id); +CREATE UNIQUE INDEX package_version_manifests_uk_package_version_id ON package_version_manifests (package_version_id); +CREATE INDEX package_version_manifests_idx_package_id ON package_version_manifests (package_id); COMMENT ON TABLE package_version_manifests IS 'package version manifest'; COMMENT ON COLUMN package_version_manifests.id IS 'primary key'; @@ -384,12 +384,12 @@ CREATE TABLE package_versions ( readme_dist_id varchar(24) NOT NULL, publish_time timestamp(3) NOT NULL, padding_version varchar(255) DEFAULT NULL, - is_pre_release smallint DEFAULT NULL + is_pre_release boolean DEFAULT NULL ); -CREATE UNIQUE INDEX uk_package_version_id ON package_versions (package_version_id); -CREATE UNIQUE INDEX uk_package_id_version ON package_versions (package_id, version); -CREATE INDEX idx_pkg_id_is_pre_release_padding_version ON package_versions (package_id, padding_version, is_pre_release, version); +CREATE UNIQUE INDEX package_versions_uk_package_version_id ON package_versions (package_version_id); +CREATE UNIQUE INDEX package_versions_uk_package_id_version ON package_versions (package_id, version); +CREATE INDEX package_versions_idx_pkg_id_is_pre_release_padding_version ON package_versions (package_id, padding_version, is_pre_release, version); COMMENT ON TABLE package_versions IS 'package version info'; COMMENT ON COLUMN package_versions.id IS 'primary key'; @@ -412,7 +412,7 @@ CREATE TABLE packages ( gmt_create timestamp(3) NOT NULL, gmt_modified timestamp(3) NOT NULL, package_id varchar(24) NOT NULL, - is_private smallint NOT NULL DEFAULT 0, + is_private boolean NOT NULL DEFAULT false, name varchar(214) NOT NULL, scope varchar(214) NOT NULL, description varchar(10240) DEFAULT NULL, @@ -421,8 +421,8 @@ CREATE TABLE packages ( registry_id varchar(24) DEFAULT NULL ); -CREATE UNIQUE INDEX uk_package_id ON packages (package_id); -CREATE UNIQUE INDEX uk_scope_name ON packages (scope, name); +CREATE UNIQUE INDEX packages_uk_package_id ON packages (package_id); +CREATE UNIQUE INDEX packages_uk_scope_name ON packages (scope, name); COMMENT ON TABLE packages IS 'package info'; COMMENT ON COLUMN packages.id IS 'primary key'; @@ -448,8 +448,8 @@ CREATE TABLE proxy_caches ( file_path varchar(512) NOT NULL DEFAULT '' ); -CREATE UNIQUE INDEX uk_package_version_path_name ON proxy_caches (file_path); -CREATE UNIQUE INDEX ux_package_version_file_name ON proxy_caches (fullname, file_type, version); +CREATE UNIQUE INDEX proxy_caches_uk_package_version_path_name ON proxy_caches (file_path); +CREATE UNIQUE INDEX proxy_caches_ux_package_version_file_name ON proxy_caches (fullname, file_type, version); COMMENT ON TABLE proxy_caches IS 'proxy mode cached files index'; COMMENT ON COLUMN proxy_caches.id IS 'primary key'; @@ -474,7 +474,7 @@ CREATE TABLE registries ( auth_token varchar(256) DEFAULT NULL ); -CREATE UNIQUE INDEX uk_name ON registries (name); +CREATE UNIQUE INDEX registries_uk_name ON registries (name); COMMENT ON TABLE registries IS 'registry info'; COMMENT ON COLUMN registries.id IS 'primary key'; @@ -498,7 +498,7 @@ CREATE TABLE scopes ( registry_id varchar(24) NOT NULL ); -CREATE UNIQUE INDEX uk_name ON scopes (name); +CREATE UNIQUE INDEX scopes_uk_name ON scopes (name); COMMENT ON TABLE scopes IS 'scope info'; COMMENT ON COLUMN scopes.id IS 'primary key'; @@ -527,11 +527,11 @@ CREATE TABLE tasks ( biz_id varchar(100) DEFAULT NULL ); -CREATE UNIQUE INDEX uk_task_id ON tasks (task_id); -CREATE UNIQUE INDEX uk_biz_id ON tasks (biz_id); -CREATE INDEX idx_type_state_target_name ON tasks (target_name, type, state); -CREATE INDEX idx_type_state_gmt_modified ON tasks (type, state, gmt_modified); -CREATE INDEX idx_gmt_modified ON tasks (gmt_modified); +CREATE UNIQUE INDEX tasks_uk_task_id ON tasks (task_id); +CREATE UNIQUE INDEX tasks_uk_biz_id ON tasks (biz_id); +CREATE INDEX tasks_idx_type_state_target_name ON tasks (target_name, type, state); +CREATE INDEX tasks_idx_type_state_gmt_modified ON tasks (type, state, gmt_modified); +CREATE INDEX tasks_idx_gmt_modified ON tasks (gmt_modified); COMMENT ON TABLE tasks IS 'task info'; COMMENT ON COLUMN tasks.id IS 'primary key'; @@ -559,9 +559,9 @@ CREATE TABLE token_packages ( package_id varchar(24) NOT NULL ); -CREATE UNIQUE INDEX uk_token_id_package_id ON token_packages (token_id, package_id); -CREATE INDEX idx_token_id ON token_packages (token_id); -CREATE INDEX idx_package_id ON token_packages (package_id); +CREATE UNIQUE INDEX token_packages_uk_token_id_package_id ON token_packages (token_id, package_id); +CREATE INDEX token_packages_idx_token_id ON token_packages (token_id); +CREATE INDEX token_packages_idx_package_id ON token_packages (package_id); COMMENT ON TABLE token_packages IS 'token allowed packages'; COMMENT ON COLUMN token_packages.id IS 'primary key'; @@ -578,8 +578,8 @@ CREATE TABLE tokens ( token_id varchar(24) NOT NULL, token_mark varchar(20) NOT NULL, token_key varchar(200) NOT NULL, - is_readonly smallint NOT NULL DEFAULT 0, - is_automation smallint NOT NULL DEFAULT 0, + is_readonly boolean NOT NULL DEFAULT false, + is_automation boolean NOT NULL DEFAULT false, cidr_whitelist json NOT NULL, user_id varchar(24) NOT NULL, name varchar(255) DEFAULT NULL, @@ -590,10 +590,10 @@ CREATE TABLE tokens ( last_used_at timestamp(3) DEFAULT NULL ); -CREATE UNIQUE INDEX uk_token_id ON tokens (token_id); -CREATE UNIQUE INDEX uk_token_key ON tokens (token_key); -CREATE UNIQUE INDEX uk_user_id_name ON tokens (user_id, name); -CREATE INDEX idx_user_id ON tokens (user_id); +CREATE UNIQUE INDEX tokens_uk_token_id ON tokens (token_id); +CREATE UNIQUE INDEX tokens_uk_token_key ON tokens (token_key); +CREATE UNIQUE INDEX tokens_uk_user_id_name ON tokens (user_id, name); +CREATE INDEX tokens_idx_user_id ON tokens (user_id); COMMENT ON TABLE tokens IS 'token info'; COMMENT ON COLUMN tokens.id IS 'primary key'; @@ -630,7 +630,7 @@ CREATE TABLE total ( change_stream_seq varchar(100) DEFAULT NULL ); -CREATE UNIQUE INDEX uk_total_id ON total (total_id); +CREATE UNIQUE INDEX total_uk_total_id ON total (total_id); COMMENT ON TABLE total IS 'total info'; COMMENT ON COLUMN total.id IS 'primary key'; @@ -658,12 +658,12 @@ CREATE TABLE users ( password_salt varchar(100) NOT NULL, password_integrity varchar(512) NOT NULL, ip varchar(100) NOT NULL, - is_private smallint NOT NULL DEFAULT 1, + is_private boolean NOT NULL DEFAULT true, scopes json DEFAULT NULL ); -CREATE UNIQUE INDEX uk_user_id ON users (user_id); -CREATE UNIQUE INDEX uk_name ON users (name); +CREATE UNIQUE INDEX users_uk_user_id ON users (user_id); +CREATE UNIQUE INDEX users_uk_name ON users (name); COMMENT ON TABLE users IS 'user info'; COMMENT ON COLUMN users.id IS 'primary key'; @@ -690,8 +690,8 @@ CREATE TABLE webauthn_credentials ( browser_type varchar(20) DEFAULT NULL ); -CREATE UNIQUE INDEX uk_wanc_id ON webauthn_credentials (wanc_id); -CREATE INDEX idx_user_id ON webauthn_credentials (user_id); +CREATE UNIQUE INDEX webauthn_credentials_uk_wanc_id ON webauthn_credentials (wanc_id); +CREATE INDEX webauthn_credentials_idx_user_id ON webauthn_credentials (user_id); COMMENT ON TABLE webauthn_credentials IS 'webauthn credential info'; COMMENT ON COLUMN webauthn_credentials.id IS 'primary key'; diff --git a/sql/postgresql/3.68.0.sql b/sql/postgresql/3.68.0.sql index a823811b..0922ec89 100644 --- a/sql/postgresql/3.68.0.sql +++ b/sql/postgresql/3.68.0.sql @@ -6,14 +6,14 @@ CREATE TABLE binaries ( category varchar(50) NOT NULL, parent varchar(500) NOT NULL, name varchar(200) NOT NULL, - is_dir smallint NOT NULL DEFAULT 0, + is_dir boolean NOT NULL DEFAULT false, size integer NOT NULL, date varchar(100) NOT NULL ); -CREATE UNIQUE INDEX uk_binary_id ON binaries (binary_id); -CREATE UNIQUE INDEX uk_category_parent_name ON binaries (category, parent, name); -CREATE INDEX idx_category_parent ON binaries (category, parent); +CREATE UNIQUE INDEX binaries_uk_binary_id ON binaries (binary_id); +CREATE UNIQUE INDEX binaries_uk_category_parent_name ON binaries (category, parent, name); +CREATE INDEX binaries_idx_category_parent ON binaries (category, parent); COMMENT ON TABLE binaries IS 'binary info'; COMMENT ON COLUMN binaries.id IS 'primary key'; @@ -38,7 +38,7 @@ CREATE TABLE changes ( data json DEFAULT NULL ); -CREATE UNIQUE INDEX uk_change_id ON changes (change_id); +CREATE UNIQUE INDEX changes_uk_change_id ON changes (change_id); COMMENT ON TABLE changes IS 'change info'; COMMENT ON COLUMN changes.id IS 'primary key'; @@ -62,7 +62,7 @@ CREATE TABLE dists ( integrity varchar(512) NOT NULL ); -CREATE UNIQUE INDEX uk_dist_id ON dists (dist_id); +CREATE UNIQUE INDEX dists_uk_dist_id ON dists (dist_id); COMMENT ON TABLE dists IS 'dist info'; COMMENT ON COLUMN dists.id IS 'primary key'; @@ -93,7 +93,7 @@ CREATE TABLE history_tasks ( error text ); -CREATE UNIQUE INDEX uk_task_id ON history_tasks (task_id); +CREATE UNIQUE INDEX history_tasks_uk_task_id ON history_tasks (task_id); COMMENT ON TABLE history_tasks IS 'history task info'; COMMENT ON COLUMN history_tasks.id IS 'primary key'; @@ -123,11 +123,11 @@ CREATE TABLE hooks ( endpoint varchar(2048) NOT NULL, secret varchar(200) NOT NULL, latest_task_id varchar(24) DEFAULT NULL, - enable smallint NOT NULL DEFAULT 0 + enable boolean NOT NULL DEFAULT false ); -CREATE UNIQUE INDEX uk_type_name_owner_id ON hooks (type, name, owner_id); -CREATE INDEX idx_type_name_id ON hooks (type, name, id); +CREATE UNIQUE INDEX hooks_uk_type_name_owner_id ON hooks (type, name, owner_id); +CREATE INDEX hooks_idx_type_name_id ON hooks (type, name, id); COMMENT ON TABLE hooks IS 'task info'; COMMENT ON COLUMN hooks.id IS 'primary key'; @@ -151,9 +151,9 @@ CREATE TABLE maintainers ( user_id varchar(24) NOT NULL ); -CREATE UNIQUE INDEX uk_package_id_user_id ON maintainers (package_id, user_id); -CREATE INDEX idx_package_id ON maintainers (package_id); -CREATE INDEX idx_user_id ON maintainers (user_id); +CREATE UNIQUE INDEX maintainers_uk_package_id_user_id ON maintainers (package_id, user_id); +CREATE INDEX maintainers_idx_package_id ON maintainers (package_id); +CREATE INDEX maintainers_idx_user_id ON maintainers (user_id); COMMENT ON TABLE maintainers IS 'package maintainers'; COMMENT ON COLUMN maintainers.id IS 'primary key'; @@ -174,8 +174,8 @@ CREATE TABLE package_deps ( spec varchar(100) NOT NULL ); -CREATE UNIQUE INDEX uk_package_dep_id ON package_deps (package_dep_id); -CREATE UNIQUE INDEX uk_package_version_id_scope_name ON package_deps (package_version_id, scope, name); +CREATE UNIQUE INDEX package_deps_uk_package_dep_id ON package_deps (package_dep_id); +CREATE UNIQUE INDEX package_deps_uk_package_version_id_scope_name ON package_deps (package_version_id, scope, name); COMMENT ON TABLE package_deps IS 'package dependency info'; COMMENT ON COLUMN package_deps.id IS 'primary key'; @@ -198,8 +198,8 @@ CREATE TABLE package_tags ( version varchar(256) NOT NULL ); -CREATE UNIQUE INDEX uk_package_tag_id ON package_tags (package_tag_id); -CREATE UNIQUE INDEX uk_package_tag ON package_tags (package_id, tag); +CREATE UNIQUE INDEX package_tags_uk_package_tag_id ON package_tags (package_tag_id); +CREATE UNIQUE INDEX package_tags_uk_package_tag ON package_tags (package_id, tag); COMMENT ON TABLE package_tags IS 'package tag info'; COMMENT ON COLUMN package_tags.id IS 'primary key'; @@ -221,8 +221,8 @@ CREATE TABLE package_version_blocks ( reason text NOT NULL ); -CREATE UNIQUE INDEX uk_package_version_block_id ON package_version_blocks (package_version_block_id); -CREATE UNIQUE INDEX uk_name_version ON package_version_blocks (package_id, version); +CREATE UNIQUE INDEX package_version_blocks_uk_package_version_block_id ON package_version_blocks (package_version_block_id); +CREATE UNIQUE INDEX package_version_blocks_uk_name_version ON package_version_blocks (package_id, version); COMMENT ON TABLE package_version_blocks IS 'blocklist package versions'; COMMENT ON COLUMN package_version_blocks.id IS 'primary key'; @@ -274,9 +274,9 @@ CREATE TABLE package_version_downloads ( d31 integer NOT NULL DEFAULT 0 ); -CREATE UNIQUE INDEX uk_year_month_package_id_version ON package_version_downloads (year_month, package_id, version); -CREATE INDEX idx_year_month ON package_version_downloads (year_month); -CREATE INDEX idx_packageid_yearmonth ON package_version_downloads (package_id, year_month); +CREATE UNIQUE INDEX package_version_downloads_uk_year_month_package_id_version ON package_version_downloads (year_month, package_id, version); +CREATE INDEX package_version_downloads_idx_year_month ON package_version_downloads (year_month); +CREATE INDEX package_version_downloads_idx_packageid_yearmonth ON package_version_downloads (package_id, year_month); COMMENT ON TABLE package_version_downloads IS 'package version download total info'; COMMENT ON COLUMN package_version_downloads.id IS 'primary key'; @@ -331,8 +331,8 @@ CREATE TABLE package_version_files ( mtime timestamp(3) NOT NULL ); -CREATE UNIQUE INDEX uk_package_version_file_id ON package_version_files (package_version_file_id); -CREATE UNIQUE INDEX ux_package_version_id_directory_name ON package_version_files (package_version_id, directory, name); +CREATE UNIQUE INDEX package_version_files_uk_package_version_file_id ON package_version_files (package_version_file_id); +CREATE UNIQUE INDEX package_version_files_ux_package_version_id_directory_name ON package_version_files (package_version_id, directory, name); COMMENT ON TABLE package_version_files IS 'package version file'; COMMENT ON COLUMN package_version_files.id IS 'primary key'; @@ -357,9 +357,9 @@ CREATE TABLE package_version_manifests ( manifest json NOT NULL ); -CREATE UNIQUE INDEX uk_package_version_manifest_id ON package_version_manifests (package_version_manifest_id); -CREATE UNIQUE INDEX uk_package_version_id ON package_version_manifests (package_version_id); -CREATE INDEX idx_package_id ON package_version_manifests (package_id); +CREATE UNIQUE INDEX package_version_manifests_uk_package_version_manifest_id ON package_version_manifests (package_version_manifest_id); +CREATE UNIQUE INDEX package_version_manifests_uk_package_version_id ON package_version_manifests (package_version_id); +CREATE INDEX package_version_manifests_idx_package_id ON package_version_manifests (package_id); COMMENT ON TABLE package_version_manifests IS 'package version manifest'; COMMENT ON COLUMN package_version_manifests.id IS 'primary key'; @@ -384,12 +384,12 @@ CREATE TABLE package_versions ( readme_dist_id varchar(24) NOT NULL, publish_time timestamp(3) NOT NULL, padding_version varchar(255) DEFAULT NULL, - is_pre_release smallint DEFAULT NULL + is_pre_release boolean DEFAULT NULL ); -CREATE UNIQUE INDEX uk_package_version_id ON package_versions (package_version_id); -CREATE UNIQUE INDEX uk_package_id_version ON package_versions (package_id, version); -CREATE INDEX idx_pkg_id_is_pre_release_padding_version ON package_versions (package_id, padding_version, is_pre_release, version); +CREATE UNIQUE INDEX package_versions_uk_package_version_id ON package_versions (package_version_id); +CREATE UNIQUE INDEX package_versions_uk_package_id_version ON package_versions (package_id, version); +CREATE INDEX package_versions_idx_pkg_id_is_pre_release_padding_version ON package_versions (package_id, padding_version, is_pre_release, version); COMMENT ON TABLE package_versions IS 'package version info'; COMMENT ON COLUMN package_versions.id IS 'primary key'; @@ -412,7 +412,7 @@ CREATE TABLE packages ( gmt_create timestamp(3) NOT NULL, gmt_modified timestamp(3) NOT NULL, package_id varchar(24) NOT NULL, - is_private smallint NOT NULL DEFAULT 0, + is_private boolean NOT NULL DEFAULT false, name varchar(214) NOT NULL, scope varchar(214) NOT NULL, description varchar(10240) DEFAULT NULL, @@ -421,8 +421,8 @@ CREATE TABLE packages ( registry_id varchar(24) DEFAULT NULL ); -CREATE UNIQUE INDEX uk_package_id ON packages (package_id); -CREATE UNIQUE INDEX uk_scope_name ON packages (scope, name); +CREATE UNIQUE INDEX packages_uk_package_id ON packages (package_id); +CREATE UNIQUE INDEX packages_uk_scope_name ON packages (scope, name); COMMENT ON TABLE packages IS 'package info'; COMMENT ON COLUMN packages.id IS 'primary key'; @@ -448,8 +448,8 @@ CREATE TABLE proxy_caches ( file_path varchar(512) NOT NULL DEFAULT '' ); -CREATE UNIQUE INDEX uk_package_version_path_name ON proxy_caches (file_path); -CREATE UNIQUE INDEX ux_package_version_file_name ON proxy_caches (fullname, file_type, version); +CREATE UNIQUE INDEX proxy_caches_uk_package_version_path_name ON proxy_caches (file_path); +CREATE UNIQUE INDEX proxy_caches_ux_package_version_file_name ON proxy_caches (fullname, file_type, version); COMMENT ON TABLE proxy_caches IS 'proxy mode cached files index'; COMMENT ON COLUMN proxy_caches.id IS 'primary key'; @@ -474,7 +474,7 @@ CREATE TABLE registries ( auth_token varchar(256) DEFAULT NULL ); -CREATE UNIQUE INDEX uk_name ON registries (name); +CREATE UNIQUE INDEX registries_uk_name ON registries (name); COMMENT ON TABLE registries IS 'registry info'; COMMENT ON COLUMN registries.id IS 'primary key'; @@ -498,7 +498,7 @@ CREATE TABLE scopes ( registry_id varchar(24) NOT NULL ); -CREATE UNIQUE INDEX uk_name ON scopes (name); +CREATE UNIQUE INDEX scopes_uk_name ON scopes (name); COMMENT ON TABLE scopes IS 'scope info'; COMMENT ON COLUMN scopes.id IS 'primary key'; @@ -527,11 +527,11 @@ CREATE TABLE tasks ( biz_id varchar(100) DEFAULT NULL ); -CREATE UNIQUE INDEX uk_task_id ON tasks (task_id); -CREATE UNIQUE INDEX uk_biz_id ON tasks (biz_id); -CREATE INDEX idx_type_state_target_name ON tasks (target_name, type, state); -CREATE INDEX idx_type_state_gmt_modified ON tasks (type, state, gmt_modified); -CREATE INDEX idx_gmt_modified ON tasks (gmt_modified); +CREATE UNIQUE INDEX tasks_uk_task_id ON tasks (task_id); +CREATE UNIQUE INDEX tasks_uk_biz_id ON tasks (biz_id); +CREATE INDEX tasks_idx_type_state_target_name ON tasks (target_name, type, state); +CREATE INDEX tasks_idx_type_state_gmt_modified ON tasks (type, state, gmt_modified); +CREATE INDEX tasks_idx_gmt_modified ON tasks (gmt_modified); COMMENT ON TABLE tasks IS 'task info'; COMMENT ON COLUMN tasks.id IS 'primary key'; @@ -559,9 +559,9 @@ CREATE TABLE token_packages ( package_id varchar(24) NOT NULL ); -CREATE UNIQUE INDEX uk_token_id_package_id ON token_packages (token_id, package_id); -CREATE INDEX idx_token_id ON token_packages (token_id); -CREATE INDEX idx_package_id ON token_packages (package_id); +CREATE UNIQUE INDEX token_packages_uk_token_id_package_id ON token_packages (token_id, package_id); +CREATE INDEX token_packages_idx_token_id ON token_packages (token_id); +CREATE INDEX token_packages_idx_package_id ON token_packages (package_id); COMMENT ON TABLE token_packages IS 'token allowed packages'; COMMENT ON COLUMN token_packages.id IS 'primary key'; @@ -578,8 +578,8 @@ CREATE TABLE tokens ( token_id varchar(24) NOT NULL, token_mark varchar(20) NOT NULL, token_key varchar(200) NOT NULL, - is_readonly smallint NOT NULL DEFAULT 0, - is_automation smallint NOT NULL DEFAULT 0, + is_readonly boolean NOT NULL DEFAULT false, + is_automation boolean NOT NULL DEFAULT false, cidr_whitelist json NOT NULL, user_id varchar(24) NOT NULL, name varchar(255) DEFAULT NULL, @@ -590,10 +590,10 @@ CREATE TABLE tokens ( last_used_at timestamp(3) DEFAULT NULL ); -CREATE UNIQUE INDEX uk_token_id ON tokens (token_id); -CREATE UNIQUE INDEX uk_token_key ON tokens (token_key); -CREATE UNIQUE INDEX uk_user_id_name ON tokens (user_id, name); -CREATE INDEX idx_user_id ON tokens (user_id); +CREATE UNIQUE INDEX tokens_uk_token_id ON tokens (token_id); +CREATE UNIQUE INDEX tokens_uk_token_key ON tokens (token_key); +CREATE UNIQUE INDEX tokens_uk_user_id_name ON tokens (user_id, name); +CREATE INDEX tokens_idx_user_id ON tokens (user_id); COMMENT ON TABLE tokens IS 'token info'; COMMENT ON COLUMN tokens.id IS 'primary key'; @@ -630,7 +630,7 @@ CREATE TABLE total ( change_stream_seq varchar(100) DEFAULT NULL ); -CREATE UNIQUE INDEX uk_total_id ON total (total_id); +CREATE UNIQUE INDEX total_uk_total_id ON total (total_id); COMMENT ON TABLE total IS 'total info'; COMMENT ON COLUMN total.id IS 'primary key'; @@ -658,12 +658,12 @@ CREATE TABLE users ( password_salt varchar(100) NOT NULL, password_integrity varchar(512) NOT NULL, ip varchar(100) NOT NULL, - is_private smallint NOT NULL DEFAULT 1, + is_private boolean NOT NULL DEFAULT true, scopes json DEFAULT NULL ); -CREATE UNIQUE INDEX uk_user_id ON users (user_id); -CREATE UNIQUE INDEX uk_name ON users (name); +CREATE UNIQUE INDEX users_uk_user_id ON users (user_id); +CREATE UNIQUE INDEX users_uk_name ON users (name); COMMENT ON TABLE users IS 'user info'; COMMENT ON COLUMN users.id IS 'primary key'; @@ -690,8 +690,8 @@ CREATE TABLE webauthn_credentials ( browser_type varchar(20) DEFAULT NULL ); -CREATE UNIQUE INDEX uk_wanc_id ON webauthn_credentials (wanc_id); -CREATE INDEX idx_user_id ON webauthn_credentials (user_id); +CREATE UNIQUE INDEX webauthn_credentials_uk_wanc_id ON webauthn_credentials (wanc_id); +CREATE INDEX webauthn_credentials_idx_user_id ON webauthn_credentials (user_id); COMMENT ON TABLE webauthn_credentials IS 'webauthn credential info'; COMMENT ON COLUMN webauthn_credentials.id IS 'primary key'; diff --git a/test/TestUtil.ts b/test/TestUtil.ts index 32ae1870..1cbd41ad 100644 --- a/test/TestUtil.ts +++ b/test/TestUtil.ts @@ -5,11 +5,13 @@ import { tmpdir } from 'os'; import { mkdtempSync } from 'fs'; import { Readable } from 'stream'; import mysql from 'mysql2'; +import pg from 'pg'; import path from 'path'; import crypto from 'crypto'; import { cleanUserPrefix, getScopeAndName } from '../app/common/PackageUtil'; import semver from 'semver'; import { PackageJSONType } from '../app/repository/PackageRepository'; +import { database, DATABASE_TYPE } from '../config/database'; type PackageOptions = { name?: string; @@ -44,20 +46,14 @@ export class TestUtil { private static _app; private static ua = 'npm/7.0.0 cnpmcore-unittest/1.0.0'; - static getMySqlConfig() { + static getDatabaseConfig() { return { - host: process.env.MYSQL_HOST || '127.0.0.1', - port: process.env.MYSQL_PORT || 3306, - user: process.env.MYSQL_USER || 'root', - password: process.env.MYSQL_PASSWORD, + ...database, + database: database.name ?? 'cnpmcore_unittest', multipleStatements: true, }; } - static getDatabase() { - return process.env.MYSQL_DATABASE || 'cnpmcore_unittest'; - } - // 不同的 npm 版本 cli 命令不同 // 通过 coffee 运行时获取对应版本号 static async getNpmVersion() { @@ -65,26 +61,17 @@ export class TestUtil { return semver.clean(res.stdout); } - // 获取当前所有 sql 脚本内容 - // 目前统一放置在 ../sql 文件夹中 - // 默认根据版本号排序,确保向后兼容 - static async getTableSqls(): Promise { - const dirents = await fs.readdir(path.join(__dirname, '../sql')); - let versions = dirents.filter(t => path.extname(t) === '.sql').map(t => path.basename(t, '.sql')); - versions = semver.sort(versions); - const sqls = await Promise.all(versions.map(version => { - return fs.readFile(path.join(__dirname, '../sql', `${version}.sql`), 'utf8'); - })); - return sqls.join('\n'); - } - static async query(sql: string): Promise { const conn = this.getConnection(); return new Promise((resolve, reject) => { - conn.query(sql, (err: Error, rows: any[]) => { + conn.query(sql, (err: Error, rows: any) => { if (err) { return reject(err); } + if (rows.rows) { + // pg: { rows } + return resolve(rows.rows); + } return resolve(rows); }); }); @@ -92,11 +79,15 @@ export class TestUtil { static getConnection() { if (!this.connection) { - const config: any = this.getMySqlConfig(); + const config = this.getDatabaseConfig(); if (process.env.CI) { - console.log('[TestUtil] connection to mysql: %j', config); + console.log('[TestUtil] connection to database: %j', config); + } + if (config.type === DATABASE_TYPE.MySQL) { + this.connection = mysql.createConnection(config as any); + } else if (config.type === DATABASE_TYPE.PostgreSQL) { + this.connection = new pg.Client(config as any); } - this.connection = mysql.createConnection(config); this.connection.connect(); } return this.connection; @@ -111,18 +102,26 @@ export class TestUtil { static async getTableNames() { if (!this.tables) { - const database = this.getDatabase(); - const sql = `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '${database}';`; - const rows = await this.query(sql); - this.tables = rows.map(row => row.TABLE_NAME); + const config = this.getDatabaseConfig(); + if (config.type === DATABASE_TYPE.MySQL) { + const sql = ` + SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '${config.database}';`; + const rows = await this.query(sql); + this.tables = rows.map(row => row.TABLE_NAME); + } else if (config.type === DATABASE_TYPE.PostgreSQL) { + const sql = 'SELECT * FROM pg_catalog.pg_tables where schemaname = \'public\';'; + const rows = await this.query(sql); + this.tables = rows.map(row => row.tablename); + } } return this.tables; } static async truncateDatabase() { - const database = this.getDatabase(); const tables = await this.getTableNames(); - await Promise.all(tables.map((table: string) => this.query(`TRUNCATE TABLE ${database}.${table};`))); + await Promise.all(tables.map(async (table: string) => { + await this.query(`TRUNCATE TABLE ${table};`); + })); } static get app() {