lildude / ha-addon-teslamate

Home Assistant add-on for TeslaMate
MIT License
21 stars 2 forks source link

Migration error - ERROR: Failed to create database TeslaMateDB #42

Closed DBestman closed 1 month ago

DBestman commented 1 month ago

I followed the steps to migrate from the old addon, but the new addon does not successfully start:

s6-rc: info: service s6rc-oneshot-runner: starting
s6-rc: info: service s6rc-oneshot-runner successfully started
s6-rc: info: service fix-attrs: starting
s6-rc: info: service fix-attrs successfully started
s6-rc: info: service legacy-cont-init: starting
s6-rc: info: service legacy-cont-init successfully started
s6-rc: info: service legacy-services: starting
services-up: info: copying legacy longrun nginx (no readiness notification)
services-up: info: copying legacy longrun teslamate (no readiness notification)
[10:27:01] INFO: Starting NGINX...
s6-rc: info: service legacy-services successfully started
[10:27:02] INFO: Checking for Grafana folder: TeslaMate
[10:27:02] INFO: ... RESTORED drives.json
[10:27:02] INFO: ... RESTORED drive-stats.json
[10:27:02] INFO: ... RESTORED battery-health.json
[10:27:02] INFO: ... RESTORED overview.json
[10:27:02] INFO: ... RESTORED mileage.json
[10:27:02] INFO: ... RESTORED states.json
[10:27:02] INFO: ... RESTORED updates.json
[10:27:02] INFO: ... RESTORED vampire-drain.json
[10:27:02] INFO: ... RESTORED charging-stats.json
[10:27:02] INFO: ... RESTORED timeline.json
[10:27:02] INFO: ... RESTORED efficiency.json
[10:27:03] INFO: ... RESTORED visited.json
[10:27:03] INFO: ... RESTORED charge-level.json
[10:27:03] INFO: ... RESTORED charges.json
[10:27:03] INFO: ... RESTORED statistics.json
[10:27:03] INFO: ... RESTORED locations.json
[10:27:03] INFO: ... RESTORED trip.json
[10:27:03] INFO: ... RESTORED projected-range.json
[10:27:03] INFO: ... RESTORED drive-details.json
[10:27:03] INFO: ... RESTORED charge-details.json
[10:27:03] INFO: Finished Importing Grafana Dashboards
[10:27:03] INFO: Creating database 'TeslaMateDB' on 29b65938-postgres
ERROR:  database "TeslaMateDB" already exists
[10:27:03] ERROR: Failed to create database TeslaMateDB
[10:27:03] ERROR: TeslaMate encountered an error, halting add-on
s6-rc: info: service legacy-services: stopping
s6-rc: info: service legacy-services successfully stopped
s6-rc: info: service legacy-cont-init: stopping
s6-rc: info: service legacy-cont-init successfully stopped
s6-rc: info: service fix-attrs: stopping
s6-rc: info: service fix-attrs successfully stopped
s6-rc: info: service s6rc-oneshot-runner: stopping
s6-rc: info: service s6rc-oneshot-runner successfully stopped

As you can see, the log indicates the database already exists, and fails trying to create it (which it shouldn't).

Did I miss something in the migration steps?

I can provide further information if required. Thanks a lot!

DBestman commented 1 month ago

I also see the follwing in the log for the addon matt-FFFFFF/hassio-addon-postgres

2024-09-12 10:02:27.258 EDT [32153] FATAL:  password authentication failed for user "root"
2024-09-12 10:02:27.258 EDT [32153] DETAIL:  Role "root" does not exist.
    Connection matched pg_hba.conf line 99: "host all all all md5"
2024-09-12 10:02:27.324 EDT [32155] ERROR:  database "TeslaMateDB" already exists
2024-09-12 10:02:27.324 EDT [32155] STATEMENT:  CREATE DATABASE "TeslaMateDB"
2024-09-12 10:27:03.322 EDT [32228] FATAL:  password authentication failed for user "root"
2024-09-12 10:27:03.322 EDT [32228] DETAIL:  Role "root" does not exist.
    Connection matched pg_hba.conf line 99: "host all all all md5"
2024-09-12 10:27:03.391 EDT [32230] ERROR:  database "TeslaMateDB" already exists
2024-09-12 10:27:03.391 EDT [32230] STATEMENT:  CREATE DATABASE "TeslaMateDB"

I have defined the same user in my new TeslaMate configuration as in the old: database_user: TeslaMate

lildude commented 1 month ago

🤔 this looks like https://github.com/lildude/ha-addon-teslamate/issues/15 but I'm sure I fixed this in https://github.com/lildude/ha-addon-teslamate/pull/23.

Are you using the latest version of the addon?

DBestman commented 1 month ago

Yes, I installed version 1.1.4

DBestman commented 1 month ago

Could it be because in line 56 of run , DATABASE_NAME is not quoted? if [[ -n $(PGPASSWORD="$DATABASE_PASS" psql -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USER" postgres -Atqc "\list $DATABASE_NAME") ]]; then

"Some commands take an SQL identifier (such as a table name) as argument. These arguments follow the syntax rules of SQL: Unquoted letters are forced to lowercase, while double quotes (") protect letters from case conversion and allow incorporation of whitespace into the identifier"

lildude commented 1 month ago

🤦 you're right. I fixed this in https://github.com/lildude/ha-addon-teslamate/pull/23 in one place but not in the command that checks if the DB exists.

Fixed in https://github.com/lildude/ha-addon-teslamate/pull/43 and the release I'll make once it's been merged.

lildude commented 1 month ago

Should be fixed in v1.1.5

DBestman commented 1 month ago

Works now. Thanks!