00-init.sql 3.6 KB

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