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