166 lines
8.6 KiB
Markdown
Executable File
166 lines
8.6 KiB
Markdown
Executable File
# Laravel 13 with Db2 LUW
|
|
|
|
This Laravel application includes a project-local Db2 driver built on PHP PDO_ODBC, unixODBC, and the IBM Db2 CLI driver. The verified setup uses Laravel 13.25.0, PHP 8.5.11 on macOS, and Db2 LUW 12.1.5.0 in Docker.
|
|
|
|
## Create a database in Docker
|
|
|
|
Run these commands from your Mac terminal. The first command shows the container name and published ports. This setup uses the `Db2-Server` container with port `50000` published to the Mac.
|
|
|
|
```bash
|
|
docker ps --format 'table {{.Names}}\t{{.Image}}\t{{.Ports}}'
|
|
docker exec -it Db2-Server bash
|
|
```
|
|
|
|
At the container's `root` prompt, switch to the Db2 instance owner.
|
|
|
|
```bash
|
|
su - db2inst1
|
|
```
|
|
|
|
Wait for the prompt to show `db2inst1`, then load the Db2 environment and list the databases.
|
|
|
|
```bash
|
|
. ~/sqllib/db2profile
|
|
db2 list db directory
|
|
```
|
|
|
|
If `LARAVEL` does not already appear in the database directory, create it alongside `MAIN`.
|
|
|
|
```bash
|
|
db2 create database LARAVEL
|
|
```
|
|
|
|
Connect to the new database and confirm the target before creating application objects.
|
|
|
|
```bash
|
|
db2 connect to LARAVEL
|
|
db2 "VALUES CURRENT SERVER"
|
|
db2 "CREATE SCHEMA DB2_TEST AUTHORIZATION DB2INST1"
|
|
db2 connect reset
|
|
```
|
|
|
|
Run `CREATE SCHEMA` once if you want the dedicated `DB2_TEST` schema. To use the connection's default schema, skip that statement and omit `DB2_SCHEMA` from Laravel's environment. With the local `db2inst1` account, the fallback previously resolved to `DB2INST1`. The database account needs privileges to create and manage application objects in the chosen schema.
|
|
|
|
## Register the ODBC data source on the Mac
|
|
|
|
Use a distinct DSN called `LARAVELDB` so the existing `MEDIUK` entry can still point to `MAIN`. Confirm PHP has the ODBC extension and locate the active unixODBC configuration.
|
|
|
|
```bash
|
|
php -r 'print_r(PDO::getAvailableDrivers());'
|
|
odbcinst -j
|
|
odbcinst -q -d
|
|
```
|
|
|
|
The driver list should include `odbc`, and unixODBC should list `IBM DB2 ODBC DRIVER`. Add this section once to the active `odbc.ini`. In the verified Mac setup the system file is `/opt/homebrew/etc/odbc.ini`. Back up the file before editing.
|
|
|
|
```ini
|
|
[LARAVELDB]
|
|
Driver=IBM DB2 ODBC DRIVER
|
|
Database=LARAVEL
|
|
Hostname=127.0.0.1
|
|
Port=50000
|
|
Protocol=TCPIP
|
|
```
|
|
|
|
Register the same alias with the IBM client. The commands below use the verified installation paths. If `DB2DSDRIVER_CFG_PATH` selects another file, back up and use that active configuration instead.
|
|
|
|
```bash
|
|
cp -p "$HOME/IBM/db2/clidriver/cfg/db2dsdriver.cfg" \
|
|
"$HOME/IBM/db2/clidriver/cfg/db2dsdriver.cfg.bak-$(date +%Y%m%d-%H%M%S)"
|
|
"$HOME/IBM/db2/clidriver/bin/db2cli" writecfg add \
|
|
-dsn LARAVELDB -database LARAVEL -host 127.0.0.1 -port 50000
|
|
odbcinst -q -s
|
|
"$HOME/IBM/db2/clidriver/bin/db2cli" validate -dsn LARAVELDB
|
|
```
|
|
|
|
`SQL1531N` with `LARAVELDB` already listed by unixODBC usually means the IBM client cannot find the alias in its active configuration. Check that the PHP process and your terminal use the same IBM client configuration.
|
|
|
|
## Configure Laravel and run migrations
|
|
|
|
In the project's `.env`, set the connection details and use your own credentials.
|
|
|
|
```dotenv
|
|
DB_CONNECTION=db2_local
|
|
DB2_DSN=LARAVELDB
|
|
DB2_DATABASE=LARAVEL
|
|
DB2_SCHEMA=DB2_TEST
|
|
DB2_USERNAME=db2inst1
|
|
DB2_PASSWORD=your_password
|
|
```
|
|
|
|
The DSN selects the actual target and supplies the host and port. `DB2_DATABASE` is Laravel's connection metadata. This connector gets its network settings from the DSN.
|
|
|
|
From the project directory, clear cached configuration and verify the database and schema through Laravel.
|
|
|
|
```bash
|
|
php artisan config:clear
|
|
php artisan tinker --execute='dump(DB::selectOne("SELECT CURRENT SERVER AS DB_NAME, CURRENT SCHEMA AS SCHEMA_NAME FROM SYSIBM.SYSDUMMY1"));'
|
|
```
|
|
|
|
When the output shows `LARAVEL` and your intended schema, run:
|
|
|
|
```bash
|
|
php artisan migrate
|
|
php artisan migrate:status
|
|
```
|
|
|
|
Create the database first. Migrations create application tables in that database. Existing data in `MAIN` stays there until you transfer it separately.
|
|
|
|
## Browse or remove schemas
|
|
|
|
DBeaver Community can connect using its **IBM Db2 LUW** driver. Use host `127.0.0.1`, port `50000`, database `LARAVEL`, and your Db2 credentials. DBeaver connects via JDBC and can have a separate connection for `MAIN`. [DBeaver Community download](https://dbeaver.com/edition/community/)
|
|
|
|
To inspect the old `DB2_TEST` schema, run the following in the same `db2inst1` shell inside Docker.
|
|
|
|
```bash
|
|
db2 connect to MAIN
|
|
db2 "VALUES CURRENT SERVER"
|
|
db2 "LIST TABLES FOR SCHEMA DB2_TEST"
|
|
```
|
|
|
|
After confirming `MAIN`, use `db2 "DROP SCHEMA DB2_TEST RESTRICT"` to remove an empty schema. `SQL0478N` means dependent objects remain. If you intend to permanently delete those objects and their data from `MAIN`, run:
|
|
|
|
```bash
|
|
db2 "CALL SYSPROC.ADMIN_DROP_SCHEMA('DB2_TEST', NULL, 'DB2INST1', 'DROP_DB2_TEST_ERRORS')"
|
|
db2 "SELECT SCHEMANAME FROM SYSCAT.SCHEMATA WHERE SCHEMANAME = 'DB2_TEST'"
|
|
db2 connect reset
|
|
```
|
|
|
|
Zero rows confirms removal. If the procedure reports an error table, inspect it for individual failures. `SQL1024N` means you need to connect to the database from the current shell first.
|
|
|
|
## How the driver works
|
|
|
|
| Component | Responsibility |
|
|
| --- | --- |
|
|
| [Database configuration](config/database.php) | Defines the `db2_local` connection with the `odbc` driver. |
|
|
| [AppServiceProvider](app/Providers/AppServiceProvider.php) | Registers the driver with Laravel, opens PDO using the DSN, and applies the configured schema or reads `CURRENT SCHEMA` for metadata. |
|
|
| [Db2Connection](app/Database/Db2/Db2Connection.php) | Wires up the Db2 builder, grammars, and processor. Decodes the internal NULL indicators on eligible result reads. |
|
|
| [Db2QueryBuilder](app/Database/Db2/Db2QueryBuilder.php) and [Db2QueryGrammar](app/Database/Db2/Db2QueryGrammar.php) | Compile Db2 pagination, savepoints, case-aware LIKE, and MERGE-based upserts and insert-or-ignore. Apply PDO_ODBC result handling when fetching eligible rows. |
|
|
| [Db2Processor](app/Database/Db2/Db2Processor.php) | Retrieves generated identities on the same connection after insertion. |
|
|
| [Db2SchemaGrammar](app/Database/Db2/Db2SchemaGrammar.php) | Converts Laravel schema blueprints into Db2 DDL for tables, columns, indexes, and constraints. |
|
|
| [Db2SchemaBuilder](app/Database/Db2/Db2SchemaBuilder.php) | Reads table, column, index, and foreign-key metadata from the Db2 catalog. |
|
|
|
|
The result-only SQL path adds NULL indicators where needed and formats nullable timestamps for Eloquent date casting. Embedded queries keep their declared columns, which is required for scalar subqueries, `WHERE IN`, and `UNION`.
|
|
|
|
On Db2 LUW, unqualified table names resolve in one current schema. Use a qualified table such as `DB::connection('db2_local')->table('ARCHIVE.CUSTOMERS')` to access another schema in the same database, subject to privileges. The IBM i Access `DBQ` library-list setting does not provide a table search path on Db2 LUW.
|
|
|
|
Laravel can also select its standard MySQL driver through `DB_CONNECTION=mysql` and the corresponding `DB_*` settings. Switching the connection leaves existing Db2 data in Db2. The release notes cover the verified Db2 setup.
|
|
|
|
## Developer workflow
|
|
|
|
1. Check the installed Illuminate APIs and the existing driver classes before editing. Keep changes in application code. Add accurate PHPDoc headers and explicit return types to methods you add or modify, matching inherited signatures.
|
|
2. Reproduce the problem through the normal Laravel entry point in a focused test under [tests/Feature/Db2](tests/Feature/Db2). Use a disposable database or schema. The Db2 tests explicitly use `db2_local`, and the release test creates and removes its own schema.
|
|
3. Run the focused test against Db2 before the fix. Check the SQL, catalog state, or PHP values relevant to the failure. Run PHP syntax checks on changed files and then rerun the focused and regression suites.
|
|
|
|
```bash
|
|
composer validate --no-check-publish --no-interaction
|
|
APP_ENV=testing php artisan test --do-not-cache-result tests/Feature/Db2/ReleaseCompatibilityTest.php
|
|
APP_ENV=testing php artisan test --do-not-cache-result tests/Feature/Db2
|
|
APP_ENV=testing php artisan test --do-not-cache-result
|
|
git diff --check
|
|
```
|
|
|
|
A skipped Db2 test means the connection or extension was unavailable. The general application tests use SQLite and the Db2 integration tests use the configured Db2 connection.
|
|
|
|
Review the [schema compatibility notes](DB2-SCHEMA-COMPATIBILITY.md) for type mappings, index limits, and when Db2 may require `REORG`. The [query compatibility notes](DB2-QUERY-COMPATIBILITY.md) describe the scope of NULL handling and query support. The [release verification](DB2-RELEASE-VERIFICATION.md) records the tested version matrix and the migration and authenticated passkey-page checks. Browser WebAuthn enrolment and sign-in were outside that verification.
|