Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to `initphp/database` are documented here. The format is bas
on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and this project
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [5.0.0] - 2026-11-22

### Changed (breaking)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Topic-by-topic guides live in [`docs/`](docs/Home.md):
| 07 | [Query log](docs/Query-Logging.md) — `enableQueryLog` + the `log` connection channel |
| 08 | [Datatables](docs/DataTables-Introduction.md) — server-side DataTables.js integration end-to-end |
| 09 | [Multiple connections](docs/Multiple-Connections.md) — secondary databases via `DB::connect()` / `Model::$credentials` |
| 10 | [Upgrading from 3.x / 4.x](docs/Migration-Guide.md) — breaking changes in 5.0 and the migration path |
| 10 | [Upgrading from 3.x / 4.x](docs/Migration-Guide.md) — breaking changes in 4.0, the PHP floor of 5.0 and the migration path |

## At a glance

Expand Down
2 changes: 1 addition & 1 deletion docs/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Anything PDO supports. The package ships per-driver SQL dialects for MySQL, Post

### Q. Why does my second `DB::createImmutable()` throw?

That is the new 5.0 behaviour — `createImmutable()` enforces "set exactly once". If you need to swap the shared connection (test fixtures, multi-tenant routing), use `DB::replaceImmutable()`. See [Migration Guide](Migration-Guide.md).
That is the behaviour since 4.0 — `createImmutable()` enforces "set exactly once". If you need to swap the shared connection (test fixtures, multi-tenant routing), use `DB::replaceImmutable()`. See [Migration Guide](Migration-Guide.md).

### Q. Can I use the package without `DB::createImmutable()` at all?

