<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240304190000 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
# making all `user_id` columns of the same type
$this->addSql('ALTER TABLE user_points_history DROP FOREIGN KEY FK_CED765BAA76ED395');
$this->addSql('ALTER TABLE user_vouchers DROP FOREIGN KEY FK_7398E348A76ED395');
# update users.id and user_id columns as INT UNSIGNED NOT NULL
$this->addSql('ALTER TABLE users CHANGE id id INT UNSIGNED AUTO_INCREMENT NOT NULL');
$this->addSql('ALTER TABLE feedbacks CHANGE user_id user_id INT UNSIGNED NOT NULL');
$this->addSql('ALTER TABLE user_points_history CHANGE user_id user_id INT UNSIGNED NOT NULL');
$this->addSql('ALTER TABLE user_vouchers CHANGE user_id user_id INT UNSIGNED NOT NULL');
$this->addSql('ALTER TABLE user_verification_tokens CHANGE user_id user_id INT UNSIGNED NOT NULL');
# re-add fk
$this->addSql('ALTER TABLE user_points_history ADD CONSTRAINT FK_CED765BAA76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE user_vouchers ADD CONSTRAINT FK_7398E348A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
# remove entries that have no correspondent in `users` table
$this->addSql('DELETE FROM api_token WHERE user_id NOT IN (SELECT id FROM users)');
$this->addSql('DELETE FROM cardiometabolic_profiles WHERE user_id NOT IN (SELECT id FROM users)');
$this->addSql('DELETE FROM favourite_menus WHERE user_id NOT IN (SELECT id FROM users)');
$this->addSql('DELETE FROM favourite_menus_sessions WHERE user_id NOT IN (SELECT id FROM users)');
$this->addSql('DELETE FROM feedbacks WHERE user_id NOT IN (SELECT id FROM users)');
$this->addSql('DELETE FROM objectives WHERE user_id NOT IN (SELECT id FROM users)');
$this->addSql('DELETE FROM payments WHERE user_id NOT IN (SELECT id FROM users)');
$this->addSql('DELETE FROM user_fcm_tokens WHERE user_id NOT IN (SELECT id FROM users)');
$this->addSql('DELETE FROM user_notifications WHERE user_id NOT IN (SELECT id FROM users)');
$this->addSql('DELETE FROM user_subscriptions WHERE user_id NOT IN (SELECT id FROM users)');
$this->addSql('DELETE FROM user_verification_tokens WHERE user_id NOT IN (SELECT id FROM users)');
$this->addSql('DELETE FROM user_viewed_notifications WHERE user_id NOT IN (SELECT id FROM users)');
# add new fk
$this->addSql('ALTER TABLE api_token ADD CONSTRAINT FK_7BA2F5EBA76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE cardiometabolic_profiles ADD CONSTRAINT FK_ACE5AC0DA76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE favourite_menus ADD CONSTRAINT FK_D30089BAA76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE favourite_menus_sessions ADD CONSTRAINT FK_B1346200A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE feedbacks ADD CONSTRAINT FK_7E6C3F89A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE objectives ADD CONSTRAINT FK_6CB0696CA76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE payments ADD CONSTRAINT FK_65D29B32A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE user_fcm_tokens ADD CONSTRAINT FK_7811FBD5A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE user_notifications ADD CONSTRAINT FK_8E8E1D83A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE user_subscriptions ADD CONSTRAINT FK_EAF92751A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE user_verification_tokens ADD CONSTRAINT FK_EB6CD4A9A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE user_viewed_notifications ADD CONSTRAINT FK_9D7C6B5AA76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
# foreign key indexes
$this->addSql('ALTER TABLE api_token RENAME INDEX user_id TO IDX_7BA2F5EBA76ED395');
$this->addSql('ALTER TABLE cardiometabolic_profiles RENAME INDEX user_id TO IDX_ACE5AC0DA76ED395');
$this->addSql('ALTER TABLE favourite_menus RENAME INDEX user_id TO IDX_D30089BAA76ED395');
$this->addSql('ALTER TABLE favourite_menus_sessions RENAME INDEX user_id TO IDX_B1346200A76ED395');
$this->addSql('ALTER TABLE objectives RENAME INDEX user_id TO IDX_6CB0696CA76ED395');
$this->addSql('ALTER TABLE payments RENAME INDEX user_id TO IDX_65D29B32A76ED395');
$this->addSql('CREATE INDEX IDX_7811FBD5A76ED395 ON user_fcm_tokens (user_id)');
$this->addSql('ALTER TABLE user_notifications RENAME INDEX user_id TO IDX_8E8E1D83A76ED395');
$this->addSql('ALTER TABLE user_subscriptions RENAME INDEX user_id TO IDX_EAF92751A76ED395');
$this->addSql('CREATE INDEX IDX_EB6CD4A9A76ED395 ON user_verification_tokens (user_id)');
$this->addSql('ALTER TABLE user_viewed_notifications RENAME INDEX user_id TO IDX_9D7C6B5AA76ED395');
}
public function down(Schema $schema): void
{
# drop fks added in upgrade
$this->addSql('ALTER TABLE api_token DROP FOREIGN KEY FK_7BA2F5EBA76ED395');
$this->addSql('ALTER TABLE cardiometabolic_profiles DROP FOREIGN KEY FK_ACE5AC0DA76ED395');
$this->addSql('ALTER TABLE favourite_menus DROP FOREIGN KEY FK_D30089BAA76ED395');
$this->addSql('ALTER TABLE favourite_menus_sessions DROP FOREIGN KEY FK_B1346200A76ED395');
$this->addSql('ALTER TABLE feedbacks DROP FOREIGN KEY FK_7E6C3F89A76ED395');
$this->addSql('ALTER TABLE objectives DROP FOREIGN KEY FK_6CB0696CA76ED395');
$this->addSql('ALTER TABLE payments DROP FOREIGN KEY FK_65D29B32A76ED395');
$this->addSql('ALTER TABLE user_fcm_tokens DROP FOREIGN KEY FK_7811FBD5A76ED395');
$this->addSql('ALTER TABLE user_notifications DROP FOREIGN KEY FK_8E8E1D83A76ED395');
$this->addSql('ALTER TABLE user_subscriptions DROP FOREIGN KEY FK_EAF92751A76ED395');
$this->addSql('ALTER TABLE user_verification_tokens DROP FOREIGN KEY FK_EB6CD4A9A76ED395');
$this->addSql('ALTER TABLE user_viewed_notifications DROP FOREIGN KEY FK_9D7C6B5AA76ED395');
# foreign key indexes
$this->addSql('ALTER TABLE api_token RENAME INDEX IDX_7BA2F5EBA76ED395 TO user_id');
$this->addSql('ALTER TABLE cardiometabolic_profiles RENAME INDEX idx_ace5ac0da76ed395 TO user_id');
$this->addSql('ALTER TABLE favourite_menus RENAME INDEX idx_d30089baa76ed395 TO user_id');
$this->addSql('ALTER TABLE favourite_menus_sessions RENAME INDEX idx_b1346200a76ed395 TO user_id');
$this->addSql('ALTER TABLE objectives RENAME INDEX idx_6cb0696ca76ed395 TO user_id');
$this->addSql('ALTER TABLE payments RENAME INDEX idx_65d29b32a76ed395 TO user_id');
$this->addSql('DROP INDEX IDX_7811FBD5A76ED395 ON user_fcm_tokens');
$this->addSql('ALTER TABLE user_notifications RENAME INDEX idx_8e8e1d83a76ed395 TO user_id');
$this->addSql('ALTER TABLE user_subscriptions RENAME INDEX idx_eaf92751a76ed395 TO user_id');
$this->addSql('DROP INDEX IDX_EB6CD4A9A76ED395 ON user_verification_tokens');
$this->addSql('ALTER TABLE user_viewed_notifications RENAME INDEX idx_9d7c6b5aa76ed395 TO user_id');
# making all `user_id` columns of the same type
$this->addSql('ALTER TABLE user_points_history DROP FOREIGN KEY FK_CED765BAA76ED395');
$this->addSql('ALTER TABLE user_vouchers DROP FOREIGN KEY FK_7398E348A76ED395');
# update users.id and user_id columns as INT UNSIGNED NOT NULL
$this->addSql('ALTER TABLE users CHANGE id id INT AUTO_INCREMENT NOT NULL');
$this->addSql('ALTER TABLE feedbacks CHANGE user_id user_id INT NOT NULL');
$this->addSql('ALTER TABLE user_points_history CHANGE user_id user_id INT NOT NULL');
$this->addSql('ALTER TABLE user_vouchers CHANGE user_id user_id INT NOT NULL');
$this->addSql('ALTER TABLE user_verification_tokens CHANGE user_id user_id INT NOT NULL');
# re-add fk
$this->addSql('ALTER TABLE user_points_history ADD CONSTRAINT FK_CED765BAA76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE user_vouchers ADD CONSTRAINT FK_7398E348A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
}
}