Add Prisma 7 flag support
This commit is contained in:
@@ -70,6 +70,7 @@ func (r *Reader) ReadTable() (*models.Table, error) {
|
||||
// parsePrisma parses Prisma schema content and returns a Database model
|
||||
func (r *Reader) parsePrisma(content string) (*models.Database, error) {
|
||||
db := models.InitDatabase("database")
|
||||
db.SourceFormat = "prisma"
|
||||
|
||||
if r.options.Metadata != nil {
|
||||
if name, ok := r.options.Metadata["name"].(string); ok {
|
||||
@@ -139,7 +140,7 @@ func (r *Reader) parsePrisma(content string) (*models.Database, error) {
|
||||
case "datasource":
|
||||
r.parseDatasource(blockContent, db)
|
||||
case "generator":
|
||||
// We don't need to do anything with generator blocks
|
||||
r.parseGenerator(blockContent, db)
|
||||
case "model":
|
||||
if currentTable != nil {
|
||||
r.parseModelFields(blockContent, currentTable)
|
||||
@@ -173,10 +174,34 @@ func (r *Reader) parsePrisma(content string) (*models.Database, error) {
|
||||
// Second pass: resolve relationships
|
||||
r.resolveRelationships(schema)
|
||||
|
||||
if db.SourceFormat == "prisma" && r.options != nil && r.options.Prisma7 {
|
||||
db.SourceFormat = "prisma7"
|
||||
}
|
||||
|
||||
db.Schemas = append(db.Schemas, schema)
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func (r *Reader) parseGenerator(lines []string, db *models.Database) {
|
||||
providerRegex := regexp.MustCompile(`provider\s*=\s*"([^"]+)"`)
|
||||
|
||||
for _, line := range lines {
|
||||
if matches := providerRegex.FindStringSubmatch(line); matches != nil {
|
||||
switch matches[1] {
|
||||
case "prisma-client":
|
||||
db.SourceFormat = "prisma7"
|
||||
default:
|
||||
db.SourceFormat = "prisma"
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if r.options != nil && r.options.Prisma7 {
|
||||
db.SourceFormat = "prisma7"
|
||||
}
|
||||
}
|
||||
|
||||
// parseDatasource extracts database type from datasource block
|
||||
func (r *Reader) parseDatasource(lines []string, db *models.Database) {
|
||||
providerRegex := regexp.MustCompile(`provider\s*=\s*"?(\w+)"?`)
|
||||
|
||||
77
pkg/readers/prisma/reader_test.go
Normal file
77
pkg/readers/prisma/reader_test.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package prisma
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/readers"
|
||||
)
|
||||
|
||||
func TestReadDatabase_Prisma7GeneratorSetsSourceFormat(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
schemaPath := filepath.Join(tmpDir, "schema.prisma")
|
||||
|
||||
content := `datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client"
|
||||
output = "./generated"
|
||||
}
|
||||
|
||||
model User {
|
||||
id Int @id @default(autoincrement())
|
||||
}`
|
||||
|
||||
if err := os.WriteFile(schemaPath, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("failed to write schema: %v", err)
|
||||
}
|
||||
|
||||
reader := NewReader(&readers.ReaderOptions{FilePath: schemaPath})
|
||||
db, err := reader.ReadDatabase()
|
||||
if err != nil {
|
||||
t.Fatalf("ReadDatabase() failed: %v", err)
|
||||
}
|
||||
|
||||
if db.SourceFormat != "prisma7" {
|
||||
t.Fatalf("expected SourceFormat prisma7, got %q", db.SourceFormat)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadDatabase_Prisma7FlagSetsSourceFormatWithoutGenerator(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
schemaPath := filepath.Join(tmpDir, "schema.prisma")
|
||||
|
||||
content := `datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
model User {
|
||||
id Int @id @default(autoincrement())
|
||||
}`
|
||||
|
||||
if err := os.WriteFile(schemaPath, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("failed to write schema: %v", err)
|
||||
}
|
||||
|
||||
reader := NewReader(&readers.ReaderOptions{
|
||||
FilePath: schemaPath,
|
||||
Prisma7: true,
|
||||
})
|
||||
db, err := reader.ReadDatabase()
|
||||
if err != nil {
|
||||
t.Fatalf("ReadDatabase() failed: %v", err)
|
||||
}
|
||||
|
||||
if db.SourceFormat != "prisma7" {
|
||||
t.Fatalf("expected SourceFormat prisma7 from flag, got %q", db.SourceFormat)
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,9 @@ type ReaderOptions struct {
|
||||
// ConnectionString is the database connection string (for DB readers)
|
||||
ConnectionString string
|
||||
|
||||
// Prisma7 enables Prisma 7-specific handling for Prisma schemas.
|
||||
Prisma7 bool
|
||||
|
||||
// Additional options can be added here as needed
|
||||
Metadata map[string]interface{}
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ func (w *Writer) databaseToPrisma(db *models.Database) string {
|
||||
sb.WriteString("\n")
|
||||
|
||||
// Write generator block
|
||||
sb.WriteString(w.generateGenerator())
|
||||
sb.WriteString(w.generateGenerator(db))
|
||||
sb.WriteString("\n")
|
||||
|
||||
// Process all schemas (typically just one in Prisma)
|
||||
@@ -114,13 +114,28 @@ func (w *Writer) generateDatasource(db *models.Database) string {
|
||||
}
|
||||
|
||||
// generateGenerator generates the generator block
|
||||
func (w *Writer) generateGenerator() string {
|
||||
func (w *Writer) generateGenerator(db *models.Database) string {
|
||||
if w.usePrisma7Generator(db) {
|
||||
return `generator client {
|
||||
provider = "prisma-client"
|
||||
output = "./generated"
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
return `generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
func (w *Writer) usePrisma7Generator(db *models.Database) bool {
|
||||
if w.options != nil && w.options.Prisma7 {
|
||||
return true
|
||||
}
|
||||
return db != nil && db.SourceFormat == "prisma7"
|
||||
}
|
||||
|
||||
// enumToPrisma converts an Enum to Prisma enum block
|
||||
func (w *Writer) enumToPrisma(enum *models.Enum) string {
|
||||
var sb strings.Builder
|
||||
|
||||
52
pkg/writers/prisma/writer_test.go
Normal file
52
pkg/writers/prisma/writer_test.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package prisma
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/models"
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/writers"
|
||||
)
|
||||
|
||||
func TestGenerateGenerator_DefaultsToPrismaClientJS(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
writer := NewWriter(&writers.WriterOptions{})
|
||||
db := models.InitDatabase("testdb")
|
||||
|
||||
got := writer.generateGenerator(db)
|
||||
if !strings.Contains(got, `provider = "prisma-client-js"`) {
|
||||
t.Fatalf("expected prisma-client-js generator, got:\n%s", got)
|
||||
}
|
||||
if strings.Contains(got, `output = "./generated"`) {
|
||||
t.Fatalf("did not expect prisma7 output path in default generator:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateGenerator_Prisma7FlagUsesPrismaClient(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
writer := NewWriter(&writers.WriterOptions{Prisma7: true})
|
||||
db := models.InitDatabase("testdb")
|
||||
|
||||
got := writer.generateGenerator(db)
|
||||
if !strings.Contains(got, `provider = "prisma-client"`) {
|
||||
t.Fatalf("expected prisma-client generator, got:\n%s", got)
|
||||
}
|
||||
if !strings.Contains(got, `output = "./generated"`) {
|
||||
t.Fatalf("expected prisma7 output path, got:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateGenerator_Prisma7SourceFormatUsesPrismaClient(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
writer := NewWriter(&writers.WriterOptions{})
|
||||
db := models.InitDatabase("testdb")
|
||||
db.SourceFormat = "prisma7"
|
||||
|
||||
got := writer.generateGenerator(db)
|
||||
if !strings.Contains(got, `provider = "prisma-client"`) {
|
||||
t.Fatalf("expected prisma-client generator from source format, got:\n%s", got)
|
||||
}
|
||||
}
|
||||
@@ -51,6 +51,9 @@ type WriterOptions struct {
|
||||
// "stdlib" — database/sql (sql.NullString, sql.NullInt32, …)
|
||||
NullableTypes string
|
||||
|
||||
// Prisma7 enables Prisma 7-specific output for Prisma writers.
|
||||
Prisma7 bool
|
||||
|
||||
// Additional options can be added here as needed
|
||||
Metadata map[string]interface{}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user