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