summary refs log tree commit diff
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2022-08-15 09:47:49 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2022-08-15 09:47:49 +0200
commit6d9ea1a7bc9d44a547fe1566393e122ae252eb8c (patch)
tree801bb36025a4610bf1861f038c008bbb8f9ace53
parentoops.. move migrations to right folder (diff)
downloadserver-6d9ea1a7bc9d44a547fe1566393e122ae252eb8c.tar.xz
Fix nullables, fix user settings hanging stuff
-rwxr-xr-xscripts/db_migrations.sh6
-rw-r--r--src/gateway/opcodes/Identify.ts2
-rw-r--r--src/util/entities/User.ts6
-rw-r--r--src/util/migrations/mariadb/1660549252130-fix_nullables.ts30
-rw-r--r--src/util/migrations/postgres/1660549242936-fix_nullables.ts30
-rw-r--r--src/util/migrations/sqlite/1660549233583-fix_nullables.ts240
6 files changed, 307 insertions, 7 deletions
diff --git a/scripts/db_migrations.sh b/scripts/db_migrations.sh
index 89404878..9ec8230a 100755
--- a/scripts/db_migrations.sh
+++ b/scripts/db_migrations.sh
@@ -19,9 +19,9 @@ make_migration() {
     mkdir "src/util/migrations/$2" 2>/dev/null
 #    npm run build clean logerrors pretty-errors
     THREADS=1 DATABASE="$1" DB_MIGRATE=a npm run start:bundle
-    THREADS=1 DATABASE="$1" DB_MIGRATE=a npx typeorm-ts-node-commonjs migration:generate "src/migrations/$2/$FILENAME" -d src/util/util/Database.ts -p
-    npm run build clean logerrors pretty-errors
-    THREADS=1 DATABASE="$1" DB_MIGRATE=a npm run start:bundle
+    THREADS=1 DATABASE="$1" DB_MIGRATE=a npx typeorm-ts-node-commonjs migration:generate "src/util/migrations/$2/$FILENAME" -d src/util/util/Database.ts -p
+    #npm run build clean logerrors pretty-errors
+    #THREADS=1 DATABASE="$1" DB_MIGRATE=a npm run start:bundle
 }
 
 npm i sqlite3
diff --git a/src/gateway/opcodes/Identify.ts b/src/gateway/opcodes/Identify.ts
index d5dae7b0..44db598c 100644
--- a/src/gateway/opcodes/Identify.ts
+++ b/src/gateway/opcodes/Identify.ts
@@ -105,7 +105,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
 	if (!user.settings) { //settings may not exist after updating...
 		user.settings = new UserSettings();
 		user.settings.id = user.id;
-		await user.settings.save();
+		//await (user.settings as UserSettings).save();
 	}
 
 	if (!identify.intents) identify.intents = "30064771071";
