Browse code

- check for database table version

Jan Janak authored on 29/11/2003 00:57:22
Showing 4 changed files
... ...
@@ -47,10 +47,10 @@ int is_domain_local(str* _host)
47 47
 		db_key_t cols[1]; 
48 48
 		db_res_t* res;
49 49
 
50
-		keys[0]=domain_col;
51
-		cols[0]=domain_col;
50
+		keys[0]=domain_col.s;
51
+		cols[0]=domain_col.s;
52 52
 		
53
-		if (db_use_table(db_handle, domain_table) < 0) {
53
+		if (db_use_table(db_handle, domain_table.s) < 0) {
54 54
 			LOG(L_ERR, "is_local(): Error while trying to use domain table\n");
55 55
 			return -1;
56 56
 		}
... ...
@@ -51,13 +51,26 @@ static int child_init(int rank);
51 51
 
52 52
 MODULE_VERSION
53 53
 
54
+/*
55
+ * Version of domain table required by the module,
56
+ * increment this value if you change the table in
57
+ * an backwards incompatible way
58
+ */
59
+#define TABLE_VERSION 1
60
+
61
+#define DOMAIN_TABLE "domain"
62
+#define DOMAIN_TABLE_LEN (sizeof(DOMAIN_TABLE) - 1)
63
+
64
+#define DOMAIN_COL "domain"
65
+#define DOMAIN_COL_LEN (sizeof(DOMAIN_COL) - 1)
66
+
54 67
 /*
55 68
  * Module parameter variables
56 69
  */
57
-char* db_url = DEFAULT_RODB_URL;
70
+str db_url = {DEFAULT_RODB_URL, DEFAULT_RODB_URL_LEN};
58 71
 int db_mode = 0;			/* Database usage mode: 0 = no cache, 1 = cache */
59
-char* domain_table = "domain";          /* Name of domain table */
60
-char* domain_col = "domain";            /* Name of domain column */
72
+str domain_table = {DOMAIN_TABLE, DOMAIN_TABLE_LEN};     /* Name of domain table */
73
+str domain_col = {DOMAIN_COL, DOMAIN_COL_LEN};           /* Name of domain column */
61 74
 
62 75
 /*
63 76
  * Other module variables
... ...
@@ -82,10 +95,10 @@ static cmd_export_t cmds[] = {
82 95
  * Exported parameters
83 96
  */
84 97
 static param_export_t params[] = {
85
-	{"db_url",		STR_PARAM, &db_url	},
86
-	{"db_mode",             INT_PARAM, &db_mode     },
87
-	{"domain_table",        STR_PARAM, &domain_table},
88
-	{"domain_col",          STR_PARAM, &domain_col  },
98
+	{"db_url",		STR_PARAM, &db_url.s	  },
99
+	{"db_mode",             INT_PARAM, &db_mode       },
100
+	{"domain_table",        STR_PARAM, &domain_table.s},
101
+	{"domain_col",          STR_PARAM, &domain_col.s  },
89 102
 	{0, 0, 0}
90 103
 };
91 104
 
... ...
@@ -107,24 +120,40 @@ struct module_exports exports = {
107 120
 
108 121
 static int mod_init(void)
109 122
 {
110
-	int i;
123
+	int i, ver;
111 124
 
112 125
 	DBG("domain - initializing\n");
113 126
 	
127
+	db_url.len = strlen(db_url.s);
128
+	domain_table.len = strlen(domain_table.s);
129
+	domain_col.len = strlen(domain_col.s);
130
+
114 131
 	/* Check if database module has been loaded */
115
-	if (bind_dbmod(db_url)) {
132
+	if (bind_dbmod(db_url.s)) {
116 133
 		LOG(L_ERR, "domain:mod_init(): Unable to bind database module\n");
117 134
 		return -1;
118 135
 	}
119 136
 
120 137
 	/* Check if cache needs to be loaded from domain table */
121 138
 	if (db_mode == 1) {
122
-		db_handle = db_init(db_url);
139
+		db_handle = db_init(db_url.s);
123 140
 		if (!db_handle) {
124 141
 			LOG(L_ERR, "domain:mod_init(): Unable to connect database\n");
125 142
 			return -1;
126 143
 		}
127
-		
144
+
145
+		     /* Check table version */
146
+		ver = table_version(db_handle, &domain_table);
147
+		if (ver < 0) {
148
+			LOG(L_ERR, "domain:mod_init(): Error while querying table version\n");
149
+			db_close(db_handle);
150
+			return -1;
151
+		} else if (ver < TABLE_VERSION) {
152
+			LOG(L_ERR, "domain:mod_init(): Invalid table version (use ser_mysql.sh reinstall)\n");
153
+			db_close(db_handle);
154
+			return -1;
155
+		}		
156
+
128 157
 		/* Initialize fifo interface */
129 158
 		(void)init_domain_fifo();
130 159
 
... ...
@@ -161,7 +190,7 @@ static int child_init(int rank)
161 190
 {
162 191
 	/* Check if database is needed by child */
163 192
 	if (((db_mode == 0) && (rank > 0)) || ((db_mode == 1) && (rank == PROC_FIFO))) {
164
-		db_handle = db_init(db_url);
193
+		db_handle = db_init(db_url.s);
165 194
 		if (!db_handle) {
166 195
 			LOG(L_ERR, "domain:child_init(): Unable to connect database\n");
167 196
 			return -1;
... ...
@@ -33,6 +33,7 @@
33 33
 
34 34
 
35 35
 #include "../../db/db.h"
36
+#include "../../str.h"
36 37
 
37 38
 
38 39
 /*
... ...
@@ -51,10 +52,10 @@ struct domain_list {
51 52
 /*
52 53
  * Module parameters variables
53 54
  */
54
-extern char* db_url;            /* Database URL */
55
+extern str db_url;              /* Database URL */
55 56
 extern int db_mode;             /* Database usage mode: 0 = no cache, 1 = cache */
56
-extern char* domain_table;	/* Domain table name */
57
-extern char* domain_col;	/* Domain column name */
57
+extern str domain_table;	/* Domain table name */
58
+extern str domain_col;   	/* Domain column name */
58 59
 
59 60
 
60 61
 /*
... ...
@@ -52,9 +52,9 @@ int reload_domain_table ( void )
52 52
 	struct domain_list **new_hash_table;
53 53
 	int i;
54 54
 
55
-	cols[0] = domain_col;
55
+	cols[0] = domain_col.s;
56 56
 
57
-	if (db_use_table(db_handle, domain_table) < 0) {
57
+	if (db_use_table(db_handle, domain_table.s) < 0) {
58 58
 		LOG(L_ERR, "reload_domain_table(): Error while trying to use domain table\n");
59 59
 		return -1;
60 60
 	}