@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

      When migrateOtherColumns is enabled, additional lookup keys will be created for username, email, and name columns if they exist in the tables.

      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;
            migrateOtherColumns?: boolean;
            primaryKeyColumn?: string;
            skipCache?: boolean;
            tablesToCheck?: string[];
        } = {}

        Optional migration configuration

        • OptionalmaxRecordsToCheck?: number
        • OptionalmigrateOtherColumns?: boolean

          When true, creates additional lookup keys for username, email, and name columns

        • OptionalprimaryKeyColumn?: string
        • OptionalskipCache?: boolean
        • OptionaltablesToCheck?: string[]

      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, {
      migrateOtherColumns: true
      });
      if (result.migrationPerformed) {
      console.log(`Auto-migrated ${result.recordsMigrated} records`);
      }