<?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 Version20180823171039 extends AbstractMigration
{
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('
ALTER TABLE customers_transactions_history
ADD card_brand VARCHAR(255) DEFAULT NULL,
ADD card_owner VARCHAR(255) DEFAULT NULL,
ADD card_number VARCHAR(255) DEFAULT NULL,
ADD card_exp_month INT DEFAULT NULL,
ADD card_exp_year INT DEFAULT NULL,
ADD card_cvc VARCHAR(255) DEFAULT NULL,
ADD e_check_owner VARCHAR(255) DEFAULT NULL,
ADD e_check_bank_name VARCHAR(255) DEFAULT NULL,
ADD e_check_account_number VARCHAR(255) DEFAULT NULL,
ADD e_check_routing VARCHAR(255) DEFAULT NULL,
ADD e_check_number VARCHAR(255) DEFAULT NULL,
ADD e_check_phone_number VARCHAR(255) DEFAULT NULL,
ADD cashier_check_number VARCHAR(255) DEFAULT NULL
');
$stmt = $this->connection->prepare('SELECT count(1) FROM customers_transactions_history');
$stmt->execute();
// get total entries
$total = $stmt->fetchColumn();
// insert using pagination
for ($i = 0; $i < ceil($total / 100); ++$i) {
$stmt = $this->connection->prepare(sprintf('SELECT id, transaction_value FROM customers_transactions_history ORDER BY id ASC LIMIT %d, 100', 100 * $i));
$stmt->execute();
while ($row = $stmt->fetch(\PDO::FETCH_OBJ)) {
$metadata = $row->transaction_value ? json_decode($row->transaction_value, true) : [];
if (empty($metadata)) {
continue;
}
if ($ccExpiresMonth = $metadata['cc_expires_month'] ?? null) {
$ccExpiresMonth = (int) $ccExpiresMonth;
} else {
$ccExpiresMonth = null;
}
if ($ccExpiresYear = $metadata['cc_expires_year'] ?? null) {
$ccExpiresYear = (int) $ccExpiresYear;
} else {
$ccExpiresYear = null;
}
$this->addSql('
UPDATE customers_transactions_history
SET
card_brand = :card_brand,
card_owner = :card_owner,
card_number = :card_number,
card_exp_month = :card_exp_month,
card_exp_year = :card_exp_year,
card_cvc = :card_cvc,
e_check_owner = :e_check_owner,
e_check_bank_name = :e_check_bank_name,
e_check_account_number = :e_check_account_number,
e_check_routing = :e_check_routing,
e_check_number = :e_check_number,
e_check_phone_number = :e_check_phone_number,
cashier_check_number = :cashier_check_number
WHERE id = :id
', [
'id' => $row->id,
'card_brand' => $metadata['cc_type'] ?? null,
'card_owner' => $metadata['cc_owner'] ?? null,
'card_number' => $metadata['cc_number'] ?? null,
'card_exp_month' => $ccExpiresMonth,
'card_exp_year' => $ccExpiresYear,
'card_cvc' => $metadata['cc_cvc'] ?? null,
'e_check_owner' => $metadata['echk_owner'] ?? null,
'e_check_bank_name' => $metadata['echk_bank'] ?? null,
'e_check_account_number' => $metadata['echk_account'] ?? null,
'e_check_routing' => $metadata['echk_routing'] ?? null,
'e_check_number' => $metadata['echk_number_check'] ?? null,
'e_check_phone_number' => $metadata['echk_telephone'] ?? null,
'cashier_check_number' => $metadata['ccod_number_check'] ?? null,
]);
}
}
$this->addSql('RENAME TABLE customers_transactions_history TO customer_transaction');
$this->addSql('ALTER TABLE customer_transaction ENGINE = InnoDB');
$this->addSql('ALTER TABLE customer_transaction CHARACTER SET = utf8mb4');
$this->addSql('UPDATE customer_transaction SET ran_processed = 0 WHERE ran_processed IS NULL');
$this->addSql('UPDATE customer_transaction SET pending = 0 WHERE pending IS NULL');
$this->addSql('UPDATE customer_transaction SET processing = 0 WHERE processing IS NULL');
$this->addSql('UPDATE customer_transaction SET status = 0 WHERE status IS NULL');
$this->addSql('
ALTER TABLE customer_transaction
DROP administrators_id_3,
DROP administrators_id_4,
DROP administrators_id_5,
DROP transaction_value,
CHANGE customers_id customer_id INT DEFAULT NULL,
CHANGE administrators_id user_id INT DEFAULT NULL,
CHANGE customers_services_id invoice_id INT DEFAULT NULL,
CHANGE processors_id processor_id INT DEFAULT NULL,
CHANGE collectors_id collector_id INT DEFAULT NULL,
CHANGE administrators_id_1 rep1_id INT DEFAULT NULL,
CHANGE administrators_id_2 rep2_id INT DEFAULT NULL,
CHANGE ran_by ran_by_id INT DEFAULT NULL,
CHANGE funded_by funded_by_id INT DEFAULT NULL,
CHANGE customers_payment_status_id type VARCHAR(255) DEFAULT NULL,
CHANGE transaction_code method VARCHAR(255) DEFAULT NULL,
CHANGE amount amount NUMERIC(10, 2) DEFAULT NULL,
CHANGE late_fee late_fee NUMERIC(10, 2) DEFAULT NULL,
CHANGE proc_fee processing_fee NUMERIC(10, 2) DEFAULT NULL,
CHANGE points points NUMERIC(10, 2) DEFAULT NULL,
CHANGE ppd ppd NUMERIC(10, 2) DEFAULT NULL,
CHANGE color color VARCHAR(6) DEFAULT NULL,
CHANGE comments message LONGTEXT DEFAULT NULL,
CHANGE ran_note ran_message VARCHAR(255) DEFAULT NULL,
CHANGE ran_processed ran_processed TINYINT(1) NOT NULL,
CHANGE ran_date ran_at DATETIME DEFAULT NULL,
CHANGE funded_date funded_at DATETIME DEFAULT NULL,
CHANGE pending pending TINYINT(1) NOT NULL,
CHANGE processing processing TINYINT(1) NOT NULL,
CHANGE status enabled TINYINT(1) NOT NULL,
CHANGE due_date due_at DATETIME DEFAULT NULL,
CHANGE date_added created_at DATETIME NOT NULL,
CHANGE last_modified modified_at DATETIME NOT NULL
');
$this->addSql('UPDATE customer_transaction SET type = \'scheduling\' WHERE type = \'1\'');
$this->addSql('UPDATE customer_transaction SET type = \'paid\' WHERE type = \'2\'');
$this->addSql('UPDATE customer_transaction SET type = \'returned\' WHERE type = \'3\'');
$this->addSql('UPDATE customer_transaction SET method = \'cod\' WHERE method = \'Cash on Deli\'');
$this->addSql('ALTER TABLE customer_transaction CHANGE type type ENUM(\'scheduling\', \'paid\', \'returned\') DEFAULT NULL COMMENT \'(DC2Type:TransactionType)\'');
$this->addSql('ALTER TABLE customer_transaction CHANGE method method ENUM(\'cc\', \'echk\', \'ccod\', \'pcod\', \'cod\') DEFAULT NULL COMMENT \'(DC2Type:TransactionMethodType)\'');
$this->addSql('DROP INDEX idx_customers_id ON customer_transaction');
$this->addSql('DROP INDEX idx_administrators_id ON customer_transaction');
$this->addSql('DROP INDEX idx_customers_payment_status_id ON customer_transaction');
$this->addSql('DROP INDEX idx_customers_services_id ON customer_transaction');
$this->addSql('CREATE INDEX IDX_717C2ED99395C3F3 ON customer_transaction (customer_id)');
$this->addSql('CREATE INDEX IDX_717C2ED9A76ED395 ON customer_transaction (user_id)');
$this->addSql('CREATE INDEX IDX_717C2ED92989F1FD ON customer_transaction (invoice_id)');
$this->addSql('CREATE INDEX IDX_717C2ED937BAC19A ON customer_transaction (processor_id)');
$this->addSql('CREATE INDEX IDX_717C2ED9670BAFFE ON customer_transaction (collector_id)');
$this->addSql('CREATE INDEX IDX_717C2ED996E7E3BC ON customer_transaction (rep1_id)');
$this->addSql('CREATE INDEX IDX_717C2ED984524C52 ON customer_transaction (rep2_id)');
$this->addSql('CREATE INDEX IDX_717C2ED96E89055B ON customer_transaction (ran_by_id)');
$this->addSql('CREATE INDEX IDX_717C2ED98B81BE4F ON customer_transaction (funded_by_id)');
$this->addSql('RENAME TABLE customers_transactions_collections TO customer_transaction_collection_log');
$this->addSql('ALTER TABLE customer_transaction_collection_log ENGINE = InnoDB');
$this->addSql('ALTER TABLE customer_transaction_collection_log CHARACTER SET = utf8mb4');
$this->addSql('
ALTER TABLE customer_transaction_collection_log
CHANGE customers_id customer_id INT DEFAULT NULL,
CHANGE customers_transactions_id transaction_id INT DEFAULT NULL,
CHANGE customers_messages_id message_id INT DEFAULT NULL,
CHANGE date_added created_at DATETIME NOT NULL
');
$this->addSql('DROP INDEX idx_customers_id ON customer_transaction_collection_log');
$this->addSql('DROP INDEX idx_customers_transactions_id ON customer_transaction_collection_log');
$this->addSql('DROP INDEX idx_customers_messages_id ON customer_transaction_collection_log');
$this->addSql('CREATE INDEX IDX_D4FDBC749395C3F3 ON customer_transaction_collection_log (customer_id)');
$this->addSql('CREATE INDEX IDX_D4FDBC742FC0CB0F ON customer_transaction_collection_log (transaction_id)');
$this->addSql('CREATE INDEX IDX_D4FDBC74537A1329 ON customer_transaction_collection_log (message_id)');
$this->addSql('DELETE FROM customer_transaction WHERE customer_id NOT IN (SELECT id FROM customer)');
$this->addSql('UPDATE customer_transaction SET user_id = NULL WHERE user_id NOT IN (SELECT id FROM user)');
$this->addSql('UPDATE customer_transaction SET invoice_id = NULL WHERE invoice_id NOT IN (SELECT id FROM customer_invoice)');
$this->addSql('UPDATE customer_transaction SET processor_id = NULL WHERE processor_id NOT IN (SELECT id FROM user)');
$this->addSql('UPDATE customer_transaction SET collector_id = NULL WHERE collector_id NOT IN (SELECT id FROM user)');
$this->addSql('UPDATE customer_transaction SET rep1_id = NULL WHERE rep1_id NOT IN (SELECT id FROM user)');
$this->addSql('UPDATE customer_transaction SET rep2_id = NULL WHERE rep2_id NOT IN (SELECT id FROM user)');
$this->addSql('UPDATE customer_transaction SET ran_by_id = NULL WHERE ran_by_id NOT IN (SELECT id FROM user)');
$this->addSql('UPDATE customer_transaction SET funded_by_id = NULL WHERE funded_by_id NOT IN (SELECT id FROM user)');
$this->addSql('DELETE FROM customer_transaction_collection_log WHERE customer_id NOT IN (SELECT id FROM customer)');
$this->addSql('DELETE FROM customer_transaction_collection_log WHERE transaction_id NOT IN (SELECT id FROM customer_transaction)');
$this->addSql('DELETE FROM customer_transaction_collection_log WHERE message_id NOT IN (SELECT id FROM customer_message)');
$this->addSql('ALTER TABLE customer_transaction ADD CONSTRAINT FK_717C2ED99395C3F3 FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE customer_transaction ADD CONSTRAINT FK_717C2ED9A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE SET NULL');
$this->addSql('ALTER TABLE customer_transaction ADD CONSTRAINT FK_717C2ED92989F1FD FOREIGN KEY (invoice_id) REFERENCES customer_invoice (id) ON DELETE SET NULL');
$this->addSql('ALTER TABLE customer_transaction ADD CONSTRAINT FK_717C2ED937BAC19A FOREIGN KEY (processor_id) REFERENCES config_processor (id) ON DELETE SET NULL');
$this->addSql('ALTER TABLE customer_transaction ADD CONSTRAINT FK_717C2ED9670BAFFE FOREIGN KEY (collector_id) REFERENCES user (id) ON DELETE SET NULL');
$this->addSql('ALTER TABLE customer_transaction ADD CONSTRAINT FK_717C2ED996E7E3BC FOREIGN KEY (rep1_id) REFERENCES user (id) ON DELETE SET NULL');
$this->addSql('ALTER TABLE customer_transaction ADD CONSTRAINT FK_717C2ED984524C52 FOREIGN KEY (rep2_id) REFERENCES user (id) ON DELETE SET NULL');
$this->addSql('ALTER TABLE customer_transaction ADD CONSTRAINT FK_717C2ED96E89055B FOREIGN KEY (ran_by_id) REFERENCES user (id) ON DELETE SET NULL');
$this->addSql('ALTER TABLE customer_transaction ADD CONSTRAINT FK_717C2ED98B81BE4F FOREIGN KEY (funded_by_id) REFERENCES user (id) ON DELETE SET NULL');
$this->addSql('ALTER TABLE customer_transaction_collection_log ADD CONSTRAINT FK_D4FDBC749395C3F3 FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE customer_transaction_collection_log ADD CONSTRAINT FK_D4FDBC742FC0CB0F FOREIGN KEY (transaction_id) REFERENCES customer_transaction (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE customer_transaction_collection_log ADD CONSTRAINT FK_D4FDBC74537A1329 FOREIGN KEY (message_id) REFERENCES customer_message (id) ON DELETE CASCADE');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE customer_transaction_collection_log DROP FOREIGN KEY FK_D4FDBC74537A1329');
$this->addSql('ALTER TABLE customer_transaction_collection_log DROP FOREIGN KEY FK_D4FDBC742FC0CB0F');
$this->addSql('ALTER TABLE customer_transaction_collection_log DROP FOREIGN KEY FK_D4FDBC749395C3F3');
$this->addSql('ALTER TABLE customer_transaction DROP FOREIGN KEY FK_717C2ED98B81BE4F');
$this->addSql('ALTER TABLE customer_transaction DROP FOREIGN KEY FK_717C2ED96E89055B');
$this->addSql('ALTER TABLE customer_transaction DROP FOREIGN KEY FK_717C2ED984524C52');
$this->addSql('ALTER TABLE customer_transaction DROP FOREIGN KEY FK_717C2ED996E7E3BC');
$this->addSql('ALTER TABLE customer_transaction DROP FOREIGN KEY FK_717C2ED9670BAFFE');
$this->addSql('ALTER TABLE customer_transaction DROP FOREIGN KEY FK_717C2ED937BAC19A');
$this->addSql('ALTER TABLE customer_transaction DROP FOREIGN KEY FK_717C2ED92989F1FD');
$this->addSql('ALTER TABLE customer_transaction DROP FOREIGN KEY FK_717C2ED9A76ED395');
$this->addSql('ALTER TABLE customer_transaction DROP FOREIGN KEY FK_717C2ED99395C3F3');
$this->addSql('ALTER TABLE customer_transaction ADD metadata LONGTEXT DEFAULT NULL COLLATE utf8mb4_general_ci COMMENT \'(DC2Type:array)\' AFTER method');
$stmt = $this->connection->prepare('SELECT count(1) FROM customer_transaction');
$stmt->execute();
// get total entries
$total = $stmt->fetchColumn();
// insert using pagination
for ($i = 0; $i < ceil($total / 100); ++$i) {
$stmt = $this->connection->prepare(sprintf('SELECT * FROM customer_transaction ORDER BY id ASC LIMIT %d, 100', 100 * $i));
$stmt->execute();
while ($row = $stmt->fetch(\PDO::FETCH_OBJ)) {
$metadata = [];
switch ($row->method) {
case 'cc':
$metadata = [
'cc_type' => $row->card_brand,
'cc_owner' => $row->card_owner,
'cc_number' => $row->card_number,
'cc_expires_month' => $row->card_exp_month,
'cc_expires_year' => $row->card_exp_year,
'cc_cvc' => $row->card_cvc,
];
break;
case 'echk':
$metadata = [
'echk_owner' => $row->e_check_owner,
'echk_bank' => $row->e_check_bank_name,
'echk_account' => $row->e_check_account_number,
'echk_routing' => $row->e_check_routing,
'echk_number_check' => $row->e_check_number,
'echk_telephone' => $row->e_check_phone_number,
];
break;
case 'ccod':
$metadata = [
'ccod_number_check' => $row->cashier_check_number,
];
break;
case 'pcod':
case 'cod':
default:
// do nothing
}
$this->addSql('UPDATE customer_transaction SET metadata = :metadata WHERE id = :id', [
'id' => $row->id,
'metadata' => json_encode($metadata),
]);
}
}
$this->addSql('RENAME TABLE customer_transaction TO customers_transactions_history');
$this->addSql('ALTER TABLE customers_transactions_history ENGINE = InnoDB');
$this->addSql('ALTER TABLE customers_transactions_history CHARACTER SET = utf8mb4');
$this->addSql('
ALTER TABLE customers_transactions_history
DROP card_brand,
DROP card_owner,
DROP card_number,
DROP card_exp_month,
DROP card_exp_year,
DROP card_cvc,
DROP e_check_owner,
DROP e_check_bank_name,
DROP e_check_account_number,
DROP e_check_routing,
DROP e_check_number,
DROP e_check_phone_number,
DROP cashier_check_number,
ADD administrators_id_3 INT NOT NULL AFTER administrators_id_2,
ADD administrators_id_4 INT NOT NULL AFTER administrators_id_3,
ADD administrators_id_5 INT NOT NULL AFTER administrators_id_4,
CHANGE customer_id customers_id INT NOT NULL,
CHANGE user_id administrators_id INT NOT NULL,
CHANGE invoice_id customers_services_id INT NOT NULL,
CHANGE processor_id processors_id INT NOT NULL,
CHANGE collector_id collectors_id INT NOT NULL,
CHANGE rep1_id administrators_id_1 INT NOT NULL,
CHANGE rep2_id administrators_id_2 INT NOT NULL,
CHANGE ran_by_id ran_by INT NOT NULL,
CHANGE funded_by_id funded_by INT DEFAULT NULL,
CHANGE type customers_payment_status_id VARCHAR(255) DEFAULT NULL,
CHANGE method transaction_code VARCHAR(32) DEFAULT \'\',
CHANGE metadata transaction_value TEXT DEFAULT NULL,
CHANGE amount amount NUMERIC(15, 2) DEFAULT \'0.00\',
CHANGE late_fee late_fee NUMERIC(15, 2) DEFAULT \'0.00\',
CHANGE processing_fee proc_fee NUMERIC(15, 2) DEFAULT \'0.00\',
CHANGE points points NUMERIC(15, 2) DEFAULT \'0.00\',
CHANGE ppd ppd NUMERIC(15, 2) DEFAULT \'0.00\',
CHANGE color color VARCHAR(6) DEFAULT NULL,
CHANGE message comments TEXT DEFAULT NULL,
CHANGE ran_message ran_note VARCHAR(150) NOT NULL,
CHANGE ran_processed ran_processed TINYINT(1) NOT NULL DEFAULT \'0\',
CHANGE ran_at ran_date DATETIME DEFAULT NULL,
CHANGE funded_at funded_date DATETIME DEFAULT NULL,
CHANGE pending pending TINYINT(1) DEFAULT \'0\',
CHANGE processing processing TINYINT(1) DEFAULT \'0\',
CHANGE enabled status TINYINT(1) DEFAULT \'1\',
CHANGE due_at due_date DATETIME DEFAULT NULL,
CHANGE created_at date_added DATETIME DEFAULT NULL,
CHANGE modified_at last_modified DATETIME DEFAULT NULL
');
$this->addSql('UPDATE customers_transactions_history SET customers_payment_status_id = \'1\' WHERE customers_payment_status_id = \'scheduling\'');
$this->addSql('UPDATE customers_transactions_history SET customers_payment_status_id = \'2\' WHERE customers_payment_status_id = \'paid\'');
$this->addSql('UPDATE customers_transactions_history SET customers_payment_status_id = \'3\' WHERE customers_payment_status_id = \'returned\'');
$this->addSql('ALTER TABLE customers_transactions_history CHANGE customers_payment_status_id customers_payment_status_id INT NOT NULL');
$this->addSql('DROP INDEX IDX_717C2ED99395C3F3 ON customers_transactions_history');
$this->addSql('DROP INDEX IDX_717C2ED9A76ED395 ON customers_transactions_history');
$this->addSql('DROP INDEX IDX_717C2ED92989F1FD ON customers_transactions_history');
$this->addSql('DROP INDEX IDX_717C2ED937BAC19A ON customers_transactions_history');
$this->addSql('DROP INDEX IDX_717C2ED9670BAFFE ON customers_transactions_history');
$this->addSql('DROP INDEX IDX_717C2ED996E7E3BC ON customers_transactions_history');
$this->addSql('DROP INDEX IDX_717C2ED984524C52 ON customers_transactions_history');
$this->addSql('DROP INDEX IDX_717C2ED96E89055B ON customers_transactions_history');
$this->addSql('DROP INDEX IDX_717C2ED98B81BE4F ON customers_transactions_history');
$this->addSql('CREATE INDEX idx_administrators_id ON customers_transactions_history (administrators_id)');
$this->addSql('CREATE INDEX idx_customers_id ON customers_transactions_history (customers_id)');
$this->addSql('CREATE INDEX idx_customers_payment_status_id ON customers_transactions_history (customers_payment_status_id)');
$this->addSql('CREATE INDEX idx_customers_services_id ON customers_transactions_history (customers_services_id)');
$this->addSql('RENAME TABLE customer_transaction_collection_log TO customers_transactions_collections');
$this->addSql('ALTER TABLE customers_transactions_collections ENGINE = InnoDB');
$this->addSql('ALTER TABLE customers_transactions_collections CHARACTER SET = utf8mb4');
$this->addSql('
ALTER TABLE customers_transactions_collections
CHANGE customer_id customers_id INT NOT NULL,
CHANGE transaction_id customers_transactions_id INT NOT NULL,
CHANGE message_id customers_messages_id INT NOT NULL,
CHANGE created_at date_added DATETIME DEFAULT NULL
');
$this->addSql('DROP INDEX IDX_D4FDBC749395C3F3 ON customers_transactions_collections');
$this->addSql('DROP INDEX IDX_D4FDBC742FC0CB0F ON customers_transactions_collections');
$this->addSql('DROP INDEX IDX_D4FDBC74537A1329 ON customers_transactions_collections');
$this->addSql('CREATE INDEX idx_customers_id ON customers_transactions_collections (customers_id)');
$this->addSql('CREATE INDEX idx_customers_transactions_id ON customers_transactions_collections (customers_transactions_id)');
$this->addSql('CREATE INDEX idx_customers_messages_id ON customers_transactions_collections (customers_messages_id)');
}
}