You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This may not be the best forum to ask a question on implementation but I couldn't find a better place so I apologize in advance if it's misguided.
I'm looking at using umzug for lazily running migrations once per player. I want to be able to add migrations, and when a player connects to my server, I want to run a migration for them if we haven't already. My initial thought is we need to be able to dynamically modify the params that get passed to the list of migrations as a function that returns the connected user's id and then run the migrations against that (injecting into WHERE clauses), but how can we mark the migration as having ran for that player and never run them again? Do I need a custom storage method to store this info in a separate db table? Any guidance on how to implement this with umzug would be extremely helpful.
Interesting use case! Yes, I'd think you'll need a custom storage. They're not difficult to write. I don't know what your stack is, but here's a working example I threw together with pg-mem as a pseudo-db. You can swap it out with whatever you want though:
constumzug=require("umzug@3.0.0-beta.15")constpgMem=require('pg-mem')constdb=pgMem.newDb()db.public.query('create table migrations(player_id text, name text)')classPlayerStorage{constructor(playerId){this.playerId=playerId}asyncexecuted(){constresult=db.public.query(` select name from migrations where player_id = '${this.playerId}'`)returnresult.rows.map(r=>r.name)}asynclogMigration({name}){db.public.query(`insert into migrations (player_id, name) values ('${this.playerId}', '${name}')`)}asyncunlogMigration({name}){db.public.query(`delete from migrations where player_id = '${this.playerId}' and name = '${name}'`)}}constgetMigrator=playerId=>{returnnewumzug.Umzug({migrations: [{name: 'm1',up: async({ context })=>console.log('m1 running for '+context.playerId),down: async()=>{}},{name: 'm2',up: async({ context })=>console.log('m2 running for '+context.playerId),down: async()=>{}},],context: { playerId },// you could pass `db` in here, assuming your migrations interact with the dbstorage: newPlayerStorage(playerId),logger: console,})}(async()=>{awaitgetMigrator('player1').up()console.log('____')awaitgetMigrator('player1').up()console.log('____')awaitgetMigrator('player2').up()})()
If you run this (say by pasting into https://npm.runkit.com/umzug, where I tried it out), you'll see that no migrations are run for the second time await getMigrator('player1').up() is called.
Activity
mmkal commentedon Sep 20, 2021
Interesting use case! Yes, I'd think you'll need a custom storage. They're not difficult to write. I don't know what your stack is, but here's a working example I threw together with
pg-memas a pseudo-db. You can swap it out with whatever you want though:If you run this (say by pasting into https://npm.runkit.com/umzug, where I tried it out), you'll see that no migrations are run for the second time
await getMigrator('player1').up()is called.