Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Check for Pg:

-added check constraints to migrate, pull, push
- added tests

* added check constraints to mysql
added some tests

* - added sqlite, turso checks
- added and updated tests

* removed console.log
merged beta to branch

* rewrote introspect tests
added introspect tests for checks

* added progress callbacks for introspect with checks

* updated tests for introspect

* Implement limit and order by in MySQL

* Add limit and order by to update and delete in SQLite

* Format

* Update SQLite test

* Format

* added postgresql generate handling for views

* added support for views and materialized views in postgres (push + introspect + generate)

* fixed build errors

* added alter for using

* added psql rename views and change schema handlings

* added generate for mysql views

* added introspect for mysql views

* Added sqlite and libsql view handling

* added all tests for all dialect views. updated introspect tests

* Updated introspect tests

* minor fixes

* fixed numeric introspect in pg

* fixed introspect numeric in pg

* fixed introspect numeric pg

* fixed sqlite + libsql alter existing handling

* updated psql introspect + sqlite introspect for views

* added progress callback to views. changed query columns for materialized views

* updated pg introspect columns query. updated mysql test.

* Updated enum handling for generate. Adde drop, add value, rename, set schema. Updated tests

* made test for enum ordering for push

* Added neon-serverless (websocket) tests, fixed neon-serverless session's lack of own count procedure (issue drizzle-team#3081)

* fixed lockfile

* Added leftover data deletion, imported table from common instead of re-creating locally

* Fixed lack of custom schema cleanup on locally remade skipped tests of neon-serverless

* squash add + drop enum values to drop only

* Upgrade to TS 5.6.3

* removed commented code

* Remove global set for node-postgres, neon and vercel; move to on per-query

* Fix import

* Fix test

* Fix view tests

* Check tests with docker

* Update tests

* Skip sqljs

* Add ignore on neon

* removed console logs in tests

* Updated drizzle constructors, removed monodriver

* Fixed type error

* Fixed sqlite instance checks

* Fixed mysql rename view sql statement

* Fixed tests, added MockDriver & mock support for reworked drivers

* Added `drizzle.mock()`, reworked replicas tests

* Fixed neon client identification

* Removed `bun-sqlite` from import tests

* Added missing connectionString param

* Fixed broken connection, added missing cases

* Updated versions for orm and kit

---------

Co-authored-by: Aleksandr Sherman <[email protected]>
Co-authored-by: Mario564 <[email protected]>
Co-authored-by: Sukairo-02 <[email protected]>
Co-authored-by: Sergey Reka <[email protected]>
  • Loading branch information
5 people authored Oct 15, 2024
1 parent 6974798 commit 6f4fc4c
Show file tree
Hide file tree
Showing 243 changed files with 14,561 additions and 3,410 deletions.
122 changes: 122 additions & 0 deletions changelogs/drizzle-kit/0.26.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# New Features

## Checks support in `drizzle-kit`

You can use drizzle-kit to manage your `check` constraint defined in drizzle-orm schema definition

For example current drizzle table:

```ts
import { sql } from "drizzle-orm";
import { check, pgTable } from "drizzle-orm/pg-core";

export const users = pgTable(
"users",
(c) => ({
id: c.uuid().defaultRandom().primaryKey(),
username: c.text().notNull(),
age: c.integer(),
}),
(table) => ({
checkConstraint: check("age_check", sql`${table.age} > 21`),
})
);
```

will be generated into

```sql
CREATE TABLE IF NOT EXISTS "users" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"username" text NOT NULL,
"age" integer,
CONSTRAINT "age_check" CHECK ("users"."age" > 21)
);
```

The same is supported in all dialects

### Limitations

- `generate` will work as expected for all check constraint changes.
- `push` will detect only check renames and will recreate the constraint. All other changes to SQL won't be detected and will be ignored.

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.

## Views support in `drizzle-kit`

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

### PostgreSQL

For example current drizzle table:

```ts
import { sql } from "drizzle-orm";
import {
check,
pgMaterializedView,
pgTable,
pgView,
} from "drizzle-orm/pg-core";

export const users = pgTable(
"users",
(c) => ({
id: c.uuid().defaultRandom().primaryKey(),
username: c.text().notNull(),
age: c.integer(),
}),
(table) => ({
checkConstraint: check("age_check", sql`${table.age} > 21`),
})
);

export const simpleView = pgView("simple_users_view").as((qb) =>
qb.select().from(users)
);

export const materializedView = pgMaterializedView(
"materialized_users_view"
).as((qb) => qb.select().from(users));
```

will be generated into

```sql
CREATE TABLE IF NOT EXISTS "users" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"username" text NOT NULL,
"age" integer,
CONSTRAINT "age_check" CHECK ("users"."age" > 21)
);

CREATE VIEW "public"."simple_users_view" AS (select "id", "username", "age" from "users");

CREATE MATERIALIZED VIEW "public"."materialized_users_view" AS (select "id", "username", "age" from "users");
```

