Skip to content

Commit 856c975

Browse files
committed
add timestamp, time
1 parent c37f204 commit 856c975

File tree

5 files changed

+31
-17
lines changed

5 files changed

+31
-17
lines changed

packages/deparser/__tests__/pretty/__snapshots__/constraints-pretty.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ exports[`non-pretty: pretty/constraints-9.sql 1`] = `"ALTER TABLE finance.transa
2020

2121
exports[`non-pretty: pretty/constraints-10.sql 1`] = `"ALTER TABLE school.enrollments ADD CONSTRAINT fk_student_course FOREIGN KEY (student_id, course_id) REFERENCES school.courses_students (student_id, course_id)"`;
2222

23-
exports[`non-pretty: pretty/constraints-11.sql 1`] = `"CREATE TABLE orders (id serial PRIMARY KEY, user_id int NOT NULL, total numeric(10, 2) CHECK (total > 0), status varchar(20) DEFAULT 'pending', created_at pg_catalog.timestamp DEFAULT now(), CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE, CONSTRAINT unique_user_date UNIQUE (user_id, created_at), CONSTRAINT check_status CHECK (status IN ('pending', 'completed', 'cancelled')))"`;
23+
exports[`non-pretty: pretty/constraints-11.sql 1`] = `"CREATE TABLE orders (id serial PRIMARY KEY, user_id int NOT NULL, total numeric(10, 2) CHECK (total > 0), status varchar(20) DEFAULT 'pending', created_at timestamp DEFAULT now(), CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE, CONSTRAINT unique_user_date UNIQUE (user_id, created_at), CONSTRAINT check_status CHECK (status IN ('pending', 'completed', 'cancelled')))"`;
2424

2525
exports[`non-pretty: pretty/constraints-12.sql 1`] = `"ALTER TABLE products ADD CONSTRAINT fk_category FOREIGN KEY (category_id) REFERENCES categories (id) ON UPDATE CASCADE ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED"`;
2626

@@ -104,7 +104,7 @@ exports[`pretty: pretty/constraints-11.sql 1`] = `
104104
user_id int NOT NULL,
105105
total numeric(10, 2) CHECK (total > 0),
106106
status varchar(20) DEFAULT 'pending',
107-
created_at pg_catalog.timestamp DEFAULT now(),
107+
created_at timestamp DEFAULT now(),
108108
CONSTRAINT fk_user
109109
FOREIGN KEY(user_id)
110110
REFERENCES users (id)

packages/deparser/__tests__/pretty/__snapshots__/create-table-pretty.test.ts.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
exports[`non-pretty: pretty/create_table-1.sql 1`] = `"CREATE TABLE users (id serial PRIMARY KEY, name text NOT NULL, email text UNIQUE)"`;
44

5-
exports[`non-pretty: pretty/create_table-2.sql 1`] = `"CREATE TABLE products (id serial PRIMARY KEY, name varchar(255) NOT NULL, price numeric(10, 2) CHECK (price > 0), category_id int, description text, created_at pg_catalog.timestamp DEFAULT now(), updated_at pg_catalog.timestamp, UNIQUE (name, category_id), FOREIGN KEY (category_id) REFERENCES categories (id))"`;
5+
exports[`non-pretty: pretty/create_table-2.sql 1`] = `"CREATE TABLE products (id serial PRIMARY KEY, name varchar(255) NOT NULL, price numeric(10, 2) CHECK (price > 0), category_id int, description text, created_at timestamp DEFAULT now(), updated_at timestamp, UNIQUE (name, category_id), FOREIGN KEY (category_id) REFERENCES categories (id))"`;
66

77
exports[`non-pretty: pretty/create_table-3.sql 1`] = `"CREATE TABLE orders (id serial PRIMARY KEY, subtotal numeric(10, 2) NOT NULL, tax_rate numeric(5, 4) DEFAULT 0.0825, tax_amount numeric(10, 2) GENERATED ALWAYS AS (subtotal * tax_rate) STORED, total numeric(10, 2) GENERATED ALWAYS AS (subtotal + tax_amount) STORED)"`;
88

