]> jfr.im git - irc/quakenet/newserv.git/blob - trusts/db-migration.c
e8bea9b3720a2345afd03abe5eff3808e9702c13
[irc/quakenet/newserv.git] / trusts / db-migration.c
1 #include "../dbapi2/dbapi2.h"
2 #include "../core/error.h"
3 #include "../core/nsmalloc.h"
4 #include "trusts.h"
5
6 extern DBAPIConn *trustsdb;
7 static trustmigration *migration;
8
9 static void tm_group(void *, trustgroup *);
10 static void tm_host(void *, trusthost *, unsigned int);
11 static void tm_final(void *, int);
12
13 trustmigration *migration_start(TrustMigrationGroup, TrustMigrationHost, TrustMigrationFini, void *);
14 void migration_stop(trustmigration *);
15 void createtrusttables(int);
16
17 struct callbackdata {
18 void *tag;
19 unsigned int hostid;
20 TrustDBMigrationCallback callback;
21 };
22
23 int trusts_migration_start(TrustDBMigrationCallback callback, void *tag) {
24 struct callbackdata *cbd;
25
26 if(migration)
27 return 1;
28
29 cbd = nsmalloc(POOL_TRUSTS, sizeof(struct callbackdata));
30 if(!cbd)
31 return 2;
32
33 cbd->callback = callback;
34 cbd->tag = tag;
35 cbd->hostid = 1;
36
37 createtrusttables(TABLES_MIGRATION);
38 trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "migration_groups");
39 trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "migration_hosts");
40
41 migration = migration_start(tm_group, tm_host, tm_final, cbd);
42 if(!migration) {
43 nsfree(POOL_TRUSTS, cbd);
44 return 3;
45 }
46
47 return 0;
48 }
49
50 void trusts_migration_stop(void) {
51 if(!migration)
52 return;
53
54 migration_stop(migration);
55 }
56
57 static void tm_group(void *tag, trustgroup *tg) {
58 if(tg->id % 25 == 0)
59 Error("trusts_migration", ERR_INFO, "Migration currently at id: %d", tg->id);
60
61 trustsdb_inserttg("migration_groups", tg);
62 }
63
64 static void tm_host(void *tag, trusthost *th, unsigned int groupid) {
65 struct callbackdata *cbd = tag;
66
67 th->id = cbd->hostid++;
68 trustsdb_insertth("migration_hosts", th, groupid);
69 }
70
71 static void tm_complete(const DBAPIResult *r, void *tag) {
72 struct callbackdata *cbd = tag;
73 int errcode = 0;
74
75 if(!r) {
76 errcode = MIGRATION_STOPPED;
77 } else {
78 if(!r->success) {
79 Error("trusts_migration", ERR_ERROR, "A error occured executing the rename table query.");
80 errcode = MIGRATION_LASTERROR;
81 } else {
82 Error("trusts_migration", ERR_INFO, "Migration table copying complete.");
83 }
84 r->clear(r);
85 }
86
87 if(cbd->callback)
88 cbd->callback(errcode, cbd->tag);
89
90 nsfree(POOL_TRUSTS, cbd);
91 }
92
93 static void tm_final(void *tag, int errcode) {
94 struct callbackdata *cbd = tag;
95 migration = NULL;
96
97 if(errcode) {
98 Error("trusts_migration", ERR_ERROR, "Migration error: %d", errcode);
99 if(cbd) {
100 cbd->callback(errcode, cbd->tag);
101 nsfree(POOL_TRUSTS, cbd);
102 }
103 } else {
104 trusts_closedb(0);
105
106 Error("trusts_migration", ERR_INFO, "Migration completed, copying tables...");
107
108 trustsdb->squery(trustsdb, "BEGIN TRANSACTION", "");
109 trustsdb->squery(trustsdb, "DROP TABLE ?", "T", "groups");
110 trustsdb->squery(trustsdb, "ALTER TABLE ? RENAME TO ?", "Ts", "migration_groups", "groups");
111 trustsdb->squery(trustsdb, "DROP TABLE ?", "T", "hosts");
112 trustsdb->squery(trustsdb, "ALTER TABLE ? RENAME TO ?", "Ts", "migration_hosts", "hosts");
113 trustsdb->query(trustsdb, tm_complete, cbd, "COMMIT", "");
114 }
115 }