Skip to content

Commit 5ed29d9

Browse files
Merge pull request #3849 from drizzle-team/main
Sync
2 parents 5ee9619 + 06be106 commit 5ed29d9

File tree

6 files changed

+38
-8
lines changed

6 files changed

+38
-8
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ rollup.config-*.mjs
1313
*.log
1414
.DS_Store
1515
drizzle-seed/src/test.ts
16+
drizzle-seed/src/testMysql.ts
17+
drizzle-seed/src/testSqlite.ts
1618
drizzle-seed/src/schemaTest.ts

changelogs/drizzle-seed/0.2.0.md renamed to changelogs/drizzle-seed/0.2.1.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ export const table = p.sqliteTable('table', {
159159
await seed(db, { table })
160160
```
161161

162-
Functions from refine that will be affected by this change: ``
163162

164163
## Bug fixes
165164
- 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
166-
- [[BUG]: seeding postgresql char column doesn't respect length option](https://github.com/drizzle-team/drizzle-orm/issues/3774)
165+
- [[BUG]: seeding postgresql char column doesn't respect length option](https://github.com/drizzle-team/drizzle-orm/issues/3774)

drizzle-seed/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "drizzle-seed",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"main": "index.js",
55
"type": "module",
66
"scripts": {

drizzle-seed/src/index.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,8 @@ const getMySqlInfo = (schema: { [key: string]: MySqlTable }) => {
927927
typeParams['scale'] = Number(match[2]);
928928
}
929929
} else if (
930-
sqlType.startsWith('varchar')
930+
sqlType.startsWith('char')
931+
|| sqlType.startsWith('varchar')
931932
|| sqlType.startsWith('binary')
932933
|| sqlType.startsWith('varbinary')
933934
) {
@@ -1129,12 +1130,38 @@ const getSqliteInfo = (schema: { [key: string]: SQLiteTable }) => {
11291130
}
11301131
tableRelations[dbToTsTableNamesMap[tableConfig.name] as string]!.push(...newRelations);
11311132

1133+
const getTypeParams = (sqlType: string) => {
1134+
// get type params and set only type
1135+
const typeParams: Column['typeParams'] = {};
1136+
1137+
if (
1138+
sqlType.startsWith('decimal')
1139+
) {
1140+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
1141+
if (match) {
1142+
typeParams['precision'] = Number(match[1]);
1143+
typeParams['scale'] = Number(match[2]);
1144+
}
1145+
} else if (
1146+
sqlType.startsWith('char')
1147+
|| sqlType.startsWith('varchar')
1148+
|| sqlType.startsWith('text')
1149+
) {
1150+
const match = sqlType.match(/\((\d+)\)/);
1151+
if (match) {
1152+
typeParams['length'] = Number(match[1]);
1153+
}
1154+
}
1155+
1156+
return typeParams;
1157+
};
1158+
11321159
tables.push({
11331160
name: dbToTsTableNamesMap[tableConfig.name] as string,
11341161
columns: tableConfig.columns.map((column) => ({
11351162
name: dbToTsColumnNamesMap[column.name] as string,
11361163
columnType: column.getSQLType(),
1137-
typeParams: {},
1164+
typeParams: getTypeParams(column.getSQLType()),
11381165
dataType: column.dataType,
11391166
hasDefault: column.hasDefault,
11401167
default: column.default,

drizzle-seed/src/services/SeedService.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ export class SeedService {
260260
columnPossibleGenerator.generator = arrayGen;
261261
}
262262

263+
columnPossibleGenerator.generator.isUnique = col.isUnique;
263264
const uniqueGen = columnPossibleGenerator.generator.replaceIfUnique();
264265
if (uniqueGen !== undefined) {
265266
columnPossibleGenerator.generator = uniqueGen;
@@ -268,7 +269,6 @@ export class SeedService {
268269
// selecting version of generator
269270
columnPossibleGenerator.generator = this.selectVersionOfGenerator(columnPossibleGenerator.generator);
270271

271-
columnPossibleGenerator.generator.isUnique = col.isUnique;
272272
// TODO: for now only GenerateValuesFromArray support notNull property
273273
columnPossibleGenerator.generator.notNull = col.notNull;
274274
columnPossibleGenerator.generator.dataType = col.dataType;
@@ -309,6 +309,8 @@ export class SeedService {
309309
const newGenerator = new generatorConstructor(generator.params);
310310
newGenerator.baseColumnDataType = generator.baseColumnDataType;
311311
newGenerator.isUnique = generator.isUnique;
312+
// TODO: for now only GenerateValuesFromArray support notNull property
313+
newGenerator.notNull = generator.notNull;
312314
newGenerator.dataType = generator.dataType;
313315
newGenerator.stringLength = generator.stringLength;
314316

drizzle-seed/src/services/versioning/v2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export class GenerateStringV2 extends AbstractGenerator<{
126126
override init({ count, seed }: { count: number; seed: number }) {
127127
super.init({ count, seed });
128128

129-
let minStringLength = 8;
129+
let minStringLength = 7;
130130
let maxStringLength = 20;
131131
if (this.stringLength !== undefined) {
132132
maxStringLength = this.stringLength;
@@ -182,7 +182,7 @@ export class GenerateUniqueStringV2 extends AbstractGenerator<{ isUnique?: boole
182182
override init({ seed, count }: { seed: number; count: number }) {
183183
const rng = prand.xoroshiro128plus(seed);
184184

185-
let minStringLength = 8;
185+
let minStringLength = 7;
186186
let maxStringLength = 20;
187187
// TODO: revise later
188188
if (this.stringLength !== undefined) {

0 commit comments

Comments
 (0)