使用SQLC从Docker迁移数据库不起作用。

huangapple go评论80阅读模式
英文:

Migrating datbase using SQLC from Docker not working

问题

我正在尝试在Windows操作系统上使用SQLC生成数据库迁移。根据官方文档的说明,我需要运行Docker命令(因为它支持Windows上的Postgres)。

如果我在命令行中运行docker run --rm -v "${pwd}:/src" -w /src kjconroy/sqlc generate,它可以正常工作。但是如果我运行make sqlc,就会显示一个错误:

docker run --rm -v ":/src" -w /src kjconroy/sqlc generate

error parsing configuration files. sqlc.yaml or sqlc.json: file does not exist

make: *** [Makefile:12: sqlc] Error 1

我的文件夹结构如下:

  • app/
    • db/
      • migration/
      • query/
        • myquery.sql
      • sqlc/
    • sqlc.yaml
    • Makefile

我的sqlc.yaml文件内容如下:

version: "1"
packages:
    - name: "db"
      path: "./db/sqlc"
      queries: "./db/query"
      schema: "./db/migration/"
      engine: "postgresql"
      emit_json_tags: true
      emit_prepared_queries: false
      emit_interface: false
      emit_exact_table_names: false

Makefile文件内容如下:

sqlc:
	docker run --rm -v "${pwd}:/src" -w /src kjconroy/sqlc generate

.PHONY: sqlc

那么,如何使其在Makefile中正常工作?我在这里没有得到错误信息。也许字段映射有问题?

英文:

I am trying to generate database migrations using SQLC over Windows OS. According to the official documentation I need to run the Docker command (because it supports Postgres over Windows).

If I run docker run --rm -v "${pwd}:/src" -w /src kjconroy/sqlc generate from a CLI it works. But if I run make sqlc it displays an error:

> docker run --rm -v ":/src" -w /src kjconroy/sqlc generate
>
> error parsing configuration files. sqlc.yaml or sqlc.json: file does not exist
>
> make: *** [Makefile:12: sqlc] Error 1

I have my folder structure like this:

  • app/
    • db/
      • migration/
      • query/
        • myquery.sql
      • sqlc/
    • sqlc.yaml
    • Makefile

My sqlc.yaml file has this:

version: "1"
packages:
    - name: "db"
      path: "./db/sqlc"
      queries: "./db/query"
      schema: "./db/migration/"
      engine: "postgresql"
      emit_json_tags: true
      emit_prepared_queries: false
      emit_interface: false
      emit_exact_table_names: false

And Makefile this:

sqlc:
	docker run --rm -v "${pwd}:/src" -w /src kjconroy/sqlc generate

.PHONY: sqlc

So, how to make it work from the Makefile? I am not getting the error here? Maybe the field mapping is wrong?

答案1

得分: 2

错误sqlc.yaml或sqlc.json:文件不存在只是表示sqlc(在容器中运行)在/src中找不到sqlc.yaml文件。当启动容器时,您将${pwd}映射到/src,所以问题是${pwd}没有按照您的期望解析为app/db文件夹。这可能是由于以下原因之一:

根据您的情况(根据您的评论),是前者,将${pwd}更改为${CURDIR}即可解决此问题。

英文:

The error sqlc.yaml or sqlc.json: file does not exist simply means that sqlc (running in a container) is not finding the sqlc.yaml file in /src. When starting the container you are mapping ${pwd} to /src so the issue is that ${pwd} is not evaluating to the app/db folder as you expect it to. This is could be due to either:

In your case (based on your comment) it's the former and changing ${pwd} to ${CURDIR} fixes the issue.

huangapple
  • 本文由 发表于 2022年9月13日 08:49:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/73696535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定