|
| 1 | +-- Schema for orm-test: M:N integration tests |
| 2 | +-- posts <-> tags via post_tags junction table |
| 3 | + |
| 4 | +CREATE SCHEMA IF NOT EXISTS "orm_test"; |
| 5 | + |
| 6 | +GRANT USAGE ON SCHEMA "orm_test" TO administrator, authenticated, anonymous; |
| 7 | + |
| 8 | +ALTER DEFAULT PRIVILEGES IN SCHEMA "orm_test" |
| 9 | + GRANT ALL ON TABLES TO administrator; |
| 10 | +ALTER DEFAULT PRIVILEGES IN SCHEMA "orm_test" |
| 11 | + GRANT USAGE ON SEQUENCES TO administrator, authenticated; |
| 12 | +ALTER DEFAULT PRIVILEGES IN SCHEMA "orm_test" |
| 13 | + GRANT ALL ON FUNCTIONS TO administrator, authenticated, anonymous; |
| 14 | + |
| 15 | +-- ============================================================================= |
| 16 | +-- Table: posts |
| 17 | +-- ============================================================================= |
| 18 | +CREATE TABLE "orm_test".posts ( |
| 19 | + id uuid PRIMARY KEY DEFAULT uuid_generate_v4(), |
| 20 | + title text NOT NULL, |
| 21 | + body text, |
| 22 | + is_published boolean DEFAULT false, |
| 23 | + created_at timestamptz DEFAULT now(), |
| 24 | + updated_at timestamptz DEFAULT now() |
| 25 | +); |
| 26 | + |
| 27 | +-- ============================================================================= |
| 28 | +-- Table: tags |
| 29 | +-- ============================================================================= |
| 30 | +CREATE TABLE "orm_test".tags ( |
| 31 | + id uuid PRIMARY KEY DEFAULT uuid_generate_v4(), |
| 32 | + name text NOT NULL UNIQUE, |
| 33 | + color text DEFAULT '#888888', |
| 34 | + created_at timestamptz DEFAULT now() |
| 35 | +); |
| 36 | + |
| 37 | +-- ============================================================================= |
| 38 | +-- Table: post_tags (junction) |
| 39 | +-- M:N relationship between posts and tags |
| 40 | +-- ============================================================================= |
| 41 | +CREATE TABLE "orm_test".post_tags ( |
| 42 | + id uuid PRIMARY KEY DEFAULT uuid_generate_v4(), |
| 43 | + post_id uuid NOT NULL REFERENCES "orm_test".posts(id) ON DELETE CASCADE, |
| 44 | + tag_id uuid NOT NULL REFERENCES "orm_test".tags(id) ON DELETE CASCADE, |
| 45 | + created_at timestamptz DEFAULT now(), |
| 46 | + UNIQUE(post_id, tag_id) |
| 47 | +); |
| 48 | + |
| 49 | +CREATE INDEX post_tags_post_id_idx ON "orm_test".post_tags (post_id); |
| 50 | +CREATE INDEX post_tags_tag_id_idx ON "orm_test".post_tags (tag_id); |
| 51 | + |
| 52 | +-- Enable many-to-many for this junction table |
| 53 | +COMMENT ON TABLE "orm_test".post_tags IS E'@behavior +manyToMany'; |
| 54 | + |
| 55 | +-- ============================================================================= |
| 56 | +-- Timestamp triggers |
| 57 | +-- ============================================================================= |
| 58 | +CREATE TRIGGER posts_timestamps_tg |
| 59 | + BEFORE INSERT OR UPDATE ON "orm_test".posts |
| 60 | + FOR EACH ROW EXECUTE PROCEDURE stamps.timestamps(); |
| 61 | + |
| 62 | +-- ============================================================================= |
| 63 | +-- Grant table permissions |
| 64 | +-- ============================================================================= |
| 65 | +GRANT SELECT, INSERT, UPDATE, DELETE ON "orm_test".posts TO administrator, authenticated, anonymous; |
| 66 | +GRANT SELECT, INSERT, UPDATE, DELETE ON "orm_test".tags TO administrator, authenticated, anonymous; |
| 67 | +GRANT SELECT, INSERT, UPDATE, DELETE ON "orm_test".post_tags TO administrator, authenticated, anonymous; |
0 commit comments