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