Skip to content

Commit

Permalink
Add rewriteDatabase option to rewrite database schema snapshot befo…
Browse files Browse the repository at this point in the history
…re codegen
  • Loading branch information
oyvindberg committed Apr 22, 2024
1 parent 1b296da commit c777bee
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 18 deletions.
1 change: 1 addition & 0 deletions site-in/customization/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ val options = Options(
| `readonlyRepo` | Specifies whether to generate read-only repositories for specific repositories. Useful when you're working on a part of the system where you only consume certain tables. (default is `false` - all mutable). |
| `enableDsl` | Enables the [SQL DSL](what-is/dsl.md) for code generation (default is `false`). |
| `keepDependencies` | Specifies whether to generate [table dependencies](type-safety/type-flow.md) in generated code even if you didn't select them (default is `false`). |
| `rewriteDatabase` | Let's you perform arbitrary rewrites of database schema snapshot. you can add/remove rows, foreign keys and so on. |

## Development options

Expand Down
2 changes: 1 addition & 1 deletion site-in/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ 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))
generateFromDb(options, targetFolder = targetDir, selector = selector, scriptsPaths = List(scriptsFolder))
.overwriteFolder()

// add changed files to git, so you can keep them under control
Expand Down
7 changes: 1 addition & 6 deletions typo-scripts/src/scala/scripts/GenHardcodedFiles.scala
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,7 @@ object GenHardcodedFiles extends BleepCodegenScript("GenHardcodedFiles") {
else (DbLibName.Anorm, JsonLibName.PlayJson)
val domains = Nil

val metaDb = MetaDb(
relations = all.map(t => t.name -> Lazy(t)).toMap,
enums = enums,
domains = domains,
TypeMapperDb(enums, domains)
)
val metaDb = MetaDb(relations = all.map(t => t.name -> Lazy(t)).toMap, enums = enums, domains = domains)

val generated: List[Generated] =
generate(
Expand Down
10 changes: 6 additions & 4 deletions typo/src/scala/typo/MetaDb.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import scala.collection.immutable.SortedSet
case class MetaDb(
relations: Map[db.RelationName, Lazy[db.Relation]],
enums: List[db.StringEnum],
domains: List[db.Domain],
typeMapperDb: TypeMapperDb
)
domains: List[db.Domain]
) {
val typeMapperDb = TypeMapperDb(enums, domains)

}

object MetaDb {
case class Input(
Expand Down Expand Up @@ -234,6 +236,6 @@ object MetaDb {
}
}.toMap

MetaDb(tables ++ views, enums, domains, typeMapperDb)
MetaDb(tables ++ views, enums, domains)
}
}
3 changes: 2 additions & 1 deletion typo/src/scala/typo/Options.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ case class Options(
debugTypes: Boolean = false,
inlineImplicits: Boolean = true,
fixVerySlowImplicit: Boolean = true,
keepDependencies: Boolean = false
keepDependencies: Boolean = false,
rewriteDatabase: MetaDb => MetaDb = identity
)

object Options {
Expand Down
22 changes: 18 additions & 4 deletions typo/src/scala/typo/generateFromDb.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@ import typo.internal.sqlfiles.readSqlFileDirectories
import java.nio.file.Path
import java.sql.Connection

/** Main entry-point for generating code from a database.
*/
object generateFromDb {
def apply(options: Options, folder: Path, selector: Selector = Selector.ExcludePostgresInternal, scriptsPaths: List[Path] = Nil)(implicit c: Connection): Generated = {
apply(options, ProjectGraph(name = "", folder, selector, scriptsPaths, Nil)).head
}

def apply(options: Options, graph: ProjectGraph[Selector, List[Path]])(implicit c: Connection): List[Generated] = {
/** Allows you to generate code into *one* folder
*/
def apply(
options: Options,
targetFolder: Path,
selector: Selector = Selector.ExcludePostgresInternal,
scriptsPaths: List[Path] = Nil
)(implicit c: Connection): Generated =
apply(options, ProjectGraph(name = "", targetFolder, selector, scriptsPaths, Nil)).head

/** Allows you to generate code into multiple folders
*/
def apply(
options: Options,
graph: ProjectGraph[Selector, List[Path]]
)(implicit c: Connection): List[Generated] = {
Banner.maybePrint(options)
internal.generate(
options,
Expand Down
4 changes: 2 additions & 2 deletions typo/src/scala/typo/internal/generate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ object generate {
private type Files = Map[sc.Type.Qualified, sc.File]

// use this constructor if you need to run `typo` multiple times with different options but same database/scripts
def apply(publicOptions: Options, metaDb: MetaDb, graph: ProjectGraph[Selector, List[SqlFile]]): List[Generated] = {
def apply(publicOptions: Options, metaDb0: MetaDb, graph: ProjectGraph[Selector, List[SqlFile]]): List[Generated] = {
Banner.maybePrint(publicOptions)

val metaDb = publicOptions.rewriteDatabase(metaDb0)
val pkg = sc.Type.Qualified(publicOptions.pkg).value
val customTypesPackage = pkg / sc.Ident("customtypes")

Expand Down

0 comments on commit c777bee

Please sign in to comment.