phalcon / migrations

Generate or migrate database changes via migrations.
https://docs.phalcon.io/latest/en/db-migrations
BSD 3-Clause "New" or "Revised" License
28 stars 23 forks source link

Dropped tables don't get removed via migration #103

Closed ghost closed 3 months ago

ghost commented 3 years ago

Dropped tables don't get removed via migration.

Steps to reproduce:

Insert data / tables to db

-- Server-Version: 10.4.17-MariaDB
-- PHP-Version: 7.4.15

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

CREATE DATABASE IF NOT EXISTS `test` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE `test`;

CREATE TABLE `car` (
  `id` int(11) NOT NULL,
  `license_plate` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL,
  `owner` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `driver` (
  `id` int(11) NOT NULL,
  `name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `inserted_at` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE `car`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `license_plate` (`license_plate`),
  ADD KEY `FK_driver` (`owner`);

ALTER TABLE `driver`
  ADD PRIMARY KEY (`id`),
  ADD KEY `name` (`name`);

ALTER TABLE `car`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `driver`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `car`
  ADD CONSTRAINT `FK_driver` FOREIGN KEY (`owner`) REFERENCES `driver` (`id`);
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

create migration

vendor/bin/phalcon-migrations generate --descr=mig

Phalcon Migrations

  Success: Version 1614024400249000_mig was successfully generated

delete a table

DROP TABLE `car`;

// only one table is left

grafik

create a migration again to save the deletion of the table

vendor/bin/phalcon-migrations generate --descr=mig

Phalcon Migrations

  Success: Version 1614024518829813_mig was successfully generated

delete the table which is left and try to run the migrations

DROP TABLE `driver`;

// after dropping the table try to run the migrations

vendor/bin/phalcon-migrations run

Phalcon Migrations

  Success: Version 1614024400249000_mig was successfully migrated
  Success: Version 1614024518829813_mig was successfully migrated

The dropped table appears:

grafik

Jeckerson commented 3 years ago

Ideally, table drop must be manual, as not always you want to remove the table in production. But there might be configuration option to scan the difference.

For now, you can simply generate empty migration and inside up() method call $this->getConnection()->dropTable('table_name')

CleverHosting commented 6 months ago

... ->dropTable('table_name', 'schema');

Without schema it don't work for me in 5.x