Updates
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { execSync, ExecSyncOptions } from "child_process";
|
||||
import os from "os";
|
||||
import connDbHandler from "../db/conn-db-handler";
|
||||
|
||||
export type ExportMariaDBDatabaseParam = {
|
||||
dbFullName: string;
|
||||
targetFilePath: string;
|
||||
mariadbUser?: string;
|
||||
mariadbHost?: string;
|
||||
mariadbPass?: string;
|
||||
};
|
||||
|
||||
export default async function importMariadbDatabase({
|
||||
dbFullName,
|
||||
targetFilePath,
|
||||
mariadbHost,
|
||||
mariadbPass,
|
||||
mariadbUser,
|
||||
}: ExportMariaDBDatabaseParam) {
|
||||
const mysqlPath = os.platform().match(/win/i)
|
||||
? "'" +
|
||||
"C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysql.exe" +
|
||||
"'"
|
||||
: "mysql";
|
||||
|
||||
const finalMariadbUser = mariadbUser || process.env.DSQL_DB_USERNAME;
|
||||
const finalMariadbHost = mariadbHost || process.env.DSQL_DB_HOST;
|
||||
const finalMariadbPass = mariadbPass || process.env.DSQL_DB_PASSWORD;
|
||||
|
||||
await connDbHandler(
|
||||
global.DSQL_DB_CONN,
|
||||
`CREATE DATABASE IF NOT EXISTS ${dbFullName}`
|
||||
);
|
||||
|
||||
const cmd = `${mysqlPath} -u ${finalMariadbUser} -h ${finalMariadbHost} -p${finalMariadbPass} ${dbFullName} < ${targetFilePath}`;
|
||||
|
||||
let execSyncOptions: ExecSyncOptions = {
|
||||
encoding: "utf-8",
|
||||
};
|
||||
|
||||
const importDb = execSync(cmd, execSyncOptions);
|
||||
|
||||
return importDb;
|
||||
}
|
||||
@@ -20,7 +20,7 @@ export default function grabDirNames(param?: Param) {
|
||||
|
||||
const pakageSharedDir = path.join(appDir, `package-shared`);
|
||||
|
||||
const mainDbTypeDefFile = path.join(appDir, `types/dsql.ts`);
|
||||
const mainDbTypeDefFile = path.join(pakageSharedDir, `types/dsql.ts`);
|
||||
const mainShemaJSONFilePath = path.join(schemasDir, `main.json`);
|
||||
const defaultTableFieldsJSONFilePath = path.join(
|
||||
pakageSharedDir,
|
||||
@@ -57,6 +57,11 @@ export default function grabDirNames(param?: Param) {
|
||||
? path.join(userPrivateSQLExportsDir, userPrivateDbExportZipFileName)
|
||||
: undefined;
|
||||
|
||||
const userPrivateDbImportZipFileName = `db-export.zip`;
|
||||
const userPrivateDbImportZipFilePath = userPrivateSQLExportsDir
|
||||
? path.join(userPrivateSQLExportsDir, userPrivateDbImportZipFileName)
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
schemasDir,
|
||||
userDirPath,
|
||||
@@ -73,5 +78,7 @@ export default function grabDirNames(param?: Param) {
|
||||
userPrivateTempJSONSchemaFilePath,
|
||||
userPrivateDbExportZipFileName,
|
||||
userPrivateDbExportZipFilePath,
|
||||
userPrivateDbImportZipFileName,
|
||||
userPrivateDbImportZipFilePath,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
type Param = {
|
||||
str: string;
|
||||
userId: string | number;
|
||||
};
|
||||
export default function replaceDatasquirelDbName({
|
||||
str,
|
||||
userId,
|
||||
}: Param): string {
|
||||
const dbNamePrefix = process.env.DSQL_USER_DB_PREFIX;
|
||||
const userNameRegex = new RegExp(`${dbNamePrefix}\\d+_`, "g");
|
||||
const newPrefix = `${dbNamePrefix}${userId}_`;
|
||||
|
||||
return str.replace(userNameRegex, newPrefix);
|
||||
}
|
||||
Reference in New Issue
Block a user