用途:在编写或编辑 sqlanvil 数据项目(.sqlx 模型、workflow_settings.yaml、.df-credentials.json)并面向 PostgreSQL 或 Supabase 时,纠正沿用 Dataform/BigQuery 习惯导致的错误写法。核心原则是使用 postgres: {} 配置块和地道 Postgres 语法,而非 BigQuery 选项或手写 DDL。
主要能力与要点:项目配置用 workflow_settings.yaml,warehouse 为扁平字符串 postgres 或 supabase,defaultDataset 即 Postgres schema,字段为 sqlanvilCoreVersion(非 dataformCoreVersion),去掉 defaultProject 与 defaultLocation。凭据文件名为 .df-credentials.json,内容为扁平 PostgresConnection,字段为 host、port、database、user、password、sslMode、defaultSchema,需加入 gitignore。索引、fillfactor、tablespace、分区通过 postgres: {} 块声明,索引 method 为数字枚举(BTREE=0、HASH=1、GIN=2、GIST=3、BRIN=4),opclass 为单个字符串。物化视图用 type: "view" 加 materialized: true,可设 refreshPolicy、noData、indexes。语句分隔符为 ---,不是分号。存储过程、函数、触发器、扩展、授权用 type: "operations"。增量表的 upsert 键为顶层 uniqueKey,与 assertions.uniqueKey 不同。一次性 DDL 需包在 when(!incremental()) 中。外部表用 type: "declaration" 声明。跨仓库读取用命名连接加自动生成的 FDW 桥接,并用 sqlanvil introspect 生成声明,需提供 columnTypes。
使用方式:安装 npm i -g @sqlanvil/cli,命令为 sqlanvil init、compile、run、test,run 需 --credentials,可加 --full-refresh、--actions、--include-deps。仓库检出可用 ./scripts/run 替代。
限制:--dry-run 目前仅校验 BigQuery,在 Postgres 上不做 EXPLAIN 校验;读取端仓库必须是 postgres 或 supabase;连接为只读来源,无写回;带 connection 的声明缺少 columnTypes 会编译报错。字段不确定时以 protos/configs.proto 为准。