Skip to content

Commit 822b391

Browse files
committed
chore: add single-file, multi-file schema example
Schema comes from https://github.com/pgschema/pgschema/tree/main/testdata/include
1 parent 5fd9d01 commit 822b391

21 files changed

+499
-0
lines changed

multifile/.DS_Store

8 KB
Binary file not shown.

multifile/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Multi-file Schema Dump
2+
3+
Each file represents a different database object. Different database object types are organized into different directories. The entire directory represents
4+
the whole schema. The dump is generated by:
5+
6+
```bash
7+
pgschema dump --host localhost --user postgres --password testpwd1 --db employee --multi-file --file main.sql
8+
```
9+
10+
`main.sql` is the entry file that uses `psql` compatible `\i` directive to include other files.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
--
2+
-- Name: email_address; Type: DOMAIN; Schema: -; Owner: -
3+
--
4+
5+
CREATE DOMAIN email_address AS text
6+
CONSTRAINT email_address_check CHECK (VALUE ~~ '%@%');
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
--
2+
-- Name: positive_integer; Type: DOMAIN; Schema: -; Owner: -
3+
--
4+
5+
CREATE DOMAIN positive_integer AS integer
6+
CONSTRAINT positive_integer_check CHECK (VALUE > 0);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--
2+
-- Name: get_order_count; Type: FUNCTION; Schema: -; Owner: -
3+
--
4+
5+
CREATE OR REPLACE FUNCTION get_order_count(
6+
user_id_param integer
7+
)
8+
RETURNS integer
9+
LANGUAGE sql
10+
SECURITY INVOKER
11+
VOLATILE
12+
AS $$
13+
SELECT COUNT(*) FROM orders WHERE user_id = user_id_param;
14+
$$;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--
2+
-- Name: get_user_count; Type: FUNCTION; Schema: -; Owner: -
3+
--
4+
5+
CREATE OR REPLACE FUNCTION get_user_count()
6+
RETURNS integer
7+
LANGUAGE sql
8+
SECURITY INVOKER
9+
VOLATILE
10+
AS $$
11+
SELECT COUNT(*) FROM users;
12+
$$;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--
2+
-- Name: update_timestamp; Type: FUNCTION; Schema: -; Owner: -
3+
--
4+
5+
CREATE OR REPLACE FUNCTION update_timestamp()
6+
RETURNS trigger
7+
LANGUAGE plpgsql
8+
SECURITY INVOKER
9+
STABLE
10+
AS $$
11+
BEGIN
12+
NEW.updated_at = NOW();
13+
RETURN NEW;
14+
END;
15+
$$;

multifile/main.sql

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
-- Main schema file demonstrating \i include functionality
2+
-- This represents a modular approach to organizing database schema
3+
-- Includes ALL supported PostgreSQL database objects
4+
5+
-- Include custom types first (dependencies for tables)
6+
\i types/user_status.sql
7+
\i types/order_status.sql
8+
\i types/address.sql
9+
10+
-- Include domain types (constrained base types)
11+
\i domains/email_address.sql
12+
\i domains/positive_integer.sql
13+
14+
-- Include sequences (may be used by tables)
15+
\i sequences/global_id_seq.sql
16+
\i sequences/order_number_seq.sql
17+
18+
-- Include trigger function (needed by users table trigger)
19+
\i functions/update_timestamp.sql
20+
21+
-- Include core tables (with their constraints, indexes, and policies)
22+
\i tables/users.sql
23+
\i tables/orders.sql
24+
25+
-- Include other functions (after tables that they reference)
26+
\i functions/get_user_count.sql
27+
\i functions/get_order_count.sql
28+
29+
-- Include procedures
30+
\i procedures/cleanup_orders.sql
31+
\i procedures/update_status.sql
32+
33+
-- Include views (depend on tables and functions)
34+
\i views/user_summary.sql
35+
\i views/order_details.sql
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--
2+
-- Name: cleanup_orders; Type: PROCEDURE; Schema: -; Owner: -
3+
--
4+
5+
CREATE OR REPLACE PROCEDURE cleanup_orders()
6+
LANGUAGE sql
7+
AS $$
8+
DELETE FROM orders WHERE status = 'completed';
9+
$$;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--
2+
-- Name: update_status; Type: PROCEDURE; Schema: -; Owner: -
3+
--
4+
5+
CREATE OR REPLACE PROCEDURE update_status(
6+
user_id_param integer,
7+
new_status text
8+
)
9+
LANGUAGE sql
10+
AS $$
11+
UPDATE orders SET status = new_status WHERE user_id = user_id_param;
12+
$$;

0 commit comments

Comments
 (0)