| Hendrik Hofstadt | 3e6018f | 2019-10-28 21:29:42 +0100 | [diff] [blame^] | 1 | ##SQLboiler |
| 2 | |
| 3 | This rule allows to generate sqlboiler models and ORM code for golang from a stack of SQL migrations. |
| 4 | |
| 5 | It uses `sql-migrate`, `sqlboiler` and `sqlboiker-crdb`. |
| 6 | |
| 7 | ###How to use |
| 8 | |
| 9 | Create a package and create a `0_initial.sql` file with the following template: |
| 10 | |
| 11 | ``` |
| 12 | -- +migrate Up |
| 13 | |
| 14 | Your initial SQL goes here |
| 15 | |
| 16 | -- +migrate Down |
| 17 | |
| 18 | ``` |
| 19 | |
| 20 | Then create a `BUILD` file with the following rules: |
| 21 | |
| 22 | ``` |
| 23 | load("//build/sqlboiler:sqlboiler.bzl", "go_sqlboiler_library", "sqlboiler") |
| 24 | load("@io_bazel_rules_go//go:def.bzl", "go_library") |
| 25 | |
| 26 | # gazelle:ignore . |
| 27 | |
| 28 | sqlboiler( |
| 29 | name = "sqlboiler", |
| 30 | srcs = glob(["*.sql"]), |
| 31 | tables = [ |
| 32 | <your table names> |
| 33 | ], |
| 34 | ) |
| 35 | |
| 36 | go_sqlboiler_library( |
| 37 | name = "sqlboiler_lib", |
| 38 | importpath = "git.monogon.dev/source/nexantic.git/<target>", |
| 39 | sqlboiler = ":sqlboiler", |
| 40 | ) |
| 41 | |
| 42 | go_library( |
| 43 | name = "go_default_library", |
| 44 | embed = [":sqlboiler_lib"], |
| 45 | importpath = "git.monogon.dev/source/nexantic.git/<target>", |
| 46 | visibility = ["//visibility:public"], |
| 47 | ) |
| 48 | |
| 49 | ``` |
| 50 | |
| 51 | Replace `target` with your intended importpath and add all created tables to the `tables` argument of the sqlboiler rule. |
| 52 | |
| 53 | Running the tasks will apply the migrations to a temporary database using `sql-migrate` and generate sqlboiler code from it. |
| 54 | The code will be importable from the specified `importpath`. |
| 55 | |
| 56 | When making changes to the schema please generate new migrations using the `<n>_<description>.sql` pattern for file names. |
| 57 | The migrations will automatically be applied and the models updated. |
| 58 | |
| 59 | Make sure to also update the `tables` argument when creating new tables in migrations. |