Libraries
    Preparing search index...

    Function generateMigrationsForContext

    • Generate a single combined migration (up + down) for a set of entities by diffing the current code-first schemas against a serialized snapshot stored in the repository — no live database connection required.

      For each entity's table the function:

      • New tables (in entities but not in prevSnapshot): emits CREATE TABLE.
      • Existing tables (in both): diffs entity schema against the snapshot state via diffSchema and emits ALTER TABLE when changes exist.
      • Dropped tables (in prevSnapshot but not in entities): emits DROP TABLE with a best-effort CREATE TABLE in the down direction.
      • Orders tables topologically by FK dependencies so parent tables are created before child tables in up (and dropped after in down).

      Parameters

      Returns {
          down: string;
          full: string;
          isEmpty: boolean;
          nextSnapshot: SchemaSnapshot;
          up: string;
      }

      { up, down, full, isEmpty, nextSnapshot } where nextSnapshot should be written to disk after the migration file is created.

      const prev = loadSnapshot('./migrations/snapshot.json');
      const result = generateMigrationsForContext(
      Object.values({ todos: TodoEntity, users: UserEntity }),
      prev
      );
      if (!result.isEmpty) {
      fs.writeFileSync('migrations/20260423000000_init.ts', result.full);
      writeSnapshot('./migrations/snapshot.json', result.nextSnapshot);
      }