Upgrade The Moonraker Database

My Qidi Q1 Pro was originally running a very old stack of Klipper, Moonraker and operating system, so when I needed new features not available on such an old stack, I decided to upgrade the printer to FreeDi, an alternative stack with support for the integrated display, otherwise not supported by Klipper natively.

The procedure was simple enough, but the old Moonraker was saving all data into data.mdb and lock.mdb (a LMDB database which is architecture specific!), while the newest release uses a SQLite database.

Upgrading the printer to FreeDi involved losing all the printer statistics and job history, which I didn’t like.

The new Moonraker was already configured by the maintainer of FreeDi with macros grouped according to need, and with the interface already configured. I didn’t want to lose this configuration either.

The steps to import the old print history and statistics into the new database are as follows:

  1. Recover data.mdb and lock.mdb from a backup you should have performed before upgrading to FreeDi
  2. Stop moonraker: sudo service moonraker stop
  3. Rename moonraker-sql.db using SSH to moonraker-sql-new.db, which is found in /home/mks/printer_data/database
  4. Download it to a local computer
  5. Upload data.mdb and lock.mdb to /home/mks/printer_data/database
  6. Start and the stop Moonraker: sudo service moonraker start && sudo service moonraker stop
  7. Download moonraker-sql.db to a local computer
  8. Using “DB Browser for SQLite” export the tables job_history, job_totals, namespace_store in SQL format with the options “Keep column names…” and “Export data only”, see image below.
  9. Close the file and delete it.
  10. Open the exported SQL file and delete the lines except for those which are needed for the history, then save. See the following excerpt:
    BEGIN TRANSACTION;
    INSERT INTO "job_history" …

    INSERT INTO "job_totals" …

    INSERT INTO "namespace_store" ("namespace","key","value") VALUES ('gcode_metadata',…

    COMMIT;
  11. Open moonraker-sql-new.db with “DB Browser for SQLite” and delete all records from the table job_totals
  12. Import the SQL file previously edited.
  13. Save and rename to moonraker-sql.db
  14. Upload to FreeDi replacing the existing one
  15. Start moonraker: sudo service moonraker start
Exporting table from moonraker-sql.db in SQL format

Moonraker will have now full history: