Handle new user having builds in account migration

This commit is contained in:
Kalle 2025-07-02 19:29:44 +03:00
parent b4b03950bb
commit c5240696ff
3 changed files with 67 additions and 1 deletions

View File

@ -58,6 +58,10 @@ export function migrate(args: { newUserId: number; oldUserId: number }) {
.deleteFrom("UserWeapon")
.where("userId", "=", args.newUserId)
.execute();
await trx
.deleteFrom("Build")
.where("ownerId", "=", args.newUserId)
.execute();
await trx
.deleteFrom("UserFriendCode")
.where("userId", "=", args.newUserId)

View File

@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, test, vi } from "vitest";
import { db } from "~/db/sql";
import * as BuildRepository from "~/features/builds/BuildRepository.server";
import * as PlusVotingRepository from "~/features/plus-voting/PlusVotingRepository.server";
import * as TeamRepository from "~/features/team/TeamRepository.server";
import * as UserRepository from "~/features/user-page/UserRepository.server";
@ -348,4 +349,62 @@ describe("Account migration", () => {
expect(membershipAfterMigration).toBeUndefined();
});
it("deletes weapon pool from the new user when migrating (takes weapon pool from the old user)", async () => {
await UserRepository.updateProfile({
userId: 1,
weapons: [{ weaponSplId: 1, isFavorite: 1 }],
});
await UserRepository.updateProfile({
userId: 2,
weapons: [{ weaponSplId: 10 }],
});
await migrateUserAction();
const oldUser = await UserRepository.findProfileByIdentifier("0");
const newUser = await UserRepository.findProfileByIdentifier("1");
expect(oldUser).toBeNull();
expect(newUser?.weapons).toEqual([{ weaponSplId: 1, isFavorite: 1 }]);
});
it("deletes builds from the new user when migrating", async () => {
await BuildRepository.create({
title: "Test build",
ownerId: 2,
headGearSplId: 1,
clothesGearSplId: 1,
shoesGearSplId: 1,
abilities: [
["SCU", "SCU", "SCU", "SCU"],
["SCU", "SCU", "SCU", "SCU"],
["SCU", "SCU", "SCU", "SCU"],
],
modes: null,
weaponSplIds: [1],
description: null,
private: 0,
});
const buildsBefore = await BuildRepository.allByUserId({
userId: 2,
showPrivate: false,
});
expect(buildsBefore.length).toBe(1);
await migrateUserAction();
const oldUser = await UserRepository.findProfileByIdentifier("0");
expect(oldUser).toBeNull();
for (const userId of [1, 2]) {
const buildsAfter = await BuildRepository.allByUserId({
userId,
showPrivate: false,
});
expect(buildsAfter.length).toBe(0);
}
});
});

View File

@ -18,6 +18,7 @@ import {
SendouTabs,
} from "~/components/elements/Tabs";
import { UserSearch } from "~/components/elements/UserSearch";
import { FormMessage } from "~/components/FormMessage";
import { Input } from "~/components/Input";
import { SearchIcon } from "~/components/icons/Search";
import { Main } from "~/components/Main";
@ -31,7 +32,6 @@ import {
STOP_IMPERSONATING_URL,
userPage,
} from "~/utils/urls";
import { action } from "../actions/admin.server";
import { loader } from "../loaders/admin.server";
export { loader, action };
@ -190,6 +190,9 @@ function MigrateUser() {
Migrate
</SubmitButton>
</div>
<FormMessage type="info">
Note: data on "New user" will be deleted (e.g. builds)
</FormMessage>
</fetcher.Form>
);
}