]);
// Индексы
- $this->createIndex(
- 'idx_p1c_class_dyn_product_id',
- self::TABLE_NAME,
- 'product_id'
- );
-
- $this->createIndex(
- 'idx_p1c_class_dyn_active',
- self::TABLE_NAME,
- 'active'
- );
-
- $this->createIndex(
- 'idx_p1c_class_dyn_dates',
- self::TABLE_NAME,
- ['date_from', 'date_to']
- );
-
- $this->createIndex(
- 'idx_p1c_class_dyn_class_tip',
- self::TABLE_NAME,
- 'class_tip'
- );
-
- $this->createIndex(
- 'idx_p1c_class_dyn_product_active',
- self::TABLE_NAME,
- ['product_id', 'active']
- );
-
- $this->createIndex(
- 'idx_p1c_class_dyn_parent_id',
- self::TABLE_NAME,
- 'parent_id'
- );
+ if (!$this->indexExists('idx_p1c_class_dyn_product_id')) {
+ $this->createIndex(
+ 'idx_p1c_class_dyn_product_id',
+ self::TABLE_NAME,
+ 'product_id'
+ );
+ }
+
+ if (!$this->indexExists('idx_p1c_class_dyn_active')) {
+ $this->createIndex(
+ 'idx_p1c_class_dyn_active',
+ self::TABLE_NAME,
+ 'active'
+ );
+ }
+
+ if (!$this->indexExists('idx_p1c_class_dyn_dates')) {
+ $this->createIndex(
+ 'idx_p1c_class_dyn_dates',
+ self::TABLE_NAME,
+ ['date_from', 'date_to']
+ );
+ }
+
+ if (!$this->indexExists('idx_p1c_class_dyn_class_tip')) {
+ $this->createIndex(
+ 'idx_p1c_class_dyn_class_tip',
+ self::TABLE_NAME,
+ 'class_tip'
+ );
+ }
+
+ if (!$this->indexExists('idx_p1c_class_dyn_product_active')) {
+ $this->createIndex(
+ 'idx_p1c_class_dyn_product_active',
+ self::TABLE_NAME,
+ ['product_id', 'active']
+ );
+ }
+
+ if (!$this->indexExists('idx_p1c_class_dyn_parent_id')) {
+ $this->createIndex(
+ 'idx_p1c_class_dyn_parent_id',
+ self::TABLE_NAME,
+ 'parent_id'
+ );
+ }
// Covering index для исторических запросов по датам
// Оптимизирует: WHERE product_id = ? AND date_from <= ? AND date_to >= ?
- $this->createIndex(
- 'idx_p1c_class_dyn_product_dates',
- self::TABLE_NAME,
- ['product_id', 'date_from', 'date_to']
- );
+ if (!$this->indexExists('idx_p1c_class_dyn_product_dates')) {
+ $this->createIndex(
+ 'idx_p1c_class_dyn_product_dates',
+ self::TABLE_NAME,
+ ['product_id', 'date_from', 'date_to']
+ );
+ }
// Partial unique index: только одна активная запись на товар
- $this->execute("
- CREATE UNIQUE INDEX idx_p1c_class_dyn_product_active_unique
- ON " . self::TABLE_NAME . " (product_id)
- WHERE active = 1
- ");
+ if (!$this->indexExists('idx_p1c_class_dyn_product_active_unique')) {
+ $this->execute("
+ CREATE UNIQUE INDEX idx_p1c_class_dyn_product_active_unique
+ ON " . self::TABLE_NAME . " (product_id)
+ WHERE active = 1
+ ");
+ }
// Комментарий к таблице
$this->execute("COMMENT ON TABLE " . self::TABLE_NAME . " IS 'Версионированный каталог товаров с классификацией (SCD Type 2)'");
$this->dropTable(self::TABLE_NAME);
}
}
+
+ private function indexExists(string $indexName): bool
+ {
+ $result = $this->db->createCommand(
+ "SELECT 1 FROM pg_indexes WHERE indexname = :name"
+ )->bindValue(':name', $indexName)->queryScalar();
+
+ return $result !== false;
+ }
}
]);
// Создаем уникальный составной индекс для предотвращения дублирования напоминаний
- $this->createIndex(
- 'idx-shift_reminder_shown-user_reminder_date',
- self::TABLE_NAME,
- ['user_id', 'reminder_key', 'reference_date'],
- true
- );
+ if (!$this->indexExists('idx-shift_reminder_shown-user_reminder_date')) {
+ $this->createIndex(
+ 'idx-shift_reminder_shown-user_reminder_date',
+ self::TABLE_NAME,
+ ['user_id', 'reminder_key', 'reference_date'],
+ true
+ );
+ }
// Создаем индекс для быстрого поиска по дате подтверждения
- $this->createIndex(
- 'idx-shift_reminder_shown-confirmed_at',
- self::TABLE_NAME,
- 'confirmed_at'
- );
+ if (!$this->indexExists('idx-shift_reminder_shown-confirmed_at')) {
+ $this->createIndex(
+ 'idx-shift_reminder_shown-confirmed_at',
+ self::TABLE_NAME,
+ 'confirmed_at'
+ );
+ }
}
/**
{
$this->dropTable(self::TABLE_NAME);
}
+
+ private function indexExists(string $indexName): bool
+ {
+ $result = $this->db->createCommand(
+ "SELECT 1 FROM pg_indexes WHERE indexname = :name"
+ )->bindValue(':name', $indexName)->queryScalar();
+
+ return $result !== false;
+ }
}
'created_at' => $this->timestamp()->notNull()->defaultExpression('CURRENT_TIMESTAMP')->comment('Дата и время теста'),
]);
- $this->createIndex(
- 'idx-upload_speed_tests-admin_id',
- self::TABLE_NAME,
- 'admin_id'
- );
-
- $this->createIndex(
- 'idx-upload_speed_tests-created_at',
- self::TABLE_NAME,
- 'created_at'
- );
-
- $this->createIndex(
- 'idx-upload_speed_tests-ip',
- self::TABLE_NAME,
- 'ip'
- );
-
- $this->createIndex(
- 'idx-upload_speed_tests-verdict',
- self::TABLE_NAME,
- 'verdict'
- );
+ if (!$this->indexExists('idx-upload_speed_tests-admin_id')) {
+ $this->createIndex(
+ 'idx-upload_speed_tests-admin_id',
+ self::TABLE_NAME,
+ 'admin_id'
+ );
+ }
+
+ if (!$this->indexExists('idx-upload_speed_tests-created_at')) {
+ $this->createIndex(
+ 'idx-upload_speed_tests-created_at',
+ self::TABLE_NAME,
+ 'created_at'
+ );
+ }
+
+ if (!$this->indexExists('idx-upload_speed_tests-ip')) {
+ $this->createIndex(
+ 'idx-upload_speed_tests-ip',
+ self::TABLE_NAME,
+ 'ip'
+ );
+ }
+
+ if (!$this->indexExists('idx-upload_speed_tests-verdict')) {
+ $this->createIndex(
+ 'idx-upload_speed_tests-verdict',
+ self::TABLE_NAME,
+ 'verdict'
+ );
+ }
}
public function safeDown()
{
$this->dropTable(self::TABLE_NAME);
}
+
+ private function indexExists(string $indexName): bool
+ {
+ $result = $this->db->createCommand(
+ "SELECT 1 FROM pg_indexes WHERE indexname = :name"
+ )->bindValue(':name', $indexName)->queryScalar();
+
+ return $result !== false;
+ }
}