Skip to content

Commit

Permalink
Merge pull request drizzle-team#3615 from drizzle-team/main
Browse files Browse the repository at this point in the history
Beta sync
  • Loading branch information
AndriiSherman authored Nov 25, 2024
2 parents 718822d + 599da5e commit a96956e
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 28 deletions.
2 changes: 2 additions & 0 deletions changelogs/drizzle-seed/0.1.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Fixed: [[BUG]: drizzle-seed reset fails without @electric-sql/pglite installed](https://github.com/drizzle-team/drizzle-orm/issues/3603)
- Fixed: [[BUG]: TypeScript type error in drizzle-seed with schema passed to drizzle in IDE](https://github.com/drizzle-team/drizzle-orm/issues/3599)
3 changes: 2 additions & 1 deletion drizzle-seed/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "drizzle-seed",
"version": "0.1.1",
"version": "0.1.2",
"main": "index.js",
"type": "module",
"scripts": {
"build": "tsx scripts/build.ts",
"pack": "(cd dist && npm pack --pack-destination ..) && rm -f package.tgz && mv *.tgz package.tgz",
"test": "vitest --config ./src/tests/vitest.config.ts",
"test:types": "cd src/type-tests && tsc",
"generate-for-tests:pg": "drizzle-kit generate --config=./src/tests/pg/drizzle.config.ts",
"generate-for-tests:mysql": "drizzle-kit generate --config=./src/tests/mysql/drizzle.config.ts",
"generate-for-tests:sqlite": "drizzle-kit generate --config=./src/tests/sqlite/drizzle.config.ts",
Expand Down
20 changes: 10 additions & 10 deletions drizzle-seed/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import type { Relation, Table } from './types/tables.ts';

type InferCallbackType<
DB extends
| PgDatabase<any>
| PgDatabase<any, any>
| MySqlDatabase<any, any>
| BaseSQLiteDatabase<any, any>,
SCHEMA extends {
[key: string]: PgTable | PgSchema | MySqlTable | MySqlSchema | SQLiteTable;
},
> = DB extends PgDatabase<any> ? SCHEMA extends {
> = DB extends PgDatabase<any, any> ? SCHEMA extends {
[key: string]:
| PgTable
| PgSchema
Expand Down Expand Up @@ -124,7 +124,7 @@ type InferCallbackType<

class SeedPromise<
DB extends
| PgDatabase<any>
| PgDatabase<any, any>
| MySqlDatabase<any, any>
| BaseSQLiteDatabase<any, any>,
SCHEMA extends {
Expand Down Expand Up @@ -311,7 +311,7 @@ export async function seedForDrizzleStudio(
*/
export function seed<
DB extends
| PgDatabase<any>
| PgDatabase<any, any>
| MySqlDatabase<any, any>
| BaseSQLiteDatabase<any, any>,
SCHEMA extends {
Expand All @@ -328,7 +328,7 @@ export function seed<
}

const seedFunc = async (
db: PgDatabase<any> | MySqlDatabase<any, any> | BaseSQLiteDatabase<any, any>,
db: PgDatabase<any, any> | MySqlDatabase<any, any> | BaseSQLiteDatabase<any, any>,
schema: {
[key: string]:
| PgTable
Expand All @@ -341,7 +341,7 @@ const seedFunc = async (
options: { count?: number; seed?: number } = {},
refinements?: RefinementsType,
) => {
if (is(db, PgDatabase<any>)) {
if (is(db, PgDatabase<any, any>)) {
const { pgSchema } = filterPgTables(schema);

await seedPostgres(db, pgSchema, options, refinements);
Expand Down Expand Up @@ -404,7 +404,7 @@ const seedFunc = async (
*/
export async function reset<
DB extends
| PgDatabase<any>
| PgDatabase<any, any>
| MySqlDatabase<any, any>
| BaseSQLiteDatabase<any, any>,
SCHEMA extends {
Expand All @@ -417,7 +417,7 @@ export async function reset<
| any;
},
>(db: DB, schema: SCHEMA) {
if (is(db, PgDatabase<any>)) {
if (is(db, PgDatabase<any, any>)) {
const { pgSchema } = filterPgTables(schema);

if (Object.entries(pgSchema).length > 0) {
Expand All @@ -444,7 +444,7 @@ export async function reset<

// Postgres-----------------------------------------------------------------------------------------------------------
const resetPostgres = async (
db: PgDatabase<any>,
db: PgDatabase<any, any>,
schema: { [key: string]: PgTable },
) => {
const tablesToTruncate = Object.entries(schema).map(([_, table]) => {
Expand Down Expand Up @@ -474,7 +474,7 @@ const filterPgTables = (schema: {
};

const seedPostgres = async (
db: PgDatabase<any>,
db: PgDatabase<any, any>,
schema: { [key: string]: PgTable },
options: { count?: number; seed?: number } = {},
refinements?: RefinementsType,
Expand Down
4 changes: 2 additions & 2 deletions drizzle-seed/src/services/SeedService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { MySqlTable } from 'drizzle-orm/mysql-core';
import { MySqlDatabase } from 'drizzle-orm/mysql-core';
import type { PgTable } from 'drizzle-orm/pg-core';
import { PgDatabase } from 'drizzle-orm/pg-core';
import { PgliteSession } from 'drizzle-orm/pglite';
import type { SQLiteTable } from 'drizzle-orm/sqlite-core';
import { BaseSQLiteDatabase } from 'drizzle-orm/sqlite-core';
import type {
Expand Down Expand Up @@ -1037,7 +1036,8 @@ class SeedService {
}
let maxParametersNumber: number;
if (is(db, PgDatabase<any>)) {
maxParametersNumber = is(db._.session, PgliteSession)
// @ts-ignore
maxParametersNumber = db.constructor[entityKind] === 'PgliteDatabase'
? this.postgresPgLiteMaxParametersNumber
: this.postgresMaxParametersNumber;
} else if (is(db, MySqlDatabase<any, any>)) {
Expand Down
17 changes: 17 additions & 0 deletions drizzle-seed/src/type-tests/mysql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { MySqlColumn } from 'drizzle-orm/mysql-core';
import { int, mysqlTable, text } from 'drizzle-orm/mysql-core';
import { drizzle as mysql2Drizzle } from 'drizzle-orm/mysql2';
import { reset, seed } from '../index.ts';

const mysqlUsers = mysqlTable('users', {
id: int().primaryKey().autoincrement(),
name: text(),
inviteId: int('invite_id').references((): MySqlColumn => mysqlUsers.id),
});

{
const db = mysql2Drizzle('');

await seed(db, { users: mysqlUsers });
await reset(db, { users: mysqlUsers });
}
48 changes: 48 additions & 0 deletions drizzle-seed/src/type-tests/pg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { drizzle as nodePostgresDrizzle } from 'drizzle-orm/node-postgres';
import type { PgColumn } from 'drizzle-orm/pg-core';
import { integer, pgTable, text } from 'drizzle-orm/pg-core';
import { drizzle as pgliteDrizzle } from 'drizzle-orm/pglite';
import { drizzle as postgresJsDrizzle } from 'drizzle-orm/postgres-js';
import { reset, seed } from '../index.ts';

const pgUsers = pgTable('users', {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
name: text(),
inviteId: integer('invite_id').references((): PgColumn => pgUsers.id),
});

{
const db0 = nodePostgresDrizzle('', { schema: { users: pgUsers } });

await seed(db0, { users: pgUsers });
await reset(db0, { users: pgUsers });

const db1 = nodePostgresDrizzle('');

await seed(db1, { users: pgUsers });
await reset(db1, { users: pgUsers });
}

{
const db0 = pgliteDrizzle('', { schema: { users: pgUsers } });

await seed(db0, { users: pgUsers });
await reset(db0, { users: pgUsers });

const db1 = pgliteDrizzle('');

await seed(db1, { users: pgUsers });
await reset(db1, { users: pgUsers });
}

{
const db0 = postgresJsDrizzle('', { schema: { users: pgUsers } });

await seed(db0, { users: pgUsers });
await reset(db0, { users: pgUsers });

const db1 = postgresJsDrizzle('');

await seed(db1, { users: pgUsers });
await reset(db1, { users: pgUsers });
}
17 changes: 17 additions & 0 deletions drizzle-seed/src/type-tests/sqlite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { drizzle as betterSqlite3Drizzle } from 'drizzle-orm/better-sqlite3';
import type { SQLiteColumn } from 'drizzle-orm/sqlite-core';
import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core';
import { reset, seed } from '../index.ts';

const mysqlUsers = sqliteTable('users', {
id: int().primaryKey(),
name: text(),
inviteId: int('invite_id').references((): SQLiteColumn => mysqlUsers.id),
});

{
const db = betterSqlite3Drizzle('');

await seed(db, { users: mysqlUsers });
await reset(db, { users: mysqlUsers });
}
11 changes: 11 additions & 0 deletions drizzle-seed/src/type-tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.build.json",
"compilerOptions": {
"composite": false,
"noEmit": true,
"rootDir": "..",
"outDir": "./.cache"
},
"include": [".", "../src"],
"exclude": ["**/playground"]
}
8 changes: 5 additions & 3 deletions integration-tests/tests/seeder/pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ test('valuesFromArray unique generator test', async () => {
},
},
})),
).rejects.toThrow('there are no enough values to fill unique column.');
).rejects.toThrow('There are no enough values to fill unique column.');

await expect(
seed(db, { valuesFromArrayUniqueTable: schema.valuesFromArrayUniqueTable }, { seed: 1 }).refine((funcs) => ({
Expand All @@ -955,7 +955,7 @@ test('valuesFromArray unique generator test', async () => {
},
},
})),
).rejects.toThrow('there are no enough values to fill unique column.');
).rejects.toThrow('There are no enough values to fill unique column.');
});

test('intPrimaryKey generator test', async () => {
Expand Down Expand Up @@ -1771,5 +1771,7 @@ test('weightedRandom with unique gens generator test', async () => {
},
},
})),
).rejects.toThrow("sum of all weights don't equal to 1; 1.1 !== 1");
).rejects.toThrow(
'The weights for the Weighted Random feature must add up to exactly 1. Please review your weights to ensure they total 1 before proceeding',
);
});
24 changes: 12 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a96956e

Please sign in to comment.