Skip to content

Commit

Permalink
Committing migrate-migrations script
Browse files Browse the repository at this point in the history
  • Loading branch information
evert committed Mar 29, 2023
1 parent 3cc7e4d commit 63c4970
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions bin/migrate-migrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* This script was used to migrate the old-style .sql migraitons to knex migrations.
* I suspect we'll never need this again, but I committed it just in case I neede to go back.
*
* Delete this in 2024 if it wasn't used.
*/

const fs = require('fs');

const path = __dirname + '/mysql-schema';

const filenames = fs.readdirSync(path);

for(const filename of filenames) {

const [, num, name] = filename.match(/^([0-9]{3})-(.*)\.sql$/);

const migrationSql = generateKnexMigration(+num, name, fs.readFileSync(path + '/' + filename, 'utf-8'));

fs.writeFileSync(__dirname + `/src/migrations/${num}_${name}.ts`, migrationSql);

}



function generateKnexMigration(num, name, sql) {

const sqlLines = sql.split(';');

let rawLines = sqlLines
.map(line => {
return line.replace('\n', '').trim()
})
.filter(line => {
// Skip empty lines
if (!line.trim().length) return false;
if (line==='COMMIT') return false;
if (line==='START TRANSACTION') return false;
if (line==='SET NAMES utf8mb4') return false;
if (line.startsWith('INSERT INTO changelog ')) return false;
return true;
})
.map(line => {
return ` await knex.raw(${'`' + line + '`'});`;
})
.join('\n') + '\n';


const template = `import { Knex } from 'knex';
export async function up(knex: Knex): Promise<void> {
const result = await knex('changelog').select('id').where({id: ${num}});
if (result.length) {
// Migration has been applied using the old patch system
return;
}
await knex('changelog').insert({
id: ${num},
timestamp: Math.floor(Date.now()/1000)
});
${rawLines}
}
export async function down(knex: Knex): Promise<void> {
throw new Error('This migration doesn\\'t have a "down" script');
}
`;

return template;

}

0 comments on commit 63c4970

Please sign in to comment.