|
| 1 | +--- |
| 2 | +title: Configure SQL Review Policies with Bytebase API |
| 3 | +author: Ningjing |
| 4 | +updated_at: 2024/11/12 18:00 |
| 5 | +tags: Tutorial |
| 6 | +integrations: General, API |
| 7 | +level: Advanced |
| 8 | +estimated_time: '40 mins' |
| 9 | +description: 'Learn how to use the Bytebase API to configure SQL review rules in Bytebase' |
| 10 | +--- |
| 11 | + |
| 12 | +Bytebase is a database DevOps and CI/CD tool designed for developers, DBAs, and platform engineering teams. While it offers an intuitive GUI for managing database schema changes and access control, some teams may want to integrate Bytebase into their existing DevOps platforms using the [Bytebase API](/docs/api/overview/). |
| 13 | + |
| 14 | +This tutorial will guide you through configuring SQL review rules using the Bytebase API. This approach allows you to manage SQL review rules as code within your repository, enabling DBAs or platform engineering teams to apply them to Bytebase as needed. |
| 15 | + |
| 16 | +<HintBlock type="info"> |
| 17 | + |
| 18 | +This tutorial code repository is at https://github.com/bytebase/api-example/tree/main/sql-review |
| 19 | + |
| 20 | +</HintBlock> |
| 21 | + |
| 22 | +## Prerequisites |
| 23 | + |
| 24 | +1. [Docker](https://www.docker.com/) installed |
| 25 | +1. Node.js >= v18 |
| 26 | + |
| 27 | +## Start Bytebase |
| 28 | + |
| 29 | +<IncludeBlock url="/docs/share/tutorials/start-bytebase"></IncludeBlock> |
| 30 | + |
| 31 | +## Create Service Account |
| 32 | + |
| 33 | +<IncludeBlock url="/docs/share/tutorials/create-service-account"></IncludeBlock> |
| 34 | + |
| 35 | +## Run Scripts in Terminal |
| 36 | + |
| 37 | +### Generate the Token |
| 38 | + |
| 39 | +1. Go to [Bytebase API Example |
| 40 | + repo](https://github.com/bytebase/api-example) and clone it. |
| 41 | + |
| 42 | +1. Go to subfolder `sql-review`, and follow the instructions in the `README.md` to run the scripts. replace the `bytebase_url`, `bytebase_account`, `bytebase_password` with your own values. Then you will get a `bytebase_token` looks like `ey....9V8s`. |
| 43 | + |
| 44 | + ```bash |
| 45 | + export bytebase_url=http://localhost:8080 |
| 46 | + |
| 47 | + export bytebase_password=bbs_xxxxxxxxxxxxxilcLVG |
| 48 | + bytebase_token=$(curl -v ${bytebase_url}/v1/auth/login \ |
| 49 | + --data-raw '{"email":"'${bytebase_account}'","password":"'${bytebase_password}'","web":true}' \ |
| 50 | + --compressed 2>&1 | grep token | grep -o 'access-token=[^;]*;' | grep -o '[^;]*' | sed 's/access-token=//g; s/;//g') |
| 51 | + echo $bytebase_token |
| 52 | + ``` |
| 53 | + |
| 54 | +### Configure SQL Review Policies |
| 55 | + |
| 56 | +1. Continue following the `README.md` to run the scripts. |
| 57 | + |
| 58 | + ```bash |
| 59 | + curl --request PATCH ${bytebase_url}/v1/reviewConfigs/basic \ |
| 60 | + --header 'Authorization: Bearer '${bytebase_token} \ |
| 61 | + --data @policy/basic.json |
| 62 | + |
| 63 | + curl --request PATCH ${bytebase_url}/v1/reviewConfigs/advanced \ |
| 64 | + --header 'Authorization: Bearer '${bytebase_token} \ |
| 65 | + --data @policy/advanced.json |
| 66 | + ``` |
| 67 | + |
| 68 | +1. In the Bytebase console, navigate to **CI/CD > SQL Review** to see the applied SQL review rules. You may click **Edit** to change the rules. |
| 69 | +  |
| 70 | + |
| 71 | +1. To delete the SQL review rules, use the following commands: |
| 72 | + |
| 73 | + ```bash |
| 74 | + curl --request PATCH "${bytebase_url}/v1/reviewConfigs/basic?allow_missing=true&update_mask=rules" \ |
| 75 | + --header 'Authorization: Bearer '${bytebase_token} \ |
| 76 | + --data @policy/basic.json |
| 77 | + |
| 78 | + curl --request PATCH "${bytebase_url}/v1/reviewConfigs/advanced?allow_missing=true&update_mask=rules" \ |
| 79 | + --header 'Authorization: Bearer '${bytebase_token} \ |
| 80 | + --data @policy/advanced.json |
| 81 | + ``` |
| 82 | + |
| 83 | +### Apply SQL Review Policies to Resources |
| 84 | + |
| 85 | +You may notice that the SQL review rules are not applied to any resources yet from the above screenshot. In Bytebase, the SQL review rules can be applied to the `environments` or `projects`. Project-level rules take precedence over environment-level rules. |
| 86 | + |
| 87 | +1. Follow the `README.md` to run the scripts to apply the SQL review rules to environments. |
| 88 | + |
| 89 | + ```bash |
| 90 | + curl --request PATCH "${bytebase_url}/v1/environments/test/policies/tag?allow_missing=true&update_mask=payload" \ |
| 91 | + --header 'Authorization: Bearer '${bytebase_token} \ |
| 92 | + --data @binding/environments/test.json |
| 93 | + |
| 94 | + curl --request PATCH "${bytebase_url}/v1/environments/prod/policies/tag?allow_missing=true&update_mask=payload" \ |
| 95 | + --header 'Authorization: Bearer '${bytebase_token} \ |
| 96 | + --data @binding/environments/prod.json |
| 97 | + ``` |
| 98 | + |
| 99 | +1. Continue with the `README.md` to apply the SQL review rules to projects. |
| 100 | + |
| 101 | + ```bash |
| 102 | + curl --request PATCH "${bytebase_url}/v1/projects/project-sample/policies/tag?allow_missing=true&update_mask=payload" \ |
| 103 | + --header 'Authorization: Bearer '${bytebase_token} \ |
| 104 | + --data @binding/projects/project-sample.json |
| 105 | + ``` |
| 106 | + |
| 107 | +1. On the **CI/CD > SQL Review** page, you will see the SQL review rules are applied to environments and projects. |
| 108 | +  |
| 109 | + |
| 110 | +1. Go to **Environments** page, click **Test** environment to see the applied SQL review rules. |
| 111 | +  |
| 112 | + |
| 113 | +1. Go to `Sample Project` page, click **Setting** on the left sidebar to see the applied the SQL review rules. |
| 114 | +  |
| 115 | + |
| 116 | +1. To delete SQL review rules from environments and projects, use the following commands: |
| 117 | + |
| 118 | + ```bash |
| 119 | + curl --request DELETE ${bytebase_url}/v1/environments/test/policies/tag \ |
| 120 | + --header 'Authorization: Bearer '${bytebase_token} |
| 121 | + |
| 122 | + curl --request DELETE ${bytebase_url}/v1/environments/prod/policies/tag \ |
| 123 | + --header 'Authorization: Bearer '${bytebase_token} |
| 124 | + ``` |
| 125 | + |
| 126 | + ```bash |
| 127 | + curl --request DELETE ${bytebase_url}/v1/projects/project-sample/policies/tag \ |
| 128 | + --header 'Authorization: Bearer '${bytebase_token} |
| 129 | + ``` |
| 130 | + |
| 131 | +## Summary |
| 132 | + |
| 133 | +Congratulations! You can now configure SQL review rules using the Bytebase API, in addition to the Bytebase GUI, making SQL review policy as code a reality. |
0 commit comments