diff --git a/src/util/entities/User.ts b/src/util/entities/User.ts
index 61343e81..5432f298 100644
--- a/src/util/entities/User.ts
+++ b/src/util/entities/User.ts
@@ -97,7 +97,7 @@ export class User extends BaseClass {
 	@Column()
 	bot: boolean = false; // if user is bot
 
-	@Column()
+	@Column({ nullable: true })
 	bio: string; // short description of the user (max 190 chars -> should be configurable)
 
 	@Column()
@@ -106,7 +106,7 @@ export class User extends BaseClass {
 	@Column({ select: false })
 	nsfw_allowed: boolean = true; // if the user can do age-restricted actions (NSFW channels/guilds/commands) // TODO: depending on age
 
-	@Column({ select: false })
+	@Column({ select: false, nullable: true })
 	mfa_enabled: boolean; // if multi factor authentication is enabled
 
 	@Column({ select: false, nullable: true })
@@ -281,8 +281,8 @@ export class User extends BaseClass {
 			settings: { ...new UserSettings(), locale: language }
 		});
 
+		//await (user.settings as UserSettings).save();
 		await user.save();
-		await user.settings.save();
 
 		setImmediate(async () => {
 			if (Config.get().guild.autoJoin.enabled) {
diff --git a/src/util/migrations/mariadb/1660549252130-fix_nullables.ts b/src/util/migrations/mariadb/1660549252130-fix_nullables.ts
new file mode 100644
index 00000000..c9456b54
--- /dev/null
+++ b/src/util/migrations/mariadb/1660549252130-fix_nullables.ts
@@ -0,0 +1,30 @@
+import { MigrationInterface, QueryRunner } from "typeorm";
+
+export class fixNullables1660549252130 implements MigrationInterface {
+    name = 'fixNullables1660549252130'
+
+    public async up(queryRunner: QueryRunner): Promise<void> {
+        await queryRunner.query(`
+            DROP INDEX \`IDX_76ba283779c8441fd5ff819c8c\` ON \`users\`
+        `);
+        await queryRunner.query(`
+            ALTER TABLE \`users\` CHANGE \`bio\` \`bio\` varchar(255) NULL
+        `);
+        await queryRunner.query(`
+            ALTER TABLE \`users\` CHANGE \`mfa_enabled\` \`mfa_enabled\` tinyint NULL
+        `);
+    }
+
+    public async down(queryRunner: QueryRunner): Promise<void> {
+        await queryRunner.query(`
+            ALTER TABLE \`users\` CHANGE \`mfa_enabled\` \`mfa_enabled\` tinyint NOT NULL
+        `);
+        await queryRunner.query(`
+            ALTER TABLE \`users\` CHANGE \`bio\` \`bio\` varchar(255) NOT NULL
+        `);
+        await queryRunner.query(`
+            CREATE UNIQUE INDEX \`IDX_76ba283779c8441fd5ff819c8c\` ON \`users\` (\`settingsId\`)
+        `);
+    }
+
+}
diff --git a/src/util/migrations/postgres/1660549242936-fix_nullables.ts b/src/util/migrations/postgres/1660549242936-fix_nullables.ts
new file mode 100644
index 00000000..b9a0194d
--- /dev/null
+++ b/src/util/migrations/postgres/1660549242936-fix_nullables.ts
@@ -0,0 +1,30 @@
+import { MigrationInterface, QueryRunner } from "typeorm";
+
+export class fixNullables1660549242936 implements MigrationInterface {
+    name = 'fixNullables1660549242936'
+
+    public async up(queryRunner: QueryRunner): Promise<void> {
+        await queryRunner.query(`
+            ALTER TABLE "users"
+            ALTER COLUMN "bio" DROP NOT NULL
+        `);
+        await queryRunner.query(`
+            ALTER TABLE "users"
+            ALTER COLUMN "mfa_enabled" DROP NOT NULL
+        `);
+    }
+
+    public async down(queryRunner: QueryRunner): Promise<void> {
+        await queryRunner.query(`
+            ALTER TABLE "users"
+            ALTER COLUMN "mfa_enabled"
+            SET NOT NULL
+        `);
+        await queryRunner.query(`
+            ALTER TABLE "users"
+            ALTER COLUMN "bio"
+            SET NOT NULL
+        `);
+    }
+
+}
diff --git a/src/util/migrations/sqlite/1660549233583-fix_nullables.ts b/src/util/migrations/sqlite/1660549233583-fix_nullables.ts
new file mode 100644
index 00000000..68f650c7
--- /dev/null
+++ b/src/util/migrations/sqlite/1660549233583-fix_nullables.ts
@@ -0,0 +1,240 @@
+import { MigrationInterface, QueryRunner } from "typeorm";
+
+export class fixNullables1660549233583 implements MigrationInterface {
+    name = 'fixNullables1660549233583'
+
+    public async up(queryRunner: QueryRunner): Promise<void> {
+        await queryRunner.query(`
+            CREATE TABLE "temporary_users" (
+                "id" varchar PRIMARY KEY NOT NULL,
+                "username" varchar NOT NULL,
+                "discriminator" varchar NOT NULL,
+                "avatar" varchar,
+                "accent_color" integer,
+                "banner" varchar,
+                "phone" varchar,
+                "desktop" boolean NOT NULL,
+                "mobile" boolean NOT NULL,
+                "premium" boolean NOT NULL,
+                "premium_type" integer NOT NULL,
+                "bot" boolean NOT NULL,
+                "bio" varchar,
+                "system" boolean NOT NULL,
+                "nsfw_allowed" boolean NOT NULL,
+                "mfa_enabled" boolean,
+                "totp_secret" varchar,
+                "totp_last_ticket" varchar,
+                "created_at" datetime NOT NULL,
+                "premium_since" datetime,
+                "verified" boolean NOT NULL,
+                "disabled" boolean NOT NULL,
+                "deleted" boolean NOT NULL,
+                "email" varchar,
+                "flags" varchar NOT NULL,
+                "public_flags" integer NOT NULL,
+                "rights" bigint NOT NULL,
+                "data" text NOT NULL,
+                "fingerprints" text NOT NULL,
+                "extended_settings" text NOT NULL,
+                "notes" text NOT NULL,
+                "settingsId" varchar,
+                CONSTRAINT "UQ_b1dd13b6ed980004a795ca184a6" UNIQUE ("settingsId"),
+                CONSTRAINT "FK_76ba283779c8441fd5ff819c8cf" FOREIGN KEY ("settingsId") REFERENCES "user_settings" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION
+            )
+        `);
+        await queryRunner.query(`
+            INSERT INTO "temporary_users"(
+                    "id",
+                    "username",
+                    "discriminator",
+                    "avatar",
+                    "accent_color",
+                    "banner",
+                    "phone",
+                    "desktop",
+                    "mobile",
+                    "premium",
+                    "premium_type",
+                    "bot",
+                    "bio",
+                    "system",
+                    "nsfw_allowed",
+                    "mfa_enabled",
+                    "totp_secret",
+                    "totp_last_ticket",
+                    "created_at",
+                    "premium_since",
+                    "verified",
+                    "disabled",
+                    "deleted",
+                    "email",
+                    "flags",
+                    "public_flags",
+                    "rights",
+                    "data",
+                    "fingerprints",
+                    "extended_settings",
+                    "notes",
+                    "settingsId"
+                )
+            SELECT "id",
+                "username",
+                "discriminator",
+                "avatar",
+                "accent_color",
+                "banner",
+                "phone",
+                "desktop",
+                "mobile",
+                "premium",
+                "premium_type",
+                "bot",
+                "bio",
+                "system",
+                "nsfw_allowed",
+                "mfa_enabled",
+                "totp_secret",
+                "totp_last_ticket",
+                "created_at",
+                "premium_since",
+                "verified",
+                "disabled",
+                "deleted",
+                "email",
+                "flags",
+                "public_flags",
+                "rights",
+                "data",
+                "fingerprints",
+                "extended_settings",
+                "notes",
+                "settingsId"
+            FROM "users"
+        `);
+        await queryRunner.query(`
+            DROP TABLE "users"
+        `);
+        await queryRunner.query(`
+            ALTER TABLE "temporary_users"
+                RENAME TO "users"
+        `);
+    }
+
+    public async down(queryRunner: QueryRunner): Promise<void> {
+        await queryRunner.query(`
+            ALTER TABLE "users"
+                RENAME TO "temporary_users"
+        `);
+        await queryRunner.query(`
+            CREATE TABLE "users" (
+                "id" varchar PRIMARY KEY NOT NULL,
+                "username" varchar NOT NULL,
+                "discriminator" varchar NOT NULL,
+                "avatar" varchar,
+                "accent_color" integer,
+                "banner" varchar,
+                "phone" varchar,
+                "desktop" boolean NOT NULL,
+                "mobile" boolean NOT NULL,
+                "premium" boolean NOT NULL,
+                "premium_type" integer NOT NULL,
+                "bot" boolean NOT NULL,
+                "bio" varchar NOT NULL,
+                "system" boolean NOT NULL,
+                "nsfw_allowed" boolean NOT NULL,
+                "mfa_enabled" boolean NOT NULL,
+                "totp_secret" varchar,
+                "totp_last_ticket" varchar,
+                "created_at" datetime NOT NULL,
+                "premium_since" datetime,
+                "verified" boolean NOT NULL,
+                "disabled" boolean NOT NULL,
+                "deleted" boolean NOT NULL,
+                "email" varchar,
+                "flags" varchar NOT NULL,
+                "public_flags" integer NOT NULL,
+                "rights" bigint NOT NULL,
+                "data" text NOT NULL,
+                "fingerprints" text NOT NULL,
+                "extended_settings" text NOT NULL,
+                "notes" text NOT NULL,
+                "settingsId" varchar,
+                CONSTRAINT "UQ_b1dd13b6ed980004a795ca184a6" UNIQUE ("settingsId"),
+                CONSTRAINT "FK_76ba283779c8441fd5ff819c8cf" FOREIGN KEY ("settingsId") REFERENCES "user_settings" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION
+            )
+        `);
+        await queryRunner.query(`
+            INSERT INTO "users"(
+                    "id",
+                    "username",
+                    "discriminator",
+                    "avatar",
+                    "accent_color",
+                    "banner",
+                    "phone",
+                    "desktop",
+                    "mobile",
+                    "premium",
+                    "premium_type",
+                    "bot",
+                    "bio",
+                    "system",
+                    "nsfw_allowed",
+                    "mfa_enabled",
+                    "totp_secret",
+                    "totp_last_ticket",
+                    "created_at",
+                    "premium_since",
+                    "verified",
+                    "disabled",
+                    "deleted",
+                    "email",
+                    "flags",
+                    "public_flags",
+                    "rights",
+                    "data",
+                    "fingerprints",
+                    "extended_settings",
+                    "notes",
+                    "settingsId"
+                )
+            SELECT "id",
+                "username",
+                "discriminator",
+                "avatar",
+                "accent_color",
+                "banner",
+                "phone",
+                "desktop",
+                "mobile",
+                "premium",
+                "premium_type",
+                "bot",
+                "bio",
+                "system",
+                "nsfw_allowed",
+                "mfa_enabled",
+                "totp_secret",
+                "totp_last_ticket",
+                "created_at",
+                "premium_since",
+                "verified",
+                "disabled",
+                "deleted",
+                "email",
+                "flags",
+                "public_flags",
+                "rights",
+                "data",
+                "fingerprints",
+                "extended_settings",
+                "notes",
+                "settingsId"
+            FROM "temporary_users"
+        `);
+        await queryRunner.query(`
+            DROP TABLE "temporary_users"
+        `);
+    }
+
+}