mirror of
https://github.com/certd/certd.git
synced 2026-04-05 07:20:56 +08:00
perf: 支持mysql
This commit is contained in:
@@ -25,21 +25,21 @@ import fs from 'fs';
|
||||
* sqlite: integer
|
||||
* postgresql: bigint
|
||||
*/
|
||||
function transform() {
|
||||
function transformPG() {
|
||||
// 读取文件列表
|
||||
const sqliteFiles = fs.readdirSync('./migration/');
|
||||
const pgFiles = fs.readdirSync('./migration-pg');
|
||||
const pgFiles = fs.readdirSync('./migration-pg/');
|
||||
//找出pg里面没有的文件
|
||||
const notFiles = sqliteFiles.filter(file => !pgFiles.includes(file));
|
||||
for (const notFile of notFiles) {
|
||||
//开始转换
|
||||
const sqliteSql = fs.readFileSync(`./migration/${notFile}`, 'utf-8');
|
||||
let pgSql = sqliteSql.replace(/AUTOINCREMENT/g, 'GENERATED BY DEFAULT AS IDENTITY');
|
||||
pgSql = pgSql.replace(/datetime/g, 'timestamp');
|
||||
pgSql = pgSql.replace(/boolean DEFAULT \(0\)/g, 'boolean DEFAULT (false)');
|
||||
pgSql = pgSql.replace(/boolean.*NOT NULL DEFAULT \(0\)/g, 'boolean NOT NULL DEFAULT (false)');
|
||||
pgSql = pgSql.replace(/integer/g, 'bigint');
|
||||
pgSql = pgSql.replace(/last_insert_rowid\(\)/g, 'LASTVAL()');
|
||||
let pgSql = sqliteSql.replaceAll(/AUTOINCREMENT/g, 'GENERATED BY DEFAULT AS IDENTITY');
|
||||
pgSql = pgSql.replaceAll(/datetime/g, 'timestamp');
|
||||
pgSql = pgSql.replaceAll(/boolean DEFAULT \(0\)/g, 'boolean DEFAULT (false)');
|
||||
pgSql = pgSql.replaceAll(/boolean.*NOT NULL DEFAULT \(0\)/g, 'boolean NOT NULL DEFAULT (false)');
|
||||
pgSql = pgSql.replaceAll(/integer/g, 'bigint');
|
||||
pgSql = pgSql.replaceAll(/last_insert_rowid\(\)/g, 'LASTVAL()');
|
||||
fs.writeFileSync(`./migration-pg/${notFile}`, pgSql);
|
||||
}
|
||||
|
||||
@@ -51,4 +51,37 @@ function transform() {
|
||||
console.log('sql无需更新');
|
||||
}
|
||||
}
|
||||
transform();
|
||||
|
||||
function transformMysql() {
|
||||
// 读取文件列表
|
||||
const sqliteFiles = fs.readdirSync('./migration/');
|
||||
const pgFiles = fs.readdirSync('./migration-mysql/');
|
||||
//找出pg里面没有的文件
|
||||
const notFiles = sqliteFiles.filter(file => !pgFiles.includes(file));
|
||||
for (const notFile of notFiles) {
|
||||
//开始转换
|
||||
const sqliteSql = fs.readFileSync(`./migration/${notFile}`, 'utf-8');
|
||||
let pgSql = sqliteSql.replaceAll(/AUTOINCREMENT/g, 'AUTO_INCREMENT');
|
||||
pgSql = pgSql.replaceAll(/datetime/g, 'timestamp');
|
||||
//DEFAULT (xxx) 替换成 DEFAULT xxx
|
||||
pgSql = pgSql.replaceAll(/DEFAULT \(([^)]*)\)/g, 'DEFAULT $1');
|
||||
pgSql = pgSql.replaceAll(/integer/g, 'bigint');
|
||||
pgSql = pgSql.replaceAll(/last_insert_rowid\(\)/g, 'LAST_INSERT_ID()');
|
||||
|
||||
//双引号 替换成反引号
|
||||
pgSql = pgSql.replaceAll(/"/g, '`');
|
||||
|
||||
fs.writeFileSync(`./migration-mysql/${notFile}`, pgSql);
|
||||
}
|
||||
|
||||
if (notFiles.length > 0) {
|
||||
console.log('sqlite->mysql 转换完成');
|
||||
|
||||
throw new Error('sqlite->mysql 转换完成,有更新,需要测试mysql');
|
||||
} else {
|
||||
console.log('sql无需更新');
|
||||
}
|
||||
}
|
||||
|
||||
transformPG();
|
||||
transformMysql();
|
||||
|
||||
Reference in New Issue
Block a user