Explorar el Código

add cooldown changes to model

Pouria Ezzati hace 1 año
padre
commit
a0155a9708
Se han modificado 2 ficheros con 24 adiciones y 15 borrados
  1. 22 12
      server/migrations/20240911230301_change_cooldown.js
  2. 2 3
      server/models/user.model.js

+ 22 - 12
server/migrations/20240911230301_change_cooldown.js

@@ -1,19 +1,29 @@
 async function up(knex) {
-  await knex.schema.alterTable("users", table => {
-    table.dropColumn("cooldowns");
-    table.datetime("cooldown").nullable();
-    table.integer("malicious_attempts").defaultTo(0);
-  });
-}
 
-async function down(knex) {
-  await knex.schema.alterTable("users", table => {
-    table.dropColumn("cooldown");
-    table.json("cooldowns").defaultTo("[]");
-    table.dropColumn("malicious_attempts");
-  });
+  const hasCooldowns = await knex.schema.hasColumn("users", "cooldowns");
+  if (hasCooldowns) {
+    await knex.schema.alterTable("users", table => {
+      table.dropColumn("cooldowns");
+    });
+  }
+
+  const hasCooldown = await knex.schema.hasColumn("users", "cooldown");
+  if (!hasCooldown) {
+    await knex.schema.alterTable("users", table => {
+      table.datetime("cooldown").nullable();
+    });
+  }
+
+  const hasMaliciousAttempts = await knex.schema.hasColumn("users", "malicious_attempts");
+  if (!hasMaliciousAttempts) {
+    await knex.schema.alterTable("users", table => {
+      table.integer("malicious_attempts").notNullable().defaultTo(0);
+    });
+  }
 }
 
+async function down(knex) {}
+
 module.exports = {
   up,
   down

+ 2 - 3
server/models/user.model.js

@@ -12,14 +12,13 @@ async function createUserTable(knex) {
         .integer("banned_by_id")
         .references("id")
         .inTable("users");
-      table
-        .json("cooldowns")
-        .defaultTo("[]");
       table
         .string("email")
         .unique()
         .notNullable();
       table.string("password").notNullable();
+      table.datetime("cooldown").nullable();
+      table.integer("malicious_attempts").notNullable().defaultTo(0);
       table.dateTime("reset_password_expires");
       table.string("reset_password_token");
       table.dateTime("change_email_expires");