diff --git a/.gitignore b/.gitignore index 570a706f8..c266f115f 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,6 @@ rollup.config-*.mjs *.log .DS_Store drizzle-seed/src/test.ts +drizzle-seed/src/testMysql.ts +drizzle-seed/src/testSqlite.ts drizzle-seed/src/schemaTest.ts \ No newline at end of file diff --git a/changelogs/drizzle-seed/0.2.0.md b/changelogs/drizzle-seed/0.2.0.md index a34509510..0978c7a98 100644 --- a/changelogs/drizzle-seed/0.2.0.md +++ b/changelogs/drizzle-seed/0.2.0.md @@ -159,8 +159,7 @@ export const table = p.sqliteTable('table', { await seed(db, { table }) ``` -Functions from refine that will be affected by this change: `` ## Bug fixes - 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) \ No newline at end of file +- [[BUG]: seeding postgresql char column doesn't respect length option](https://github.com/drizzle-team/drizzle-orm/issues/3774) diff --git a/drizzle-seed/src/index.ts b/drizzle-seed/src/index.ts index 98c449095..c73e497cb 100644 --- a/drizzle-seed/src/index.ts +++ b/drizzle-seed/src/index.ts @@ -927,7 +927,8 @@ const getMySqlInfo = (schema: { [key: string]: MySqlTable }) => { typeParams['scale'] = Number(match[2]); } } else if ( - sqlType.startsWith('varchar') + sqlType.startsWith('char') + || sqlType.startsWith('varchar') || sqlType.startsWith('binary') || sqlType.startsWith('varbinary') ) { @@ -1129,12 +1130,38 @@ const getSqliteInfo = (schema: { [key: string]: SQLiteTable }) => { } tableRelations[dbToTsTableNamesMap[tableConfig.name] as string]!.push(...newRelations); + const getTypeParams = (sqlType: string) => { + // get type params and set only type + const typeParams: Column['typeParams'] = {}; + + if ( + sqlType.startsWith('decimal') + ) { + const match = sqlType.match(/\((\d+), *(\d+)\)/); + if (match) { + typeParams['precision'] = Number(match[1]); + typeParams['scale'] = Number(match[2]); + } + } else if ( + sqlType.startsWith('char') + || sqlType.startsWith('varchar') + || sqlType.startsWith('text') + ) { + const match = sqlType.match(/\((\d+)\)/); + if (match) { + typeParams['length'] = Number(match[1]); + } + } + + return typeParams; + }; + tables.push({ name: dbToTsTableNamesMap[tableConfig.name] as string, columns: tableConfig.columns.map((column) => ({ name: dbToTsColumnNamesMap[column.name] as string, columnType: column.getSQLType(), - typeParams: {}, + typeParams: getTypeParams(column.getSQLType()), dataType: column.dataType, hasDefault: column.hasDefault, default: column.default, diff --git a/drizzle-seed/src/services/SeedService.ts b/drizzle-seed/src/services/SeedService.ts index 84165169e..1370138b4 100644 --- a/drizzle-seed/src/services/SeedService.ts +++ b/drizzle-seed/src/services/SeedService.ts @@ -260,6 +260,7 @@ export class SeedService { columnPossibleGenerator.generator = arrayGen; } + columnPossibleGenerator.generator.isUnique = col.isUnique; const uniqueGen = columnPossibleGenerator.generator.replaceIfUnique(); if (uniqueGen !== undefined) { columnPossibleGenerator.generator = uniqueGen; @@ -268,7 +269,6 @@ export class SeedService { // selecting version of generator columnPossibleGenerator.generator = this.selectVersionOfGenerator(columnPossibleGenerator.generator); - columnPossibleGenerator.generator.isUnique = col.isUnique; // TODO: for now only GenerateValuesFromArray support notNull property columnPossibleGenerator.generator.notNull = col.notNull; columnPossibleGenerator.generator.dataType = col.dataType; @@ -309,6 +309,8 @@ export class SeedService { const newGenerator = new generatorConstructor(generator.params); newGenerator.baseColumnDataType = generator.baseColumnDataType; newGenerator.isUnique = generator.isUnique; + // TODO: for now only GenerateValuesFromArray support notNull property + newGenerator.notNull = generator.notNull; newGenerator.dataType = generator.dataType; newGenerator.stringLength = generator.stringLength; diff --git a/drizzle-seed/src/services/versioning/v2.ts b/drizzle-seed/src/services/versioning/v2.ts index e09cc45f6..f4dbf32f4 100644 --- a/drizzle-seed/src/services/versioning/v2.ts +++ b/drizzle-seed/src/services/versioning/v2.ts @@ -126,7 +126,7 @@ export class GenerateStringV2 extends AbstractGenerator<{ override init({ count, seed }: { count: number; seed: number }) { super.init({ count, seed }); - let minStringLength = 8; + let minStringLength = 7; let maxStringLength = 20; if (this.stringLength !== undefined) { maxStringLength = this.stringLength; @@ -182,7 +182,7 @@ export class GenerateUniqueStringV2 extends AbstractGenerator<{ isUnique?: boole override init({ seed, count }: { seed: number; count: number }) { const rng = prand.xoroshiro128plus(seed); - let minStringLength = 8; + let minStringLength = 7; let maxStringLength = 20; // TODO: revise later if (this.stringLength !== undefined) {