#Knex migrations for someone who is new?
11 messages · Page 1 of 1 (latest)
There's lots of examples in the main backstage repo. A perhaps good example is https://github.com/backstage/backstage/tree/master/plugins/user-settings-backend which is relatively small and easy to digest.
Some things of note:
-
package.json ensures to include the migrations directory https://github.com/backstage/backstage/blob/master/plugins/user-settings-backend/package.json#L53
-
the entry point of the plugin accepts a PluginDatabaseManager https://github.com/backstage/backstage/blob/master/plugins/user-settings-backend/src/service/router.ts#L29
-
the database layer in the plugin gets a client from that manager, and runs the migrations https://github.com/backstage/backstage/blob/master/plugins/user-settings-backend/src/database/DatabaseUserSettingsStore.ts#L51
Thank you
Hi @odd nova ,In the migrations init.js file,If I had a schema already present for one table and the table is created in the database and If I want to create one more table which is related to the first table that is present and start my backend the table is not getting created.The table is getting created only if I drop all the tables in the database.And Even if I want to update the schema of a table,it is getting done only if I drop the table and start my Backend.Can u suggest me any solution to this?
You may be misunderstanding how the migrations system works
You can't just edit those files
The migrations system ensures that each filename is applied once and only once
if you edit a file, it's completely ignored by future runs on the same database
You need to add MORE files that do things in addition to the previous files
that's why mature plugins have several files that do ALTER TABLE and stuff like that https://github.com/backstage/backstage/blob/2bd94c454e82c955450cb791a8f54c0da7adc7af/plugins/catalog-backend/migrations