diff --git a/.github/workflows/sql-review.yml b/.github/workflows/sql-review.yml index 615d652..d706bee 100644 --- a/.github/workflows/sql-review.yml +++ b/.github/workflows/sql-review.yml @@ -15,6 +15,21 @@ jobs: override-file-path: ./sql/sql-review-override.yml database-type: MYSQL file-pattern: ^sql/.*\.sql$ + - uses: yu-iskw/action-sqlfluff@v3 + id: lint-sql + with: + github_token: ${{ secrets.github_token }} + reporter: github-pr-review + sqlfluff_version: "3.0.6" + sqlfluff_command: "lint" # Or "lint" + paths: '${{ github.workspace }}/sql/myfirstdb' + dialect: 'mysql' + - name: 'Show outputs (Optional)' + shell: bash + run: | + 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 . 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';