umami-software / umami

Umami is a simple, fast, privacy-focused alternative to Google Analytics.
https://umami.is
MIT License
22.53k stars 4.2k forks source link

Migration partial failure - v1.37.0 #1418

Closed razinj closed 2 years ago

razinj commented 2 years ago

After updating the Docker container with:

docker-compose pull && docker-compose up -d

The dashboard seems to be working fine, but once I clicked on realtime page nothing there, looking at the network tab I see a 500 HTTP error:

URL: https://sub.domain.com/api/realtime/init

Error: 500 Internal Server Error

Checking the logs of the containers:

Bumami-db | 2022-08-11 19:41:48.261 UTC [42] ERROR:  column event.event_name does not exist at character 154
umami-db | 2022-08-11 19:41:48.261 UTC [42] HINT:  Perhaps you meant to reference the column "event.event_type" or the column "event.event_value".
umami-db | 2022-08-11 19:41:48.261 UTC [42] STATEMENT:  SELECT "public"."event"."event_id", "public"."event"."website_id", "public"."event"."session_id", "public"."event"."created_at", "public"."event"."url", "public"."event"."event_name" FROM "public"."event" WHERE (("public"."event"."event_id") IN (SELECT "t0"."event_id" FROM "public"."event" AS "t0" INNER JOIN "public"."website" AS "j0" ON ("j0"."website_id") = ("t0"."website_id") WHERE ("j0"."website_id" IN ($1,$2,$3,$4) AND "t0"."event_id" IS NOT NULL)) AND "public"."event"."created_at" >= $5) OFFSET $6
umami-app | PrismaClientKnownRequestError: The column `event.event_name` does not exist in the current database.
umami-app |     at RequestHandler.handleRequestError (/app/node_modules/@prisma/client/runtime/index.js:28658:13)
umami-app |     at RequestHandler.request (/app/node_modules/@prisma/client/runtime/index.js:28640:12)
umami-app |     at async consumer (/app/node_modules/@prisma/client/runtime/index.js:29618:18)
umami-app |     at async PrismaClient._request (/app/node_modules/@prisma/client/runtime/index.js:29639:16)
umami-app |     at async Promise.all (index 2)
umami-app |     at async getRealtimeData (/app/.next/server/chunks/1573.js:760:43)
umami-app |     at async __WEBPACK_DEFAULT_EXPORT__ (/app/.next/server/pages/api/realtime/init.js:143:22)
umami-app |     at async Object.apiResolver (/app/node_modules/next/dist/server/api-utils/node.js:184:9)
umami-app |     at async NextNodeServer.runApi (/app/node_modules/next/dist/server/next-server.js:381:9)
umami-app |     at async Object.fn (/app/node_modules/next/dist/server/base-server.js:491:37) {
umami-app |   code: 'P2022',
umami-app |   clientVersion: '4.1.1',
umami-app |   meta: { column: 'event.event_name' }
umami-app | }

Do you have any idea on how to solve this?

mikecao commented 2 years ago

It looks like the DB migration didn't run. Can you run yarn check-db and try again?

razinj commented 2 years ago

All checked, weird:

/app $ yarn check-db
yarn run v1.22.19
$ node scripts/check-db.js
✓ DATABASE_URL is defined.
✓ Database connection successful.
✓ Database tables found.
✓ Database is up to date.
Done in 2.57s.
/app $
razinj commented 2 years ago

As a note, with this update, I changed for the first time the collect API and the script name if it helps.

I just recreated the containers and the logs looks good:

umami-db |
umami-db | PostgreSQL Database directory appears to contain a database; Skipping initialization
umami-db |
umami-db | 2022-08-11 20:07:11.847 UTC [1] LOG:  starting PostgreSQL 12.11 on x86_64-pc-linux-musl, compiled by gcc (Alpine 11.2.1_git20220219) 11.2.1 20220219, 64-bit
umami-db | 2022-08-11 20:07:11.848 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
umami-db | 2022-08-11 20:07:11.849 UTC [1] LOG:  listening on IPv6 address "::", port 5432
umami-db | 2022-08-11 20:07:11.852 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
umami-db | 2022-08-11 20:07:11.888 UTC [21] LOG:  database system was shut down at 2022-08-11 20:07:10 UTC
umami-db | 2022-08-11 20:07:11.896 UTC [1] LOG:  database system is ready to accept connections
umami-db | 2022-08-11 20:07:16.194 UTC [30] LOG:  could not receive data from client: Connection reset by peer
umami-app | yarn run v1.22.19
umami-app | $ npm-run-all check-db update-tracker start-server
umami-app | $ node scripts/check-db.js
umami-app | ✓ DATABASE_URL is defined.
umami-app | ✓ Database connection successful.
umami-app | ✓ Database tables found.
umami-app | ✓ Database is up to date.
umami-app | $ node scripts/update-tracker.js
umami-app | Updated tracker endpoint: /api/rj-collect.
umami-app | $ node server.js
umami-app | Listening on port 3000
razinj commented 2 years ago

You are right, the migration failed:

I ran the yarn prisma migrate status and the output is:

/app $ yarn prisma migrate status
yarn run v1.22.19
$ /app/node_modules/.bin/prisma migrate status
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "umami_db", schema "public" at "db:5432"

2 migrations found in prisma/migrations

Following migration have failed:
02_add_event_data

During development if the failed migration(s) have not been deployed to a production database you can then fix the migration(s) and run yarn prisma migrate dev.

