Skip to content

Commit

Permalink
added support for gel connection without dbCredentials
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrSherman committed Feb 18, 2025
1 parent 8628c8a commit 2fb137b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion drizzle-kit/src/cli/commands/introspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export const introspectGel = async (
casing: Casing,
out: string,
breakpoints: boolean,
credentials: GelCredentials,
credentials: GelCredentials | undefined,
tablesFilter: string[],
schemasFilter: string[],
prefix: Prefix,
Expand Down
2 changes: 1 addition & 1 deletion drizzle-kit/src/cli/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ export const preparePullConfig = async (
}
| {
dialect: 'gel';
credentials: GelCredentials;
credentials?: GelCredentials;
}
) & {
out: string;
Expand Down
21 changes: 18 additions & 3 deletions drizzle-kit/src/cli/connections.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AwsDataApiPgQueryResult, AwsDataApiSessionOptions } from 'drizzle-orm/aws-data-api/pg';
import type { MigrationConfig } from 'drizzle-orm/migrator';
import type { PreparedQueryConfig } from 'drizzle-orm/pg-core';
import { Client } from 'edgedb';
import { Client, ClientConnectionError } from 'edgedb';
import fetch from 'node-fetch';
import ws from 'ws';
import { assertUnreachable } from '../global';
Expand Down Expand Up @@ -419,7 +419,7 @@ export const preparePostgresDB = async (
};

export const prepareGelDB = async (
credentials: GelCredentials,
credentials?: GelCredentials,
): Promise<
DB & {
proxy: Proxy;
Expand All @@ -429,7 +429,22 @@ export const prepareGelDB = async (
const edgedb = await import('edgedb');

let client: Client;
if ('url' in credentials) {
if (!credentials) {
client = edgedb.createClient();
try {
await client.querySQL(`select 1;`);
} catch (error: any) {
if (error instanceof ClientConnectionError) {
console.error(
`It looks like you forgot to link the Gel project or provide the database credentials.
To link your project, please refer https://docs.edgedb.com/cli/edgedb_instance/edgedb_instance_link, or add the dbCredentials to your configuration file.`,
);
process.exit(1);
}

throw error;
}
} else if ('url' in credentials) {
'tlsSecurity' in credentials
? client = edgedb.createClient({ dsn: credentials.url, tlsSecurity: credentials.tlsSecurity, concurrency: 1 })
: client = edgedb.createClient({ dsn: credentials.url, concurrency: 1 });
Expand Down
4 changes: 4 additions & 0 deletions drizzle-kit/src/cli/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ const optionsDatabaseCredentials = {
ssl: string().desc('ssl mode'),
// Turso
authToken: string('auth-token').desc('Database auth token [Turso]'),
// gel
tlsSecurity: string('tlsSecurity').desc('tls security mode'),
// specific cases
driver: optionDriver,
} as const;
Expand Down Expand Up @@ -282,6 +284,7 @@ export const push = command({
'extensionsFilters',
'tablesFilter',
'casing',
'tlsSecurity',
],
);

Expand Down Expand Up @@ -498,6 +501,7 @@ export const pull = command({
'tablesFilter',
'schemaFilters',
'extensionsFilters',
'tlsSecurity',
],
);
return preparePullConfig(opts, from);
Expand Down
17 changes: 11 additions & 6 deletions drizzle-kit/src/cli/validations/gel.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { coerce, literal, object, string, TypeOf, undefined, union } from 'zod';
import { coerce, literal, object, string, TypeOf, undefined as undefinedType, union } from 'zod';
import { error } from '../views';
import { wrapParam } from './common';

export const gelCredentials = union([
object({
driver: undefined(),
driver: undefinedType(),
host: string().min(1),
port: coerce.number().min(1).optional(),
user: string().min(1).optional(),
Expand All @@ -21,7 +21,7 @@ export const gelCredentials = union([
return o as Omit<typeof o, 'driver'>;
}),
object({
driver: undefined(),
driver: undefinedType(),
url: string().min(1),
tlsSecurity: union([
literal('insecure'),
Expand All @@ -40,6 +40,11 @@ export const gelCredentials = union([
delete o.driver;
return o;
}),
object({
driver: undefinedType(),
}).transform<undefined>((o) => {
return undefined;
}),
]);

export type GelCredentials = TypeOf<typeof gelCredentials>;
Expand All @@ -48,14 +53,14 @@ export const printConfigConnectionIssues = (
options: Record<string, unknown>,
) => {
if ('url' in options) {
let text = `Please provide required params for Postgres driver:\n`;
let text = `Please provide required params for Gel driver:\n`;
console.log(error(text));
console.log(wrapParam('url', options.url, false, 'url'));
process.exit(1);
}

if ('host' in options || 'database' in options) {
let text = `Please provide required params for Postgres driver:\n`;
let text = `Please provide required params for Gel driver:\n`;
console.log(error(text));
console.log(wrapParam('host', options.host));
console.log(wrapParam('port', options.port, true));
Expand All @@ -68,7 +73,7 @@ export const printConfigConnectionIssues = (

console.log(
error(
`Either connection "url" or "host", "database" are required for PostgreSQL database connection`,
`Either connection "url" or "host", "database" are required for Gel database connection`,
),
);
process.exit(1);
Expand Down
2 changes: 1 addition & 1 deletion drizzle-kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export type Config =
}
| {
dialect: Verify<Dialect, 'gel'>;
dbCredentials:
dbCredentials?:
& {
tlsSecurity?:
| 'insecure'
Expand Down

0 comments on commit 2fb137b

Please sign in to comment.