Skip to content

Commit

Permalink
fix(migrate): getDbInfo `TypeError: Cannot read property 'url' of und…
Browse files Browse the repository at this point in the history
…efined` (#11958)

Co-authored-by: Joël Galeran <Jolg42@users.noreply.github.com>
Co-authored-by: Jan Piotrowski <piotrowski+github@gmail.com>
  • Loading branch information
Jolg42 and janpio committed Feb 24, 2022
1 parent 5ece32b commit 52eed07
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 44 deletions.
101 changes: 64 additions & 37 deletions packages/migrate/src/__tests__/DbPull.test.ts
Expand Up @@ -194,14 +194,14 @@ describe('common/sqlite', () => {
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.error'].mock.calls.join('\n')).toMatchInlineSnapshot(`
// *** WARNING ***
//
// These models were enriched with \`@@map\` information taken from the previous Prisma schema.
// - Model "AwesomeNewPost"
// - Model "AwesomeProfile"
// - Model "AwesomeUser"
//
`)
// *** WARNING ***
//
// These models were enriched with \`@@map\` information taken from the previous Prisma schema.
// - Model "AwesomeNewPost"
// - Model "AwesomeProfile"
// - Model "AwesomeUser"
//
`)

expect(ctx.fs.read('prisma/reintrospection.prisma')).toStrictEqual(originalSchema)
})
Expand Down Expand Up @@ -624,14 +624,14 @@ describeIf(process.platform !== 'win32' && !isMacOrWindowsCI)('MongoDB', () => {
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.error'].mock.calls.join('\n')).toMatchInlineSnapshot(`
// *** WARNING ***
//
// The following fields had data stored in multiple types. The most common type was chosen. If loading data with a type that does not match the one in the data model, the client will crash. Please see the issue: https://github.com/prisma/prisma/issues/9654
// - Model "users", field: "numberOrString1", chosen data type: "Int32"
// - Type "UsersHobbies", field: "numberOrString2", chosen data type: "Int32"
// - Type "UsersHobbiesObjects", field: "numberOrString3", chosen data type: "Int32"
//
`)
// *** WARNING ***
//
// The following fields had data stored in multiple types. The most common type was chosen. If loading data with a type that does not match the one in the data model, the client will crash. Please see the issue: https://github.com/prisma/prisma/issues/9654
// - Model "users", field: "numberOrString1", chosen data type: "Int32"
// - Type "UsersHobbies", field: "numberOrString2", chosen data type: "Int32"
// - Type "UsersHobbiesObjects", field: "numberOrString3", chosen data type: "Int32"
//
`)
})

