Skip to content

Commit

Permalink
fix blob column type
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodriguespn committed Dec 16, 2024
1 parent 6dc290a commit 683bb23
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 25 deletions.
2 changes: 2 additions & 0 deletions drizzle-kit/src/serializer/singlestoreSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ export const generateSingleStoreSnapshot = (
} else {
if (typeof column.default === 'string') {
columnToSet.default = `'${column.default}'`;
} else if (typeof column.default === 'bigint') {
columnToSet.default = Number(column.default);
} else {
if (sqlTypeLowered === 'json') {
columnToSet.default = `'${JSON.stringify(column.default)}'`;
Expand Down
13 changes: 0 additions & 13 deletions drizzle-kit/src/snapshotsDiffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2686,19 +2686,6 @@ export const applyMysqlSnapshotsDiff = async (
};
};

// This is necessary to make sure that BigInt is serialized to Number
// at the diffSchemasOrTables function. Ohterwise, it will be serialized to BigInt
// and the diff will throw the following error
// "TypeError: Do not know how to serialize a BigInt"

declare global {
interface BigInt {
toJSON(): Number;
}
}

BigInt.prototype.toJSON = function () { return Number(this) }

export const applySingleStoreSnapshotsDiff = async (
json1: SingleStoreSchemaSquashed,
json2: SingleStoreSchemaSquashed,
Expand Down
3 changes: 1 addition & 2 deletions drizzle-kit/tests/push/singlestore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ const singlestoreSuite: DialectSuite = {
jsonSimple: blob('json_simple', { mode: 'json' }),
jsonColumnNotNull: blob('json_column_not_null', { mode: 'json' }).notNull(),
jsonColumnDefault: blob('json_column_default', { mode: 'json' }).default('{"hello":"world"}'),
})
}),
};

const { statements } = await diffTestSchemasPushSingleStore(
Expand All @@ -268,7 +268,6 @@ const singlestoreSuite: DialectSuite = {
'drizzle',
false,
);
console.log(statements);
expect(statements.length).toBe(0);
expect(statements).toEqual([]);

Expand Down
26 changes: 18 additions & 8 deletions drizzle-orm/src/singlestore-core/columns/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export class SingleStoreBigIntBuilder<T extends ColumnBuilderBaseConfig<'bigint'
override build<TTableName extends string>(
table: AnySingleStoreTable<{ name: TTableName }>,
): SingleStoreBigInt<MakeColumnConfig<T, TTableName>> {
return new SingleStoreBigInt<MakeColumnConfig<T, TTableName>>(table, this.config as ColumnBuilderRuntimeConfig<any>);
return new SingleStoreBigInt<MakeColumnConfig<T, TTableName>>(
table,
this.config as ColumnBuilderRuntimeConfig<any>,
);
}
}

Expand All @@ -48,8 +51,8 @@ export class SingleStoreBigInt<T extends ColumnBaseConfig<'bigint', 'SingleStore
return BigInt(String.fromCodePoint(...value));
}

override mapToDriverValue(value: bigint): Buffer {
return Buffer.from(value.toString());
override mapToDriverValue(value: bigint): string {
return value.toString();
}
}

Expand Down Expand Up @@ -82,7 +85,9 @@ export class SingleStoreBlobJsonBuilder<T extends ColumnBuilderBaseConfig<'json'
}
}

export class SingleStoreBlobJson<T extends ColumnBaseConfig<'json', 'SingleStoreBlobJson'>> extends SingleStoreColumn<T> {
export class SingleStoreBlobJson<T extends ColumnBaseConfig<'json', 'SingleStoreBlobJson'>>
extends SingleStoreColumn<T>
{
static override readonly [entityKind]: string = 'SingleStoreBlobJson';

getSQLType(): string {
Expand All @@ -104,8 +109,8 @@ export class SingleStoreBlobJson<T extends ColumnBaseConfig<'json', 'SingleStore
return JSON.parse(String.fromCodePoint(...value));
}

override mapToDriverValue(value: T['data']): Buffer {
return Buffer.from(JSON.stringify(value));
override mapToDriverValue(value: T['data']): string {
return JSON.stringify(value);
}
}

Expand All @@ -131,11 +136,16 @@ export class SingleStoreBlobBufferBuilder<T extends ColumnBuilderBaseConfig<'buf
override build<TTableName extends string>(
table: AnySingleStoreTable<{ name: TTableName }>,
): SingleStoreBlobBuffer<MakeColumnConfig<T, TTableName>> {
return new SingleStoreBlobBuffer<MakeColumnConfig<T, TTableName>>(table, this.config as ColumnBuilderRuntimeConfig<any>);
return new SingleStoreBlobBuffer<MakeColumnConfig<T, TTableName>>(
table,
this.config as ColumnBuilderRuntimeConfig<any>,
);
}
}

export class SingleStoreBlobBuffer<T extends ColumnBaseConfig<'buffer', 'SingleStoreBlobBuffer'>> extends SingleStoreColumn<T> {
export class SingleStoreBlobBuffer<T extends ColumnBaseConfig<'buffer', 'SingleStoreBlobBuffer'>>
extends SingleStoreColumn<T>
{
static override readonly [entityKind]: string = 'SingleStoreBlobBuffer';

getSQLType(): string {
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/singlestore-core/columns/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './blob.ts';
export * from './bigint.ts';
export * from './binary.ts';
export * from './blob.ts';
export * from './boolean.ts';
export * from './char.ts';
export * from './common.ts';
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/type-tests/singlestore/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { type Equal, Expect } from 'type-tests/utils.ts';
import type { BuildColumn } from '~/column-builder.ts';
import { eq } from '~/expressions.ts';
import {
blob,
bigint,
binary,
blob,
boolean,
char,
customType,
Expand Down

0 comments on commit 683bb23

Please sign in to comment.