From 34b2036386f3774e3591be45989d062dc08dd46a Mon Sep 17 00:00:00 2001 From: AlanZheng2580 <168727160+AlanZheng2580@users.noreply.github.com> Date: Wed, 22 May 2024 10:56:47 +0800 Subject: [PATCH 1/6] add sqlfluff in sql-review.yml --- .github/workflows/sql-review.yml | 3 +++ sql/lint_sql.py | 18 ++++++++++++++++++ ...user_test.sql => 20240517002_add_email.sql} | 0 sql/myfirstdb/20240517003_inset_user_test.sql | 5 +++++ 4 files changed, 26 insertions(+) create mode 100644 sql/lint_sql.py rename sql/myfirstdb/{20240517002_inset_user_test.sql => 20240517002_add_email.sql} (100%) create mode 100644 sql/myfirstdb/20240517003_inset_user_test.sql diff --git a/.github/workflows/sql-review.yml b/.github/workflows/sql-review.yml index 615d652..bbfb81e 100644 --- a/.github/workflows/sql-review.yml +++ b/.github/workflows/sql-review.yml @@ -15,6 +15,9 @@ jobs: override-file-path: ./sql/sql-review-override.yml database-type: MYSQL file-pattern: ^sql/.*\.sql$ + - name: sqlfluff + run: | + pip install sqlfluff==3.0.6 && python3 sql/lint_sql.py - name: Build Docker image run: | DOCKER_BUILDKIT=0 docker build -t init-db . diff --git a/sql/lint_sql.py b/sql/lint_sql.py new file mode 100644 index 0000000..b91a887 --- /dev/null +++ b/sql/lint_sql.py @@ -0,0 +1,18 @@ +import os +import subprocess + +def lint_sql_file(file_path, dialect='mysql'): + try: + print(f"Linting {file_path}...") + subprocess.run(['sqlfluff', 'lint', file_path, '--dialect', dialect], check=True) + print(f"{file_path} linted successfully!") + except subprocess.CalledProcessError as e: + print(f"Error linting {file_path}: {e}") + +if __name__ == "__main__": + sql_directory = '.' + for root, dirs, files in os.walk(sql_directory): + for file in files: + if file.endswith('.sql'): + file_path = os.path.join(root, file) + lint_sql_file(file_path) diff --git a/sql/myfirstdb/20240517002_inset_user_test.sql b/sql/myfirstdb/20240517002_add_email.sql similarity index 100% rename from sql/myfirstdb/20240517002_inset_user_test.sql rename to sql/myfirstdb/20240517002_add_email.sql diff --git a/sql/myfirstdb/20240517003_inset_user_test.sql b/sql/myfirstdb/20240517003_inset_user_test.sql new file mode 100644 index 0000000..d88eefe --- /dev/null +++ b/sql/myfirstdb/20240517003_inset_user_test.sql @@ -0,0 +1,5 @@ +-- +migrate Up +INSERT INTO user_test (username) VALUES ('Ray'); + +-- +migrate Down +DELETE FROM user_test WHERE username = 'Ray'; From c42248cf821c3b4c63457ba5c012618e3e91c61a Mon Sep 17 00:00:00 2001 From: AlanZheng2580 <168727160+AlanZheng2580@users.noreply.github.com> Date: Wed, 22 May 2024 11:23:22 +0800 Subject: [PATCH 2/6] wip --- .github/workflows/sql-review.yml | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/.github/workflows/sql-review.yml b/.github/workflows/sql-review.yml index bbfb81e..e861547 100644 --- a/.github/workflows/sql-review.yml +++ b/.github/workflows/sql-review.yml @@ -15,9 +15,21 @@ jobs: override-file-path: ./sql/sql-review-override.yml database-type: MYSQL file-pattern: ^sql/.*\.sql$ - - name: sqlfluff + - uses: yu-iskw/action-sqlfluff@v3 + id: lint-sql + with: + github_token: ${{ secrets.github_token }} + reporter: github-pr-review + sqlfluff_version: "2.0.7" + sqlfluff_command: "lint" # Or "lint" + config: "${{ github.workspace }}/.sqlfluff" + paths: '${{ github.workspace }}/sql/myfirstdb' + - name: 'Show outputs (Optional)' + shell: bash run: | - pip install sqlfluff==3.0.6 && python3 sql/lint_sql.py + echo '${{ steps.lint-sql.outputs.sqlfluff-results }}' | jq -r '.' + echo '${{ steps.lint-sql.outputs.sqlfluff-results-rdjson }}' | jq -r '.' + - name: Build Docker image run: | DOCKER_BUILDKIT=0 docker build -t init-db . From fcdb35f0c727ca7cdbaf9765436111f48f243802 Mon Sep 17 00:00:00 2001 From: AlanZheng2580 <168727160+AlanZheng2580@users.noreply.github.com> Date: Wed, 22 May 2024 11:39:14 +0800 Subject: [PATCH 3/6] wip --- .github/workflows/sql-review.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/sql-review.yml b/.github/workflows/sql-review.yml index e861547..af96d10 100644 --- a/.github/workflows/sql-review.yml +++ b/.github/workflows/sql-review.yml @@ -15,6 +15,9 @@ jobs: override-file-path: ./sql/sql-review-override.yml database-type: MYSQL file-pattern: ^sql/.*\.sql$ + - name: pip install sqlfluff + run: | + pip install sqlfluff==2.0.7 - uses: yu-iskw/action-sqlfluff@v3 id: lint-sql with: From 2e4bac2c4641b8cabb7efa3889d4e82705cdd7c8 Mon Sep 17 00:00:00 2001 From: AlanZheng2580 <168727160+AlanZheng2580@users.noreply.github.com> Date: Wed, 22 May 2024 11:43:35 +0800 Subject: [PATCH 4/6] wip --- .github/workflows/sql-review.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/sql-review.yml b/.github/workflows/sql-review.yml index af96d10..84a0e1d 100644 --- a/.github/workflows/sql-review.yml +++ b/.github/workflows/sql-review.yml @@ -15,9 +15,6 @@ jobs: override-file-path: ./sql/sql-review-override.yml database-type: MYSQL file-pattern: ^sql/.*\.sql$ - - name: pip install sqlfluff - run: | - pip install sqlfluff==2.0.7 - uses: yu-iskw/action-sqlfluff@v3 id: lint-sql with: @@ -25,7 +22,6 @@ jobs: reporter: github-pr-review sqlfluff_version: "2.0.7" sqlfluff_command: "lint" # Or "lint" - config: "${{ github.workspace }}/.sqlfluff" paths: '${{ github.workspace }}/sql/myfirstdb' - name: 'Show outputs (Optional)' shell: bash From 27c50ca1d51a3fe9f71738ec7f51b3d53186bea1 Mon Sep 17 00:00:00 2001 From: AlanZheng2580 <168727160+AlanZheng2580@users.noreply.github.com> Date: Wed, 22 May 2024 11:47:11 +0800 Subject: [PATCH 5/6] wip --- .github/workflows/sql-review.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/sql-review.yml b/.github/workflows/sql-review.yml index 84a0e1d..0f7f775 100644 --- a/.github/workflows/sql-review.yml +++ b/.github/workflows/sql-review.yml @@ -23,6 +23,7 @@ jobs: sqlfluff_version: "2.0.7" sqlfluff_command: "lint" # Or "lint" paths: '${{ github.workspace }}/sql/myfirstdb' + dialect: 'mysql' - name: 'Show outputs (Optional)' shell: bash run: | From 48e632952a3dec8f610dcf245bde89bad81a7ed9 Mon Sep 17 00:00:00 2001 From: AlanZheng2580 <168727160+AlanZheng2580@users.noreply.github.com> Date: Wed, 22 May 2024 11:49:17 +0800 Subject: [PATCH 6/6] wip --- .github/workflows/sql-review.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/sql-review.yml b/.github/workflows/sql-review.yml index 0f7f775..d706bee 100644 --- a/.github/workflows/sql-review.yml +++ b/.github/workflows/sql-review.yml @@ -20,7 +20,7 @@ jobs: with: github_token: ${{ secrets.github_token }} reporter: github-pr-review - sqlfluff_version: "2.0.7" + sqlfluff_version: "3.0.6" sqlfluff_command: "lint" # Or "lint" paths: '${{ github.workspace }}/sql/myfirstdb' dialect: 'mysql'