0

umami1.0更新到2.0

 1 year ago
source link: https://www.bboy.app/2023/04/18/umami1.0%E6%9B%B4%E6%96%B0%E5%88%B02.0/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

umami1.0更新到2.0

April 18, 2023

20230418-2.webp

今天umami发布了2.0的更新,所以就想着更新下,坑还是有一点的

https://umami.is/docs/migrate-v1-v2

首先看下官方文档

1.0到2.0的变化还是很大的,首先表结构变了,然后ui也变了,api接口也变了,就连追踪脚本也从 umami.js改为script.js

操作首先要备份你的数据库,这个很重要,一定要备份,不然出错了绝对JJ

不推荐使用

cd umami
yarn add @umami/migrate-v1-v2
npx @umami/migrate-v1-v2

这个方法去更新,因为会有问题,详细可以看

https://github.com/umami-software/umami/issues/1888

我使用的是mysql的数据库,之后就是运行一个node容器,当然在系统里面直接安装node也可以

docker run -it node bash

之后clone下面这个项目

git clone https://github.com/umami-software/migrate-v1-v2.git

cd migrate-v1-v2

之后创建.env文件

DATABASE_URL=mysql://root:[email protected]:3306/umami

yarn install

yarn build

yarn start

yarn start的时候一直提示我

✗ Database v1 tables are not ready for migration.

我看了一下源码

async function checkV1TablesReady() {
  try {
    await prisma.$queryRaw`select * from v1_account limit 1`;

    success('Database v1 tables ready for migration.');
  } catch (e) {
    throw new Error('Database v1 tables are not ready for migration.');
  }
}

await prisma.$queryRawselect * from v1_account limit 1;

这里执行出问题了

先不看代码逻辑,直接看数据库,的确是没有的,应该之前有执行备份的步骤,然后失败了,看了下v1_account这个表在这里生成的

async function renameV1Tables(databaseType) {
  try {
    // rename tables
    if (databaseType === 'postgresql') {
      await prisma.$transaction([
        prisma.$executeRaw`ALTER TABLE IF EXISTS _prisma_migrations RENAME TO v1_prisma_migrations;`,
        prisma.$executeRaw`ALTER TABLE IF EXISTS account RENAME TO v1_account;`,
        prisma.$executeRaw`ALTER TABLE IF EXISTS event RENAME TO v1_event;`,
        prisma.$executeRaw`ALTER TABLE IF EXISTS event_data RENAME TO v1_event_data;`,
        prisma.$executeRaw`ALTER TABLE IF EXISTS pageview RENAME TO v1_pageview;`,
        prisma.$executeRaw`ALTER TABLE IF EXISTS session RENAME TO v1_session;`,
        prisma.$executeRaw`ALTER TABLE IF EXISTS website RENAME TO v1_website;`,
      ]);
    } else {
        await executeRawIgnore('RENAME TABLE _prisma_migrations TO v1_prisma_migrations;');
        await executeRawIgnore('RENAME TABLE account TO v1_account;');
        await executeRawIgnore('RENAME TABLE event TO v1_event;');
        await executeRawIgnore('RENAME TABLE event_data TO v1_event_data;');
        await executeRawIgnore('RENAME TABLE pageview TO v1_pageview;');
        await executeRawIgnore('RENAME TABLE session TO v1_session;');
        await executeRawIgnore('RENAME TABLE website TO v1_website;');
    }

那就手动执行了一下,可能是因为我使用的是mariadb的问题

mariadb:10.3.27

提示语法错误,用下面的命令去重新命名的就好

RENAME TABLE account TO v1_account;
RENAME TABLE event TO v1_event;
RENAME TABLE event_data TO v1_event_data;
RENAME TABLE pageview TO v1_pageview;
RENAME TABLE session TO v1_session;
RENAME TABLE website TO v1_website;

之后继续执行

yarn start

root@01edbeefcd1f:/migrate-v1-v2# yarn start
yarn run v1.22.19
$ node index.js
✓ DATABASE_URL is defined.
✓ Database connection successful.
✓ Database v1 tables ready for migration.
Database v2 tables not found.
Adding v2 tables...
✓ Ran sql file /prisma/migrations/01_init/migration.sql.
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": MySQL database "umami" at "10.0.0.61:3306"
Migration 01_init marked as applied.

✓ Database is ready for migration.
✓ Starting v2 data migration. Please do no cancel this process, it may take a while.
✓ Ran sql file /prisma/data-migration-v2.sql.
✓ Data migration from V1 to V2 tables completed.
✔ Do you want to delete V1 database tables? (Y/N) … n
✓ Migration successfully completed.
Done in 103.83s.

更新完成之后尝试登录,发现报错

这个应该是一个bug,其实1.0的时候也是有的,详细的可以看

https://github.com/umami-software/umami/issues/1892

最后不要忘记把站点中所有追踪脚本名字umami.js改为script.js

最终的效果可以看

https://umami.bboysoul.cn/share/o3KpnfdB/bboy.app

欢迎关注我的博客www.bboy.app

Have Fun


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK