1 #include "../dbapi2/dbapi2.h"
2 #include "../core/error.h"
3 #include "../core/nsmalloc.h"
6 extern DBAPIConn
*trustsdb
;
7 static trustmigration
*migration
;
9 static void tm_group(void *, trustgroup
*);
10 static void tm_host(void *, trusthost
*, unsigned int);
11 static void tm_final(void *, int);
13 trustmigration
*migration_start(TrustMigrationGroup
, TrustMigrationHost
, TrustMigrationFini
, void *);
14 void migration_stop(trustmigration
*);
15 void createtrusttables(int);
20 TrustDBMigrationCallback callback
;
23 int trusts_migration_start(TrustDBMigrationCallback callback
, void *tag
) {
24 struct callbackdata
*cbd
;
29 cbd
= nsmalloc(POOL_TRUSTS
, sizeof(struct callbackdata
));
33 cbd
->callback
= callback
;
37 createtrusttables(TABLES_MIGRATION
);
38 trustsdb
->squery(trustsdb
, "DELETE FROM ?", "T", "migration_groups");
39 trustsdb
->squery(trustsdb
, "DELETE FROM ?", "T", "migration_hosts");
41 migration
= migration_start(tm_group
, tm_host
, tm_final
, cbd
);
43 nsfree(POOL_TRUSTS
, cbd
);
50 void trusts_migration_stop(void) {
54 migration_stop(migration
);
57 static void tm_group(void *tag
, trustgroup
*tg
) {
59 Error("trusts_migration", ERR_INFO
, "Migration currently at id: %d", tg
->id
);
61 trustsdb_inserttg("migration_groups", tg
);
64 static void tm_host(void *tag
, trusthost
*th
, unsigned int groupid
) {
65 struct callbackdata
*cbd
= tag
;
67 th
->id
= cbd
->hostid
++;
68 trustsdb_insertth("migration_hosts", th
, groupid
);
71 static void tm_complete(const DBAPIResult
*r
, void *tag
) {
72 struct callbackdata
*cbd
= tag
;
76 errcode
= MIGRATION_STOPPED
;
79 Error("trusts_migration", ERR_ERROR
, "A error occured executing the rename table query.");
80 errcode
= MIGRATION_LASTERROR
;
82 Error("trusts_migration", ERR_INFO
, "Migration table copying complete.");
88 cbd
->callback(errcode
, cbd
->tag
);
90 nsfree(POOL_TRUSTS
, cbd
);
93 static void tm_final(void *tag
, int errcode
) {
94 struct callbackdata
*cbd
= tag
;
98 Error("trusts_migration", ERR_ERROR
, "Migration error: %d", errcode
);
100 cbd
->callback(errcode
, cbd
->tag
);
101 nsfree(POOL_TRUSTS
, cbd
);
106 Error("trusts_migration", ERR_INFO
, "Migration completed, copying tables...");
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", "");