Skip to content

Commit

Permalink
add support for schema.prisma
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Jul 9, 2019
1 parent b1204a3 commit edf7e4a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
11 changes: 8 additions & 3 deletions cli/src/getCwd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const exists = promisify(fs.exists)
const readFile = promisify(fs.readFile)

const datamodelFile = 'project.prisma'
const schemaFile = 'schema.prisma'

/**
* Prefer the folder that has the project.prisma.
Expand All @@ -20,8 +21,10 @@ export async function getCwd(): Promise<string> {
prismaDatamodelExists,
] = await Promise.all([
exists(path.join(cwd, datamodelFile)),
exists(path.join(cwd, schemaFile)),
exists(path.join(cwd, 'prisma/')),
exists(path.join(cwd, 'prisma/', datamodelFile)),
exists(path.join(cwd, 'prisma/', schemaFile)),
])

if (datamodelCwdExists) {
Expand All @@ -37,9 +40,11 @@ export async function getCwd(): Promise<string> {

export async function getDatamodel(): Promise<string> {
const cwd = await getCwd()
const datamodelPath = path.join(cwd, datamodelFile)
const datamodelExists = await exists(datamodelPath)
if (!datamodelExists) {
let datamodelPath = path.join(cwd, datamodelFile)
if (!(await exists(datamodelPath))) {
let datamodelPath = path.join(cwd, schemaFile)
}
if (!(await exists(datamodelPath))) {
throw new Error(`Could not find ${datamodelPath}`)
}
return readFile(datamodelPath, 'utf-8')
Expand Down
9 changes: 8 additions & 1 deletion introspection/src/commands/Init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ export class Init implements Command {
Please run ${chalk.bold('prisma2 init')} in an empty directory.`)
}

if (existsSync(join(outputDir, 'schema.prisma'))) {
throw new Error(`Can't start ${chalk.bold('prisma2 init')} as ${chalk.redBright(
join(outputDir, 'schema.prisma'),
)} exists.
Please run ${chalk.bold('prisma2 init')} in an empty directory.`)
}

if (existsSync(join(outputDir, 'project.prisma'))) {
throw new Error(`Can't start ${chalk.bold('prisma2 init')} as ${chalk.redBright(
join(outputDir, 'project.prisma'),
Expand Down Expand Up @@ -84,7 +91,7 @@ Please run ${chalk.bold('prisma2 init')} in an empty directory.`)
return
}
mkdirpSync(join(outputDir, 'prisma'))
writeFileSync(join(outputDir, 'prisma/project.prisma'), result.introspectionResult.sdl)
writeFileSync(join(outputDir, 'prisma/schema.prisma'), result.introspectionResult.sdl)
}

help() {
Expand Down
5 changes: 4 additions & 1 deletion introspection/src/commands/Introspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ ${chalk.bold('Created 1 new file:')} Prisma DML datamodel (derived from existing
}

getExistingDatamodel(): string | undefined {
const datamodelPath = path.join(this.env.cwd, 'project.prisma')
let datamodelPath = path.join(this.env.cwd, 'project.prisma')
if (!fs.existsSync(datamodelPath)) {
datamodelPath = path.join(this.env.cwd, 'schema.prisma')
}
if (!fs.existsSync(datamodelPath)) {
return undefined
}
Expand Down
15 changes: 13 additions & 2 deletions introspection/src/introspect/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,23 @@ export async function getDatabaseSchemas(connector: IConnector): Promise<string[
}
}

function getSchemaPath(cwd: string): string | undefined {
if (existsSync(join(cwd, 'project.prisma'))) {
return join(cwd, 'project.prisma')
}
if (existsSync(join(cwd, 'schema.prisma'))) {
return join(cwd, 'schema.prisma')
}
return undefined
}

export async function getCredentialsFromExistingDatamodel(
env: Env,
lift: LiftEngine,
): Promise<undefined | DatabaseCredentials> {
if (existsSync(join(env.cwd, 'project.prisma'))) {
const datamodel = readFileSync(join(env.cwd, 'project.prisma'), 'utf-8')
const schemaPath = getSchemaPath(env.cwd)
if (schemaPath) {
const datamodel = readFileSync(schemaPath, 'utf-8')
const { datasources } = await lift.getConfig({
datamodel,
})
Expand Down
5 changes: 4 additions & 1 deletion prisma2/src/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export async function getConfig(cwd: string) {
}

export async function getDatamodel(cwd: string): Promise<string> {
const datamodelPath = path.join(cwd, 'project.prisma')
let datamodelPath = path.join(cwd, 'project.prisma')
if (!(await exists(datamodelPath))) {
datamodelPath = path.join(cwd, 'schema.prisma')
}
if (!(await exists(datamodelPath))) {
throw new Error(`Could not find ${datamodelPath}`)
}
Expand Down

0 comments on commit edf7e4a

Please sign in to comment.