Expand Down
2 changes: 1 addition & 1 deletion docs/Home.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ The package re-exports the [InitORM](https://github.com/InitORM) stack under the
- **Building an ORM layer?** See [Models](Models.md) and [Entities](Entities.md).
- **Wiring up DataTables.js?** Start with [DataTables Introduction](DataTables-Introduction.md).
- **Need a specific pattern?** Browse the [Recipes](Recipes.md).
- **Upgrading from 4.x?** Read the [Migration Guide](Migration-Guide.md).
- **Upgrading from 3.x or 4.x?** Read the [Migration Guide](Migration-Guide.md).

## Feature matrix

Expand Down
65 changes: 42 additions & 23 deletions docs/Migration-Guide.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
# Migration Guide — 4.x → 5.0
# Migration Guide — 3.x → 4.0 → 5.0

5.0 is the first release that crossed the bar of breaking BC on purpose. The 4.x line worked but shipped with a handful of mismatches against upstream and a few latent bugs in the DataTables helper. This page is the migration path.
This page covers two upgrades:

## Why 5.0
- [4.x → 5.0](#4x--50) — the PHP floor and the code style only; the API is unchanged.
- [3.x → 4.0](#3x--40) — the breaking API changes (the `DB` facade and the DataTables helper).

- The 4.x `composer.json` declared `php: >= 8.0` but its required dependencies refused to install on PHP 8.0. The runtime requirement is being raised to match reality.
If you are coming from 3.x, read both: 5.0 only adds the PHP `^8.2` floor on top of 4.0.

## 4.x → 5.0

| Area | 4.x | 5.0 |
|---|---|---|
| PHP minimum | `^8.1` | `^8.2` |
| Code style | PSR-12 (PHP_CodeSniffer) | PER-CS (php-cs-fixer); source reformatted |
| Public API and behaviour | | unchanged |

What to do: run on PHP 8.2 or later (`"php": "^8.2"` in your `composer.json`). PHP 8.1 is no longer supported. Nothing else changes for users of the package; the code-style move only affects contributors.

## 3.x → 4.0

4.0 is the first release that crossed the bar of breaking BC on purpose. The 3.x line worked but shipped with a handful of mismatches against upstream and a few latent bugs in the DataTables helper. This section is the migration path.

### Why 4.0

- The 3.x `composer.json` declared `php: >= 8.0` but its required dependencies refused to install on PHP 8.0. The runtime requirement is being raised to match reality.
- `DB::createImmutable()` diverged from the upstream `InitORM\Database\Facade\DB` contract — silently overwriting the shared instance on a second call. Aligning the two changes public behaviour.
- The DataTables helper carried bugs (SQLite-incompatible result handling, type errors on string-typed payloads, `recordsFiltered === recordsTotal` regardless of the search filter). Fixing them required reshaping the class.

## At a glance
### At a glance

| Area | 4.x | 5.0 |
| Area | 3.x | 4.0 |
|---|---|---|
| PHP minimum | declared `>= 8.0` (broken — install failed there) | `^8.1` |
| `DB::createImmutable()` second call | silently overwrote the shared instance | throws `DatabaseException` |
Expand All @@ -23,18 +42,18 @@
| `recordsFiltered` | always equal to `recordsTotal` | computed separately when a search is active |
| Identifier escaping | already correct in 3.x+ via InitORM | unchanged — regression tests added |

## Step-by-step migration
### Step-by-step migration

### 1. Bump PHP to 8.2+
#### 1. Bump PHP to 8.1+

Update your CI / Dockerfile / runtime to PHP 8.2 or later. The 5.0 release raised the floor from 8.0 to 8.1; the current release requires `^8.2`. The package will not install on older versions.
Update your CI / Dockerfile / runtime to PHP 8.1 or later (8.2 or later for 5.0, see above). The 4.0 release raises the floor from 8.0 to `^8.1`. The package will not install on older versions.

```diff
- "php": "8.0",
+ "php": "8.2",
+ "php": "8.1",
```

### 2. Adjust `DB::createImmutable()` callers
#### 2. Adjust `DB::createImmutable()` callers

If you call `createImmutable()` more than once intentionally (between requests in a worker, in test fixtures), switch the subsequent calls to `replaceImmutable()`:

Expand All @@ -49,7 +68,7 @@ If you call `createImmutable()` more than once intentionally (between requests i

If you were relying on the `void` return of `createImmutable()`, no change is required — ignoring the new return value is harmless.

### 3. Drop instance-style facade use
#### 3. Drop instance-style facade use

```diff
- $db = new DB();
Expand All @@ -59,7 +78,7 @@ If you were relying on the `void` return of `createImmutable()`, no change is re

The static surface is unchanged; only the (unintentional) instance path is gone.

### 4. Update the DataTables import path
#### 4. Update the DataTables import path

```diff
- use InitPHP\Database\Utils\Datatables;
Expand All @@ -68,7 +87,7 @@ The static surface is unchanged; only the (unintentional) instance path is gone.

The class name is the same — only the namespace moved one level deeper. The public API (`__construct`, `__call`, `setColumns`, `addRender`, `addPermanentSelect`, `orderBySave`, `handle`, `toArray`, `__toString`) is unchanged.

### 5. (Optional) Inject the DataTables request payload
#### 5. (Optional) Inject the DataTables request payload

The constructor signature is now:

Expand All @@ -87,13 +106,13 @@ $request = new RequestParser($psr7Request->getParsedBody());
$dt = new Datatables(DB::getDatabase(), $request);
```

### 6. Expect a different `recordsFiltered` value
#### 6. Expect a different `recordsFiltered` value

If your client previously coped with `recordsFiltered === recordsTotal` even during a search, it will now see the **correct** smaller value when a search is active. The two paginate / show-X-of-Y behaviours converge to whatever DataTables.js does by default.

### 7. Fix entity mutators that wrote `$this->column = $value`
#### 7. Fix entity mutators that wrote `$this->column = $value`

Not strictly a 5.0 change — this was always wrong — but PHP 8.2+ emits deprecation notices and a future PHP release makes it fatal. Inside a `set{Column}Attribute($value)` method, always write through `setAttribute()`:
Not strictly a 4.0 change — this was always wrong — but PHP 8.2+ emits deprecation notices and a future PHP release makes it fatal. Inside a `set{Column}Attribute($value)` method, always write through `setAttribute()`:

```diff
public function setTitleAttribute(string $value): void
Expand All @@ -105,26 +124,26 @@ Not strictly a 5.0 change — this was always wrong — but PHP 8.2+ emits depre

See [Entities](Entities.md) for the long version.

## Migrating from 3.x
### Notes for 3.x users

3.x is the version that moved off the pre-InitORM compiler and onto the upstream stack. Most code that worked on 3.x still works on 5.0 — the additional 5.0 changes apply on top. The big things to watch out for relative to 3.x:
3.x is the version that moved off the pre-InitORM compiler and onto the upstream stack. Most code that worked on 3.x still works on 4.0 and 5.0 — the steps above are the changes on top. The big things to watch out for relative to 3.x:

- `replaceImmutable()` did not exist; the only way to swap was a second `createImmutable()` (which used to be silent). Same fix as above — switch to `replaceImmutable()`.
- The DataTables namespace move (step 4 above) applies.

## Migrating from 2.x
### Migrating from 2.x

2.x predated InitORM. The migration is closer to a rewrite — `getError()`, identifier escaping rules, model schema, and entity hooks all changed shape. If you are still on 2.x, the practical recommendation is:

1. Cut a branch.
2. Replace every `getError()` call with `try / catch` — exceptions now drive failure paths.
3. Remove any manual backtick / quote wrapping you did to work around the v2 reserved-keyword bug (see [Reserved Keywords](Reserved-Keywords.md)).
4. Audit `protected $entity = '\App\…';` declarations — the 5.0 type is `string` (was untyped).
4. Audit `protected $entity = '\App\…';` declarations — the 4.0+ type is `string` (was untyped).
5. Run your test suite; expect the bulk of changes to be ergonomic, not semantic.

If you hit something that does not have a clear migration path, [open an issue](https://github.com/InitPHP/Database/issues/new) — we'll add it here.

## Things that did NOT change in 5.0
## Things that did NOT change in 4.0 and 5.0

- The QueryBuilder surface (`select`, `where`, `join`, `groupBy`, …).
- CRUD signatures (`create`, `read`, `update`, `delete`, plus the `*Batch` siblings).
Expand All @@ -139,6 +158,6 @@ If something breaks during the upgrade and the fix isn't on this page, please op

- The version you upgraded from.
- A minimal code sample that worked on the old version.
- The error you see on 5.0.
- The error you see on the new version.

Documentation gaps are bugs and get fixed on the same day.
6 changes: 3 additions & 3 deletions docs/Reserved-Keywords.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Reserved Keywords

A column named after a SQL reserved keyword — `order`, `select`, `from`, `where`, `group`, `desc`, `key` — used to be a problem in earlier versions of this package. As of 5.0 it **is not**. The underlying [InitORM QueryBuilder](https://github.com/InitORM/QueryBuilder) escapes every identifier driver-specifically, and the package has regression tests in place to lock this behaviour in.
A column named after a SQL reserved keyword — `order`, `select`, `from`, `where`, `group`, `desc`, `key` — used to be a problem in earlier versions of this package. As of 3.0 it **is not**. The underlying [InitORM QueryBuilder](https://github.com/InitORM/QueryBuilder) escapes every identifier driver-specifically, and the package has regression tests in place to lock this behaviour in.

## The short version

Expand Down Expand Up @@ -163,12 +163,12 @@ Both statements compile and run on MySQL, PostgreSQL, and SQLite without changes

## History — why this used to be an issue

In `initphp/database` 2.x (before the 3.0 rewrite onto InitORM), the package emitted identifiers bare. A column named `order` produced SQL like `SET order = 1`, which MySQL rejects with `error 1064`. The 3.0 release migrated the package to InitORM, which already escaped identifiers correctly, so the bug disappeared as a side-effect. The 5.0 release added the regression tests to keep it that way.
In `initphp/database` 2.x (before the 3.0 rewrite onto InitORM), the package emitted identifiers bare. A column named `order` produced SQL like `SET order = 1`, which MySQL rejects with `error 1064`. The 3.0 release migrated the package to InitORM, which already escaped identifiers correctly, so the bug disappeared as a side-effect. The 4.0 release added the regression tests to keep it that way.

If you see the v2 issue described on the issue tracker, it has been closed — that behaviour does not exist on supported versions.

## Next

- [Query Builder](Query-Builder.md) — the broader API.
- [Raw Queries](Raw-Queries.md) — when escaping is your responsibility.
- [Migration Guide](Migration-Guide.md) — what else changed in 5.0.
- [Migration Guide](Migration-Guide.md) — what else changed in 4.0 and 5.0.
6 changes: 3 additions & 3 deletions docs/Troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ You called `DB::select(...)` (or any method) before `DB::createImmutable([...])`

### `An immutable Database instance has already been set`

You called `DB::createImmutable([...])` a second time. In 5.0 this throws. Use `DB::replaceImmutable($connection)` for explicit swapping. See [Migration Guide](Migration-Guide.md).
You called `DB::createImmutable([...])` a second time. Since 4.0 this throws. Use `DB::replaceImmutable($connection)` for explicit swapping. See [Migration Guide](Migration-Guide.md).

### Tests bleed state between cases

Expand Down Expand Up @@ -169,11 +169,11 @@ Inside the mutator body, you wrote `$this->column = $value` instead of `$this->s

### `recordsTotal` equals `recordsFiltered` even during a search

You are on the old 4.x. Upgrade to 5.0 — the helper now computes them separately. See [Migration Guide](Migration-Guide.md).
You are on 3.x. Upgrade to 4.0 or later — the helper now computes them separately. See [Migration Guide](Migration-Guide.md).

### `TypeError: limit(): Argument #1 ($limit) must be of type int, string given`

You are on 4.x and the DataTables payload arrived with `length` as a string. Upgrade to 5.0 — the new `RequestParser` coerces.
You are on 3.x and the DataTables payload arrived with `length` as a string. Upgrade to 4.0 or later — the new `RequestParser` coerces.

### DataTables shows "Loading..." indefinitely

Expand Down
2 changes: 1 addition & 1 deletion docs/_Sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@
- [Reserved Keywords](Reserved-Keywords.md)

**Migration & Help**
- [Migration Guide (4.x → 5.0)](Migration-Guide.md)
- [Migration Guide (3.x → 4.0 → 5.0)](Migration-Guide.md)
- [FAQ](FAQ.md)
- [Troubleshooting](Troubleshooting.md)
Loading