99
exports[`non-pretty: pretty/create_table-4.sql 1`] = `"CREATE TABLE sales (id serial, sale_date date NOT NULL, amount numeric(10, 2), region varchar(50)) PARTITION BY RANGE (sale_date)"`;
1010

1111
exports[`non-pretty: pretty/create_table-5.sql 1`] = `"CREATE TEMPORARY TABLE temp_calculations (id int, value numeric(15, 5), result text)"`;
1212

13-
exports[`non-pretty: pretty/create_table-6.sql 1`] = `"CREATE TABLE orders (id serial PRIMARY KEY, user_id int NOT NULL, total numeric(10, 2) CHECK (total > 0), status varchar(20) DEFAULT 'pending', created_at pg_catalog.timestamp DEFAULT now(), FOREIGN KEY (user_id) REFERENCES users (id))"`;
13+
exports[`non-pretty: pretty/create_table-6.sql 1`] = `"CREATE TABLE orders (id serial PRIMARY KEY, user_id int NOT NULL, total numeric(10, 2) CHECK (total > 0), status varchar(20) DEFAULT 'pending', created_at timestamp DEFAULT now(), FOREIGN KEY (user_id) REFERENCES users (id))"`;
1414

1515
exports[`pretty: pretty/create_table-1.sql 1`] = `
1616
"CREATE TABLE users (
@@ -27,8 +27,8 @@ exports[`pretty: pretty/create_table-2.sql 1`] = `
2727
price numeric(10, 2) CHECK (price > 0),
2828
category_id int,
2929
description text,
30-
created_at pg_catalog.timestamp DEFAULT now(),
31-
updated_at pg_catalog.timestamp,
30+
created_at timestamp DEFAULT now(),
31+
updated_at timestamp,
3232
UNIQUE (name, category_id),
3333
FOREIGN KEY(category_id)
3434
REFERENCES categories (id)
@@ -68,7 +68,7 @@ exports[`pretty: pretty/create_table-6.sql 1`] = `
6868
user_id int NOT NULL,
6969
total numeric(10, 2) CHECK (total > 0),
7070
status varchar(20) DEFAULT 'pending',
71-
created_at pg_catalog.timestamp DEFAULT now(),
71+
created_at timestamp DEFAULT now(),
7272
FOREIGN KEY(user_id)
7373
REFERENCES users (id)
7474
)"

packages/deparser/__tests__/pretty/__snapshots__/misc-pretty.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exports[`non-pretty: pretty/misc-3.sql 1`] = `"SELECT u.id, u.name, j.key, j.val
88

99
exports[`non-pretty: pretty/misc-4.sql 1`] = `"SELECT p.id, p.title, CASE WHEN EXISTS (SELECT 1 FROM reviews AS r WHERE r.product_id = p.id AND r.rating >= 4) THEN 'Popular' ELSE 'Unrated' END AS status FROM products AS p WHERE p.archived = false"`;
1010

11-
exports[`non-pretty: pretty/misc-5.sql 1`] = `"WITH logs AS (SELECT id, payload::json ->> 'event' AS event, CAST(payload::json ->> 'ts' AS pg_catalog.timestamp) AS ts FROM event_log WHERE ts > (now() - '7 days'::interval)) SELECT event, count(*) AS freq FROM ( SELECT DISTINCT event, ts::date AS event_day FROM logs ) AS d GROUP BY event ORDER BY freq DESC"`;
11+
exports[`non-pretty: pretty/misc-5.sql 1`] = `"WITH logs AS (SELECT id, payload::json ->> 'event' AS event, CAST(payload::json ->> 'ts' AS timestamp) AS ts FROM event_log WHERE ts > (now() - '7 days'::interval)) SELECT event, count(*) AS freq FROM ( SELECT DISTINCT event, ts::date AS event_day FROM logs ) AS d GROUP BY event ORDER BY freq DESC"`;
1212

