Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions drizzle-kit/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export const pushSchema = async (
},
};

const cur = generateDrizzleJson(imports);
const cur = generateDrizzleJson(imports, undefined, schemaFilters, drizzleInstance.casing);
const { schema: prev } = await pgPushIntrospect(
db,
filters,
Expand Down Expand Up @@ -285,7 +285,7 @@ export const pushSQLiteSchema = async (
},
};

const cur = await generateSQLiteDrizzleJson(imports);
const cur = await generateSQLiteDrizzleJson(imports, undefined, drizzleInstance.casing);
const { schema: prev } = await sqlitePushIntrospect(db, []);

const validatedPrev = sqliteSchema.parse(prev);
Expand Down Expand Up @@ -422,7 +422,7 @@ export const pushMySQLSchema = async (
return res[0] as unknown as any[];
},
};
const cur = await generateMySQLDrizzleJson(imports);
const cur = await generateMySQLDrizzleJson(imports, undefined, drizzleInstance.casing);
const { schema: prev } = await mysqlPushIntrospect(db, databaseName, []);

const validatedPrev = mysqlSchema.parse(prev);
Expand Down Expand Up @@ -558,7 +558,7 @@ export const pushSingleStoreSchema = async (
return res[0] as unknown as any[];
},
};
const cur = await generateSingleStoreDrizzleJson(imports);
const cur = await generateSingleStoreDrizzleJson(imports, undefined, drizzleInstance.casing);
const { schema: prev } = await singlestorePushIntrospect(db, databaseName, []);

const validatedPrev = singlestoreSchema.parse(prev);
Expand Down
62 changes: 62 additions & 0 deletions drizzle-kit/tests/push/mysql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,68 @@ const mysqlSuite: DialectSuite = {

await context.client.query(`DROP TABLE \`products_categories\``);
},
pushRespectsCasingConfiguration: async function(context?: any): Promise<void> {
const schema = {
users: mysqlTable('users', {
userId: serial('user_id').primaryKey(),
firstName: text('first_name').notNull(),
lastName: text('last_name'),
}),
};

const { statements, sqlStatements } = await diffTestSchemasPushMysql(
context.client as Connection,
{},
schema,
[],
'drizzle',
false,
'snake_case',
);

expect(statements).toStrictEqual([
{
type: 'create_table',
tableName: 'users',
schema: '',
columns: [
{
name: 'user_id',
type: 'serial',
primaryKey: true,
notNull: true,
autoincrement: true,
},
{
name: 'first_name',
type: 'text',
primaryKey: false,
notNull: true,
autoincrement: false,
},
{
name: 'last_name',
type: 'text',
primaryKey: false,
notNull: false,
autoincrement: false,
},
],
compositePKs: [],
compositePkName: '',
uniqueConstraints: [],
checkConstraints: [],
},
]);

expect(sqlStatements.length).toBe(1);
expect(sqlStatements[0]).toContain('CREATE TABLE');
expect(sqlStatements[0]).toContain('`user_id`');
expect(sqlStatements[0]).toContain('`first_name`');
expect(sqlStatements[0]).toContain('`last_name`');

await context.client.query(`DROP TABLE \`users\``);
},
};

run(
Expand Down
66 changes: 66 additions & 0 deletions drizzle-kit/tests/push/pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4410,3 +4410,69 @@ test('alter inherit in role', async (t) => {
await client.query(st);
}
});

