Skip to content

Commit 6a58eb1

Browse files
authored
Check SQL for errors too (#27)
1 parent 241c59b commit 6a58eb1

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

docs/features/sharding/cross-shard.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ Some statements, like `CREATE INDEX CONCURRENTLY`, cannot run inside transaction
132132

133133
```postgresql
134134
DROP INDEX IF EXISTS user_id_idx;
135-
CREATE INDEX CONCURRENTLY user_id_idx USING btree(user_id);
135+
CREATE INDEX CONCURRENTLY user_id_idx ON users USING btree(user_id);
136136
```
137137

138138
## Under the hood

docs/features/sharding/schema_management/primary_keys.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ CREATE TABLE users (
2525
If you run this command through PgDog, this table will be created on all shards. Underneath, Postgres expands `BIGSERIAL` to the following code:
2626

2727
```postgresql
28-
BIGINT NOT NULL DEFAULT nextval('users_id_seq'::regclass)
28+
CREATE TABLE users (
29+
id BIGINT UNIQUE NOT NULL DEFAULT nextval('users_id_seq'::regclass),
30+
email VARCHAR NOT NULL,
31+
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
32+
);
2933
```
3034

3135
The `users_id_seq` is a sequence, automatically created by Postgres, that will be used to generate unique values for inserted rows that don't provide one for the `id` column.

docs/features/sharding/supported-queries.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ Postgres has 3 kinds of queries, each handled a little bit differently in a shar
2424

2525
```postgresql
2626
-- Sharding key equals a single value
27-
SELECT * FROM users WHERE user_id = $1
27+
SELECT * FROM users WHERE user_id = $1;
2828
2929
-- Sharding keys IN tuple
30-
SELECT * FROM users WHERE id IN ($1, $2, $3)
30+
SELECT * FROM users WHERE id IN ($1, $2, $3);
3131
```
3232

3333
Queries that don't match this pattern will currently be routed to all shards. We are continuously adding support for more complex patterns.

tests/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
markdown-it-py
22
regex
3+
pglast

tests/test_code_blocks.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55
import subprocess
66
from markdown_it import MarkdownIt
77
import sys
8+
import pglast
89

910
from regex import sub
1011
from regex.regex import Regex, RegexFlag
1112
mdp = MarkdownIt()
1213

13-
pattern = re.compile(r'(?msi)^(?P<fence>[`~]{3,})[ \t]*toml\b[^\n]*\r?\n(?P<code>.*?)^(?P=fence)[ \t]*\r?$',)
14+
pattern = re.compile(
15+
r'(?msi)^(?P<fence>[`~]{3,})[^\n]*\r?\n(?P<code>.*?)^(?P=fence)[ \t]*\r?$'
16+
)
17+
18+
replication = [
19+
"CREATE_REPLICATION_SLOT",
20+
"START_REPLICATION",
21+
]
1422

1523
def verify(binary):
1624
for file in glob.glob("docs/**/*.md",
@@ -27,6 +35,17 @@ def verify(binary):
2735
pass
2836
else:
2937
check_file(binary, "pgdog", token.content)
38+
elif token.type == "fence" and token.info == "postgresql":
39+
try:
40+
pglast.parser.parse_sql(token.content)
41+
except Exception as e:
42+
found = False
43+
for cmd in replication:
44+
if cmd in token.content:
45+
found = True
46+
if not found:
47+
print(token.content)
48+
raise e
3049

3150
def check_file(binary, kind, content):
3251
tmp = f"/tmp/pgdog_config_test.toml"

0 commit comments

Comments
 (0)