oyvindberg / typo

Typed Postgresql integration for Scala. Hopes to avoid typos
https://oyvindberg.github.io/typo/
MIT License
99 stars 9 forks source link

typo generator wipes out directory where it puts the generated code (targetDir) #76

Open boggye opened 8 months ago

boggye commented 8 months ago

Hi,

I have the following generator:

// remember to give the project a github star if you like it <3
//
//> using dep "com.olvind.typo::typo:0.6.0"
//> using scala "3.3.0"

import typo.*

// adapt to your instance and credentials
implicit val c: java.sql.Connection =
  java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/somedb?user=postgres&password=password")

val options = Options(
  // customize package name for generated code
  pkg = "typo.models",
  // pick your database library
  dbLib = Some(DbLibName.Anorm),
  jsonLibs = List(JsonLibName.PlayJson),
  // many more possibilities for customization here
  // ...
)

// current folder, where you run the script from
val location = java.nio.file.Path.of(sys.props("user.dir"))

// destination folder. All files in this dir will be overwritten!
val targetDir = location.resolve("app")

// where Typo will look for sql files
val scriptsFolder = location.resolve("sql")

// you can use this to customize which relations you want to generate code for, see below
val selector = Selector.ExcludePostgresInternal

generateFromDb(options, folder = targetDir, selector = selector, scriptsPaths = List(scriptsFolder))
  .overwriteFolder()

// add changed files to git, so you can keep them under control
//scala.sys.process.Process(List("git", "add", targetDir.toString)).!!

I have a play framework project and I want to place the generated code in a package under app. As is, the generator wiped out the content of the app folder (it included the controllers code and others) which was somewhat unexpected. I thought the generator would remove the typo folder recursively. Luckily, I use IntelliJ and I was able to restore the deleted code. It is a brand new project and I didn't add the code yet to git until now after this accident.

Are there any options that allow me to tell the generator not to wipe out the content of the app folder, i.e. the content of targetDir?

For now, I am going to add the generated code somewhere under: target/scala-3.3.1\typo.

Thanks

oyvindberg commented 8 months ago

Yeah sorry, there was a very explicit warning about this which disappeared in the last documentation update.

I'll update with clear instructions on how to handle this tomorrow hopefully, not on a computer today.

As a workaround just generate the source somewhere else and move it manually

boggye commented 8 months ago

Just a suggestion, maybe the default should not be to delete the directory.

oyvindberg commented 8 months ago

I'm thinking about a better approach here, but haven't found it quite yet. Typo needs to be able to delete outdated files in the folder(s) you tell it you write to, so effectively it needs to control the whole folder and delete unknown files.

What I do is I add another source directory to the build and generate into it. That pattern is solid and would just need to be documented better. But there is still the possibility of data loss if people do the obvious thing

boggye commented 8 months ago

I ended up placing the code in target/scala-3.3.1/typo/main/scala and I added this path to the source paths in build.sbt with Compile / unmanagedSourceDirectories += baseDirectory.value / "target/scala-3.3.1/typo/main/scala".

I think I am good - we can close this thread.