aboutsummaryrefslogtreecommitdiff
path: root/migrations/auth/20230907_01_pjnxz-refactor-add-resource-ownership-table.py
blob: 37fcfe74c9a4fda6f59be849430b1ec6bb36439c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""
refactor: add resource_ownership table
"""

from yoyo import step

__depends__ = {'20230410_02_WZqSf-create-mrna-resources-table'}

steps = [
    step(
        """
        CREATE TABLE IF NOT EXISTS resource_ownership(
        -- This table links resources to groups, where relevant
          group_id TEXT NOT NULL,
          resource_id TEXT NOT NULL,
          PRIMARY KEY(group_id, resource_id),
          FOREIGN KEY(group_id)
            REFERENCES groups(group_id)
            ON UPDATE CASCADE ON DELETE RESTRICT,
          FOREIGN KEY(resource_id)
            REFERENCES resources(resource_id)
            ON UPDATE CASCADE ON DELETE RESTRICT
        ) WITHOUT ROWID
        """,
        "DROP TABLE IF EXISTS resource_ownership"),
    step(# Copy over data
        """
        INSERT INTO resource_ownership
        SELECT group_id, resource_id FROM resources
        """
    )
]