Skip to content

Commit

Permalink
feat: display the socket path during migration (#24222)
Browse files Browse the repository at this point in the history
* feat: display the socket path during migration
* affects: PostgreSQL, CockroachDB, and MySQL
  • Loading branch information
brian-dlee committed Jun 19, 2024
1 parent 3ac7ffa commit 3472a7e
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
79 changes: 79 additions & 0 deletions packages/migrate/src/__tests__/utils/unixSocket.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { getSocketFromDatabaseCredentials } from '../../utils/unixSocket'

describe('utils', () => {
describe('unixSocket', () => {
it('detects absolute paths', () => {
expect(
getSocketFromDatabaseCredentials({
type: 'mysql',
host: 'localhost',
port: 3306,
socket: '/var/run/my-instance.sock',
}),
).toBe('/var/run/my-instance.sock')
})

it('detects relative paths', () => {
expect(
getSocketFromDatabaseCredentials({
type: 'mysql',
host: 'localhost',
port: 3306,
socket: './my-instance.sock',
}),
).toBe('./my-instance.sock')

expect(
getSocketFromDatabaseCredentials({
type: 'mysql',
host: 'localhost',
port: 3306,
socket: '../my-instance.sock',
}),
).toBe('../my-instance.sock')
})

it('detects a socket path provided as host for postresql', () => {
expect(
getSocketFromDatabaseCredentials({
type: 'postgresql',
host: '/var/run/my-instance.sock',
}),
).toBe('/var/run/my-instance.sock')
})

it('does not confuse a host as a socket', () => {
expect(
getSocketFromDatabaseCredentials({
type: 'postgresql',
host: 'localhost',
port: 5432,
}),
).toBe(null)

expect(
getSocketFromDatabaseCredentials({
type: 'postgresql',
host: 'my.very_long.domain-001.name',
port: 5432,
}),
).toBe(null)

expect(
getSocketFromDatabaseCredentials({
type: 'postgresql',
host: '111.222.333.444',
port: 5432,
}),
).toBe(null)

expect(
getSocketFromDatabaseCredentials({
type: 'postgresql',
host: '0000:1111:2222:3333:4444:5555:6666:7777',
port: 5432,
}),
).toBe(null)
})
})
})
7 changes: 6 additions & 1 deletion packages/migrate/src/utils/ensureDatabaseExists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { bold } from 'kleur/colors'

import { ConnectorType } from './printDatasources'
import { getSocketFromDatabaseCredentials } from './unixSocket'

export type MigrateAction = 'create' | 'apply' | 'unapply' | 'dev' | 'push'
export type PrettyProvider = 'MySQL' | 'PostgreSQL' | 'SQLite' | 'SQL Server' | 'CockroachDB' | 'MongoDB'
Expand Down Expand Up @@ -210,7 +211,11 @@ export function getDbLocation(credentials: DatabaseCredentials): string | undefi
return credentials.uri!
}

if (credentials.host && credentials.port) {
const socket = getSocketFromDatabaseCredentials(credentials)

if (socket) {
return `unix:${socket}`
} else if (credentials.host && credentials.port) {
return `${credentials.host}:${credentials.port}`
} else if (credentials.host) {
return `${credentials.host}`
Expand Down
33 changes: 33 additions & 0 deletions packages/migrate/src/utils/unixSocket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { DatabaseCredentials } from '@prisma/internals'

/**
* match a string that looks like a unix file path
* implicit relative paths are ignored due to their ambiguity with hostnames
* (.e.g my-database-socket can both be a hostname and a file path missing the ./ prefix)
*
* example 1: an explicit relative path in the same directory
* ./sockets/my-database-socket
*
* example 2: an explicit relative path in a parent directory
* ../sockets/my-database-socket
*
* example 3: an absolute path
* /sockets/my-database-socket
*
* ? for more info: https://regex101.com/r/p9md3l/1
*/
const simpleUnixPathPattern = /^\.{0,2}\//

export function getSocketFromDatabaseCredentials(credentials: DatabaseCredentials): string | null {
if (['postgres', 'postgresql', 'cockroachdb'].includes(credentials.type)) {
const host = credentials.host

if (typeof host === 'string' && simpleUnixPathPattern.test(host)) {
return host
}

return null
}

return credentials.socket ?? null
}

0 comments on commit 3472a7e

Please sign in to comment.