Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async Migrations are not run in sequence #89

Open
vparpoil opened this issue Oct 23, 2024 · 3 comments · May be fixed by #90
Open

Async Migrations are not run in sequence #89

vparpoil opened this issue Oct 23, 2024 · 3 comments · May be fixed by #90

Comments

@vparpoil
Copy link

Let's say you have 2 async migrations that needs to be run.
Migration 2 should wait for Migration 1 to be completed to execute.

Since migrate() function is not async and not awaited, this doesn't work

I will work on a PR for this

@BastienRodz BastienRodz linked a pull request Nov 2, 2024 that will close this issue
@rb365
Copy link

rb365 commented Nov 20, 2024

In case someone needs quick and dirty way to do this while we wait for the PR to get merged and released

(Adjust sleep time as necessary)


const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

const SLEEP_TIME = 2000;

// TODO: remove this hack when they release this PR: https://github.com/percolatestudio/meteor-migrations/pull/90
export const runMigrationSequentially = async (targetVersion: string | number) => {
  if (targetVersion != 'latest' && typeof targetVersion !== 'number') {
    targetVersion = parseInt(targetVersion);
  }
  const currentVersion = await Migrations.getVersion();
  console.log("Current version:", currentVersion, "Target version:", targetVersion)
  const isUp = typeof targetVersion === 'number' 
    ? targetVersion > currentVersion
    : targetVersion === 'latest';
  
  if (isUp) {
    // Get all migration versions between current and target
    const allVersions = Migrations._list.map(m => m.version).sort((a, b) => a - b);
    const versionsToRun = allVersions.filter(v => v > currentVersion);

    console.log("Versions to run:", versionsToRun)
    
    // Run migrations one by one
    for (const version of versionsToRun) {
      console.log(`Running migration ${version}`);
      Migrations.migrateTo(version.toString());
      await sleep(SLEEP_TIME);
    }
  } else {
    // Handle downgrade case
    const targetVersionNum = parseInt(targetVersion as string);
    const versionsToRun = Array.from(
      { length: currentVersion - targetVersionNum },
      (_, i) => currentVersion - i
    );

    console.log("Versions to run:", versionsToRun)
    
    for (const version of versionsToRun) {
      console.log(`Rolling back migration ${version}`);
      Migrations.migrateTo((version - 1).toString());
      await sleep(SLEEP_TIME);
    }
  }
};

@peernohell
Copy link

any news about the pull request to be merged?

@vparpoil
Copy link
Author

linking here the discussion to bring it to core to bring more attention : meteor/meteor#13648

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants