|
| 1 | +-- For SQLite, we do all operations in a single migration for simplicity. |
| 2 | + |
| 3 | +CREATE TABLE IF NOT EXISTS "_identity_credential_identifiers_tmp" ( |
| 4 | +"id" TEXT PRIMARY KEY, |
| 5 | +"identifier" TEXT NOT NULL, |
| 6 | +"identity_credential_id" char(36) NOT NULL, |
| 7 | +"created_at" DATETIME NOT NULL, |
| 8 | +"updated_at" DATETIME NOT NULL, |
| 9 | +"nid" char(36), |
| 10 | +"identity_credential_type_id" char(36) NOT NULL, |
| 11 | +"identity_id" char(36) NOT NULL, |
| 12 | +FOREIGN KEY (identity_id) REFERENCES identities (id) ON UPDATE RESTRICT ON DELETE CASCADE, |
| 13 | +FOREIGN KEY (nid) REFERENCES networks (id) ON UPDATE RESTRICT ON DELETE CASCADE, |
| 14 | +FOREIGN KEY (identity_credential_id) REFERENCES identity_credentials (id) ON UPDATE RESTRICT ON DELETE CASCADE, |
| 15 | +FOREIGN KEY (identity_credential_type_id) REFERENCES identity_credential_types (id) ON UPDATE RESTRICT ON DELETE CASCADE |
| 16 | +); |
| 17 | + |
| 18 | + |
| 19 | +INSERT INTO _identity_credential_identifiers_tmp (id, identifier, identity_credential_id, created_at, updated_at, nid, identity_credential_type_id, identity_id) |
| 20 | + SELECT ici.id, ici.identifier, ici.identity_credential_id, ici.created_at, ici.updated_at, ici.nid, ici.identity_credential_type_id, ic.identity_id |
| 21 | + FROM identity_credential_identifiers ici |
| 22 | + INNER JOIN identity_credentials ic ON ici.identity_credential_id = ic.id AND ici.nid = ic.nid; |
| 23 | + |
| 24 | +DROP TABLE identity_credential_identifiers; |
| 25 | +ALTER TABLE "_identity_credential_identifiers_tmp" RENAME TO "identity_credential_identifiers"; |
| 26 | + |
| 27 | +CREATE UNIQUE INDEX "identity_credential_identifiers_identifier_nid_type_uq_idx" ON "identity_credential_identifiers" (nid, identity_credential_type_id, identifier); |
| 28 | +CREATE INDEX identity_credential_identifiers_nid_i_ici_idx ON "identity_credential_identifiers" (nid, identifier, identity_credential_id); |
| 29 | +CREATE INDEX identity_credential_identifiers_ici_nid_i_idx ON "identity_credential_identifiers" (identity_credential_id ASC, nid ASC, identifier ASC); |
| 30 | + |
| 31 | + |
| 32 | +CREATE TABLE IF NOT EXISTS "_session_devices_tmp" |
| 33 | +( |
| 34 | + "id" UUID PRIMARY KEY NOT NULL, |
| 35 | + "identity_id" UUID NOT NULL, |
| 36 | + "ip_address" VARCHAR(50) DEFAULT '', |
| 37 | + "user_agent" VARCHAR(512) DEFAULT '', |
| 38 | + "location" VARCHAR(512) DEFAULT '', |
| 39 | + "nid" UUID NOT NULL, |
| 40 | + "session_id" UUID NOT NULL, |
| 41 | + "created_at" timestamp NOT NULL, |
| 42 | + "updated_at" timestamp NOT NULL, |
| 43 | + CONSTRAINT "session_metadata_sessions_id_fk" FOREIGN KEY ("session_id") REFERENCES "sessions" ("id") ON DELETE cascade, |
| 44 | + CONSTRAINT "session_metadata_nid_fk" FOREIGN KEY ("nid") REFERENCES "networks" ("id") ON DELETE cascade, |
| 45 | + CONSTRAINT "session_devices_identity_id_fk" FOREIGN KEY ("identity_id") REFERENCES "identities" ("id") ON DELETE cascade, |
| 46 | + CONSTRAINT unique_session_device UNIQUE (nid, session_id, ip_address, user_agent) |
| 47 | +); |
| 48 | + |
| 49 | +INSERT INTO "_session_devices_tmp" (id, identity_id, ip_address, user_agent, location, nid, session_id, created_at, updated_at) |
| 50 | + SELECT sd.id, s.identity_id, sd.ip_address, sd.user_agent, sd.location, sd.nid, sd.session_id, sd.created_at, sd.updated_at |
| 51 | + FROM session_devices sd JOIN sessions s ON sd.session_id = s.id; |
| 52 | + |
| 53 | +DROP TABLE session_devices; |
| 54 | +ALTER TABLE "_session_devices_tmp" RENAME TO "session_devices"; |
| 55 | + |
| 56 | +CREATE INDEX session_devices_nid_idx ON session_devices (nid ASC); |
| 57 | +CREATE INDEX session_devices_session_id_idx ON session_devices (session_id ASC); |
0 commit comments