test('push respects casing configuration', async () => {
const client = new PGlite();

const schema = {
users: pgTable('users', {
userId: serial('user_id').primaryKey(),
firstName: text('first_name').notNull(),
lastName: text('last_name'),
}),
};

const { statements, sqlStatements } = await diffTestSchemasPush(
client,
{},
schema,
[],
false,
['public'],
'snake_case',
);

expect(statements).toStrictEqual([
{
type: 'create_table',
tableName: 'users',
schema: '',
columns: [
{
name: 'user_id',
type: 'serial',
primaryKey: true,
notNull: true,
},
{
name: 'first_name',
type: 'text',
primaryKey: false,
notNull: true,
},
{
name: 'last_name',
type: 'text',
primaryKey: false,
notNull: false,
},
],
compositePKs: [],
compositePkName: '',
uniqueConstraints: [],
checkConstraints: [],
isRLSEnabled: false,
policies: [],
},
]);

expect(sqlStatements.length).toBe(1);
expect(sqlStatements[0]).toContain('CREATE TABLE');
expect(sqlStatements[0]).toContain('"user_id"');
expect(sqlStatements[0]).toContain('"first_name"');
expect(sqlStatements[0]).toContain('"last_name"');

for (const st of sqlStatements) {
await client.query(st);
}
});
62 changes: 62 additions & 0 deletions drizzle-kit/tests/push/singlestore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,68 @@ const singlestoreSuite: DialectSuite = {

await context.client.query(`DROP TABLE \`products_categories\``);
},
pushRespectsCasingConfiguration: async function(context?: any): Promise<void> {
const schema = {
users: singlestoreTable('users', {
userId: int('user_id').primaryKey(),
firstName: text('first_name').notNull(),
lastName: text('last_name'),
}),
};

const { statements, sqlStatements } = await diffTestSchemasPushSingleStore(
context.client as Connection,
{},
schema,
[],
'drizzle',
false,
'snake_case',
);

expect(statements).toStrictEqual([
{
type: 'create_table',
tableName: 'users',
schema: '',
columns: [
{
name: 'user_id',
type: 'int',
primaryKey: true,
notNull: true,
autoincrement: false,
},
{
name: 'first_name',
type: 'text',
primaryKey: false,
notNull: true,
autoincrement: false,
},
{
name: 'last_name',
type: 'text',
primaryKey: false,
notNull: false,
autoincrement: false,
},
],
compositePKs: [],
compositePkName: '',
uniqueConstraints: [],
checkConstraints: [],
},
]);

expect(sqlStatements.length).toBe(1);
expect(sqlStatements[0]).toContain('CREATE TABLE');
expect(sqlStatements[0]).toContain('`user_id`');
expect(sqlStatements[0]).toContain('`first_name`');
expect(sqlStatements[0]).toContain('`last_name`');

await context.client.query(`DROP TABLE \`users\``);
},
};

run(
Expand Down
62 changes: 62 additions & 0 deletions drizzle-kit/tests/push/sqlite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1611,3 +1611,65 @@ test('rename table with composite primary key', async () => {
'ALTER TABLE `products_categories` RENAME TO `products_to_categories`;',
]);
});

