Skip to content

Instantly share code, notes, and snippets.

@lffg
Created June 21, 2022 17:14
Show Gist options
  • Select an option

  • Save lffg/d24721cbbeccee86573ad18af6a261a7 to your computer and use it in GitHub Desktop.

Select an option

Save lffg/d24721cbbeccee86573ad18af6a261a7 to your computer and use it in GitHub Desktop.
import { readdirSync } from "node:fs";
import { join } from "node:path";
import { cwd } from "node:process";
const MIGRATION_DIR = join(cwd(), "migrations");
const subDirsPaths = (path) =>
readdirSync(path, { withFileTypes: true })
.filter((dirEntry) => dirEntry.isDirectory())
.map((dirEntry) => join(path, dirEntry.name));
function migValidity(migDirPath) {
const error = (message) => ({ ok: false, migDirPath, message });
const ok = () => ({ ok: true });
const files = readdirSync(migDirPath);
if (!files.includes("up.sql") || !files.includes("down.sql")) {
return error("Missing `up.sql`, `down.sql` or both.");
}
if (files.length !== 2) {
return error("Migration directory should only have two files.");
}
return ok();
}
function main() {
const invalidMigs = subDirsPaths(MIGRATION_DIR)
.flatMap((path) => subDirsPaths(path))
.map(migValidity)
.filter((validity) => !validity.ok);
if (!invalidMigs.length) {
console.log("No problems found.");
return;
}
for (const { migDirPath, message } of invalidMigs) {
console.log(`[${migDirPath}] ${message}`);
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment