|
| 1 | +# New Features |
| 2 | + |
| 3 | +## Checks support in `drizzle-kit` |
| 4 | + |
| 5 | +You can use drizzle-kit to manage your `check` constraint defined in drizzle-orm schema definition |
| 6 | + |
| 7 | +For example current drizzle table: |
| 8 | + |
| 9 | +```ts |
| 10 | +import { sql } from "drizzle-orm"; |
| 11 | +import { check, pgTable } from "drizzle-orm/pg-core"; |
| 12 | + |
| 13 | +export const users = pgTable( |
| 14 | + "users", |
| 15 | + (c) => ({ |
| 16 | + id: c.uuid().defaultRandom().primaryKey(), |
| 17 | + username: c.text().notNull(), |
| 18 | + age: c.integer(), |
| 19 | + }), |
| 20 | + (table) => ({ |
| 21 | + checkConstraint: check("age_check", sql`${table.age} > 21`), |
| 22 | + }) |
| 23 | +); |
| 24 | +``` |
| 25 | + |
| 26 | +will be generated into |
| 27 | + |
| 28 | +```sql |
| 29 | +CREATE TABLE IF NOT EXISTS "users" ( |
| 30 | + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, |
| 31 | + "username" text NOT NULL, |
| 32 | + "age" integer, |
| 33 | + CONSTRAINT "age_check" CHECK ("users"."age" > 21) |
| 34 | +); |
| 35 | +``` |
| 36 | + |
| 37 | +The same is supported in all dialects |
| 38 | + |
| 39 | +### Limitations |
| 40 | + |
| 41 | +- `generate` will work as expected for all check constraint changes. |
| 42 | +- `push` will detect only check renames and will recreate the constraint. All other changes to SQL won't be detected and will be ignored. |
| 43 | + |
| 44 | +So, if you want to change the constraint's SQL definition using only `push`, you would need to manually comment out the constraint, `push`, then put it back with the new SQL definition and `push` one more time. |
| 45 | + |
| 46 | +## Views support in `drizzle-kit` |
| 47 | + |
| 48 | +You can use drizzle-kit to manage your `views` defined in drizzle-orm schema definition. It will work with all existing dialects and view options |
| 49 | + |
| 50 | +### PostgreSQL |
| 51 | + |
| 52 | +For example current drizzle table: |
| 53 | + |
| 54 | +```ts |
| 55 | +import { sql } from "drizzle-orm"; |
| 56 | +import { |
| 57 | + check, |
| 58 | + pgMaterializedView, |
| 59 | + pgTable, |
| 60 | + pgView, |
| 61 | +} from "drizzle-orm/pg-core"; |
| 62 | + |
| 63 | +export const users = pgTable( |
| 64 | + "users", |
| 65 | + (c) => ({ |
| 66 | + id: c.uuid().defaultRandom().primaryKey(), |
| 67 | + username: c.text().notNull(), |
| 68 | + age: c.integer(), |
| 69 | + }), |
| 70 | + (table) => ({ |
| 71 | + checkConstraint: check("age_check", sql`${table.age} > 21`), |
| 72 | + }) |
| 73 | +); |
| 74 | + |
| 75 | +export const simpleView = pgView("simple_users_view").as((qb) => |
| 76 | + qb.select().from(users) |
| 77 | +); |
| 78 | + |
| 79 | +export const materializedView = pgMaterializedView( |
| 80 | + "materialized_users_view" |
| 81 | +).as((qb) => qb.select().from(users)); |
| 82 | +``` |
| 83 | + |
| 84 | +will be generated into |
| 85 | + |
| 86 | +```sql |
| 87 | +CREATE TABLE IF NOT EXISTS "users" ( |
| 88 | + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, |
| 89 | + "username" text NOT NULL, |
| 90 | + "age" integer, |
| 91 | + CONSTRAINT "age_check" CHECK ("users"."age" > 21) |
| 92 | +); |
| 93 | + |
| 94 | +CREATE VIEW "public"."simple_users_view" AS (select "id", "username", "age" from "users"); |
| 95 | + |
| 96 | +CREATE MATERIALIZED VIEW "public"."materialized_users_view" AS (select "id", "username", "age" from "users"); |
| 97 | +``` |
| 98 | + |
| 99 | +Views supported in all dialects, but materialized views are supported only in PostgreSQL |
| 100 | + |
| 101 | +#### Limitations |
| 102 | + |
| 103 | +- `generate` will work as expected for all view changes |
| 104 | +- `push` limitations: |
| 105 | + |
| 106 | +1. If you want to change the view's SQL definition using only `push`, you would need to manually comment out the view, `push`, then put it back with the new SQL definition and `push` one more time. |
| 107 | + |
| 108 | +## Updates for PostgreSQL enums behavior |
| 109 | + |
| 110 | +We've updated enum behavior in Drizzle with PostgreSQL: |
| 111 | + |
| 112 | +- Add value after or before in enum: With this change, Drizzle will now respect the order of values in the enum and allow adding new values after or before a specific one. |
| 113 | + |
| 114 | +- Support for dropping a value from an enum: In this case, Drizzle will attempt to alter all columns using the enum to text, then drop the existing enum and create a new one with the updated set of values. After that, all columns previously using the enum will be altered back to the new enum. |
| 115 | + |
| 116 | +> If the deleted enum value was used by a column, this process will result in a database error. |
| 117 | +
|
| 118 | +- Support for dropping an enum |
| 119 | + |
| 120 | +- Support for moving enums between schemas |
| 121 | + |
| 122 | +- Support for renaming enums |
0 commit comments