Skip to content

Commit

Permalink
Merge branch 'drizzle-seed/studio' of https://github.com/drizzle-team…
Browse files Browse the repository at this point in the history
…/drizzle-orm into drizzle-seed/studio
  • Loading branch information
OleksiiKH0240 committed Dec 25, 2024
2 parents fa9f4b7 + 5ee9619 commit 5d7860c
Show file tree
Hide file tree
Showing 37 changed files with 2,399 additions and 350 deletions.
1 change: 1 addition & 0 deletions changelogs/drizzle-orm/0.38.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix incorrect deprecation detection for table declarations
149 changes: 94 additions & 55 deletions changelogs/drizzle-seed/0.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,86 +42,125 @@ Each update with breaking changes for generators will be documented on our docs
## Breaking changes

### `interval` unique generator was changed and upgraded to v2
### `string` generators were changed and upgraded to v2

## New Features

- added `fields` as new parameter in `interval` generator
```ts
await seed(db, { table }).refine((f) => ({
table: {
columns: {
// this function usage will output different values with the same `seed` number from previous version
column1: f.interval({ isUnique: true }),
}
}
}))
```

`interval` generator generates intervals based on the following principle:
**Reason for upgrade**
An older version of the generator could produce intervals like `1 minute 60 seconds` and `2 minutes 0 seconds`, treating them as distinct intervals.
However, when the `1 minute 60 seconds` interval is inserted into a PostgreSQL database, it is automatically converted to `2 minutes 0 seconds`. As a result, attempting to insert the `2 minutes 0 seconds` interval into a unique column afterwards will cause an error

fields to the right of the last specified field are zeroed out, while fields to the left remain valid.
**Usage**
```ts
await seed(db, schema);
// or explicit
await seed(db, schema, { version: '2' });
```

Thus, for example, there is no difference between the `year to month` fields and the `month` fields, because fields to the right of `month` (`day`, `hour`, `minute`, `second`) are zeroed out, while fields to the left (`year`) remain valid.
**Switch to the old version**
```ts
await seed(db, schema, { version: '1' });
```

Example
### `string` generators were changed and upgraded to v2

```ts
import { pgTable, interval } from "drizzle-orm/pg-core";
import { drizzle } from "drizzle-orm/node-postgres";
import { seed } from "drizzle-seed";
await seed(db, { table }).refine((f) => ({
table: {
columns: {
// this function will output different values with the same `seed` number from previous version
column1: f.string(),
}
}
}))
```

const intervals = pgTable("intervals", {
interval: interval(),
});
**Reason to upgrade**

async function main() {
const db = drizzle(process.env.DATABASE_URL!);

await seed(db, { intervals }, { count: 1000 }).refine((funcs) => ({
intervals: {
columns: {
interval: funcs.interval({
fields: "day to hour",
}),
},
},
}));
}

main();
```
Ability to generate a unique string based on the length of the text column (e.g., `varchar(20)`)

You can also specify fields in a table and seed them automatically.
#### PostgreSQL changes

Default generators for `text`, `varchar`, `char` will output different values with the same `seed` number from previous version.

```ts
import { pgTable, interval } from "drizzle-orm/pg-core";
import { drizzle } from "drizzle-orm/node-postgres";
import { seed } from "drizzle-seed";
// schema.ts
import * as p from 'drizzle-orm/pg-core'

