Skip to content

Commit

Permalink
Merge pull request #3841 from drizzle-team/drizzle-seed/studio
Browse files Browse the repository at this point in the history
Drizzle seed/studio
  • Loading branch information
AndriiSherman authored Dec 25, 2024
2 parents 5f2b404 + 021e939 commit 1446ee7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions changelogs/drizzle-seed/0.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- [[BUG]: seeding postgresql char column doesn't respect length option](https://github.com/drizzle-team/drizzle-orm/issues/3774)
31 changes: 29 additions & 2 deletions drizzle-seed/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
) {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion drizzle-seed/src/services/SeedService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions drizzle-seed/src/services/versioning/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1446ee7

Please sign in to comment.