This commit is contained in:
2026-07-30 07:19:07 +01:00
parent 246c42a214
commit 0420d50f15
43 changed files with 1942 additions and 219 deletions
+18 -8
View File
@@ -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) {
+15 -7
View File
@@ -6,9 +6,10 @@ import { select } from "@inquirer/prompts";
import grabDBDir from "../utils/grab-db-dir";
import grabSortedExports from "../utils/grab-sorted-exports";
import grabBackupData from "../utils/grab-backup-data";
import { AppData } from "../data/app-data";
import { restoreDatabase } from "../utils/mariadb-dump-restore";
import { isArchivePath, isSqlPath, readExportArchive, } from "../utils/export-archive";
import { resolveDataFile } from "../utils/resolve-and-load-data-file";
import { AppData } from "../data/app-data";
function formatChoice(name, index) {
const { backup_date } = grabBackupData({ backup_name: name });
const time = Number.isNaN(backup_date.getTime())
@@ -18,10 +19,10 @@ function formatChoice(name, index) {
}
export default function () {
return new Command("import")
.description("Import an SQL dump, or a full export archive (SQL + schema.ts)")
.description("Import an SQL dump, or a full export archive (SQL + schema)")
.argument("[file]", "Path to .sql file or export archive (.tar.gz / .tar / .zip)")
.option("--sql-only", "When importing an archive, restore SQL only (skip writing schema.ts)")
.option("--schema-only", "When importing an archive, write schema.ts only (skip SQL restore)")
.option("--sql-only", "When importing an archive, restore SQL only (skip writing schema)")
.option("--schema-only", "When importing an archive, write schema only (skip SQL restore)")
.action(async (fileArg, opts) => {
console.log(`Importing database ...`);
const config = global.CONFIG;
@@ -65,14 +66,21 @@ export default function () {
console.error(chalk.red(`Cannot combine --sql-only and --schema-only.`));
process.exit(1);
}
const { sql, schemaTs } = await readExportArchive(filePath);
const { sql, schema, schemaFileName } = await readExportArchive(filePath);
if (!opts.schemaOnly) {
await restoreDatabase(config, sql);
console.log(chalk.green(`SQL restored from archive`));
}
if (!opts.sqlOnly) {
const schemaPath = path.join(db_dir, AppData.DbSchemaFileName);
fs.writeFileSync(schemaPath, schemaTs, "utf-8");
const existing = resolveDataFile(db_dir, AppData.DbSchemaFileName);
const schemaPath = path.join(db_dir, schemaFileName);
// Remove a differently-named schema so only one format remains
if (existing &&
path.resolve(existing.path) !== path.resolve(schemaPath)) {
fs.unlinkSync(existing.path);
console.log(chalk.dim(`Removed previous schema file: ${existing.basename}`));
}
fs.writeFileSync(schemaPath, schema, "utf-8");
console.log(chalk.green(`Schema written → ${schemaPath}`));
}
console.log(`${chalk.bold(chalk.green(`DB Import Success!`))}${filePath}`);
Vendored Regular → Executable
View File
+1 -1
View File
@@ -14,7 +14,7 @@ export default function () {
const { ROOT_DIR } = grabDirNames();
const finaldbSchema = appendDefaultFieldsToDbSchema({ dbSchema });
if (!config.typedef_file_path) {
console.error(`\`typedef_file_path\` is required in bun-mariadb.config.ts to generate types.`);
console.error(`\`typedef_file_path\` is required in bun-mariadb.config to generate types.`);
process.exit(1);
}
const out_file = path.resolve(ROOT_DIR, config.typedef_file_path);