const intervals = pgTable("intervals", {
interval: interval({ fields: "day to hour" }),
export const table = p.pgTable('table', {
column1: p.text(),
column2: p.varchar(),
column3: p.char()
});

async function main() {
const db = drizzle(process.env.DATABASE_URL!);

await seed(db, { intervals }, { count: 1000 });
}
// index.ts
...
// this will be affected with new changes
await seed(db, { table });
```

main();
**Switch to the old version**
```ts
await seed(db, schema, { version: '' });
```

## Breaking changes
#### MySQL changes

- Unique `interval` generator was changed, so `2` version of this generator become available. **The latest version is `2`.**
Default generators for `text`, `char`, `varchar`, `binary`, `varbinary` will output different values with the same `seed` number.

**Cause:**
```ts
// schema.ts
import * as p from 'drizzle-orm/mysql-core'

export const table = p.mysqlTable('table', {
column1: p.text(),
column2: p.char(),
column3: p.varchar({ length: 256 }),
column4: p.binary(),
column5: p.varbinary({ length: 256 }),
});

**Bug in generator:**
Old version of generator could generate intervals like: `1 minute 60 second`, `2 minute 0 second` and treat them as different intervals.
// index.ts
...
// this will be affected with new changes
await seed(db, {table})
```

However, after inserting the `1 minute 60 second` interval, PostgreSQL database will convert it to `2 minute 0 second`. As a result, subsequent insertion of the `2 minute 0 second` interval into a unique column will cause an error.
**Switch to the old version**
```ts
await seed(db, schema, { version: '1' });
```

##
#### SQLite changes

- Both non-unique and unique `string` generators were changed, making version `2` of these generators available. **The latest version is `2`.**
Default generators for `text`, `numeric`, `blob`, `blobbuffer` will output different values with the same `seed` number.
```ts
// schema.ts
import * as p from 'drizzle-orm/sqlite-core'

export const table = p.sqliteTable('table', {
column1: p.text(),
column2: p.numeric(),
column3: p.blob({ mode:'buffer' }),
column4: p.blob(),
});

**Cause:**
// index.ts
...
// this will be affected with new changes
await seed(db, { table })
```

**Generating strings based on text-like column length:**
Now (in version 2), the maximum length of a string depends on the length of the text column (e.g., `varchar(20)`).
Functions from refine that will be affected by this change: ``

## Bug fixes
- seeding table with foreign key referencing another table, without including the second table in schema, will cause seed process to get stuck.
- not setting unique generators for unique database columns.
- Seeding a table with a foreign key referencing another table, without including the second table in the schema, will cause the seeding process to get stuck
- [[BUG]: seeding postgresql char column doesn't respect length option](https://github.com/drizzle-team/drizzle-orm/issues/3774)
25 changes: 25 additions & 0 deletions changelogs/drizzle-typebox/0.2.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Added support for SingleStore dialect

```ts
import { singlestoreTable, text, int } from 'drizzle-orm/singlestore-core';
import { createSelectSchema } from 'drizzle-typebox';
import { Value } from '@sinclair/typebox/value';

const users = singlestoreTable('users', {
id: int().primaryKey(),
name: text().notNull(),
age: int().notNull()
});

const userSelectSchema = createSelectSchema(users);

const rows = await db.select({ id: users.id, name: users.name }).from(users).limit(1);
const parsed: { id: number; name: string; age: number } = Value.Parse(userSelectSchema, rows[0]); // Error: `age` is not returned in the above query

const rows = await db.select().from(users).limit(1);
const parsed: { id: number; name: string; age: number } = Value.Parse(userSelectSchema, rows[0]); // Will parse successfully
```

# Bug fixes

- [[BUG]: drizzle-typebox infers integer() as TString](https://github.com/drizzle-team/drizzle-orm/issues/3756)
23 changes: 23 additions & 0 deletions changelogs/drizzle-valibot/0.3.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Added support for SingleStore dialect

```ts
import { singlestoreTable, text, int } from 'drizzle-orm/singlestore-core';
import { createSelectSchema } from 'drizzle-valibot';
import { parse } from 'valibot';

const users = singlestoreTable('users', {
id: int().primaryKey(),
name: text().notNull(),
age: int().notNull()
});

const userSelectSchema = createSelectSchema(users);
const rows = await db.select({ id: users.id, name: users.name }).from(users).limit(1);
const parsed: { id: number; name: string; age: number } = parse(userSelectSchema, rows[0]); // Error: `age` is not returned in the above query
const rows = await db.select().from(users).limit(1);
const parsed: { id: number; name: string; age: number } = parse(userSelectSchema, rows[0]); // Will parse successfully
```

# Bug fixes

- [[BUG]: drizzle-valibot throws Type instantiation is excessively deep and possibly infinite. for refinements](https://github.com/drizzle-team/drizzle-orm/issues/3751)
26 changes: 26 additions & 0 deletions changelogs/drizzle-zod/0.6.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# New Features

## Added support for SingleStore dialect

```ts
import { singlestoreTable, text, int } from 'drizzle-orm/singlestore-core';
import { createSelectSchema } from 'drizzle-zod';

const users = singlestoreTable('users', {
id: int().primaryKey(),
name: text().notNull(),
age: int().notNull()
});

const userSelectSchema = createSelectSchema(users);
const rows = await db.select({ id: users.id, name: users.name }).from(users).limit(1);
const parsed: { id: number; name: string; age: number } = userSelectSchema.parse(rows[0]); // Error: `age` is not returned in the above query

const rows = await db.select().from(users).limit(1);
const parsed: { id: number; name: string; age: number } = userSelectSchema.parse(rows[0]); // Will parse successfully
```

# Bug fixes

- [[BUG]: refining schema using createSelectSchema is not working with drizzle-kit 0.6.0](https://github.com/drizzle-team/drizzle-orm/issues/3735)
- [[BUG]: drizzle-zod inferring types incorrectly](https://github.com/drizzle-team/drizzle-orm/issues/3734)
2 changes: 1 addition & 1 deletion drizzle-orm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drizzle-orm",
"version": "0.38.2",
"version": "0.38.3",
"description": "Drizzle ORM package for SQL databases",
"type": "module",
"scripts": {
Expand Down
59 changes: 29 additions & 30 deletions drizzle-orm/src/mysql-core/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,35 @@ export function mysqlTableWithSchema<
}

export interface MySqlTableFn<TSchemaName extends string | undefined = undefined> {
<
TTableName extends string,
TColumnsMap extends Record<string, MySqlColumnBuilderBase>,
>(
name: TTableName,
columns: TColumnsMap,
extraConfig?: (
self: BuildColumns<TTableName, TColumnsMap, 'mysql'>,
) => MySqlTableExtraConfigValue[],
): MySqlTableWithColumns<{
name: TTableName;
schema: TSchemaName;
columns: BuildColumns<TTableName, TColumnsMap, 'mysql'>;
dialect: 'mysql';
}>;

<
TTableName extends string,
TColumnsMap extends Record<string, MySqlColumnBuilderBase>,
>(
name: TTableName,
columns: (columnTypes: MySqlColumnBuilders) => TColumnsMap,
extraConfig?: (self: BuildColumns<TTableName, TColumnsMap, 'mysql'>) => MySqlTableExtraConfigValue[],
): MySqlTableWithColumns<{
name: TTableName;
schema: TSchemaName;
columns: BuildColumns<TTableName, TColumnsMap, 'mysql'>;
dialect: 'mysql';
}>;
/**
* @deprecated The third parameter of mysqlTable is changing and will only accept an array instead of an object
*
Expand Down Expand Up @@ -187,36 +216,6 @@ export interface MySqlTableFn<TSchemaName extends string | undefined = undefined
columns: BuildColumns<TTableName, TColumnsMap, 'mysql'>;
dialect: 'mysql';
}>;

<
TTableName extends string,
TColumnsMap extends Record<string, MySqlColumnBuilderBase>,
>(
name: TTableName,
columns: TColumnsMap,
extraConfig?: (
self: BuildColumns<TTableName, TColumnsMap, 'mysql'>,
) => MySqlTableExtraConfigValue[],
): MySqlTableWithColumns<{
name: TTableName;
schema: TSchemaName;
columns: BuildColumns<TTableName, TColumnsMap, 'mysql'>;
dialect: 'mysql';
}>;

<
TTableName extends string,
TColumnsMap extends Record<string, MySqlColumnBuilderBase>,
>(
name: TTableName,
columns: (columnTypes: MySqlColumnBuilders) => TColumnsMap,
extraConfig?: (self: BuildColumns<TTableName, TColumnsMap, 'mysql'>) => MySqlTableExtraConfigValue[],
): MySqlTableWithColumns<{
name: TTableName;
schema: TSchemaName;
columns: BuildColumns<TTableName, TColumnsMap, 'mysql'>;
dialect: 'mysql';
}>;
}

export const mysqlTable: MySqlTableFn = (name, columns, extraConfig) => {
Expand Down
Loading

0 comments on commit 5d7860c

Please sign in to comment.