]> jfr.im git - irc/quakenet/newserv.git/blame - trusts/db-migration.c
Fix up migration to use transactions, also start a commands module.
[irc/quakenet/newserv.git] / trusts / db-migration.c
CommitLineData
2b6e02e2
CP
1#include "../dbapi2/dbapi2.h"
2#include "../core/error.h"
3#include "../nick/nick.h"
4#include "trusts.h"
5
6extern DBAPIConn *trustsdb;
7static trustmigration *migration;
8
9static void tm_group(void *, unsigned int, char *, unsigned int, unsigned int, unsigned int, unsigned int, time_t, time_t, time_t, char *, char *, char *);
10static void tm_host(void *tm, unsigned int id, char *host, unsigned int max, time_t lastseen);
11static void tm_final(void *, int);
12
13trustmigration *migration_start(TrustMigrationGroup, TrustMigrationHost, TrustMigrationFini, void *);
14void migration_stop(trustmigration *);
be2823bc 15void createtrusttables(int migration);
2b6e02e2
CP
16
17struct callbackdata {
18 void *tag;
19 TrustDBMigrationCallback callback;
20};
21
22int trusts_migration_start(TrustDBMigrationCallback callback, void *tag) {
23 struct callbackdata *cbd;
24
25 if(migration)
be2823bc 26 return 1;
2b6e02e2
CP
27
28 if(callback) {
29 cbd = malloc(sizeof(struct callbackdata));
30 if(!cbd)
be2823bc 31 return 2;
2b6e02e2
CP
32
33 cbd->callback = callback;
34 cbd->tag = tag;
35 } else {
36 cbd = NULL;
37 }
38
be2823bc
CP
39 createtrusttables(1);
40 trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "migration_groups");
41 trustsdb->squery(trustsdb, "DELETE FROM ?", "T", "migration_hosts");
2b6e02e2
CP
42
43 migration = migration_start(tm_group, tm_host, tm_final, cbd);
44 if(!migration) {
45 free(cbd);
be2823bc 46 return 3;
2b6e02e2
CP
47 }
48
be2823bc 49 return 0;
2b6e02e2
CP
50}
51
52void trusts_migration_stop(void) {
53 if(!migration)
54 return;
55
56 migration_stop(migration);
57}
58
59static void tm_group(void *tag, unsigned int id, char *name, unsigned int trustedfor, unsigned int mode, unsigned int maxperident, unsigned int maxseen, time_t expires, time_t lastseen, time_t lastmaxuserreset, char *createdby, char *contact, char *comment) {
60 if(id % 25 == 0)
61 Error("trusts", ERR_INFO, "Migration currently at id: %d", id);
62
63 trustsdb->squery(trustsdb,
64 "INSERT INTO ? (id, name, trustedfor, mode, maxperident, maxseen, expires, lastseen, lastmaxuserreset, createdby, contact, comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
be2823bc 65 "Tusuuuutttsss", "migration_groups", id, name, trustedfor, mode, maxperident, maxseen, expires, lastseen, lastmaxuserreset, createdby, contact, comment
2b6e02e2
CP
66 );
67}
68
69static void tm_host(void *tag, unsigned int id, char *host, unsigned int max, time_t lastseen) {
70 trustsdb->squery(trustsdb,
71 "INSERT INTO ? (groupid, host, max, lastseen) VALUES (?, ?, ?, ?)",
be2823bc 72 "Tusut", "migration_hosts", id, host, max, lastseen
2b6e02e2
CP
73 );
74}
75
be2823bc 76static void tm_complete(const DBAPIResult *r, void *tag) {
2b6e02e2 77 struct callbackdata *cbd = tag;
be2823bc 78 int errcode = 0;
2b6e02e2 79
be2823bc
CP
80 if(!r) {
81 errcode = MIGRATION_STOPPED;
2b6e02e2 82 } else {
be2823bc
CP
83 if(!r->success) {
84 Error("trusts", ERR_ERROR, "A error occured executing the rename table query.");
85 errcode = 100;
86 } else {
87 Error("trusts", ERR_INFO, "Migration table copying complete.");
88 }
89 r->clear(r);
2b6e02e2
CP
90 }
91
92 if(cbd) {
be2823bc 93 cbd->callback(errcode, cbd->tag);
2b6e02e2
CP
94 free(cbd);
95 }
96}
be2823bc
CP
97
98static void tm_final(void *tag, int errcode) {
99 struct callbackdata *cbd = tag;
100 migration = NULL;
101
102 if(errcode) {
103 Error("trusts", ERR_ERROR, "Migration error: %d", errcode);
104 if(cbd) {
105 cbd->callback(errcode, cbd->tag);
106 free(cbd);
107 }
108 } else {
109 Error("trusts", ERR_INFO, "Migration completed, copying tables...");
110/*
111 trustsdb->query(trustsdb, cbd?tm_complete:NULL, cbd,
112 "BEGIN TRANSACTION; DROP TABLE ?; ALTER TABLE ? RENAME TO ?; DROP TABLE ?; ALTER TABLE ? RENAME TO ?; COMMIT;",
113 "TTsTTs", "groups", "migration_groups", "groups", "hosts", "migration_hosts", "hosts");
114*/
115/*
116 trustsdb->query(trustsdb, cbd?tm_complete:NULL, cbd,
117 "BEGIN TRANSACTION; DELETE FROM ?; INSERT INTO ? SELECT * FROM ?; DELETE FROM ?; INSERT INTO ? SELECT * FROM ?; COMMIT;",
118 "TTTTTT", "groups", "groups", "migration_groups", "hosts", "hosts", "migration_hosts");
119*/
120/*
121 trustsdb->query(trustsdb, cbd?tm_complete:NULL, cbd,
122 "DELETE FROM ?; INSERT INTO ? SELECT * FROM ?; DELETE FROM ?; INSERT INTO ? SELECT * FROM ?;",
123 "TTTTTT", "groups", "groups", "migration_groups", "hosts", "hosts", "migration_hosts");
124*/
125 trustsdb->squery(trustsdb, "BEGIN TRANSACTION", "");
126 trustsdb->squery(trustsdb, "DROP TABLE ?", "T", "groups");
127 trustsdb->squery(trustsdb, "ALTER TABLE ? RENAME TO ?", "Ts", "migration_groups", "groups");
128 trustsdb->squery(trustsdb, "DROP TABLE ?", "T", "hosts");
129 trustsdb->squery(trustsdb, "ALTER TABLE ? RENAME TO ?", "Ts", "migration_hosts", "hosts");
130 trustsdb->query(trustsdb, tm_complete, cbd, "COMMIT", "");
131 }
132}