Hendrik Hofstadt | 3e6018f | 2019-10-28 21:29:42 +0100 | [diff] [blame] | 1 | # Copyright 2020 The Monogon Project Authors. |
| 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | load("@bazel_gazelle//:deps.bzl", "go_repository") |
| 18 | load( |
| 19 | "@io_bazel_rules_go//go/private:rules/rule.bzl", |
| 20 | "go_rule", |
| 21 | ) |
| 22 | load( |
| 23 | "@io_bazel_rules_go//go:def.bzl", |
| 24 | "GoLibrary", |
| 25 | "go_context", |
| 26 | ) |
| 27 | |
| 28 | # Default files generated by the sqlboiler + cockroach module |
| 29 | SQLBOILER_FILES = [ |
| 30 | "boil_main_test.go", |
| 31 | "boil_queries.go", |
| 32 | "boil_queries_test.go", |
| 33 | "boil_suites_test.go", |
| 34 | "boil_table_names.go", |
| 35 | "boil_types.go", |
| 36 | "crdb_main_test.go", |
| 37 | "crdb_suites_test.go", |
| 38 | "crdb_upsert.go", |
| 39 | ] |
| 40 | |
| 41 | def _sqlboiler_impl(ctx): |
| 42 | """Generate sqlboiler models from sql-migrate migrations""" |
| 43 | |
| 44 | filesn = [] |
| 45 | filesn += SQLBOILER_FILES |
| 46 | |
| 47 | for table in ctx.attr.tables: |
| 48 | filesn += [ |
| 49 | "{}.go".format(table), |
| 50 | "{}_test.go".format(table), |
| 51 | ] |
| 52 | |
| 53 | outs = [] |
| 54 | for file in filesn: |
| 55 | outs += [ctx.actions.declare_file(file)] |
| 56 | |
| 57 | # run_shell does not so set -euo pipefail |
| 58 | command = "set -euo pipefail\n" |
| 59 | |
| 60 | # Create random db name |
| 61 | command += "TMP_DB=$(cat /dev/urandom | tr -dc 'a-z' | fold -w 12 | head -n 1 ; echo)\n" |
| 62 | command += "echo DB name: $TMP_DB\n" |
| 63 | |
| 64 | # Create DB and template configs |
| 65 | command += "/usr/bin/psql --host localhost --port 26257 --user root -c \"CREATE DATABASE $TMP_DB;\"\n" |
| 66 | |
| 67 | command += "ln -s \"{}\" . \n".format(ctx.file.migrate_config.path) |
| 68 | command += "sed -i \"s/_dbname_/$TMP_DB/g\" \"{}\" \n".format(ctx.file.migrate_config.basename) |
| 69 | |
| 70 | command += "ln -s {} . \n".format(ctx.file.boiler_config.path) |
| 71 | command += "sed -i \"s/_dbname_/$TMP_DB/g\" \"{}\" \n".format(ctx.file.boiler_config.basename) |
| 72 | |
| 73 | # Copy in all sql files |
| 74 | command += "mkdir migrations\n" |
| 75 | for f in ctx.files.srcs: |
| 76 | command += "cp \"{}\" migrations/\n".format(f.path) |
| 77 | |
| 78 | # Copy in adapter |
| 79 | command += "cp \"{}\" .\n".format(ctx.file.adapter.path) |
| 80 | |
| 81 | # Apply sql-migrate |
| 82 | command += "{} up --config \"{}\" \n".format(ctx.file.migrate.path, ctx.file.migrate_config.basename) |
| 83 | |
| 84 | # Run sqlboiler |
| 85 | command += "{} --output \"{}\" --config \"{}\" --wipe crdb \n".format( |
| 86 | ctx.file.boiler.path, |
| 87 | outs[0].dirname, |
| 88 | ctx.file.boiler_config.basename, |
| 89 | ) |
| 90 | |
| 91 | ctx.actions.run_shell( |
| 92 | inputs = ctx.files.srcs + [ |
| 93 | ctx.file.migrate_config, |
| 94 | ctx.file.boiler, |
| 95 | ctx.file.adapter, |
| 96 | ctx.file.migrate, |
| 97 | ctx.file.boiler_config, |
| 98 | ], |
| 99 | outputs = outs, |
| 100 | command = command, |
| 101 | ) |
| 102 | |
| 103 | return [DefaultInfo(files = depset(outs))] |
| 104 | |
| 105 | sqlboiler = rule( |
| 106 | implementation = _sqlboiler_impl, |
| 107 | attrs = { |
| 108 | "srcs": attr.label_list(mandatory = True, allow_files = [".sql"]), |
| 109 | "tables": attr.string_list(default = []), |
Serge Bazanski | dcb3a56 | 2020-02-03 13:44:44 +0100 | [diff] [blame] | 110 | "migrate_config": attr.label(allow_single_file = True, default = Label("//build/sqlboiler:dbconfig.yml")), |
| 111 | "boiler_config": attr.label(allow_single_file = True, default = Label("//build/sqlboiler:sqlboiler.toml")), |
Hendrik Hofstadt | 3e6018f | 2019-10-28 21:29:42 +0100 | [diff] [blame] | 112 | "boiler": attr.label(allow_single_file = True, default = Label("@com_github_volatiletech_sqlboiler//:sqlboiler")), |
| 113 | "adapter": attr.label(allow_single_file = True, default = Label("@com_github_glerchundi_sqlboiler_crdb//:sqlboiler-crdb")), |
| 114 | "migrate": attr.label(allow_single_file = True, default = Label("@com_github_rubenv_sql_migrate//sql-migrate:sql-migrate")), |
| 115 | }, |
| 116 | ) |
| 117 | |
| 118 | def _sqlboiler_go_impl(ctx): |
| 119 | go = go_context(ctx) |
| 120 | |
| 121 | source_files = [] |
| 122 | |
| 123 | # Don't include test files as they cannot be processed by go_library |
| 124 | for file in ctx.attr.sqlboiler.files.to_list(): |
| 125 | if not "test" in file.path: |
| 126 | source_files += [file] |
| 127 | |
| 128 | library = go.new_library( |
| 129 | go, |
| 130 | srcs = source_files, |
| 131 | ) |
| 132 | source = go.library_to_source(go, ctx.attr, library, False) |
| 133 | providers = [library, source] |
| 134 | output_groups = { |
| 135 | "go_generated_srcs": source_files, |
| 136 | } |
| 137 | |
| 138 | return providers + [OutputGroupInfo(**output_groups)] |
| 139 | |
| 140 | go_sqlboiler_library = go_rule( |
| 141 | _sqlboiler_go_impl, |
| 142 | attrs = { |
| 143 | "sqlboiler": attr.label(providers = [DefaultInfo]), |
| 144 | "importpath": attr.string(), |
| 145 | "deps": attr.label_list( |
| 146 | providers = [GoLibrary], |
| 147 | default = [ |
| 148 | Label("@com_github_friendsofgo_errors//:go_default_library"), |
| 149 | Label("@com_github_lib_pq//:go_default_library"), |
| 150 | Label("@com_github_pkg_errors//:go_default_library"), |
| 151 | Label("@com_github_spf13_viper//:go_default_library"), |
| 152 | Label("@com_github_volatiletech_sqlboiler//boil:go_default_library"), |
| 153 | Label("@com_github_volatiletech_sqlboiler//drivers:go_default_library"), |
| 154 | Label("@com_github_volatiletech_sqlboiler//queries:go_default_library"), |
| 155 | Label("@com_github_volatiletech_sqlboiler//queries/qm:go_default_library"), |
| 156 | Label("@com_github_volatiletech_sqlboiler//randomize:go_default_library"), |
| 157 | Label("@com_github_volatiletech_sqlboiler//strmangle:go_default_library"), |
| 158 | Label("@com_github_volatiletech_sqlboiler//types:go_default_library"), |
| 159 | Label("@com_github_volatiletech_sqlboiler//queries/qmhelper:go_default_library"), |
Hendrik Hofstadt | 79d7a62 | 2020-03-11 19:18:56 +0100 | [diff] [blame^] | 160 | Label("@com_github_volatiletech_null//:go_default_library"), |
Hendrik Hofstadt | 3e6018f | 2019-10-28 21:29:42 +0100 | [diff] [blame] | 161 | ], |
| 162 | ), |
| 163 | }, |
| 164 | ) |