PHPackages                             woodholly/atk4-migrations - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. woodholly/atk4-migrations

ActiveLibrary[Database &amp; ORM](/categories/database)

woodholly/atk4-migrations
=========================

Database migrations for ATK4 Data using Doctrine Migrations

v0.1.0(7mo ago)01MITPHPPHP ^7.4 || ^8.0

Since Nov 23Pushed 7mo agoCompare

[ Source](https://github.com/woodholly/atk4-migrations)[ Packagist](https://packagist.org/packages/woodholly/atk4-migrations)[ RSS](/packages/woodholly-atk4-migrations/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (7)Versions (2)Used By (0)

ATK4 Migrations
===============

[](#atk4-migrations)

Database migrations for ATK4 Data using Doctrine Migrations.

Table of Contents
-----------------

[](#table-of-contents)

- [Overview](#overview)
- [Features](#features)
- [Installation](#installation)
- [Indexes and Foreign Keys](#indexes-and-foreign-keys)
- [Quick Start](#quick-start)
- [Available Commands](#available-commands)
- [Table Renaming](#table-renaming)
- [Fixing Mistakes](#fixing-mistakes)
- [Advanced](#advanced)
- [Configuration](#configuration)
- [Requirements](#requirements)
- [Testing](#testing)

Overview
--------

[](#overview)

Automatic database migrations for ATK4 Data using Doctrine Migrations. Define your schema in ATK4 Models, generate migrations automatically.

**Works with any database state:** Empty, existing, or production databases. The tool compares your models with the current database and generates only the necessary changes (CREATE, ALTER, or nothing if already in sync).

Features
--------

[](#features)

- **Automatic Schema Detection** - Compares models with database, generates only necessary changes
- **Full Rollback Support** - Reverse any migration
- **Zero Schema Duplication** - Schema defined once in ATK4 Models
- **Production Ready** - Built on Doctrine Migrations
- **Multi-Database** - MySQL, PostgreSQL, SQLite, Oracle, SQL Server
- **Declarative Constraints** - Define indexes and foreign keys in models (extended Model class)

Installation
------------

[](#installation)

```
composer require woodholly/atk4-migrations
```

Indexes and Foreign Keys
------------------------

[](#indexes-and-foreign-keys)

ATK4 Data lacks declarative index/FK support. This package provides `Atk4\Migrations\Model` - an extended Model class that adds:

- Field options: `'index' => true`, `'unique' => true` in `addField()`
- Relationship options: `'onDelete'`, `'onUpdate'`, `'index'`, `'unique'` in `hasOne()`
- Methods: `addIndex()`, `addForeignKey()`, `getIndexes()`, `getForeignKeys()`

**Usage:** Replace `use Atk4\Data\Model` → `use Atk4\Migrations\Model`

> **Note:** Standard `Atk4\Data\Model` works with migrations, but requires manual index/FK editing in each migration file. `Atk4\Migrations\Model` defines constraints declaratively. We hope this will be added to ATK4 Data core eventually.

**Example:**

```
use Atk4\Migrations\Model;

class Post extends Model
{
    public $table = 'post';

    protected function init(): void
    {
        parent::init();

        // Indexes
        $this->addField('title', ['index' => true]);        // Regular index
        $this->addField('slug', ['unique' => true]);        // Unique index
        $this->addIndex(['title', 'slug']);                 // Composite index

        // Foreign keys (CASCADE, RESTRICT, SET NULL, NO ACTION, SET DEFAULT)
        $this->hasOne('user_id', [
            'model' => [User::class],
            'onDelete' => 'CASCADE',
            'index' => true,                                 // FK + index
        ]);

        // Composite foreign key
        $this->addForeignKey(['product_id', 'warehouse_id'], [
            'foreignTable' => 'inventory',
            'foreignColumns' => ['product_id', 'warehouse_id'],
            'onDelete' => 'RESTRICT',
        ]);
    }
}
```

Quick Start
-----------

[](#quick-start)

### 1. Create Configuration

[](#1-create-configuration)

Create `migrations.php` in project root (or `config/migrations.php` for better organization):

```
