00-init.sql140 lines · main
1-- We apply our test seedings to template1 so every new created db will have the same structure
2\c template1
3
4-- Tables for testing
5
6CREATE TYPE public.user_status AS ENUM ('ACTIVE', 'INACTIVE');
7CREATE TYPE composite_type_with_array_attribute AS (my_text_array text[]);
8
9CREATE TABLE public.users (
10 id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
11 name text,
12 status user_status DEFAULT 'ACTIVE'
13);
14INSERT INTO
15 public.users (name)
16VALUES
17 ('Joe Bloggs'),
18 ('Jane Doe');
19
20CREATE TABLE public.todos (
21 id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
22 details text,
23 "user-id" bigint REFERENCES users NOT NULL
24);
25
26INSERT INTO
27 public.todos (details, "user-id")
28VALUES
29 ('Star the repo', 1),
30 ('Watch the releases', 2);
31
32
33CREATE FUNCTION add(integer, integer) RETURNS integer
34 AS 'select $1 + $2;'
35 LANGUAGE SQL
36 IMMUTABLE
37 RETURNS NULL ON NULL INPUT;
38
39create table public.users_audit (
40 id BIGINT generated by DEFAULT as identity,
41 created_at timestamptz DEFAULT now(),
42 user_id bigint,
43 previous_value jsonb
44);
45
46create function public.audit_action()
47returns trigger as $$
48begin
49 insert into public.users_audit (user_id, previous_value)
50 values (old.id, row_to_json(old));
51
52 return new;
53end;
54$$ language plpgsql;
55
56CREATE VIEW todos_view AS SELECT * FROM public.todos;
57-- For testing typegen on view-to-view relationships
58create view users_view as select * from public.users;
59
60create materialized view todos_matview as select * from public.todos;
61
62create function public.blurb(public.todos) returns text as
63$$
64select substring($1.details, 1, 3);
65$$ language sql stable;
66
67create function public.blurb_varchar(public.todos) returns character varying as
68$$
69select substring($1.details, 1, 3);
70$$ language sql stable;
71
72create function public.details_length(public.todos) returns integer as
73$$
74select length($1.details);
75$$ language sql stable;
76
77create function public.details_is_long(public.todos) returns boolean as
78$$
79select $1.details_length > 20;
80$$ language sql stable;
81
82create function public.details_words(public.todos) returns text[] as
83$$
84select string_to_array($1.details, ' ');
85$$ language sql stable;
86
87create extension postgres_fdw;
88create server foreign_server foreign data wrapper postgres_fdw options (host 'localhost', port '5432', dbname 'postgres');
89create user mapping for postgres server foreign_server options (user 'postgres', password 'postgres');
90create foreign table foreign_table (
91 id int8 not null,
92 name text,
93 status user_status
94) server foreign_server options (schema_name 'public', table_name 'users');
95
96create or replace function public.function_returning_row()
97returns public.users
98language sql
99stable
100as $$
101 select * from public.users limit 1;
102$$;
103
104create or replace function public.function_returning_set_of_rows()
105returns setof public.users
106language sql
107stable
108as $$
109 select * from public.users;
110$$;
111
112create or replace function public.function_returning_table()
113returns table (id int, name text)
114language sql
115stable
116as $$
117 select id, name from public.users;
118$$;
119
120create or replace function public.polymorphic_function(text) returns void language sql as '';
121create or replace function public.polymorphic_function(bool) returns void language sql as '';
122
123create table user_details (
124 user_id int8 references users(id) primary key,
125 details text
126);
127
128create view a_view as select id from users;
129
130create table empty();
131
132create table table_with_other_tables_row_type (
133 col1 user_details,
134 col2 a_view
135);
136
137create table table_with_primary_key_other_than_id (
138 other_id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
139 name text
140);