The failed migration(s) can be marked as rolled back or applied:

- If you rolled back the migration(s) manually:
yarn prisma migrate resolve --rolled-back "02_add_event_data"

- If you fixed the database manually (hotfix):
yarn prisma migrate resolve --applied "02_add_event_data"

Read more about how to resolve migration issues in a production database:
https://pris.ly/d/migrate-resolve
┌─────────────────────────────────────────────────────────┐
│  Update available 4.2.0 -> 4.2.1                        │
│  Run the following to update                            │
│    yarn add --dev prisma@latest                         │
│    yarn add @prisma/client@latest                       │
└─────────────────────────────────────────────────────────┘
Done in 1.42s.
/app $

Do you know how can I fix this?

Kaishiyoku commented 2 years ago

Same issue here. Migration 02_add_event_data fails.

mikecao commented 2 years ago

We've updated master with some migration script fixes. Can you try pulling the latest?

razinj commented 2 years ago

I'm using the latest tag, but it seems to not get it.

Image used: ghcr.io/umami-software/umami:postgresql-latest

Did I not understand you?

briancao commented 2 years ago

1.37.0 has had it's migration script updated. Users will need to pull the latest version, then run the following commands to resolve the 02_migration failure.

yarn prisma migrate resolve --rolled-back "02_add_event_data"
yarn check-db
razinj commented 2 years ago

I did get the latest image and ran the commands, but still the same issue.

Running yarn prisma migrate status outputs:

/app $ yarn prisma migrate status
yarn run v1.22.19
$ /app/node_modules/.bin/prisma migrate status
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "umami_db", schema "public" at "db:5432"

2 migrations found in prisma/migrations

Database schema is up to date!
Done in 1.28s.

But once I get into the realtime tab the error is displayed again:

umami-db | 2022-08-11 23:29:52.322 UTC [32] ERROR:  column event.event_name does not exist at character 154
umami-db | 2022-08-11 23:29:52.322 UTC [32] HINT:  Perhaps you meant to reference the column "event.event_type" or the column "event.event_value".
umami-db | 2022-08-11 23:29:52.322 UTC [32] STATEMENT:  SELECT "public"."event"."event_id", "public"."event"."website_id", "public"."event"."session_id", "public"."event"."created_at", "public"."event"."url", "public"."event"."event_name" FROM "public"."event" WHERE (("public"."event"."event_id") IN (SELECT "t0"."event_id" FROM "public"."event" AS "t0" INNER JOIN "public"."website" AS "j0" ON ("j0"."website_id") = ("t0"."website_id") WHERE ("j0"."website_id" IN ($1,$2,$3,$4) AND "t0"."event_id" IS NOT NULL)) AND "public"."event"."created_at" >= $5) OFFSET $6
umami-app | PrismaClientKnownRequestError: The column `event.event_name` does not exist in the current database.
umami-app |     at RequestHandler.handleRequestError (/app/node_modules/@prisma/client/runtime/index.js:28658:13)
umami-app |     at RequestHandler.request (/app/node_modules/@prisma/client/runtime/index.js:28640:12)
umami-app |     at async consumer (/app/node_modules/@prisma/client/runtime/index.js:29618:18)
umami-app |     at async PrismaClient._request (/app/node_modules/@prisma/client/runtime/index.js:29639:16)
umami-app |     at async Promise.all (index 2)
umami-app |     at async getRealtimeData (/app/.next/server/chunks/1573.js:760:43)
umami-app |     at async __WEBPACK_DEFAULT_EXPORT__ (/app/.next/server/pages/api/realtime/init.js:143:22)
umami-app |     at async Object.apiResolver (/app/node_modules/next/dist/server/api-utils/node.js:184:9)
umami-app |     at async NextNodeServer.runApi (/app/node_modules/next/dist/server/next-server.js:381:9)
umami-app |     at async Object.fn (/app/node_modules/next/dist/server/base-server.js:491:37) {
umami-app |   code: 'P2022',
umami-app |   clientVersion: '4.1.1',
umami-app |   meta: { column: 'event.event_name' }
umami-app | }

Maybe it's not applied? Can we re-run the migration script?

Edit: I can confirm the migration did not happen, here is the table output:

psql (12.11)
Type "help" for help.

umami_db=# select * from event limit 1;
 event_id | website_id | session_id |         created_at         | url | event_type |         event_value
----------+------------+------------+----------------------------+-----+------------+-----------------------------
        1 |          1 |          3 | 2022-01-31 20:04:11.083+00 | /   | click      | landing-page-know-more-link
(1 row)
briancao commented 2 years ago

It looks like we're missing 1 last command after the revert

yarn prisma migrate resolve --rolled-back "02_add_event_data"
yarn prisma migrate deploy
yarn check-db
razinj commented 2 years ago

Perfect, works like a charm now, I think I will close the issue now, feel free to re-open it if needed.

Thank you all for the support ❤️

wheeleran commented 2 years ago

hi, sorry, how do i run the yarn fix for a docker-compose in ssh on ubuntu? i've just pulled the latest (again) and it's still giving me a migration error on '02_add_event_data' and a "something went wrong" in the events panel on those sites that do - and do NOT - track events.

razinj commented 2 years ago

The way I did it by accessing the app's container.

Run docker ps -a

Look for the Umami app docker container, get the name or ID (I will assume it's umami-app).

Run docker exec -it umami-app sh to access the container.

Then you should be able to run the yarn commands.