You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
909 B

import { readdir, readFile } from 'node:fs/promises'
import path from 'path'
/**
* runMigrationSqlite
*
* @param {string} path
* @param {object} db
*
*/
async function runMigrationSqlite(filePath, db) {
const SQL_EXTENSION = '.sql'
// getting files
const files = await readdir(filePath)
for (const fileName of files) {
// skip if extension not sql
if (path.extname(fileName).toLowerCase() !== SQL_EXTENSION) {
continue
}
const sql = await readFile(filePath + '/' + fileName, 'utf8')
try {
if (!sql.trim()) {
throw new Error(fileName + ' is empty!')
}
db.prepare(sql.trim()).run()
console.log('migrated ' + path.basename(fileName, SQL_EXTENSION))
} catch(error) {
console.log(error)
}
}
}
export default runMigrationSqlite