-- Migration number: 0004 	 2024-11-25T09:05:22.577Z
CREATE TABLE user_tmp (
    `id` TEXT NOT NULL PRIMARY KEY,
    `name` TEXT NOT NULL,
    `avatar_hash` TEXT NULL
);

CREATE TABLE session_tmp (
    `id` TEXT NOT NULL PRIMARY KEY,
    `user_id` TEXT NOT NULL REFERENCES user_tmp(id),
    `expires_at` INTEGER NOT NULL
);

CREATE TABLE game_tmp (
    `id` TEXT NOT NULL PRIMARY KEY, -- uuid
    `user_id` TEXT NULL REFERENCES user_tmp(id),
    `mode` TEXT NOT NULL,
    `time` INTEGER NOT NULL,
    `total_score` INTEGER NOT NULL,
    `stops_type` TEXT NOT NULL
);

CREATE TABLE round_tmp (
    `id` INTEGER PRIMARY KEY ASC, -- rowid alias, should be automatically assigned
    `game_id` TEXT NOT NULL REFERENCES game_tmp(id),
    `points` INTEGER NOT NULL,
    `distance` INTEGER NOT NULL,
    `stop_name` TEXT NOT NULL
);

INSERT INTO user_tmp(id, name, avatar_hash) SELECT id, name, avatar_hash FROM user;
INSERT INTO session_tmp(id, user_id, expires_at) SELECT id, user_id, expires_at FROM session;
INSERT INTO game_tmp(id, user_id, mode, time, total_score, stops_type) SELECT id, user_id, mode, time, total_score, stops_type FROM game;
INSERT INTO round_tmp(id, game_id, points, distance, stop_name) SELECT id, game_id, points, distance, stop_name FROM round;

DROP TABLE round;
DROP TABLE game;
DROP TABLE session;
DROP TABLE user;

ALTER TABLE user_tmp RENAME TO user;
ALTER TABLE session_tmp RENAME TO session;
ALTER TABLE game_tmp RENAME TO game;
ALTER TABLE round_tmp RENAME TO round;
