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

    Function createSchemaAcrossShards

    • Applies the schema to all provided database instances in parallel. This is useful for initializing a complete sharded database system where all shards need the same table structure.

      The function executes schema creation on all shards concurrently for performance, but provides detailed error reporting that identifies which specific shard failed if any errors occur.

      A cluster that mixes backends cannot share one DDL string: INTEGER PRIMARY KEY AUTOINCREMENT is a syntax error on Postgres, and SERIAL is one on SQLite. Pass a function instead of a string to build the statement per shard from its dialect.

      Parameters

      • shards: Record<string, SQLDatabase>

        Record mapping shard names to database instances

      • schema: string | ((dialect: SQLDialect | undefined, binding: string) => string)

        Schema SQL, or a function returning it for a given shard

      Returns Promise<void>

      Promise that resolves when schema is created on all shards

      If schema creation fails on any shard, with shard identification

      const shards = {
      'db-east': env.DB_EAST,
      'db-west': env.DB_WEST,
      'db-central': env.DB_CENTRAL
      };

      try {
      await createSchemaAcrossShards(shards, userSchema);
      console.log('Schema created on all shards successfully');
      } catch (error) {
      console.error('Schema creation failed:', error.message);
      // Error will specify which shard failed
      }
      // A cluster spanning D1 and Postgres needs the generated-key column spelled
      // the way each backend spells it.
      await createSchemaAcrossShards(shards, (dialect) =>
      dialect === 'postgres' ? POSTGRES_SCHEMA : SQLITE_SCHEMA
      );