Views supported in all dialects, but materialized views are supported only in PostgreSQL

#### Limitations

- `generate` will work as expected for all view changes
- `push` limitations:

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.

## Updates for PostgreSQL enums behavior

We've updated enum behavior in Drizzle with PostgreSQL:

- 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.

- 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.

> If the deleted enum value was used by a column, this process will result in a database error.
- Support for dropping an enum

- Support for moving enums between schemas

- Support for renaming enums
85 changes: 85 additions & 0 deletions changelogs/drizzle-orm/0.35.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Important change after 0.34.0 release

## Updated the init Drizzle database API

The API from version 0.34.0 turned out to be unusable and needs to be changed. You can read more about our decisions in [this discussion](https://github.com/drizzle-team/drizzle-orm/discussions/3097)

If you still want to use the new API introduced in 0.34.0, which can create driver clients for you under the hood, you can now do so
```ts
import { drizzle } from "drizzle-orm/node-postgres";

const db = drizzle(process.env.DATABASE_URL);
// or
const db = drizzle({
connection: process.env.DATABASE_URL
});
const db = drizzle({
connection: {
user: "...",
password: "...",
host: "...",
port: 4321,
db: "...",
},
});

// if you need to pass logger or schema
const db = drizzle({
connection: process.env.DATABASE_URL,
logger: true,
schema: schema,
});
```

in order to not introduce breaking change - we will still leave support for deprecated API until V1 release.
It will degrade autocomplete performance in connection params due to `DatabaseDriver` | `ConnectionParams` types collision,
but that's a decent compromise against breaking changes

```ts
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";

const client = new Pool({ connectionString: process.env.DATABASE_URL });
const db = drizzle(client); // deprecated but available

// new version
const db = drizzle({
client: client,
});
```

# New Features

## New .orderBy() and .limit() functions in update and delete statements SQLite and MySQL

You now have more options for the `update` and `delete` query builders in MySQL and SQLite

**Example**

```ts
await db.update(usersTable).set({ verified: true }).limit(2).orderBy(asc(usersTable.name));

await db.delete(usersTable).where(eq(usersTable.verified, false)).limit(1).orderBy(asc(usersTable.name));
```

## New `drizzle.mock()` function

There were cases where you didn't need to provide a driver to the Drizzle object, and this served as a workaround
```ts
const db = drizzle({} as any)
```

Now you can do this using a mock function
```ts
const db = drizzle.mock()
```

There is no valid production use case for this, but we used it in situations where we needed to check types, etc., without making actual database calls or dealing with driver creation. If anyone was using it, please switch to using mocks now

# Internal updates

- Upgraded TS in codebase to the version 5.6.3

# Bug fixes

- [[BUG]: New $count API error with @neondatabase/serverless](https://github.com/drizzle-team/drizzle-orm/issues/3081)
2 changes: 1 addition & 1 deletion drizzle-kit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drizzle-kit",
"version": "0.25.0",
"version": "0.26.0",
"homepage": "https://orm.drizzle.team",
"keywords": [
"drizzle",
Expand Down
6 changes: 6 additions & 0 deletions drizzle-kit/src/@types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ declare global {
squashSpaces(): string;
capitalise(): string;
camelCase(): string;
snake_case(): string;

concatIf(it: string, condition: boolean): string;
}

Expand Down Expand Up @@ -44,6 +46,10 @@ String.prototype.concatIf = function(it: string, condition: boolean) {
return condition ? `${this}${it}` : String(this);
};

String.prototype.snake_case = function() {
return this && this.length > 0 ? `${this.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`)}` : String(this);
};

Array.prototype.random = function() {
return this[~~(Math.random() * this.length)];
};
Expand Down
16 changes: 13 additions & 3 deletions drizzle-kit/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { randomUUID } from 'crypto';
import type { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';
import { LibSQLDatabase } from 'drizzle-orm/libsql';
import type { MySql2Database } from 'drizzle-orm/mysql2';
import { PgDatabase } from 'drizzle-orm/pg-core';
import {
columnsResolver,
enumsResolver,
mySqlViewsResolver,
schemasResolver,
sequencesResolver,
sqliteViewsResolver,
tablesResolver,
viewsResolver,
} from './cli/commands/migrate';
import { pgPushIntrospect } from './cli/commands/pgIntrospect';
import { pgSuggestions } from './cli/commands/pgPushUtils';
Expand Down Expand Up @@ -45,6 +47,8 @@ export const generateDrizzleJson = (
prepared.enums,
prepared.schemas,
prepared.sequences,
prepared.views,
prepared.matViews,
casing,
schemaFilters,
);
Expand Down Expand Up @@ -76,6 +80,7 @@ export const generateMigration = async (
sequencesResolver,
tablesResolver,
columnsResolver,
viewsResolver,
validatedPrev,
validatedCur,
);
Expand Down Expand Up @@ -119,6 +124,7 @@ export const pushSchema = async (
sequencesResolver,
tablesResolver,
columnsResolver,
viewsResolver,
validatedPrev,
validatedCur,
'push',
Expand Down Expand Up @@ -151,7 +157,7 @@ export const generateSQLiteDrizzleJson = async (

const id = randomUUID();

const snapshot = generateSqliteSnapshot(prepared.tables, casing);
const snapshot = generateSqliteSnapshot(prepared.tables, prepared.views, casing);

return {
...snapshot,
Expand All @@ -177,6 +183,7 @@ export const generateSQLiteMigration = async (
squashedCur,
tablesResolver,
columnsResolver,
sqliteViewsResolver,
validatedPrev,
validatedCur,
);
Expand Down Expand Up @@ -217,6 +224,7 @@ export const pushSQLiteSchema = async (
squashedCur,
tablesResolver,
columnsResolver,
sqliteViewsResolver,
validatedPrev,
validatedCur,
'push',
Expand Down Expand Up @@ -255,7 +263,7 @@ export const generateMySQLDrizzleJson = async (

const id = randomUUID();

const snapshot = generateMySqlSnapshot(prepared.tables, casing);
const snapshot = generateMySqlSnapshot(prepared.tables, prepared.views, casing);

return {
...snapshot,
Expand All @@ -281,6 +289,7 @@ export const generateMySQLMigration = async (
squashedCur,
tablesResolver,
columnsResolver,
mySqlViewsResolver,
validatedPrev,
validatedCur,
);
Expand Down Expand Up @@ -322,6 +331,7 @@ export const pushMySQLSchema = async (
squashedCur,
tablesResolver,
columnsResolver,
mySqlViewsResolver,
validatedPrev,
validatedCur,
'push',
Expand Down
16 changes: 14 additions & 2 deletions drizzle-kit/src/cli/commands/introspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import { dryPg, type PgSchema, squashPgScheme } from '../../serializer/pgSchema'
import { fromDatabase as fromPostgresDatabase } from '../../serializer/pgSerializer';
import { drySQLite, type SQLiteSchema, squashSqliteScheme } from '../../serializer/sqliteSchema';
import { fromDatabase as fromSqliteDatabase } from '../../serializer/sqliteSerializer';
import { applyMysqlSnapshotsDiff, applyPgSnapshotsDiff, applySqliteSnapshotsDiff } from '../../snapshotsDiffer';
import {
applyLibSQLSnapshotsDiff,
applyMysqlSnapshotsDiff,
applyPgSnapshotsDiff,
applySqliteSnapshotsDiff,
} from '../../snapshotsDiffer';
import { prepareOutFolder } from '../../utils';
import type { Casing, Prefix } from '../validations/common';
import { LibSQLCredentials } from '../validations/libsql';
Expand All @@ -25,9 +30,12 @@ import { IntrospectProgress } from '../views';
import {
columnsResolver,
enumsResolver,
mySqlViewsResolver,
schemasResolver,
sequencesResolver,
sqliteViewsResolver,
tablesResolver,
viewsResolver,
writeResult,
} from './migrate';

Expand Down Expand Up @@ -100,6 +108,7 @@ export const introspectPostgres = async (
sequencesResolver,
tablesResolver,
columnsResolver,
viewsResolver,
dryPg,
schema,
);
Expand Down Expand Up @@ -210,6 +219,7 @@ export const introspectMysql = async (
squashMysqlScheme(schema),
tablesResolver,
columnsResolver,
mySqlViewsResolver,
dryMySql,
schema,
);
Expand Down Expand Up @@ -321,6 +331,7 @@ export const introspectSqlite = async (
squashSqliteScheme(schema),
tablesResolver,
columnsResolver,
sqliteViewsResolver,
drySQLite,
schema,
);
Expand Down Expand Up @@ -427,11 +438,12 @@ export const introspectLibSQL = async (
const { snapshots, journal } = prepareOutFolder(out, 'sqlite');

if (snapshots.length === 0) {
const { sqlStatements, _meta } = await applySqliteSnapshotsDiff(
const { sqlStatements, _meta } = await applyLibSQLSnapshotsDiff(
squashSqliteScheme(drySQLite),
squashSqliteScheme(schema),
tablesResolver,
columnsResolver,
sqliteViewsResolver,
drySQLite,
schema,
);
Expand Down
Loading

0 comments on commit 6f4fc4c

Please sign in to comment.