@earth-app/collegedb
    Preparing search index...

    Function autoDetectAndMigrate

    • Automatically detects if a database needs migration and performs it

      This function is called automatically by CollegeDB operations to detect existing databases that contain data but haven't been integrated into the sharding system. It performs seamless migration without user intervention.

      The detection process:

      1. Checks if the database has data tables with primary keys
      2. Verifies if primary key mappings exist in KV
      3. If unmapped data is found, performs automatic integration
      4. Caches results to avoid repeated checks

      Parameters

      • d1: D1Database

        The D1 database instance to check and potentially migrate

      • shardName: string

        The shard binding name for this database

      • config: CollegeDBConfig

        CollegeDB configuration containing KV and strategy

      • options: {
            maxRecordsToCheck?: number;
            primaryKeyColumn?: string;
            skipCache?: boolean;
            tablesToCheck?: string[];
        } = {}

        Optional migration configuration

      Returns Promise<
          {
              issues: string[];
              migrationNeeded: boolean;
              migrationPerformed: boolean;
              recordsMigrated: number;
              tablesProcessed: number;
          },
      >

      Promise resolving to migration result summary

      // Called automatically by CollegeDB operations
      const result = await autoDetectAndMigrate(env.DB_EXISTING, 'db-existing', config);
      if (result.migrationPerformed) {
      console.log(`Auto-migrated ${result.recordsMigrated} records`);
      }