<!-- Source: @site/projects/sqlens-for-laravel/docs/rules/pg-l4-rename-without-deploy-window.md -->
<!-- Canonical: https://docs.pushery.com/sqlens-for-laravel/rules/pg-l4-rename-without-deploy-window/ -->

# PG.L4.RENAME_WITHOUT_DEPLOY_WINDOW — The previous release still uses the old name

> A rename takes effect on commit. Every instance still serving the old release reads and writes a name that is gone.

- **Category:** safety
- **Level:** 4
- **Confidence:** heuristic
- **Downtime class:** `online`
- **Stability:** preview
- **Suites:** lint
- **Applies to:** PostgreSQL 18

A rename costs PostgreSQL almost nothing. It changes a name in the catalog, leaves the stored data as
it is and holds its lock only for that change, which is why no rule on levels 2 and 3 reports it.
This rule is about **timing**, the axis of `PG.L4.DROP_WITHOUT_DEPLOY_WINDOW`.

The new name takes effect the moment the migration commits. Between `migrate` and the switch, the
previous release serves the migrated schema: for seconds on every deploy, and until a deploy comes
through when a step after `migrate` fails. That release still selects, inserts and updates under the
old name, and each of those statements fails from that moment on.

## What it reads

- `Schema::rename()` writes `ALTER TABLE … RENAME TO …`, which renames a table.
- `renameColumn()` writes `ALTER TABLE … RENAME COLUMN … TO …`, which renames a column.
- `RENAME CONSTRAINT` is not reported. A query names a constraint only in the rare
  `ON CONFLICT ON CONSTRAINT`, and Laravel's `upsert()` names its columns instead.
- `renameIndex()` writes `ALTER INDEX … RENAME TO …`, which this rule does not read. No query names
  an index on PostgreSQL.

It stays silent on a table the same migration creates, and on a column the same migration adds
before it renames it. Neither existed for a release before this one.

## Why `heuristic`

The rule cannot see your deploy process or your code. A project that stops all traffic, migrates and
then starts the new release has no overlap window, and a rename of a name no release uses any more
breaks nothing. The finding names the risk. Whether it applies is something only your pipeline and
your code know.

## Flagged

```php
Schema::table('orders', fn (Blueprint $table) => $table->renameColumn('cart_generation', 'access_generation'));
```

## Preferred

```php
// Release N adds the column under its new name and writes both columns. Release N+1 reads
// the new one, and release N+2 drops the old one in its own migration:
Schema::table('orders', fn (Blueprint $table) => $table->integer('access_generation')->nullable());
```

## The expand–contract sequence for a rename

1. **Expand.** Add the column under its new name and ship a release that writes both and still reads
   the old one. A back-fill copies what the old column already holds.
2. **Migrate.** Ship a release that reads the new column. Wait for the old release to be gone.
3. **Contract.** Drop the old column, in its own migration.

A table cannot be added twice under two names. Its old name has to keep working through the window
instead, for example as a view under the old name that selects every column of the renamed table.
PostgreSQL lets such a view take reads and writes. The other way is a rename in a deploy that stops
the old release first.

## Sources

- [PostgreSQL 18 — `ALTER TABLE`](https://www.postgresql.org/docs/18/sql-altertable.html) — the
  `RENAME` forms change the name of a table, a column or a constraint and have no effect on the
  stored data
- [PostgreSQL 18 — `CREATE VIEW`](https://www.postgresql.org/docs/18/sql-createview.html) — a view
  with exactly one entry in its `FROM` list and no aggregates, window functions or set-returning
  functions in its select list is automatically updatable

<!-- BEGIN generated: remediation -->
## The fix material this rule carries

A finding from this rule carries machine-readable fix material. Which sequence depends on the
statement:

- **`expand_contract`** — Add the new shape, move readers and writers across, remove the old one — over three deploys.
- **`none`** — Looked at, and there is no safe standard sequence — a statement, not an absence.

The payload is material for you or an agent to apply. SQLens writes no migration and runs no
DDL. See [the remediation payload](https://docs.pushery.com/sqlens-for-laravel/remediation-schema.md) for every field, the placeholder
semantics, and the version rules a consumer has to follow.
<!-- END generated: remediation -->
