Skip to content

Commit bb686b9

Browse files
authored
feat: plpgsql check (#469)
adds support for plpgsql_check. the approach is even simpler as described in the issue: if we encounter a create function statement, we start a transaction, run the statement, then run plpgsql_check on it, and then rollback the transaction. the remaining code is just about translating the location information we get from the extension into a good range. todo: - [x] integrate into workspace api - [x] to get span, move from the first word / occurrence in the line to the next semicolon - [x] handle return trigger by checking for all variations - [x] make sure we do not report create fn syntax errors if plpgsql_check is enabled - [x] check why "not a known variable" is not getting the right span closes #190
1 parent b651c75 commit bb686b9

24 files changed

+1446
-137
lines changed

.github/workflows/pull_request.yml

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,6 @@ jobs:
7979
lint:
8080
name: Lint Project
8181
runs-on: ubuntu-latest
82-
services:
83-
postgres:
84-
image: postgres:latest
85-
env:
86-
POSTGRES_USER: postgres
87-
POSTGRES_PASSWORD: postgres
88-
POSTGRES_DB: postgres
89-
ports:
90-
- 5432:5432
9182
steps:
9283
- name: Checkout PR Branch
9384
uses: actions/checkout@v4
@@ -103,6 +94,24 @@ jobs:
10394
env:
10495
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
10596

97+
# we need to use the same database as we do locally for sqlx prepare to output the same hashes
98+
- name: Build and start PostgreSQL with plpgsql_check
99+
run: |
100+
docker build -t postgres-plpgsql-check:latest .
101+
docker run -d --name postgres \
102+
-e POSTGRES_USER=postgres \
103+
-e POSTGRES_PASSWORD=postgres \
104+
-e POSTGRES_DB=postgres \
105+
-p 5432:5432 \
106+
postgres-plpgsql-check:latest
107+
# Wait for postgres to be ready
108+
for _ in {1..30}; do
109+
if docker exec postgres pg_isready -U postgres; then
110+
break
111+
fi
112+
sleep 1
113+
done
114+
106115
- name: Setup sqlx-cli
107116
run: cargo install sqlx-cli
108117

@@ -154,13 +163,37 @@ jobs:
154163
env:
155164
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
156165

157-
# running containers via `services` only works on linux
158-
# https://github.com/actions/runner/issues/1866
159-
- name: Setup postgres
166+
# For Linux, use custom Docker image with plpgsql_check
167+
- name: Build and start PostgreSQL with plpgsql_check
168+
if: runner.os == 'Linux'
169+
run: |
170+
docker build -t postgres-plpgsql-check:latest .
171+
docker run -d --name postgres \
172+
-e POSTGRES_USER=postgres \
173+
-e POSTGRES_PASSWORD=postgres \
174+
-e POSTGRES_DB=postgres \
175+
-p 5432:5432 \
176+
postgres-plpgsql-check:latest
177+
# Wait for postgres to be ready
178+
for _ in {1..30}; do
179+
if docker exec postgres pg_isready -U postgres; then
180+
break
181+
fi
182+
sleep 1
183+
done
184+
# For Windows, use the action since PostgreSQL Docker image doesn't support Windows containers
185+
- name: Setup postgres (Windows)
186+
if: runner.os == 'Windows'
160187
id: postgres
161188
uses: ikalnytskyi/action-setup-postgres@v7
162189
- name: Print Roles
163-
run: psql ${{ steps.postgres.outputs.connection-uri }} -c "select rolname from pg_roles;"
190+
run: |
191+
if [[ "$RUNNER_OS" == "Linux" ]]; then
192+
docker exec postgres psql -U postgres -c "select rolname from pg_roles;"
193+
else
194+
psql ${{ steps.postgres.outputs.connection-uri }} -c "select rolname from pg_roles;"
195+
fi
196+
shell: bash
164197
- name: Run tests
165198
run: cargo test --workspace
166199

.sqlx/query-277e47bf46f8331549f55c8a0ebae6f3075c4f754cd379b0555c205fff95a95c.json

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.sqlx/query-4ea19fee016f1daeafdc466647d117910b19f540f19393b76aa6434e9d1d8502.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.sqlx/query-df57cc22f7d63847abce1d0d15675ba8951faa1be2ea6b2bf6714b1aa9127a6f.json

Lines changed: 0 additions & 44 deletions
This file was deleted.

Cargo.lock

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pgt_lexer = { path = "./crates/pgt_lexer", version = "0.0.0" }
7676
pgt_lexer_codegen = { path = "./crates/pgt_lexer_codegen", version = "0.0.0" }
7777
pgt_lsp = { path = "./crates/pgt_lsp", version = "0.0.0" }
7878
pgt_markup = { path = "./crates/pgt_markup", version = "0.0.0" }
79+
pgt_plpgsql_check = { path = "./crates/pgt_plpgsql_check", version = "0.0.0" }
7980
pgt_query = { path = "./crates/pgt_query", version = "0.0.0" }
8081
pgt_query_ext = { path = "./crates/pgt_query_ext", version = "0.0.0" }
8182
pgt_query_macros = { path = "./crates/pgt_query_macros", version = "0.0.0" }

Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM postgres:15
2+
3+
# Install build dependencies
4+
RUN apt-get update && \
5+
apt-get install -y postgresql-server-dev-15 gcc make git && \
6+
cd /tmp && \
7+
git clone https://github.com/okbob/plpgsql_check.git && \
8+
cd plpgsql_check && \
9+
make && \
10+
make install && \
11+
apt-get remove -y postgresql-server-dev-15 gcc make git && \
12+
apt-get autoremove -y && \
13+
rm -rf /tmp/plpgsql_check /var/lib/apt/lists/*
14+
15+
# Add initialization script directly
16+
RUN echo "CREATE EXTENSION IF NOT EXISTS plpgsql_check;" > /docker-entrypoint-initdb.d/01-create-extension.sql

crates/pgt_diagnostics_categories/src/categories.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ define_categories! {
3232
"flags/invalid",
3333
"project",
3434
"typecheck",
35+
"plpgsql_check",
3536
"internalError/panic",
3637
"syntax",
3738
"dummy",

crates/pgt_plpgsql_check/Cargo.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[package]
2+
authors.workspace = true
3+
categories.workspace = true
4+
description = "<DESCRIPTION>"
5+
edition.workspace = true
6+
homepage.workspace = true
7+
keywords.workspace = true
8+
license.workspace = true
9+
name = "pgt_plpgsql_check"
10+
repository.workspace = true
11+
version = "0.0.0"
12+
13+
14+
[dependencies]
15+
pgt_console = { workspace = true }
16+
pgt_diagnostics = { workspace = true }
17+
pgt_query = { workspace = true }
18+
pgt_query_ext = { workspace = true }
19+
pgt_schema_cache = { workspace = true }
20+
pgt_text_size = { workspace = true }
21+
regex = { workspace = true }
22+
serde = { workspace = true }
23+
serde_json = { workspace = true }
24+
sqlx = { workspace = true }
25+
tree-sitter = { workspace = true }
26+
27+
[dev-dependencies]
28+
pgt_test_utils = { workspace = true }
29+
30+
[lib]

0 commit comments

Comments
 (0)