From: fomichev Date: Wed, 3 Jun 2026 06:50:03 +0000 (+0300) Subject: ERP-396: убрать индексы из миграций store_type/city_store_params, добавить идемпотент... X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=8efd2b4f949d86c45e0f4a747c05d43f9644199b;p=erp24_rep%2Fyii-erp24%2F.git ERP-396: убрать индексы из миграций store_type/city_store_params, добавить идемпотентность Таблицы небольшие — индексы не дают выигрыша. Проверка существования колонки в safeUp/safeDown предотвращает падение при повторных накатах и откатах миграций. Co-Authored-By: Claude Sonnet 4.6 --- diff --git a/erp24/migrations/m260601_100000_add_code_to_store_type.php b/erp24/migrations/m260601_100000_add_code_to_store_type.php index ad845efb..5012e660 100644 --- a/erp24/migrations/m260601_100000_add_code_to_store_type.php +++ b/erp24/migrations/m260601_100000_add_code_to_store_type.php @@ -11,22 +11,24 @@ use yii\db\Migration; class m260601_100000_add_code_to_store_type extends Migration { private const TABLE = 'erp24.store_type'; - private const INDEX = 'idx_store_type_code'; public function safeUp(): void { - $this->addColumn( - self::TABLE, - 'code', - $this->string(50)->null()->comment('Slug-код типа магазина (large, small, medium, xlarge, na, flagship, kiosk, ...)') - ); - - $this->createIndex(self::INDEX, self::TABLE, 'code', true); + $schema = $this->db->getTableSchema(self::TABLE, true); + if ($schema !== null && $schema->getColumn('code') === null) { + $this->addColumn( + self::TABLE, + 'code', + $this->string(50)->null()->comment('Slug-код типа магазина (large, small, medium, xlarge, ...)') + ); + } } public function safeDown(): void { - $this->dropIndex(self::INDEX, self::TABLE); - $this->dropColumn(self::TABLE, 'code'); + $schema = $this->db->getTableSchema(self::TABLE, true); + if ($schema !== null && $schema->getColumn('code') !== null) { + $this->dropColumn(self::TABLE, 'code'); + } } } diff --git a/erp24/migrations/m260601_110000_add_is_active_to_city_store_params.php b/erp24/migrations/m260601_110000_add_is_active_to_city_store_params.php index eb76242d..d543866c 100644 --- a/erp24/migrations/m260601_110000_add_is_active_to_city_store_params.php +++ b/erp24/migrations/m260601_110000_add_is_active_to_city_store_params.php @@ -15,26 +15,24 @@ use yii\db\Migration; class m260601_110000_add_is_active_to_city_store_params extends Migration { private const TABLE = 'city_store_params'; - private const INDEX = 'idx_city_store_params_inactive'; public function safeUp(): void { - $this->addColumn( - self::TABLE, - 'is_active', - $this->boolean()->notNull()->defaultValue(true)->comment('Активен ли магазин (TRUE=да, FALSE=нет)') - ); - - // Partial index: только неактивные магазины (меньшинство). - // Полный индекс на boolean-колонке PostgreSQL не использует из-за низкой кардинальности. - $this->execute( - 'CREATE INDEX ' . self::INDEX . ' ON ' . self::TABLE . ' (store_id) WHERE is_active = false' - ); + $schema = $this->db->getTableSchema(self::TABLE, true); + if ($schema !== null && $schema->getColumn('is_active') === null) { + $this->addColumn( + self::TABLE, + 'is_active', + $this->boolean()->notNull()->defaultValue(true)->comment('Активен ли магазин (TRUE=да, FALSE=нет)') + ); + } } public function safeDown(): void { - $this->execute('DROP INDEX IF EXISTS ' . self::INDEX); - $this->dropColumn(self::TABLE, 'is_active'); + $schema = $this->db->getTableSchema(self::TABLE, true); + if ($schema !== null && $schema->getColumn('is_active') !== null) { + $this->dropColumn(self::TABLE, 'is_active'); + } } }