云原生声明式数据库结构迁移工具 - SchemaHero( 三 )


$ kubectl schemahero get migrations -n schemahero-tutorialIDDATABASETABLEPLANNED EXECUTED APPROVED REJECTED eaa36ef airlinedbairport11s如果没有找到资源,请等待几秒钟,然后重试,SchemaHero Operator 必须在 migration 可用之前完成 plan 阶段 。
在批准该迁移之前,我们可以查看附加到 migration 对象上面生成的 SQL 语句 。从上一个命令的输出中获取 ID,然后运行 describe migration 即可:
$ kubectl schemahero describe migration eaa36ef -n schemahero-tutorialMigration Name: eaa36ef Generated DDL Statement (generated at 2020-06-06T10:41:04-07:00):create table "airport" ("code" character (4), "name" character varying (255) not null, primary key ("code")); To apply this migration:kubectl schemahero -n schemahero-tutorial approve migration eaa36ef To recalculate this migration against the current schema:kubectl schemahero -n schemahero-tutorial recalculate migration eaa36ef To deny and cancel this migration:kubectl schemahero -n schemahero-tutorial reject migration eaa36ef在输出的顶部,生成的 DDL 语句是计划的迁移,这是 SchemaHero 将运行以应用此迁移的确切 SQL 语句 。
在后面, SchemaHero 为后续步骤提供了 3 个命令:

  • apply :运行此命令将接受 SQL 语句, SchemaHero 将对数据库执行它 。
  • recalculate SchemaHero SchemaHero
  • reject :运行此命令将拒绝迁移并且不执行它 。
比如我们这里可以看到当前的迁移是安全的符合预期的,接下来我们可以批准迁移,以便让 SchemaHero 执行该计划,执行下面的命令即可:
$ kubectl schemahero -n schemahero-tutorial approve migration eaa36efMigration eaa36ef approved我们可以再次运行 get migrations 来查看迁移的状态:
$ kubectl schemahero get migrations -n schemahero-tutorialIDDATABASETABLEPLANNEDEXECUTEDAPPROVEDREJECTEDeaa36efairlinedbairport9m38s38s52s上面的信息表明迁移计划在 9 分 38 秒前,在 52 秒前批准,在 38 秒前执行 。
现在我们在 Beekeeper Studio 工具中单击左侧导航中 Tables & Views 标题上的刷新按钮,正常我们现在可以在 public 下看到 airport 表,点开该表,可以看到表中的列数据 。
云原生声明式数据库结构迁移工具 - SchemaHero

文章插图
 
修改表接下来我们来部署一个 table 对象然后来修改表结构 。
和前面一样,首先我们创建一个 schedule 表,结构如下所示:
云原生声明式数据库结构迁移工具 - SchemaHero

文章插图
 
定义一个对应的 Table 表对象,内容如下所示:
# schedule-table.yamlapiVersion: schemas.schemahero.io/v1alpha4kind: Tablemetadata:name: schedulenamespace: schemahero-tutorialspec:database: airlinedbname: scheduleschema:postgres:primaryKey: [flight_num]columns:- name: flight_numtype: int- name: origintype: char(4)constraints:notNull: true- name: destinationtype: char(4)constraints:notNull: true- name: departure_timetype: timeconstraints:notNull: true- name: arrival_timetype: timeconstraints:notNull: true直接应用上面的资源清单即可:
$ kubectl apply -f schedule-table.yaml -n schemahero-tutorial$ kubectl schemahero get migrations -n schemahero-tutorial当 schedule 迁移准备好时,它将显示在输出中:
IDDATABASETABLEPLANNED EXECUTED APPROVED REJECTED a9626a8 airlinedb schedule 21s eaa36ef airlinedb airport4h3h3h同样可以通过 describe 查看对应的 SQL 语句过后,如果是安全的,则可以审批该变更 。
$ kubectl schemahero -n schemahero-tutorial approve migration a9626a8批准后可以在 Beekeeper Studio 中查看是否有 schedule 表了 。
云原生声明式数据库结构迁移工具 - SchemaHero

文章插图
 
现在让我们对这个表结构做一些更改:
  • 使 departure_time 和 arrival_time 列可以为空
  • 添加一个名为 duration 的新列
修改上面的 schedule-table.yaml 文件,将 departure_time 和 arrival_time 列中的 constraints 属性移出掉,然后增加一个名为 duration ,类型为 int 且没有 constraints 属性 。修改后的文件如下所示:
apiVersion: schemas.schemahero.io/v1alpha4kind: Tablemetadata:name: schedulenamespace: schemahero-tutorialspec:database: airlinedbname: scheduleschema:postgres:primaryKey: [flight_num]columns:- name: flight_numtype: int- name: origintype: char(4)constraints:notNull: true- name: destinationtype: char(4)constraints:notNull: true- name: departure_timetype: time- name: arrival_timetype: time- name: durationtype: int


推荐阅读