Libraries v5.0.0
    Preparing search index...

    Function diffSchema

    • Compare a code-first schema model against a live database table state and produce a diff describing the changes needed to bring the database in sync.

      Parameters

      • schema: ObjectSchemaBuilder<any, any, any, any, any, any, any>

        The code-first ObjectSchemaBuilder.

      • dbState: DatabaseTableState

        The current database state from introspectDatabase.

      Returns MigrationDiff

      A MigrationDiff describing columns, indexes, and foreign keys to add, drop, or alter.

      const dbState = await introspectDatabase(knex, 'users');
      const diff = diffSchema(UserSchema, dbState);
      if (diff.addColumns.length > 0) {
      const migration = generateMigration(diff, 'users');
      console.log(migration.up);
      }