fix(migrations): ensure safe migration steps for doctor_expertise and doctor_cities, drop legacy tables conditionally

This commit is contained in:
hamed
2026-06-28 09:32:27 +03:30
parent e966e11807
commit 90a908c503
3 changed files with 83 additions and 16 deletions
+4
View File
@@ -57,6 +57,10 @@ COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh \
&& mkdir -p var/cache var/log var/uploads public/uploads config/jwt \
# Source may be copied with restrictive (0600) host perms; ensure the runtime
# user (www-data) can read all app files — opcache preload runs as www-data
# and otherwise fails with "Permission denied" on /app/config/preload.php.
&& chmod -R a+rX /app \
&& chown -R www-data:www-data var public/uploads config/jwt
EXPOSE 80
+56 -14
View File
@@ -23,23 +23,65 @@ final class Version20260610140853 extends AbstractMigration
public function up(Schema $schema): void
{
// Remove rows in doctor_expertise whose service_id doesn't exist in doctor_services
$this->addSql('DELETE FROM doctor_expertise WHERE service_id NOT IN (SELECT id FROM doctor_services)');
// NOTE: All of this work is also performed by Version20260610103401
// (same day, earlier timestamp). On a fresh database that migration runs
// first and leaves doctor_expertise / doctor_cities already converted and
// the categories table already dropped, so re-running these statements
// verbatim fails (errno 121: duplicate constraint FK_ED88BE60ED5CA9E6).
//
// Existing databases, however, may have applied THIS migration without
// 103401 having done the conversion. So instead of deleting this file we
// make every step idempotent: each action is guarded against the state
// 103401 leaves behind, so the migration is a safe no-op on a fresh DB
// and still finishes the conversion on an older DB.
// Add FK constraint to doctor_expertise
$this->addSql('ALTER TABLE doctor_expertise ADD CONSTRAINT FK_ED88BE60ED5CA9E6 FOREIGN KEY (service_id) REFERENCES doctor_services (id)');
$this->addSql('CREATE INDEX IDX_ED88BE60ED5CA9E6 ON doctor_expertise (service_id)');
// doctor_expertise: ensure category_id → service_id + FK exist (only if not already done by 103401)
$this->addSql(<<<'SQL'
DROP PROCEDURE IF EXISTS __mig_140853
SQL);
$this->addSql(<<<'SQL'
CREATE PROCEDURE __mig_140853()
BEGIN
DECLARE db VARCHAR(64);
SET db = DATABASE();
// doctor_cities: category_id → city_id with FK to cities
$this->addSql('DELETE FROM doctor_cities WHERE category_id NOT IN (SELECT id FROM cities)');
$this->addSql('ALTER TABLE doctor_cities DROP FOREIGN KEY `FK_7DC181E612469DE2`');
$this->addSql('DROP INDEX IDX_7DC181E612469DE2 ON doctor_cities');
$this->addSql('ALTER TABLE doctor_cities CHANGE category_id city_id INT NOT NULL, DROP PRIMARY KEY, ADD PRIMARY KEY (doctor_id, city_id)');
$this->addSql('ALTER TABLE doctor_cities ADD CONSTRAINT FK_7DC181E68BAC62AF FOREIGN KEY (city_id) REFERENCES cities (id)');
$this->addSql('CREATE INDEX IDX_7DC181E68BAC62AF ON doctor_cities (city_id)');
-- doctor_expertise.service_id FK
IF NOT EXISTS (
SELECT 1 FROM information_schema.TABLE_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA = db AND TABLE_NAME = 'doctor_expertise'
AND CONSTRAINT_NAME = 'FK_ED88BE60ED5CA9E6'
) THEN
DELETE FROM doctor_expertise WHERE service_id NOT IN (SELECT id FROM doctor_services);
ALTER TABLE doctor_expertise
ADD CONSTRAINT FK_ED88BE60ED5CA9E6 FOREIGN KEY (service_id) REFERENCES doctor_services (id);
CREATE INDEX IDX_ED88BE60ED5CA9E6 ON doctor_expertise (service_id);
END IF;
// Drop the now-unused categories table
$this->addSql('DROP TABLE categories');
-- doctor_cities: category_id city_id with FK to cities
IF EXISTS (
SELECT 1 FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = db AND TABLE_NAME = 'doctor_cities'
AND COLUMN_NAME = 'category_id'
) THEN
DELETE FROM doctor_cities WHERE category_id NOT IN (SELECT id FROM cities);
ALTER TABLE doctor_cities DROP FOREIGN KEY `FK_7DC181E612469DE2`;
DROP INDEX IDX_7DC181E612469DE2 ON doctor_cities;
ALTER TABLE doctor_cities CHANGE category_id city_id INT NOT NULL, DROP PRIMARY KEY, ADD PRIMARY KEY (doctor_id, city_id);
ALTER TABLE doctor_cities ADD CONSTRAINT FK_7DC181E68BAC62AF FOREIGN KEY (city_id) REFERENCES cities (id);
CREATE INDEX IDX_7DC181E68BAC62AF ON doctor_cities (city_id);
END IF;
-- Drop the now-unused categories table
IF EXISTS (
SELECT 1 FROM information_schema.TABLES
WHERE TABLE_SCHEMA = db AND TABLE_NAME = 'categories'
) THEN
DROP TABLE categories;
END IF;
END
SQL);
$this->addSql('CALL __mig_140853()');
$this->addSql('DROP PROCEDURE __mig_140853');
}
public function down(Schema $schema): void
+23 -2
View File
@@ -22,8 +22,29 @@ final class Version20260611075829 extends AbstractMigration
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE user_active_context (db_uuid VARCHAR(36) NOT NULL, updated_at INT NOT NULL, user_id INT NOT NULL, PRIMARY KEY (user_id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('ALTER TABLE user_active_context ADD CONSTRAINT FK_A94E639A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
$this->addSql('DROP TABLE site_config');
$this->addSql('ALTER TABLE appointments DROP booked_by, DROP commission_rials');
// On a fresh DB site_config does not exist yet (it is created later by
// Version20260611083424). IF EXISTS keeps this a no-op on fresh installs
// while still dropping the legacy table on older drifted databases.
$this->addSql('DROP TABLE IF EXISTS site_config');
// appointments.booked_by / commission_rials are never created by any
// migration — they only exist on older drifted dev DBs. Drop them only
// if present so a fresh DB doesn't fail with errno 1091.
$this->addSql('DROP PROCEDURE IF EXISTS __mig_075829');
$this->addSql(<<<'SQL'
CREATE PROCEDURE __mig_075829()
BEGIN
DECLARE db VARCHAR(64); SET db = DATABASE();
IF EXISTS (SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = db AND TABLE_NAME = 'appointments' AND COLUMN_NAME = 'booked_by') THEN
ALTER TABLE appointments DROP booked_by;
END IF;
IF EXISTS (SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = db AND TABLE_NAME = 'appointments' AND COLUMN_NAME = 'commission_rials') THEN
ALTER TABLE appointments DROP commission_rials;
END IF;
END
SQL);
$this->addSql('CALL __mig_075829()');
$this->addSql('DROP PROCEDURE __mig_075829');
}
public function down(Schema $schema): void