Skip to content

Commit

Permalink
bugfix: Fix a bug with default values for columns of type string
Browse files Browse the repository at this point in the history
  • Loading branch information
Karibash committed Aug 1, 2024
1 parent d65afd3 commit 5510112
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion drizzle-kit/src/serializer/mysqlSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ export const fromDatabase = async (
default: columnDefault === null
? undefined
: /^-?[\d.]+(?:e-?\d+)?$/.test(columnDefault)
&& !columnType.startsWith('decimal')
&& !['decimal', 'char', 'varchar'].some((type) => columnType.startsWith(type))
? Number(columnDefault)
: isDefaultAnExpression
? clearDefaults(columnDefault, collation)
Expand Down
44 changes: 43 additions & 1 deletion drizzle-kit/tests/introspect/mysql.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Docker from 'dockerode';
import { SQL, sql } from 'drizzle-orm';
import { int, mysqlTable, text } from 'drizzle-orm/mysql-core';
import { char, int, mysqlTable, text, varchar } from 'drizzle-orm/mysql-core';
import * as fs from 'fs';
import getPort from 'get-port';
import { Connection, createConnection } from 'mysql2/promise';
Expand Down Expand Up @@ -123,3 +123,45 @@ test('generated always column virtual: link to another column', async () => {

await client.query(`drop table users;`);
});

test('Default value of character type column: char', async () => {
const schema = {
users: mysqlTable('users', {
id: int('id'),
sortKey: char('sortKey', { length: 255 }).default('0'),
}),
};

const { statements, sqlStatements } = await introspectMySQLToFile(
client,
schema,
'default-value-char-column',
'drizzle',
);

expect(statements.length).toBe(0);
expect(sqlStatements.length).toBe(0);

await client.query(`drop table users;`);
});

test('Default value of character type column: varchar', async () => {
const schema = {
users: mysqlTable('users', {
id: int('id'),
sortKey: varchar('sortKey', { length: 255 }).default('0'),
}),
};

const { statements, sqlStatements } = await introspectMySQLToFile(
client,
schema,
'default-value-varchar-column',
'drizzle',
);

expect(statements.length).toBe(0);
expect(sqlStatements.length).toBe(0);

await client.query(`drop table users;`);
});

0 comments on commit 5510112

Please sign in to comment.