chore: format

This commit is contained in:
xiaojunnuo
2026-05-31 01:41:33 +08:00
parent acd440106b
commit 4b57a0d729
557 changed files with 12530 additions and 14039 deletions
+31 -34
View File
@@ -1,4 +1,4 @@
import fs from 'fs';
import fs from "fs";
/**
* ## sqlite与postgres不同点
* 1.
@@ -27,56 +27,55 @@ import fs from 'fs';
*/
function transformPG() {
// 读取文件列表
const sqliteFiles = fs.readdirSync('./migration/');
const pgFiles = fs.readdirSync('./migration-pg/');
const sqliteFiles = fs.readdirSync("./migration/");
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.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 DEFAULT \(1\)/g, 'boolean DEFAULT (true)');
pgSql = pgSql.replaceAll(/boolean.*NOT NULL DEFAULT \(0\)/g, 'boolean NOT NULL DEFAULT (false)');
pgSql = pgSql.replaceAll(/boolean.*NOT NULL DEFAULT \(1\)/g, 'boolean NOT NULL DEFAULT (true)');
pgSql = pgSql.replaceAll(/integer/g, 'bigint');
pgSql = pgSql.replaceAll(/INTEGER/g, 'bigint');
pgSql = pgSql.replaceAll(/last_insert_rowid\(\)/g, 'LASTVAL()');
const sqliteSql = fs.readFileSync(`./migration/${notFile}`, "utf-8");
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 DEFAULT \(1\)/g, "boolean DEFAULT (true)");
pgSql = pgSql.replaceAll(/boolean.*NOT NULL DEFAULT \(0\)/g, "boolean NOT NULL DEFAULT (false)");
pgSql = pgSql.replaceAll(/boolean.*NOT NULL DEFAULT \(1\)/g, "boolean NOT NULL DEFAULT (true)");
pgSql = pgSql.replaceAll(/integer/g, "bigint");
pgSql = pgSql.replaceAll(/INTEGER/g, "bigint");
pgSql = pgSql.replaceAll(/last_insert_rowid\(\)/g, "LASTVAL()");
fs.writeFileSync(`./migration-pg/${notFile}`, pgSql);
}
if (notFiles.length > 0) {
console.log('sqlite->pg 转换完成');
console.log("sqlite->pg 转换完成");
throw new Error('sqlite->pg 转换完成,有更新,需要测试pg');
throw new Error("sqlite->pg 转换完成,有更新,需要测试pg");
} else {
console.log('sql无需更新');
console.log("sql无需更新");
}
}
function transformMysql() {
// 读取文件列表
const sqliteFiles = fs.readdirSync('./migration/');
const pgFiles = fs.readdirSync('./migration-mysql/');
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');
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(/INTEGER/g, 'bigint');
pgSql = pgSql.replaceAll(/last_insert_rowid\(\)/g, 'LAST_INSERT_ID()');
pgSql = pgSql.replaceAll(/DEFAULT \(([^)]*)\)/g, "DEFAULT $1");
pgSql = pgSql.replaceAll(/integer/g, "bigint");
pgSql = pgSql.replaceAll(/INTEGER/g, "bigint");
pgSql = pgSql.replaceAll(/last_insert_rowid\(\)/g, "LAST_INSERT_ID()");
//text 改成longtext
pgSql = pgSql.replaceAll(/text/g, 'longtext');
pgSql = pgSql.replaceAll(/text/g, "longtext");
//双引号 替换成反引号
pgSql = pgSql.replaceAll(/"/g, '`');
pgSql = pgSql.replaceAll(/"/g, "`");
//提取所有的 create table 的表格name
const tableNames = pgSql.match(/CREATE TABLE `([^`]*)`/g);
if (tableNames && tableNames.length > 0) {
@@ -88,21 +87,19 @@ CREATE TABLE `cd_audit_log`
*/
//提取表名
const tableName = item.match(/`([^`]*)`/)[1];
pgSql += `\nALTER TABLE \`${tableName}\` ENGINE = InnoDB;`
pgSql += `\nALTER TABLE \`${tableName}\` ENGINE = InnoDB;`;
}
}
fs.writeFileSync(`./migration-mysql/${notFile}`, pgSql);
}
if (notFiles.length > 0) {
console.log('sqlite->mysql 转换完成');
console.log("sqlite->mysql 转换完成");
throw new Error('sqlite->mysql 转换完成,有更新,需要测试mysql');
throw new Error("sqlite->mysql 转换完成,有更新,需要测试mysql");
} else {
console.log('sql无需更新');
console.log("sql无需更新");
}
}