mz-deploy
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesemz-deploy
mz-deploy
mz-deploy.sqlmz-deploybash
brew install materializeinc/materialize/mz-deploymz-deploy.sqlmz-deploybash
brew install materializeinc/materialize/mz-deployDiscover Before You Run
运行前先了解
The CLI is self-documenting, and its help text is the source of truth. Do not
guess command names, flags, or argument order.
- — all commands, grouped by purpose.
mz-deploy help - — the detailed usage guide for one command: behavior, every flag, examples, error recovery, and exit codes.
mz-deploy help <command> - — the configuration topic guide (profiles, variables, suffixes, file overrides, TLS).
mz-deploy help profiles
This skill covers the concepts and the shape of the workflow so you know
which command to reach for. Read that command's for the flags before
running it.
helpRun commands from the project root, or pass . Global options worth
knowing: selects the connection profile, for
machine-readable output (supported by most commands, useful in CI), for
verbose debugging, to suppress informational output.
-d <path>-p/--profile--output json-v-q该CLI自带文档,其帮助文本是权威信息来源。请勿猜测命令名称、标志或参数顺序。
- —— 按用途分组的所有命令。
mz-deploy help - —— 单个命令的详细使用指南:行为、所有标志、示例、错误恢复和退出码。
mz-deploy help <command> - —— 配置主题指南(配置文件、变量、后缀、文件覆盖、TLS)。
mz-deploy help profiles
本技能涵盖工作流的概念和流程,帮助你了解该使用哪个命令。在运行命令前,请阅读对应命令的查看标志。
help从项目根目录运行命令,或传递 。值得了解的全局选项: 选择连接配置文件, 用于机器可读输出(大多数命令支持,在CI中很有用), 用于详细调试, 抑制信息输出。
-d <path>-p/--profile--output json-v-qProject Layout
项目布局
project.toml # project config: mz_version, dependencies, per-profile settings
profiles.toml # connection profiles (also resolved from ~/.mz)
.mzprofile # gitignored per-checkout default profile
types.lock # cached schemas of external dependencies, for offline type checking
target/ # gitignored local build cache (safe to delete; see `clean`)
models/
<database>/
<schema>.sql # schema mod file — schema-level statements (see below)
<schema>/
<object>.sql # one view, materialized view, table, source, sink, connection, or secret
clusters/<name>.sql # cluster definitions (+ their GRANTs)
roles/<name>.sql # role definitions
network-policies/<name>.sqlThe path determines the object's fully qualified name: an MV in
is
. A schema mod file —
sitting beside the directory — holds statements that apply to the
schema as a whole, such as .
models/materialize/catalog/clusters.sqlmaterialize.catalog.clusters<schema>.sql<schema>/SET api = stablemz-deploy new <name>initproject.toml # 项目配置:mz_version、依赖、按配置文件设置
profiles.toml # 连接配置文件(也可从~/.mz解析)
.mzprofile # 受git忽略的每个检出版本的默认配置文件
types.lock # 外部依赖的缓存模式,用于离线类型检查
target/ # 受git忽略的本地构建缓存(可安全删除;参见`clean`命令)
models/
<database>/
<schema>.sql # 模式修改文件——模式级语句(见下文)
<schema>/
<object>.sql # 单个视图、物化视图、表、源、汇、连接或密钥
clusters/<name>.sql # 集群定义(及其GRANT语句)
roles/<name>.sql # 角色定义
network-policies/<name>.sql路径决定了对象的完全限定名称:位于中的物化视图(MV)是。模式修改文件——与目录同级的——包含应用于整个模式的语句,例如。
models/materialize/catalog/clusters.sqlmaterialize.catalog.clusters<schema>/<schema>.sqlSET api = stablemz-deploy new <name>initCommand Map
命令映射表
| Group | Commands |
|---|---|
| Getting started | |
| Develop | |
| Infrastructure | |
| Deploy | |
One-time bootstrap: creates the tracking
database, its tables, the cluster, and three roles. It
must be run by a superuser when RBAC is enabled, because it grants system
privileges. Everything after that runs as an ordinary user.
mz-deploy setup_mz_deploy_mz_deploy_server| 分组 | 命令 |
|---|---|
| 入门 | |
| 开发 | |
| 基础设施 | |
| 部署 | |
一次性引导: 创建跟踪数据库、其表、集群以及三个角色。当启用RBAC时,必须由超级用户运行,因为它会授予系统权限。之后所有操作都可由普通用户运行。
mz-deploy setup_mz_deploy_mz_deploy_serverWorkflow
工作流
bash
mz-deploy compile # parse, resolve dependencies, type-check offline
mz-deploy test # run unit tests in a local Materialize container
mz-deploy apply # converge infrastructure (see below)
mz-deploy stage # deploy changed views/MVs to suffixed staging schemas
mz-deploy wait <DEPLOY_ID> # watch clusters hydrate until ready
mz-deploy promote <DEPLOY_ID> # atomic swap into productioncompilecompilestageapply--profilefoo#staging.sqlcompile --profile productionAlongside: shows unpromoted deployments (like ), details one, shows promotion history (like ), and destroys a staging deployment without promoting it.
listgit branchdescribe <ID>loggit logabort <ID>bash
mz-deploy compile # 解析、解析依赖、离线类型检查
mz-deploy test # 在本地Materialize容器中运行单元测试
mz-deploy apply # 收敛基础设施(见下文)
mz-deploy stage # 将变更后的视图/MV部署到带后缀的预发布模式
mz-deploy wait <DEPLOY_ID> # 监控集群直到准备就绪
mz-deploy promote <DEPLOY_ID> # 原子化切换到生产环境compilecompilestageapply--profilefoo#staging.sqlcompile --profile production配套命令: 显示未推广的部署(类似), 展示单个部署的详细信息, 显示推广历史(类似), 销毁预发布部署而不进行推广。
listgit branchdescribe <ID>loggit logabort <ID>Key Concepts
核心概念
applystageapplystagestageapplydelete <type> <name>applyDeploy IDs and staging suffixes. Each deployment gets an ID — by default
the first 7 characters of the current commit SHA — used to suffix its schemas
and clusters ( → ). Staging clusters are cloned from the
corresponding production cluster's configuration, including any auto-scaling
strategy, so staged objects hydrate the way production will. Staging runs in
isolation alongside production.
publicpublic_abc123Change detection is hash-based. compares each object's SQL hash
against the last promoted snapshot and deploys only what changed, plus
anything downstream of a change. Unchanged objects are not recreated. Override
with or .
stage--redeploy-schema <db.schema>--redeploy-allPromotion is atomic and resumable. executes on
schemas and clusters inside a single transaction, then does post-swap work:
creating deferred sinks, applying replacement MVs, repointing sinks at the new
production objects, and dropping the old resources. If it dies mid-flight,
re-running the same command detects the post-swap state and resumes cleanup.
promoteALTER … SWAPSinks are deferred to promote. They must not start producing until the
deployment is live, so records them and creates them.
stagepromoteConflict detection works at schema and cluster granularity. Because a whole
schema is swapped as a unit, two deployments touching any of the same schemas
or clusters conflict — even when they modify different objects inside them. The
first to promote wins; the second is rejected and must be re-staged against
current production. skips the check, and doing so drops the other
deployer's schemas, since schemas are swapped wholesale rather than merged.
Only use it when clobbering that work is the intent.
--forceStable API schemas. By default a changed object is recreated in staging and
its whole schema is swapped, which redeploys in-project dependents
automatically but breaks consumers in other mz-deploy projects. Adding to a schema mod file marks that schema as an API boundary:
changed MVs are updated in place via , preserving object identity. Downstream consumers — in any project
— need no redeployment. Constraints: stable schemas may contain only
materialized views, and a changed replacement MV does not propagate dirtiness
to its dependents.
SET api = stableALTER MATERIALIZED VIEW … APPLY REPLACEMENTType checking is offline. and read external dependency
schemas from . Declare external objects in as
and run to refresh the
file. Source tables created by are auto-discovered
and need no declaration; regenerates the lock automatically.
compiletesttypes.lockproject.tomldependencies = ["db.schema.table"]mz-deploy lockCREATE TABLE FROM SOURCEapply tablesRoles. creates (stage, promote, abort),
(read-only deployment state, plus overlays), and
. Each user must belong to exactly one — holding
several is an error. Use separate profiles with distinct users for deploying,
developing, and monitoring.
setupmaterialize_deployermaterialize_developerdevmaterialize_monitorapplystageapplystagestageapplydelete <type> <name>apply部署ID和预发布后缀。 每个部署都会获得一个ID——默认是当前提交SHA的前7个字符——用于为其模式和集群添加后缀( → )。预发布集群会从对应生产集群的配置克隆而来,包括任何自动扩缩策略,因此预发布对象的 hydrated 方式与生产环境一致。预发布与生产环境隔离运行。
publicpublic_abc123变更检测基于哈希。 会将每个对象的SQL哈希与上次推广的快照进行比较,仅部署变更的内容,以及变更内容的下游依赖。未变更的对象不会重新创建。可使用或覆盖此行为。
stage--redeploy-schema <db.schema>--redeploy-all推广是原子化且可恢复的。 在单个事务中对模式和集群执行,然后执行切换后的工作:创建延迟的汇、应用替换物化视图、将汇重新指向新的生产对象,并删除旧资源。如果中途中断,重新运行相同命令会检测切换后的状态并恢复清理工作。
promoteALTER … SWAP汇延迟到推广阶段创建。 它们必须在部署上线后才开始生成数据,因此会记录它们,再创建它们。
stagepromote冲突检测在模式和集群粒度上工作。 由于整个模式作为一个单元进行切换,两个涉及任何相同模式或集群的部署会发生冲突——即使它们修改的是模式内的不同对象。第一个完成推广的部署获胜;第二个会被拒绝,必须针对当前生产环境重新预发布。会跳过检查,但这样做会删除其他部署者的模式,因为模式是整体切换而非合并。仅当有意覆盖其他工作时才使用此选项。
--force稳定API模式。 默认情况下,变更后的对象会在预发布环境中重新创建,其整个模式会被切换,这会自动重新部署项目内的依赖,但会破坏其他mz-deploy项目中的消费者。在模式修改文件中添加会将该模式标记为API边界:变更后的物化视图会通过进行就地更新,保留对象标识。下游消费者——无论属于哪个项目——都无需重新部署。约束条件:稳定模式可能仅包含物化视图,且变更后的替换物化视图不会将脏数据传播给其依赖。
SET api = stableALTER MATERIALIZED VIEW … APPLY REPLACEMENT类型检查是离线的。 和从读取外部依赖模式。在中声明外部对象为,并运行刷新该文件。通过创建的源表会被自动发现,无需声明;会自动重新生成锁文件。
compiletesttypes.lockproject.tomldependencies = ["db.schema.table"]mz-deploy lockCREATE TABLE FROM SOURCEapply tables角色。 创建(负责stage、promote、abort)、(只读部署状态,加上覆盖)和。每个用户必须属于恰好一个角色——同时属于多个角色是错误的。为部署、开发和监控使用不同的配置文件和不同的用户。
setupmaterialize_deployermaterialize_developerdevmaterialize_monitorProfiles and Per-Profile Configuration
配置文件和按配置文件的配置
A profile is a named connection target in , resolved from
, then , then . The active
profile resolves from , then , then the
gitignored in the project root (written by ). A built-in profile always exists, so a local Materialize
emulator works with zero configuration.
profiles.toml--profiles-dirMZ_DEPLOY_PROFILES_DIR~/.mz--profileMZ_DEPLOY_PROFILE.mzprofilemz-deploy profile setemulatorPasswords support substitution, overridable by
. follows PostgreSQL's vocabulary and
defaults to for loopback hosts, otherwise — use
for Materialize Cloud.
${VAR}MZ_PROFILE_<NAME>_PASSWORDsslmodepreferrequireverify-fullThe profile does more than pick a host. It also selects, at compile time:
- (in
profile_suffix) — appended to every database and cluster name, includingproject.tomlreferences. Write the delimiter yourself:IN CLUSTER, not"_staging". Staging suffixes stack on top ("staging"→foo→foo_staging).foo_staging_a - SQL variables — in
[<profile>.variables], referenced in anyproject.tomlfile with psql syntax:.sql(raw),:name(quoted string),:'name'(quoted identifier). Referencing an undefined variable fails compilation.:"name" - File overrides — replaces
name#<profile>.sqlwhen that profile is active. All variants are validated at compile time regardless of which is active, and all must share the same primary statement type. Views and materialized views cannot have file overrides — use SQL variables instead.name.sql
配置文件是中命名的连接目标,解析顺序为、、。活动配置文件的解析顺序为、、项目根目录中受git忽略的(由写入)。内置的配置文件始终存在,因此本地Materialize模拟器无需任何配置即可工作。
profiles.toml--profiles-dirMZ_DEPLOY_PROFILES_DIR~/.mz--profileMZ_DEPLOY_PROFILE.mzprofilemz-deploy profile setemulator密码支持替换,可通过覆盖。遵循PostgreSQL的术语,默认对回环主机使用,其他情况使用——对Materialize Cloud使用。
${VAR}MZ_PROFILE_<NAME>_PASSWORDsslmodepreferrequireverify-full配置文件的作用不仅仅是选择主机。它还会在编译时选择:
- (在
profile_suffix中)——附加到每个数据库和集群名称,包括project.toml引用。自行写入分隔符:IN CLUSTER,而非"_staging"。预发布后缀会叠加("staging"→foo→foo_staging)。foo_staging_a - SQL变量——中的
project.toml,在任何[<profile>.variables]文件中使用psql语法引用:.sql(原始)、:name(引号字符串)、:'name'(引号标识符)。引用未定义的变量会导致编译失败。:"name" - 文件覆盖——当该配置文件处于活动状态时,会替换
name#<profile>.sql。无论哪个配置文件处于活动状态,所有变体都会在编译时验证,且所有变体必须共享相同的主语句类型。视图和物化视图不能使用文件覆盖——请改用SQL变量。name.sql
Rollback
回滚
There is no rollback command. Reverse the change in the project and promote the
result:
bash
git revert <commit>
mz-deploy stage
mz-deploy promote <DEPLOY_ID>Because swaps atomically, the rollback promotion is itself atomic —
production switches back in a single transaction.
promote没有专门的回滚命令。在项目中撤销变更并推广结果:
bash
git revert <commit>
mz-deploy stage
mz-deploy promote <DEPLOY_ID>由于是原子化切换,回滚推广本身也是原子化的——生产环境会在单个事务中切换回之前的状态。
promoteUnit Tests
单元测试
mz-deploy test.sqlEXECUTE UNIT TESTmz-deploy test.sqlEXECUTE UNIT TESTGotchas
注意事项
- requires a clean git tree. Commit, stash, or pass
stage.--allow-dirty - needs a superuser under RBAC, one time only.
setup - and
testneed Docker, and share one container namedexplainacross invocations on the host. Reuse is by name, not by image, somz-deploy-sandboxhas no effect until you--docker-image.docker rm -f mz-deploy-sandbox - never cascades. If dependents exist it fails and leaves the project file in place.
delete - will not target a production cluster. Provision a dedicated dev cluster.
dev - A profile's option is ignored. mz-deploy pins every connection to its own internal
clustercluster; resize that with a standard_mz_deploy_serverif needed.ALTER CLUSTER - failures roll back automatically. Pass
stageto keep the partial deployment for debugging, then clean up with--no-rollback.abort - A stale cache can produce confusing compile or type errors. Run
target/.mz-deploy clean
- 需要干净的git工作区。 提交、暂存或传递
stage。--allow-dirty - 启用RBAC时,需要超级用户,仅需运行一次。
setup - 和
test需要Docker,并在主机上的多次调用中共享一个名为explain的容器。复用是按名称而非镜像,因此mz-deploy-sandbox在你--docker-image之前不会生效。docker rm -f mz-deploy-sandbox - 永远不会级联。 如果存在依赖,它会失败并保留项目文件。
delete - 不会指向生产集群。 请配置专用的开发集群。
dev - 配置文件的选项会被忽略。 mz-deploy会将每个连接固定到其内部的
cluster集群;如有需要,使用标准的_mz_deploy_server调整其大小。ALTER CLUSTER - 失败会自动回滚。 传递
stage以保留部分部署用于调试,然后使用--no-rollback清理。abort - 过期的缓存可能会导致混淆的编译或类型错误。运行
target/。mz-deploy clean