Skip to content
Merged

Dev #52

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 53 additions & 59 deletions bin/database-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,18 @@ import readline from "node:readline/promises";
import { DatabaseSync } from "node:sqlite";
import fs from "fs-extra";

// Build the path to the schema SQL file
const schema = path.join(import.meta.dirname, "../src/database/schema.sql");
const seeder = path.join(import.meta.dirname, "../src/database/seeder.sql");
const sqlite = path.join(
import.meta.dirname,
"../src/database/data/database.sqlite",
);

// Setup readline for interactive confirmation.
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

// Asks the user for confirmation.
async function confirm(question: string): Promise<boolean> {
const answer = await rl.question(`${question} (y/N) `);
return answer.toLowerCase() === "y";
}
export async function main(
argv: string[] = process.argv,
rootDirOverride?: string,
) {
const rootDir = rootDirOverride ?? path.join(import.meta.dirname, "..");

let database: DatabaseSync | null = null;
// Build the paths to the schema, seeder and database files
const schema = path.join(rootDir, "src/database/schema.sql");
const seeder = path.join(rootDir, "src/database/seeder.sql");
const sqlite = path.join(rootDir, "src/database/data/database.sqlite");

async function main() {
const args = process.argv.slice(2);
const args = argv.slice(2);

const useSeeder = args.includes("--use-seeder");
const noInteraction =
Expand All @@ -38,67 +26,73 @@ async function main() {
];

if (args.length !== expectedArgs.length) {
console.error(
throw new Error(
"Usage: npm run database:sync [-- --use-seeder] [--no-interaction|-n]",
);
process.exit(1);
}

console.info(
`This script will drop existing '${path.normalize(sqlite)}' to create a new one.`,
);

const proceed = async () => {
if (noInteraction) {
console.info(
"Running in non-interactive mode. Proceeding automatically.",
);
return true;
} else {
return await confirm(
"Are you sure you want to continue? This action cannot be undone.",
if (!noInteraction) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

try {
const answer = await rl.question(
"Are you sure you want to continue? This action cannot be undone. (y/N) ",
);
}
};

if (!(await proceed())) {
console.info("\nSync operation cancelled.");
return;
if (answer.toLowerCase() !== "y") {
console.info("\nSync operation cancelled.");
return;
}
} finally {
rl.close();
}
}

// Delete the existing database file if it exists
await fs.remove(sqlite);

// Ensure the parent directory exists
await fs.ensureDir(path.dirname(sqlite));

// Create a new database with the specified name
database = new DatabaseSync(sqlite);
const database = new DatabaseSync(sqlite);

// Read the SQL statements from the schema file
const sql = await fs.readFile(schema, "utf8");
try {
// Read the SQL statements from the schema file
const sql = await fs.readFile(schema, "utf8");

// Execute the SQL statements to update the database schema
database.exec(sql);
// Execute the SQL statements to update the database schema
database.exec(sql);

console.info(
`\nDatabase '${path.normalize(sqlite)}' in sync with '${path.normalize(schema)}' 🆙`,
);
console.info(
`\nDatabase '${path.normalize(sqlite)}' in sync with '${path.normalize(schema)}' 🆙`,
);

if (useSeeder) {
// Read the SQL statements from the seeder file
const sql = await fs.readFile(seeder, "utf8");
if (useSeeder) {
// Read the SQL statements from the seeder file
const sql = await fs.readFile(seeder, "utf8");

// Execute the SQL statements to seed the database
database.exec(sql);
// Execute the SQL statements to seed the database
database.exec(sql);

console.info(`\nSeeded using '${path.normalize(seeder)}' 🌱`);
console.info(`\nSeeded using '${path.normalize(seeder)}' 🌱`);
}
} finally {
database.close();
}
}

main()
.catch((err) => {
console.error("An unexpected error occurred:", err);
/* v8 ignore next 6 */
if (process.env.NODE_ENV !== "test") {
main().catch((err) => {
console.error(err instanceof Error ? err.message : err);
process.exit(1);
})
.finally(() => {
rl.close();
database?.close();
});
}
Loading
Loading