1313
exports[`non-pretty: pretty/misc-6.sql 1`] = `"SELECT o.id AS order_id, u.name AS user_name, p.name AS product_name, s.status, sh.shipped_at, r.refund_amount FROM orders AS o JOIN users AS u ON o.user_id = u.id JOIN order_items AS oi ON oi.order_id = o.id JOIN products AS p ON (p.id = oi.product_id AND p.available = true) OR (p.sku = oi.product_sku AND (p.discontinued = false OR p.replacement_id IS NOT NULL)) LEFT JOIN shipping AS sh ON sh.order_id = o.id AND ((sh.carrier = 'UPS' AND sh.tracking_number IS NOT NULL) OR (sh.carrier = 'FedEx' AND sh.shipped_at > (o.created_at + '1 day'::interval))) LEFT JOIN statuses AS s ON s.id = o.status_id AND (s.name <> 'cancelled' OR (s.name = 'cancelled' AND s.updated_at > (now() - '7 days'::interval))) LEFT JOIN refunds AS r ON r.order_id = o.id AND ((r.status = 'approved' AND r.processed_at IS NOT NULL) OR (r.status = 'pending' AND r.requested_at < (now() - '14 days'::interval))) WHERE o.created_at > (now() - '90 days'::interval) AND u.active = true AND (s.status = 'shipped' OR (s.status = 'processing' AND EXISTS (SELECT 1 FROM order_notes AS n WHERE (n.order_id = o.id AND n.note ILIKE '%expedite%')))) ORDER BY o.created_at DESC"`;
1414

@@ -109,7 +109,7 @@ exports[`pretty: pretty/misc-5.sql 1`] = `
109109
logs AS (SELECT
110110
id,
111111
payload::json ->> 'event' AS event,
112-
CAST(payload::json ->> 'ts' AS pg_catalog.timestamp) AS ts
112+
CAST(payload::json ->> 'ts' AS timestamp) AS ts
113113
FROM event_log
114114
WHERE
115115
ts > (now() - '7 days'::interval))

packages/deparser/__tests__/pretty/__snapshots__/pg-catalog.test.ts.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ exports[`non-pretty: misc/pg_catalog-17.sql 1`] = `"SELECT json_agg(name) FROM (
3636

3737
exports[`non-pretty: misc/pg_catalog-timestamp-etc-1.sql 1`] = `"CREATE TABLE public."ACTIVITY" ("ACTIVITY_ID" bigint NOT NULL, "PUBLISHED" timestamp(2) with time zone)"`;
3838

39-
exports[`non-pretty: misc/pg_catalog-timestamp-etc-2.sql 1`] = `"CREATE TABLE public."ACTIVITY" ("ACTIVITY_ID" bigint NOT NULL, "PUBLISHED" pg_catalog.timestamp(2))"`;
39+
exports[`non-pretty: misc/pg_catalog-timestamp-etc-2.sql 1`] = `"CREATE TABLE public."ACTIVITY" ("ACTIVITY_ID" bigint NOT NULL, "PUBLISHED" timestamp(2))"`;
4040

4141
exports[`non-pretty: misc/pg_catalog-timestamp-etc-3.sql 1`] = `"CREATE TABLE public."EXAMPLE" ("MESSAGE" text)"`;
4242

43-
exports[`non-pretty: misc/pg_catalog-timestamp-etc-4.sql 1`] = `"CREATE TABLE public."EXAMPLE_TIME" ("START_TIME" pg_catalog.time)"`;
43+
exports[`non-pretty: misc/pg_catalog-timestamp-etc-4.sql 1`] = `"CREATE TABLE public."EXAMPLE_TIME" ("START_TIME" time)"`;
4444

4545
exports[`non-pretty: misc/pg_catalog-timestamp-etc-5.sql 1`] = `"CREATE TABLE public."EXAMPLE_TIMETZ" ("START_TIME" time with time zone)"`;
4646

