]> jfr.im git - irc/quakenet/newserv.git/blobdiff - trusts/db-migration.c
Fix up migration to use transactions, also start a commands module.
[irc/quakenet/newserv.git] / trusts / db-migration.c
index 9ecdf473a1f83a6e210cf740c30cbbb20db32d32..694ce8259d3b09ee4231e5ddd9b0c0bb0a6805fa 100644 (file)
@@ -12,6 +12,7 @@ static void tm_final(void *, int);
 
 trustmigration *migration_start(TrustMigrationGroup, TrustMigrationHost, TrustMigrationFini, void *);
 void migration_stop(trustmigration *);
+void createtrusttables(int migration);
 
 struct callbackdata {
   void *tag;
@@ -22,12 +23,12 @@ int trusts_migration_start(TrustDBMigrationCallback callback, void *tag) {
   struct callbackdata *cbd;
 
   if(migration)
-    return 0;
+    return 1;
 
   if(callback) {
     cbd = malloc(sizeof(struct callbackdata));
     if(!cbd)
-      return 0;
+      return 2;
 
     cbd->callback = callback;
     cbd->tag = tag;
@@ -35,16 +36,17 @@ int trusts_migration_start(TrustDBMigrationCallback callback, void *tag) {
     cbd = NULL;
   }
 
-  trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "groups");
-  trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "hosts");
+  createtrusttables(1);
+  trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "migration_groups");
+  trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "migration_hosts");
 
   migration = migration_start(tm_group, tm_host, tm_final, cbd);
   if(!migration) {
     free(cbd);
-    return 0;
+    return 3;
   }
 
-  return 1;
+  return 0;
 }
 
 void trusts_migration_stop(void) {
@@ -60,29 +62,71 @@ static void tm_group(void *tag, unsigned int id, char *name, unsigned int truste
 
   trustsdb->squery(trustsdb, 
     "INSERT INTO ? (id, name, trustedfor, mode, maxperident, maxseen, expires, lastseen, lastmaxuserreset, createdby, contact, comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
-    "Tusuuuutttsss", "groups", id, name, trustedfor, mode, maxperident, maxseen, expires, lastseen, lastmaxuserreset, createdby, contact, comment
+    "Tusuuuutttsss", "migration_groups", id, name, trustedfor, mode, maxperident, maxseen, expires, lastseen, lastmaxuserreset, createdby, contact, comment
   );
 }
 
 static void tm_host(void *tag, unsigned int id, char *host, unsigned int max, time_t lastseen) {
   trustsdb->squery(trustsdb, 
     "INSERT INTO ? (groupid, host, max, lastseen) VALUES (?, ?, ?, ?)",
-    "Tusut", "hosts", id, host, max, lastseen
+    "Tusut", "migration_hosts", id, host, max, lastseen
   );
 }
 
-static void tm_final(void *tag, int errcode) {
+static void tm_complete(const DBAPIResult *r, void *tag) {
   struct callbackdata *cbd = tag;
-  migration = NULL;
+  int errcode = 0;
 
-  if(errcode) {
-    Error("trusts", ERR_ERROR, "Migration error: %d", errcode);
+  if(!r) {
+    errcode = MIGRATION_STOPPED;
   } else {
-    Error("trusts", ERR_INFO, "Migration completed.");
+    if(!r->success) {
+      Error("trusts", ERR_ERROR, "A error occured executing the rename table query.");
+      errcode = 100;
+    } else {
+      Error("trusts", ERR_INFO, "Migration table copying complete.");
+    }
+    r->clear(r);
   }
 
   if(cbd) {
-    cbd->callback(errcode, tag);
+    cbd->callback(errcode, cbd->tag);
     free(cbd);
   }
 }
+
+static void tm_final(void *tag, int errcode) {
+  struct callbackdata *cbd = tag;
+  migration = NULL;
+
+  if(errcode) {
+    Error("trusts", ERR_ERROR, "Migration error: %d", errcode);
+    if(cbd) {
+      cbd->callback(errcode, cbd->tag);
+      free(cbd);
+    }
+  } else {
+    Error("trusts", ERR_INFO, "Migration completed, copying tables...");
+/*
+    trustsdb->query(trustsdb, cbd?tm_complete:NULL, cbd,
+                    "BEGIN TRANSACTION; DROP TABLE ?; ALTER TABLE ? RENAME TO ?; DROP TABLE ?; ALTER TABLE ? RENAME TO ?; COMMIT;",
+                    "TTsTTs", "groups", "migration_groups", "groups", "hosts", "migration_hosts", "hosts");
+*/
+/*
+    trustsdb->query(trustsdb, cbd?tm_complete:NULL, cbd,
+                    "BEGIN TRANSACTION; DELETE FROM ?; INSERT INTO ? SELECT * FROM ?; DELETE FROM ?; INSERT INTO ? SELECT * FROM ?; COMMIT;",
+                    "TTTTTT", "groups", "groups", "migration_groups", "hosts", "hosts", "migration_hosts");
+*/
+/*
+    trustsdb->query(trustsdb, cbd?tm_complete:NULL, cbd,
+                    "DELETE FROM ?; INSERT INTO ? SELECT * FROM ?; DELETE FROM ?; INSERT INTO ? SELECT * FROM ?;",
+                    "TTTTTT", "groups", "groups", "migration_groups", "hosts", "hosts", "migration_hosts");
+*/
+    trustsdb->squery(trustsdb, "BEGIN TRANSACTION", "");
+    trustsdb->squery(trustsdb, "DROP TABLE ?", "T", "groups");
+    trustsdb->squery(trustsdb, "ALTER TABLE ? RENAME TO ?", "Ts", "migration_groups", "groups");
+    trustsdb->squery(trustsdb, "DROP TABLE ?", "T", "hosts");
+    trustsdb->squery(trustsdb, "ALTER TABLE ? RENAME TO ?", "Ts", "migration_hosts", "hosts");
+    trustsdb->query(trustsdb, tm_complete, cbd, "COMMIT", "");
+  }
+}