addSql('ALTER TABLE artwork ADD position INT NOT NULL DEFAULT 0'); // Backfill existing rows so the gallery keeps its current (upload-date) order // until someone drags a piece; new uploads get their position in PHP. $this->addSql(<<<'SQL' UPDATE artwork a JOIN ( SELECT id, ROW_NUMBER() OVER (ORDER BY uploaded_at ASC, id ASC) AS rn FROM artwork ) ranked ON ranked.id = a.id SET a.position = ranked.rn SQL); // Drop the bootstrap default now that every row has a real position; // the mapping has no default, so schema:validate expects none either. $this->addSql('ALTER TABLE artwork CHANGE position position INT NOT NULL'); } public function down(Schema $schema): void { $this->addSql('ALTER TABLE artwork DROP position'); } }