<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20260909120000 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add one-shot per-user temperature notifications without changing strength notifications.';
}
public function up(Schema $schema): void
{
$this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on postgresql.');
$this->addSql('CREATE SEQUENCE temperature_notifications_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql("CREATE TABLE temperature_notifications (id INT NOT NULL, user_id INT NOT NULL, channel_setting_id INT NOT NULL, temperature_trigger DOUBLE PRECISION NOT NULL, direction VARCHAR(16) DEFAULT 'above' NOT NULL, triggered_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, email VARCHAR(255) DEFAULT NULL, created_at TIMESTAMP(0) WITH TIME ZONE DEFAULT NULL, updated_at TIMESTAMP(0) WITH TIME ZONE DEFAULT NULL, PRIMARY KEY(id), CONSTRAINT CHK_TEMPERATURE_NOTIFICATION_DIRECTION CHECK (direction IN ('above', 'below')), CONSTRAINT CHK_TEMPERATURE_NOTIFICATION_RANGE CHECK (temperature_trigger >= -273.15 AND temperature_trigger <= 1416833))");
$this->addSql('COMMENT ON COLUMN temperature_notifications.temperature_trigger IS \'Temperature in degrees Celsius that triggers this notification.\'');
$this->addSql('COMMENT ON COLUMN temperature_notifications.direction IS \'Alarm direction: above or below.\'');
$this->addSql('COMMENT ON COLUMN temperature_notifications.triggered_at IS \'When this notification was triggered.\'');
$this->addSql('COMMENT ON COLUMN temperature_notifications.created_at IS \'Date of the entity creation. Should be fixed.\'');
$this->addSql('COMMENT ON COLUMN temperature_notifications.updated_at IS \'Date of the last change in entity. Shouldn’t be set to other dates.\'');
$this->addSql('CREATE INDEX IDX_TEMPERATURE_NOTIFICATION_USER ON temperature_notifications (user_id)');
$this->addSql('CREATE INDEX IDX_TEMPERATURE_NOTIFICATION_CHANNEL ON temperature_notifications (channel_setting_id)');
$this->addSql('CREATE INDEX IDX_TEMPERATURE_NOTIFICATION_PENDING ON temperature_notifications (channel_setting_id, id) WHERE (triggered_at IS NULL)');
$this->addSql('CREATE UNIQUE INDEX UNIQ_TEMPERATURE_NOTIFICATION_PENDING ON temperature_notifications (user_id, channel_setting_id, temperature_trigger, direction, email) WHERE (triggered_at IS NULL)');
$this->addSql('ALTER TABLE temperature_notifications ADD CONSTRAINT FK_TEMPERATURE_NOTIFICATION_USER FOREIGN KEY (user_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE temperature_notifications ADD CONSTRAINT FK_TEMPERATURE_NOTIFICATION_CHANNEL FOREIGN KEY (channel_setting_id) REFERENCES channel_settings (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
}
public function down(Schema $schema): void
{
$this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on postgresql.');
$this->addSql('DROP TABLE temperature_notifications');
$this->addSql('DROP SEQUENCE temperature_notifications_id_seq CASCADE');
}
}