47-
exports[`non-pretty: misc/pg_catalog-timestamp-etc-6.sql 1`] = `"CREATE TABLE public."EXAMPLE_TIME_PRECISION" ("START_TIME" pg_catalog.time(3))"`;
47+
exports[`non-pretty: misc/pg_catalog-timestamp-etc-6.sql 1`] = `"CREATE TABLE public."EXAMPLE_TIME_PRECISION" ("START_TIME" time(3))"`;
4848

4949
exports[`non-pretty: misc/pg_catalog-timestamp-etc-7.sql 1`] = `"CREATE TABLE public."EXAMPLE_TIMETZ_PRECISION" ("START_TIME" time(3) with time zone)"`;
5050

51-
exports[`non-pretty: misc/pg_catalog-timestamp-etc-8.sql 1`] = `"CREATE TABLE public."EXAMPLE_TIMESTAMP" ("CREATED_AT" pg_catalog.timestamp)"`;
51+
exports[`non-pretty: misc/pg_catalog-timestamp-etc-8.sql 1`] = `"CREATE TABLE public."EXAMPLE_TIMESTAMP" ("CREATED_AT" timestamp)"`;
5252

5353
exports[`non-pretty: misc/pg_catalog-timestamp-etc-9.sql 1`] = `"CREATE TABLE public."EXAMPLE_TIMESTAMPTZ" ("CREATED_AT" timestamp with time zone)"`;
5454

@@ -129,7 +129,7 @@ exports[`pretty: misc/pg_catalog-timestamp-etc-1.sql 1`] = `
129129
exports[`pretty: misc/pg_catalog-timestamp-etc-2.sql 1`] = `
130130
"CREATE TABLE public."ACTIVITY" (
131131
"ACTIVITY_ID" bigint NOT NULL,
132-
"PUBLISHED" pg_catalog.timestamp(2)
132+
"PUBLISHED" timestamp(2)
133133
)"
134134
`;
135135

@@ -141,7 +141,7 @@ exports[`pretty: misc/pg_catalog-timestamp-etc-3.sql 1`] = `
141141

142142
exports[`pretty: misc/pg_catalog-timestamp-etc-4.sql 1`] = `
143143
"CREATE TABLE public."EXAMPLE_TIME" (
144-
"START_TIME" pg_catalog.time
144+
"START_TIME" time
145145
)"
146146
`;
147147

@@ -153,7 +153,7 @@ exports[`pretty: misc/pg_catalog-timestamp-etc-5.sql 1`] = `
153153

154154
exports[`pretty: misc/pg_catalog-timestamp-etc-6.sql 1`] = `
155155
"CREATE TABLE public."EXAMPLE_TIME_PRECISION" (
156-
"START_TIME" pg_catalog.time(3)
156+
"START_TIME" time(3)
157157
)"
158158
`;
159159

@@ -165,7 +165,7 @@ exports[`pretty: misc/pg_catalog-timestamp-etc-7.sql 1`] = `
165165

166166
exports[`pretty: misc/pg_catalog-timestamp-etc-8.sql 1`] = `
167167
"CREATE TABLE public."EXAMPLE_TIMESTAMP" (
168-
"CREATED_AT" pg_catalog.timestamp
168+
"CREATED_AT" timestamp
169169
)"
170170
`;
171171

packages/deparser/src/deparser.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,20 @@ export class Deparser implements DeparserVisitor {
18301830
} else {
18311831
typeName = 'time with time zone';
18321832
}
1833+
} else if (type === 'timestamp') {
1834+
if (args) {
1835+
typeName = `timestamp(${args})`;
1836+
args = null; // Don't apply args again in mods()
1837+
} else {
1838+
typeName = 'timestamp';
1839+
}
1840+
} else if (type === 'time') {
1841+
if (args) {
1842+
typeName = `time(${args})`;
1843+
args = null; // Don't apply args again in mods()
1844+
} else {
1845+
typeName = 'time';
1846+
}
18331847
}
18341848

18351849
let result = mods(typeName, args);

0 commit comments

Comments
 (0)