test('introspection --print --composite-type-depth=0 (no existing models)', async () => {
Expand Down Expand Up @@ -666,12 +666,12 @@ describeIf(process.platform !== 'win32' && !isMacOrWindowsCI)('MongoDB', () => {
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.error'].mock.calls.join('\n')).toMatchInlineSnapshot(`
// *** WARNING ***
//
// The following fields had data stored in multiple types. The most common type was chosen. If loading data with a type that does not match the one in the data model, the client will crash. Please see the issue: https://github.com/prisma/prisma/issues/9654
// - Model "users", field: "numberOrString1", chosen data type: "Int32"
//
`)
// *** WARNING ***
//
// The following fields had data stored in multiple types. The most common type was chosen. If loading data with a type that does not match the one in the data model, the client will crash. Please see the issue: https://github.com/prisma/prisma/issues/9654
// - Model "users", field: "numberOrString1", chosen data type: "Int32"
//
`)
})

test('introspection --print --composite-type-depth=1 (no existing models)', async () => {
Expand Down Expand Up @@ -714,13 +714,13 @@ describeIf(process.platform !== 'win32' && !isMacOrWindowsCI)('MongoDB', () => {
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.error'].mock.calls.join('\n')).toMatchInlineSnapshot(`
// *** WARNING ***
//
// The following fields had data stored in multiple types. The most common type was chosen. If loading data with a type that does not match the one in the data model, the client will crash. Please see the issue: https://github.com/prisma/prisma/issues/9654
// - Model "users", field: "numberOrString1", chosen data type: "Int32"
// - Type "UsersHobbies", field: "numberOrString2", chosen data type: "Int32"
//
`)
// *** WARNING ***
//
// The following fields had data stored in multiple types. The most common type was chosen. If loading data with a type that does not match the one in the data model, the client will crash. Please see the issue: https://github.com/prisma/prisma/issues/9654
// - Model "users", field: "numberOrString1", chosen data type: "Int32"
// - Type "UsersHobbies", field: "numberOrString2", chosen data type: "Int32"
//
`)
})

test('introspection --force --composite-type-depth=-1 (existing models)', async () => {
Expand Down Expand Up @@ -796,14 +796,14 @@ describeIf(process.platform !== 'win32' && !isMacOrWindowsCI)('MongoDB', () => {
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.error'].mock.calls.join('\n')).toMatchInlineSnapshot(`
// *** WARNING ***
//
// The following fields had data stored in multiple types. The most common type was chosen. If loading data with a type that does not match the one in the data model, the client will crash. Please see the issue: https://github.com/prisma/prisma/issues/9654
// - Model "users", field: "numberOrString1", chosen data type: "Int32"
// - Type "UsersHobbies", field: "numberOrString2", chosen data type: "Int32"
// - Type "UsersHobbiesObjects", field: "numberOrString3", chosen data type: "Int32"
//
`)
// *** WARNING ***
//
// The following fields had data stored in multiple types. The most common type was chosen. If loading data with a type that does not match the one in the data model, the client will crash. Please see the issue: https://github.com/prisma/prisma/issues/9654
// - Model "users", field: "numberOrString1", chosen data type: "Int32"
// - Type "UsersHobbies", field: "numberOrString2", chosen data type: "Int32"
// - Type "UsersHobbiesObjects", field: "numberOrString3", chosen data type: "Int32"
//
`)
})

// describeIf is making eslint not happy about the names
Expand All @@ -820,6 +820,33 @@ describeIf(process.platform !== 'win32' && !isMacOrWindowsCI)('MongoDB', () => {
expect(ctx.mocked['console.error'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
})

// In this case it should not error and the line `Datasource "x"` not be printed
test('introspection --url - only generator defined', async () => {
ctx.fixture('schema-only-mongodb/only-generator')
const introspect = new DbPull()
const result = introspect.parse(['--url', MONGO_URI])
await expect(result).resolves.toMatchInlineSnapshot(``)
expect(ctx.mocked['console.log'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n')).not.toContain(`Datasource `)
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(`
Prisma schema loaded from schema.prisma
Introspecting …
✔ Introspected 1 model and 2 embedded documents and wrote them into schema.prisma in XXXms
*** WARNING ***
The following fields had data stored in multiple types. The most common type was chosen. If loading data with a type that does not match the one in the data model, the client will crash. Please see the issue: https://github.com/prisma/prisma/issues/9654
- Model "users", field: "numberOrString1", chosen data type: "Int32"
- Type "UsersHobbies", field: "numberOrString2", chosen data type: "Int32"
- Type "UsersHobbiesObjects", field: "numberOrString3", chosen data type: "Int32"
Run prisma generate to generate Prisma Client.
`)
expect(ctx.mocked['console.error'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
})

test('introspection with --force', async () => {
ctx.fixture('schema-only-mongodb')
const introspect = new DbPull()
Expand Down
@@ -0,0 +1,4 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["mongoDb"]
}
5 changes: 5 additions & 0 deletions packages/migrate/src/commands/DbDrop.ts
Expand Up @@ -75,6 +75,11 @@ ${chalk.bold('Examples')}
await printDatasource(schemaPath)

const dbInfo = await getDbInfo(schemaPath)
if (!dbInfo.url) {
// TODO better error
throw new Error('Connection url is undefined.')
}

const schemaDir = (await getSchemaDir(schemaPath))!

console.info() // empty line
Expand Down
18 changes: 15 additions & 3 deletions packages/migrate/src/utils/ensureDatabaseExists.ts
Expand Up @@ -10,17 +10,29 @@ export type DbType = 'MySQL' | 'PostgreSQL' | 'SQLite' | 'SQL Server' | 'Cockroa
// TODO: extract functions in their own files?

export async function getDbInfo(schemaPath?: string): Promise<{
name: string // from datasource name
url: string // from getConfig
schemaWord: 'database' // legacy? could be removed?
name?: string // from datasource name
url?: string // from getConfig
dbLocation?: string // host without credentials
dbType?: DbType // pretty name
dbName?: string // database name
schema?: string // only for postgres right now (but SQL Server has this concept too)
}> {
const datamodel = await getSchema(schemaPath)
const config = await getConfig({ datamodel })
const activeDatasource = config.datasources[0]
const activeDatasource = config.datasources?.[0]

if (!activeDatasource) {
return {
name: undefined,
schemaWord: 'database',
dbType: undefined,
dbName: undefined,
dbLocation: undefined,
url: undefined,
}
}

const url = activeDatasource.url.value

if (activeDatasource.provider === 'sqlserver') {
Expand Down
4 changes: 3 additions & 1 deletion packages/migrate/src/utils/printDatasource.ts
Expand Up @@ -25,7 +25,9 @@ export async function printDatasource(schemaPath: string): Promise<void> {
),
)
}
} else {
} else if (dbInfo.name) {
console.info(chalk.dim(`Datasource "${dbInfo.name}"`))
} else {
// Nothing
}
}
6 changes: 3 additions & 3 deletions packages/sdk/src/engine-commands/getConfig.ts
Expand Up @@ -20,9 +20,9 @@ const unlink = promisify(fs.unlink)
const MAX_BUFFER = 1_000_000_000

export interface ConfigMetaFormat {
datasources: DataSource[]
generators: GeneratorConfig[]
warnings: string[]
datasources: DataSource[] | []
generators: GeneratorConfig[] | []
warnings: string[] | []
}

export type GetConfigOptions = {
Expand Down

0 comments on commit 52eed07

Please sign in to comment.