Created
January 5, 2020 19:48
-
-
Save sheminusminus/3469c5c79217116719505033cdbe213b to your computer and use it in GitHub Desktop.
Firebase admin migration example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// =============== | |
// migration function file | |
// =============== | |
// disclaimer: i currently have zero focus on performance for migrations, since they are run one time ever | |
module.exports = async (adminFb) => { | |
try { | |
const directoriesSnap = await adminFb | |
.database() | |
.ref('directories') | |
.once('value'); | |
const directories = directoriesSnap.val(); | |
const ids = Object.keys(directories); | |
await ids.reduce(async (promise, id) => { | |
await promise; | |
await adminFb | |
.database() | |
.ref('directories') | |
.child(id) | |
.child('config') | |
.update({ | |
email: false, | |
sms: false | |
}); | |
}, Promise.resolve()); | |
return true; | |
} catch (err) { | |
console.log('error updating config for directories', err); | |
throw err; | |
} | |
}; | |
// =============== | |
// migration exports example | |
// =============== | |
// v0 | |
const migrationA = require('./migration_function_file'); | |
// v1 | |
const migrationB = require('./migration_function_file_b'); | |
module.exports.versions = { | |
0: [migration], | |
1: [migration1], | |
}; | |
module.exports.latestVersion = 1; | |
// when running migration scripts, i check my database property at `meta/migration_version`, | |
// and run any versions higher than that, up to and including `latestVersion` number, which can differ | |
// by environment depending on when i'm rolling out features in each |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment