-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathroundtrip.test.ts.snap
More file actions
223 lines (186 loc) Β· 4.81 KB
/
roundtrip.test.ts.snap
File metadata and controls
223 lines (186 loc) Β· 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
exports[`fixture round-trip (CST) alter-and-drop deparsed output matches snapshot 1`] = `
"-- Add columns to existing table
ALTER TABLE app.users
ADD COLUMN bio text;
ALTER TABLE app.users
ADD COLUMN avatar_url text;
-- Rename a column
ALTER TABLE app.users RENAME COLUMN username TO display_name;
-- Drop unused objects
DROP INDEX IF EXISTS app.idx_old_index;
-- Recreate with new definition
CREATE INDEX idx_users_display_name ON app.users (display_name);"
`;
exports[`fixture round-trip (CST) edge-cases deparsed output matches snapshot 1`] = `
"-- Comments with special characters: don't break "parsing"
SELECT 1;
-- Inline comment after statement
SELECT 2; -- trailing note
-- Adjacent comments with no blank line
-- first line
-- second line
SELECT 3;
-- Dollar-quoted body with internal comments (should NOT be extracted)
CREATE FUNCTION app.noop() RETURNS void AS $$
BEGIN
-- this comment is inside the function body
NULL;
END;
$$ LANGUAGE plpgsql;
-- String that looks like a comment
SELECT '-- not a comment' AS val;
-- Empty statement list edge
SELECT 4;"
`;
exports[`fixture round-trip (CST) grants-and-policies deparsed output matches snapshot 1`] = `
"-- RLS policies for the users table
ALTER TABLE app.users
ENABLE ROW LEVEL SECURITY;
-- Admins can see all rows
CREATE POLICY admin_all
ON app.users
AS PERMISSIVE
FOR ALL
TO admin_role
USING (
true
);
-- Users can only see their own row
CREATE POLICY own_row
ON app.users
AS PERMISSIVE
FOR SELECT
TO authenticated
USING (
id = (current_setting('app.current_user_id'))::int
);
-- Grant basic access
GRANT USAGE ON SCHEMA app TO authenticated;
GRANT SELECT ON app.users TO authenticated;
GRANT ALL ON app.users TO admin_role;"
`;
exports[`fixture round-trip (CST) mid-statement-comments deparsed output matches snapshot 1`] = `
"-- Mid-statement comments are hoisted above their enclosing statement.
-- The deparser cannot inject comments back into the middle of a
-- statement, so they are preserved as standalone lines above it.
-- Simple mid-statement comment
-- the primary key
SELECT
id,
name
FROM users;
-- Multiple mid-statement comments in one query
-- user ID
-- display name
-- role from join
SELECT
u.id,
u.name,
r.role_name
FROM users AS u
JOIN roles AS r ON r.id = u.role_id;
-- Mid-statement comment in INSERT values
-- log level
-- log body
INSERT INTO logs (
level,
message
) VALUES
('info', 'hello');
-- Comment between clauses
-- filter active only
SELECT id
FROM users
WHERE
active = true;"
`;
exports[`fixture round-trip (CST) multi-statement deparsed output matches snapshot 1`] = `
"-- Schema setup
CREATE SCHEMA IF NOT EXISTS app;
-- Users table
CREATE TABLE app.users (
id serial PRIMARY KEY,
username text NOT NULL,
created_at timestamptz DEFAULT now()
);
-- Roles table
CREATE TABLE app.roles (
id serial PRIMARY KEY,
name text UNIQUE NOT NULL
);
-- Junction table
CREATE TABLE app.user_roles (
user_id int REFERENCES app.users (id),
role_id int REFERENCES app.roles (id),
PRIMARY KEY (user_id, role_id)
);
-- Seed default roles
INSERT INTO app.roles (
name
) VALUES
('admin'),
('viewer');"
`;
exports[`fixture round-trip (CST) pgpm-header deparsed output matches snapshot 1`] = `
"-- Deploy schemas/my-app/tables/users to pg
-- requires: schemas/my-app/schema
BEGIN;
-- Create the main users table
CREATE TABLE my_app.users (
id serial PRIMARY KEY,
name text NOT NULL,
email text UNIQUE
);
-- Add an index for fast lookups
CREATE INDEX idx_users_email ON my_app.users (email);
COMMIT;"
`;
exports[`fixture round-trip (CST) plpgsql-function deparsed output matches snapshot 1`] = `
"-- Deploy schemas/app/functions/get_user to pg
-- requires: schemas/app/tables/users
BEGIN;
-- Function to get a user by ID
CREATE FUNCTION app.get_user(
p_id int
) RETURNS TABLE (
id int,
username text,
created_at timestamptz
) AS $$
BEGIN
-- Return the matching user
RETURN QUERY
SELECT u.id, u.username, u.created_at
FROM app.users u
WHERE u.id = p_id;
END;
$$ LANGUAGE plpgsql STABLE;
-- Grant execute to authenticated users
GRANT EXECUTE ON FUNCTION app.get_user(int) TO authenticated;
COMMIT;"
`;
exports[`fixture round-trip (CST) views-and-triggers deparsed output matches snapshot 1`] = `
"-- Active users view
CREATE VIEW app.active_users AS SELECT
id,
username,
created_at
FROM app.users
WHERE
created_at > (now() - '90 days'::interval);
-- Audit trigger function
CREATE FUNCTION app.audit_trigger() RETURNS trigger AS $$
BEGIN
INSERT INTO app.audit_log (table_name, action, row_id)
VALUES (TG_TABLE_NAME, TG_OP, NEW.id);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Attach trigger to users table
CREATE TRIGGER users_audit
AFTER INSERT OR UPDATE
ON app.users
FOR EACH ROW
EXECUTE PROCEDURE app.audit_trigger();"
`;