pocketbase icon indicating copy to clipboard operation
pocketbase copied to clipboard

"ReferenceError: Dao is not defined" at initial migrating

Open Strehk opened this issue 1 year ago • 4 comments

Hey there!

I tried to deploy a docker image with pocketbase inside, but I got this error and don't know whats going on:

Error: Failed to apply migration 1735157667_created_mission.js: ReferenceError: Dao is not defined at <eval>:65:13(109)

I checked back in my dev environment and could reproduce the error locally after deleting my data and restarting pocketbase serve.

The migration in the Error Message is the very first migration created by pocketbase and I have not tinkered with a single migration – they are all generated by pocketbase and none got deleted.

Here is the content of the migration; All of the migrations as well as the codebase to reproduce can be found here.

/// <reference path="../pb_data/types.d.ts" />
migrate((db) => {
  const collection = new Collection({
    "id": "8ag64scawxr32no",
    "created": "2024-12-25 20:14:27.654Z",
    "updated": "2024-12-25 20:14:27.654Z",
    "name": "mission",
    "type": "base",
    "system": false,
    "schema": [
      {
        "system": false,
        "id": "osu0lmjv",
        "name": "title",
        "type": "text",
        "required": true,
        "presentable": false,
        "unique": false,
        "options": {
          "min": null,
          "max": null,
          "pattern": ""
        }
      },
      {
        "system": false,
        "id": "brg2bpt3",
        "name": "description",
        "type": "text",
        "required": false,
        "presentable": false,
        "unique": false,
        "options": {
          "min": null,
          "max": null,
          "pattern": ""
        }
      },
      {
        "system": false,
        "id": "qyzpk1lv",
        "name": "faIcon",
        "type": "text",
        "required": false,
        "presentable": false,
        "unique": false,
        "options": {
          "min": null,
          "max": null,
          "pattern": ""
        }
      }
    ],
    "indexes": [
      "CREATE UNIQUE INDEX `idx_hdeUpgC` ON `mission` (`title`)"
    ],
    "listRule": null,
    "viewRule": null,
    "createRule": null,
    "updateRule": null,
    "deleteRule": null,
    "options": {}
  });

  return Dao(db).saveCollection(collection);
}, (db) => {
  const dao = new Dao(db);
  const collection = dao.findCollectionByNameOrId("8ag64scawxr32no");

  return dao.deleteCollection(collection);
})

Strehk avatar Dec 27 '24 23:12 Strehk

You are using PocketBase v0.23.12. Note that v0.23.0 comes with many breaking changes and requires manual migration steps. Please check the related changelog, the JSVM upgrade guide here and the new documentation.

For example, the Dao abstraction has been removed and most of the old Dao methods are now part of the $app instance.

// v0.22.x
migrate((db) => {
    const dao = new Dao(db);

    const settings = dao.findSettings()
    settings.meta.appName = "test"
    settings.logs.maxDays = 2

    dao.saveSettings(settings)
})

// v0.23+
migrate((app) => {
    const settings = app.settings()
    settings.meta.appName = "test"
    settings.logs.maxDays = 2

    app.save(settings)
})

chnfyi avatar Dec 28 '24 00:12 chnfyi

Thanks for the quick response!

That is weird, because I was on v0.22.x when the issue occured and only upgraded to v0.23.x today when I got stuck and thought it would maybe be an idea to upgrade and try again.

But anyway, I guess this is obsolete then anyways since dao got replaced?

For anyone having trouble with that as well, I meanwhile found a workaround:

  1. make sure you have a backup of your pb instance
  2. delete the pb_data folder
  3. delete the pb_migrations folder
  4. start the instance and restore the backup
  5. run pocketbase migrate collections to generate a brand new migration file

Strehk avatar Dec 28 '24 00:12 Strehk

For information, your issue occured when you were on v0.22.x probably because the Dao instance - new Dao(db) - is missing in the upgrade function.

/// <reference path="../pb_data/types.d.ts" />
migrate((db) => {
  const dao = new Dao(db);
  const collection = new Collection({
    "id": "8ag64scawxr32no",
    "created": "2024-12-25 20:14:27.654Z",
    "updated": "2024-12-25 20:14:27.654Z",
    "name": "mission",
    "type": "base",
    "system": false,
    "schema": [
      {
        "system": false,
        "id": "osu0lmjv",
        "name": "title",
        "type": "text",
        "required": true,
        "presentable": false,
        "unique": false,
        "options": {
          "min": null,
          "max": null,
          "pattern": ""
        }
      },
      {
        "system": false,
        "id": "brg2bpt3",
        "name": "description",
        "type": "text",
        "required": false,
        "presentable": false,
        "unique": false,
        "options": {
          "min": null,
          "max": null,
          "pattern": ""
        }
      },
      {
        "system": false,
        "id": "qyzpk1lv",
        "name": "faIcon",
        "type": "text",
        "required": false,
        "presentable": false,
        "unique": false,
        "options": {
          "min": null,
          "max": null,
          "pattern": ""
        }
      }
    ],
    "indexes": [
      "CREATE UNIQUE INDEX `idx_hdeUpgC` ON `mission` (`title`)"
    ],
    "listRule": null,
    "viewRule": null,
    "createRule": null,
    "updateRule": null,
    "deleteRule": null,
    "options": {}
  });

  return dao.saveCollection(collection);
}, (db) => {
  const dao = new Dao(db);
  const collection = dao.findCollectionByNameOrId("8ag64scawxr32no");

  return dao.deleteCollection(collection);
})

chnfyi avatar Dec 28 '24 01:12 chnfyi

@Strehk or anyone else that has stumbled here, please read the upgrade guide and follow the recommended upgrade steps in https://pocketbase.io/v023upgrade/jsvm/#upgrade-overview. If you don't have time to read it - stick to v0.22.x and make sure to pin your SDKs version to the latest compatible as mentioned in the upgrade guide note.

ganigeorgiev avatar Dec 28 '24 03:12 ganigeorgiev