Updates
This commit is contained in:
Vendored
+18
-8
@@ -3,16 +3,17 @@ import path from "path";
|
||||
import fs from "fs";
|
||||
import chalk from "chalk";
|
||||
import grabDBDir from "../utils/grab-db-dir";
|
||||
import { AppData } from "../data/app-data";
|
||||
import { dumpDatabase } from "../utils/mariadb-dump-restore";
|
||||
import { writeExportArchive } from "../utils/export-archive";
|
||||
import trimExports from "../utils/trim-exports";
|
||||
import { resolveDataFile, supportedDataFileNames, } from "../utils/resolve-and-load-data-file";
|
||||
import { AppData } from "../data/app-data";
|
||||
function defaultExportFileName(dbName, format) {
|
||||
return `${dbName}-${Date.now()}.${format}`;
|
||||
}
|
||||
export default function () {
|
||||
return new Command("export")
|
||||
.description("Export database SQL dump + schema.ts into a portable archive")
|
||||
.description("Export database SQL dump + schema into a portable archive")
|
||||
.option("-o, --output <path>", "Output archive path (.tar.gz or .zip). Defaults to db exports dir")
|
||||
.option("-f, --format <format>", "Archive format when --output is omitted: tar.gz | zip", "tar.gz")
|
||||
.action(async (opts) => {
|
||||
@@ -22,9 +23,14 @@ export default function () {
|
||||
if (!fs.existsSync(export_dir)) {
|
||||
fs.mkdirSync(export_dir, { recursive: true });
|
||||
}
|
||||
const schemaPath = path.join(db_dir, AppData.DbSchemaFileName);
|
||||
if (!fs.existsSync(schemaPath)) {
|
||||
console.error(chalk.red(`Schema file not found: ${schemaPath}`));
|
||||
const schemaResolved = (global.SCHEMA_FILE_PATH &&
|
||||
fs.existsSync(global.SCHEMA_FILE_PATH) && {
|
||||
path: global.SCHEMA_FILE_PATH,
|
||||
basename: path.basename(global.SCHEMA_FILE_PATH),
|
||||
}) ||
|
||||
resolveDataFile(db_dir, AppData.DbSchemaFileName);
|
||||
if (!schemaResolved) {
|
||||
console.error(chalk.red(`Schema file not found in \`${db_dir}\` (${supportedDataFileNames(AppData.DbSchemaFileName)})`));
|
||||
process.exit(1);
|
||||
}
|
||||
const formatRaw = String(opts.format || "tar.gz").toLowerCase();
|
||||
@@ -42,16 +48,20 @@ export default function () {
|
||||
}
|
||||
try {
|
||||
const sql = await dumpDatabase(config);
|
||||
const schemaTs = fs.readFileSync(schemaPath, "utf-8");
|
||||
const schema = fs.readFileSync(schemaResolved.path, "utf-8");
|
||||
await writeExportArchive({
|
||||
contents: { sql, schemaTs },
|
||||
contents: {
|
||||
sql,
|
||||
schema,
|
||||
schemaFileName: schemaResolved.basename,
|
||||
},
|
||||
outPath,
|
||||
});
|
||||
if (path.dirname(outPath) === export_dir) {
|
||||
trimExports({ config });
|
||||
}
|
||||
console.log(`${chalk.bold(chalk.green(`DB Export Success!`))} → ${outPath}`);
|
||||
console.log(chalk.dim(`Contains: dump.sql + ${AppData.DbSchemaFileName}`));
|
||||
console.log(chalk.dim(`Contains: dump.sql + ${schemaResolved.basename}`));
|
||||
process.exit(0);
|
||||
}
|
||||
catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user