feat: dialect version accessors (#1502)

This commit is contained in:
chenos 2023-02-26 11:58:45 +08:00 committed by GitHub
parent dfbdbc741d
commit 750081e6aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 29 deletions

View File

@ -43,9 +43,8 @@ jobs:
node-version: ${{ matrix.node_version }}
cache: 'yarn'
- run: yarn install
# - run: yarn build
- name: Test with Sqlite
run: yarn test
run: yarn nocobase install -f && yarn test
env:
DB_DIALECT: sqlite
DB_STORAGE: /tmp/db.sqlite
@ -84,7 +83,7 @@ jobs:
- run: yarn install
# - run: yarn build
- name: Test with postgres
run: yarn test
run: yarn nocobase install -f && yarn test
env:
DB_DIALECT: postgres
DB_HOST: postgres
@ -119,7 +118,7 @@ jobs:
- run: yarn install
# - run: yarn build
- name: Test with MySQL
run: yarn test
run: yarn nocobase install -f && yarn test
env:
DB_DIALECT: mysql
DB_HOST: mysql

View File

@ -15,7 +15,7 @@ import {
Sequelize,
SyncOptions,
Transactionable,
Utils,
Utils
} from 'sequelize';
import { SequelizeStorage, Umzug } from 'umzug';
import { Collection, CollectionOptions, RepositoryType } from './collection';
@ -58,7 +58,7 @@ import {
SyncListener,
UpdateListener,
UpdateWithAssociationsListener,
ValidateListener,
ValidateListener
} from './types';
import { patchSequelizeQueryInterface, snakeCase } from './utils';
@ -105,6 +105,30 @@ export type AddMigrationsOptions = {
type OperatorFunc = (value: any, ctx?: RegisterOperatorsContext) => any;
export const DialectVersionAccessors = {
sqlite: {
sql: 'select sqlite_version() as version',
get: (v: string) => v,
},
mysql: {
sql: 'select version() as version',
get: (v: string) => {
if (v.toLowerCase().includes('mariadb')) {
return '';
}
const m = /([\d+\.]+)/.exec(v);
return m[0];
},
},
postgres: {
sql: 'select version() as version',
get: (v: string) => {
const m = /([\d+\.]+)/.exec(v);
return semver.minVersion(m[0]).version;
},
},
};
class DatabaseVersion {
db: Database;
@ -113,33 +137,14 @@ class DatabaseVersion {
}
async satisfies(versions) {
const dialects = {
sqlite: {
sql: 'select sqlite_version() as version',
get: (v) => v,
},
mysql: {
sql: 'select version() as version',
get: (v) => {
const m = /([\d+\.]+)/.exec(v);
return m[0];
},
},
postgres: {
sql: 'select version() as version',
get: (v) => {
const m = /([\d+\.]+)/.exec(v);
return semver.minVersion(m[0]).version;
},
},
};
for (const dialect of Object.keys(dialects)) {
const accessors = DialectVersionAccessors;
for (const dialect of Object.keys(accessors)) {
if (this.db.inDialect(dialect)) {
if (!versions?.[dialect]) {
return false;
}
const [result] = (await this.db.sequelize.query(dialects[dialect].sql)) as any;
return semver.satisfies(dialects[dialect].get(result?.[0]?.version), versions[dialect]);
const [result] = (await this.db.sequelize.query(accessors[dialect].sql)) as any;
return semver.satisfies(accessors[dialect].get(result?.[0]?.version), versions[dialect]);
}
}
return false;