Contents

Blog 12-21-23

Contents

The update from v1.xx to v2.xx for  Umami is not without a bumpy road, because of Prigma Migration.

When the troublemaker started to harass me:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import { PrismaClient } from '@prisma/client/edge'
const prisma = new PrismaClient()

See other ways of importing Prisma Client: http://pris.ly/d/importing-client
$ node scripts/check-db.js
✓ DATABASE_URL is defined.
✓ Database connection successful.
✓ Database version check successful.
✗ Umami v1 tables detected. For how to upgrade from v1 to v2 go to https://umami.is/docs/migrate-v1-v2.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
ERROR: "check-db" exited with 1.
error Command failed with exit code 1.

There are several things you need to resolve when deploying to Vercel:

  1. Modify the script check-db.js in case you encounter problem with Prisma migration process, you’re able to bypass it.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
async function applyMigration() {
  try {
    console.log(execSync('prisma migrate deploy').toString());

    success('Database is up to date.');
  } catch (error) {
    console.error('Failed to apply migration. Attempting to resolve...');

    try {
      console.log(execSync('prisma migrate resolve --applied "20201127134938_my_migration"').toString());
      success('Migration resolved successfully.');
    } catch (resolveError) {
      console.error('Failed to resolve migration:', resolveError.message);
      // Handle the resolve failure as needed
      // Replace "20201127134938_my_migration" with actual failed migration file.
    }
  }
}

This script should work too but I don’t have time to test:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
async function applyMigration() {
  try {
    console.log(execSync('prisma migrate deploy').toString());

    success('Database is up to date.');
  } catch (error) {
    console.error('Failed to apply migration. Attempting to resolve...');

    try {
      // Get the latest migration name
      const migrationList = execSync('prisma migrate list --name-only').toString().split('\n');
      const latestMigration = migrationList[migrationList.length - 2]; // Assumes the last line is the latest migration

      // Resolve the latest migration
      console.log(execSync(`prisma migrate resolve --applied "${latestMigration}"`).toString());
      success('Migration resolved successfully.');
    } catch (resolveError) {
      console.error('Failed to resolve migration:', resolveError.message);
      // Handle the resolve failure as needed
    }
  }
}

Learn more about Failed migration at  Prisma.io


  1. About umami/migrate-v1-v2, Script for migrating data from Umami v1 to v2.

Fork https://github.com/umami-software/migrate-v1-v2.git and modify the code:

Replace this part:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
  (async () => {
      const response = await prompts({
        type: 'select',
        name: 'value',
        choices: [
          { title: 'PostgreSQL', value: 'postgresql' },
          { title: 'MySQL', value: 'mysql' }
        ],
        message: 'Which database are you using?',
      });

with:

1
2
 // Use an environment variable to specify the database type
    const databaseType = process.env.DATABASE_TYPE || 'postgresql';

Then Vercel should deploy it without trouble (hopefully, at least it works for me).