Database Migration / Deployment Questions

It’s in the progress of being documented here (manual migrations):

But If there is issues with the auto-migration process, please share them.

1 Like

Hi @DMehaffy ,

Thanks for sharing the documentation. I am on strapi v4.1.12 and using postgres as database.I have tried adding the migration file under database/migrations/test.js file with below content

'use strict';

async function up(knex) {
  await knex.from('tabs_components').update({ field: 'tabsData' }).where({ field: 'tabs' });
}

async function down() {}

module.exports = { up, down };

However its always throwing error

error: Migration test.js (up) failed: Original error: db.getConnection(...).transaction is not a function

This error is coming even if I have empty up method in migration file or write any other script. I have also tried naming the file with timestamp(as mentioned in the documentation) but no success.

I am facing the same issue. Any progress on this?

I am working on fixing this one. Will share the patch in few days :slight_smile: .

Here is the patch

diff --git a/node_modules/@strapi/database/lib/migrations/index.js b/node_modules/@strapi/database/lib/migrations/index.js
index 82ad394..c8ff985 100644
--- a/node_modules/@strapi/database/lib/migrations/index.js
+++ b/node_modules/@strapi/database/lib/migrations/index.js
@@ -7,7 +7,7 @@ const { Umzug } = require('umzug');
 const createStorage = require('./storage');

 const wrapTransaction = db => fn => () =>
-  db.getConnection().transaction(trx => Promise.resolve(fn(trx)));
+  db.connection.transaction(trx => Promise.resolve(fn(trx)));

 // TODO: check multiple commands in one sql statement
 const migrationResolver = ({ name, path, context }) => {

Pull request is also raised on strapi. https://github.com/strapi/strapi/pull/13637

1 Like

I have the same problem, did you manage to solve it somehow?

Yes. Can you apply the above mention patch using patch-package

Do you have any estimated ETA?

Estimated time for what? The documentation PR should be merged soon, looks like we were waiting for clarification around the down function.

Hello, not sure what the progress is on this issue, however by following the documentation PR I am unable to get migrations to properly save the changes in the database.

Here’s a sample migration I tested:

'use strict'

const tableName = 'artworks'

/** @param {import('knex').Knex} knex */
exports.up = async (knex) => {
  await knex.schema.table(tableName, async t => {
    t.string('test').nullable()
  })
}

/** @param {import('knex').Knex} knex */
exports.down = async () => { }

The migration runs without errors and is saved inside the strapi_migrations table, however the table schema is not changed (I think Strapi automatically removes all unknown columns and even indexes after the migration.)

1 Like

What do you use to migrate Strapi databases?

same as me. I’m using Mysql 8. this is mine

const TABLE = 'articles';

async function up(knex) {
  return knex.schema.alterTable(TABLE, function(t) {
    t.dateTime('published_date', { precision: 6 }).defaultTo(knex.fn.now(6)).alter();
  });
}

async function down() { }

module.exports = { up, down };

I tried raw query but no luck

return knex.raw(`ALTER TABLE ${TABLE} MODIFY COLUMN published_date datetime DEFAULT CURRENT_TIMESTAMP NULL`)

UPDATE: after a test by throwing an error right before alter table, I saw that strapi will update the table again follow the schema config json.

Hello! I have the same issue. My db is Postgres, the migration to reproduce:

'use strict'

module.exports = {
  async up(knex) {
    await knex.schema.table('testcontents', table => {
      table.renameColumn('title', 'name');
    });
  },
  async down() {},
};

After yarn develop
migration name is saved inside the strapi_migrations table, but nothing changes in testcontents schema

3 Likes

Has anybody got the migrations to actually work? I’m getting the same thing. I’m running Knex, and I get a response, but I don’t see any updates to the table.

Hey, I’m getting the same thing. I need to add a unique constraint, and it’s not working at all. I’m wondering if anyone has got this to work.

Same here. The code is definitely being run but the changes aren’t being applied.

Any updates on this yet? I’m currently on strapi v4.15.5 and I’m still getting the same thing. Migration runs without errors with the migration file name saved in strapi_migrations but still no updates to the table.

We had the same issue. For us, the workaround we settled with was to use Dbeaver and keep schema backups whenever we do major schema changes so that we can roll back. for seeding data, we would use the bootstrap lifecycle function (Lifecycle Functions | Strapi Documentation),

I was also facing the same issue. Now, i have found the reason why it was not updating in the database.

When we start the application, Strapi automatically runs the migrations and commits the changes to the database. Post that due to the schema validation it reverts the migration changes that was applied previously and data of that column is stored with default value(mostly Null).

Steps to overcome the issue:

  1. Create the migration file for renaming the column
  2. In the schema.json file, update the ‘attributes’ in the similar manner of renaming the field name

Migration File:

module.exports = {
  async up(knex) {
    await knex.schema.table('testcontents', table => {
      table.renameColumn('title', 'name');
    });
  },
  async down() {},
};

Schema.json file: