LoginSignup
0
0

prisma-dbml-generatorで複数行のNoteが正しく生成されない

Posted at

prisma-dbml-generator について

prisma.schemaファイルからdbmlファイルを生成することができます。
また、生成したファイルをdbdocsというサービスで共有する事ができます

問題

下記のように、複数行に渡るコメントは正しく変換されない問題がありました。

変換前

prisma.schema
/// User comment line1
/// User comment line2
model User {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

変換後

schema.dbml
Table User {
  id Int [pk, increment]
  createdAt DateTime [default: `now()`, not null]
  updatedAt DateTime [not null]

  Note: 'User comment line1
User comment line2'
}

解決方法

本当はリポジトリに修正PRを作成するのがいいですが、急ぎだったためpythonスクリプトで置換する方針で対応しました

convert_multiline_note.py
import re


input_file_path = 'prisma/dbml/schema.dbml'
output_file_path = 'prisma/dbml/new_schema.dbml'
match_pattern = r"Note:\s*'([^']+)'"
replace_pattern = r"Note: '''\n\1\n'''"

def main(input_file, output_file):
    with open(input_file, 'r', encoding='utf-8') as f:
        input_text = f.read()

    output_text = re.sub(match_pattern, replace_pattern, input_text, flags=re.MULTILINE)

    with open(output_file, 'w', encoding='utf-8') as f:
        f.write(output_text)

main(input_file_path, output_file_path)

下記実行するとoutput_file_pathに置換後のデータが保存されます

$ python3 convert_multiline_note.py

下記の様に変換されます

new_schema.dbml
Table User {
  id Int [pk, increment]
  createdAt DateTime [default: `now()`, not null]
  updatedAt DateTime [not null]

  Note: '''
User comment line1
User comment line2
'''
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0