test('push respects casing configuration', async () => {
const client = new Database(':memory:');

const schema = {
users: sqliteTable('users', {
userId: integer('user_id').primaryKey(),
firstName: text('first_name').notNull(),
lastName: text('last_name'),
}),
};

const { statements, sqlStatements } = await diffTestSchemasPushSqlite(
client,
{},
schema,
[],
false,
[],
'snake_case',
);

expect(statements).toStrictEqual([
{
type: 'sqlite_create_table',
tableName: 'users',
columns: [
{
name: 'user_id',
type: 'integer',
primaryKey: true,
notNull: true,
autoincrement: false,
},
{
name: 'first_name',
type: 'text',
primaryKey: false,
notNull: true,
autoincrement: false,
},
{
name: 'last_name',
type: 'text',
primaryKey: false,
notNull: false,
autoincrement: false,
},
],
compositePKs: [],
referenceData: [],
uniqueConstraints: [],
checkConstraints: [],
},
]);

expect(sqlStatements.length).toBe(1);
expect(sqlStatements[0]).toContain('CREATE TABLE');
expect(sqlStatements[0]).toContain('`user_id`');
expect(sqlStatements[0]).toContain('`first_name`');
expect(sqlStatements[0]).toContain('`last_name`');
});
2 changes: 2 additions & 0 deletions drizzle-orm/src/casing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ export class CasingCache {
cache: Record<string, string> = {};
private cachedTables: Record<string, true> = {};
private convert: (input: string) => string;
readonly casing?: Casing;

constructor(casing?: Casing) {
this.casing = casing;
this.convert = casing === 'snake_case'
? toSnakeCase
: casing === 'camelCase'
Expand Down
5 changes: 4 additions & 1 deletion drizzle-orm/src/gel-core/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { ExtractTablesWithRelations, RelationalSchemaConfig, TablesRelation
import { SelectionProxyHandler } from '~/selection-proxy.ts';
import { type ColumnsSelection, type SQL, sql, type SQLWrapper } from '~/sql/sql.ts';
import { WithSubquery } from '~/subquery.ts';
import type { DrizzleTypeError } from '~/utils.ts';
import type { Casing, DrizzleTypeError } from '~/utils.ts';
import type { GelColumn } from './columns/index.ts';
import { GelCountBuilder } from './query-builders/count.ts';
import { RelationalQueryBuilder } from './query-builders/query.ts';
Expand All @@ -38,6 +38,8 @@ export class GelDatabase<
readonly session: GelSession<TQueryResult, TFullSchema, TSchema>;
};

readonly casing: Casing | undefined;

query: TFullSchema extends Record<string, never>
? DrizzleTypeError<'Seems like the schema generic is missing - did you forget to add it to your DB type?'>
: {
Expand Down Expand Up @@ -80,6 +82,7 @@ export class GelDatabase<
}

this.$cache = { invalidate: async (_params: any) => {} };
this.casing = dialect.casing.casing;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion drizzle-orm/src/mysql-core/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ExtractTablesWithRelations, RelationalSchemaConfig, TablesRelation
import { SelectionProxyHandler } from '~/selection-proxy.ts';
import { type ColumnsSelection, type SQL, sql, type SQLWrapper } from '~/sql/sql.ts';
import { WithSubquery } from '~/subquery.ts';
import type { DrizzleTypeError } from '~/utils.ts';
import type { Casing, DrizzleTypeError } from '~/utils.ts';
import type { MySqlDialect } from './dialect.ts';
import { MySqlCountBuilder } from './query-builders/count.ts';
import {
Expand Down Expand Up @@ -45,6 +45,8 @@ export class MySqlDatabase<
readonly tableNamesMap: Record<string, string>;
};

readonly casing: Casing | undefined;

query: TFullSchema extends Record<string, never>
? DrizzleTypeError<'Seems like the schema generic is missing - did you forget to add it to your DB type?'>
: {
Expand Down Expand Up @@ -87,6 +89,7 @@ export class MySqlDatabase<
}
}
this.$cache = { invalidate: async (_params: any) => {} };
this.casing = dialect.casing.casing;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion drizzle-orm/src/pg-core/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type { ExtractTablesWithRelations, RelationalSchemaConfig, TablesRelation
import { SelectionProxyHandler } from '~/selection-proxy.ts';
import { type ColumnsSelection, type SQL, sql, type SQLWrapper } from '~/sql/sql.ts';
import { WithSubquery } from '~/subquery.ts';
import type { DrizzleTypeError, NeonAuthToken } from '~/utils.ts';
import type { Casing, DrizzleTypeError, NeonAuthToken } from '~/utils.ts';
import type { PgColumn } from './columns/index.ts';
import { PgCountBuilder } from './query-builders/count.ts';
import { RelationalQueryBuilder } from './query-builders/query.ts';
Expand All @@ -47,6 +47,8 @@ export class PgDatabase<
readonly session: PgSession<TQueryResult, TFullSchema, TSchema>;
};

readonly casing: Casing | undefined;

query: TFullSchema extends Record<string, never>
? DrizzleTypeError<'Seems like the schema generic is missing - did you forget to add it to your DB type?'>
: {
Expand Down Expand Up @@ -88,6 +90,7 @@ export class PgDatabase<
}
}
this.$cache = { invalidate: async (_params: any) => {} };
this.casing = dialect.casing.casing;
}

/**
Expand Down
Loading