Browse code

kamctl: cleaned db creation

Daniel-Constantin Mierla authored on 22/01/2013 22:09:47
Showing 1 changed files
... ...
@@ -84,18 +84,18 @@ sql_query()
84 84
 
85 85
 kamailio_drop()  # pars: <database name>
86 86
 {
87
-if [ $# -ne 1 ] ; then
88
-	merr "kamailio_drop function takes two params"
89
-	exit 1
90
-fi
87
+	if [ $# -ne 1 ] ; then
88
+		merr "kamailio_drop function takes two params"
89
+		exit 1
90
+	fi
91 91
 
92
-sql_query "" "drop database $1;"
92
+	sql_query "" "DROP DATABASE $1;"
93 93
 
94
-if [ $? -ne 0 ] ; then
95
-	merr "Dropping database $1 failed!"
96
-	exit 1
97
-fi
98
-minfo "Database $1 deleted"
94
+	if [ $? -ne 0 ] ; then
95
+		merr "Dropping database $1 failed!"
96
+		exit 1
97
+	fi
98
+	minfo "Database $1 deleted"
99 99
 }
100 100
 
101 101
 
... ...
@@ -123,33 +123,66 @@ db_charset_test()
123 123
 	CHARSET=$CURRCHARSET
124 124
 }
125 125
 
126
-
127
-kamailio_create () # pars: <database name>
126
+kamailio_db_create () # pars: <database name>
128 127
 {
129
-if [ $# -ne 1 ] ; then
130
-	merr "kamailio_create function takes one param"
131
-	exit 1
132
-fi
128
+	if [ $# -ne 1 ] ; then
129
+		merr "kamailio_db_create function takes one param"
130
+		exit 1
131
+	fi
133 132
 
134
-minfo "test server charset"
133
+	minfo "test server charset"
135 134
 
136
-db_charset_test
135
+	db_charset_test
137 136
 
138
-minfo "creating database $1 ..."
137
+	minfo "creating database $1 ..."
139 138
 
140
-# Users: kamailio is the regular user, kamailioro only for reading
141
-sql_query "" "create database $1 character set $CHARSET;
142
-	GRANT ALL PRIVILEGES ON $1.* TO '$DBRWUSER' IDENTIFIED  BY '$DBRWPW';
143
-	GRANT ALL PRIVILEGES ON $1.* TO '${DBRWUSER}'@'$DBHOST' IDENTIFIED BY '$DBRWPW';
144
-	GRANT SELECT ON $1.* TO '$DBROUSER' IDENTIFIED BY '$DBROPW';
145
-	GRANT SELECT ON $1.* TO '${DBROUSER}'@'$DBHOST' IDENTIFIED BY '$DBROPW';"
139
+	sql_query "" "CREATE DATACASE $1 CHARACTER SET $CHARSET;"
146 140
 
141
+	if [ $? -ne 0 ] ; then
142
+		merr "Creating database $1 failed!"
143
+		exit 1
144
+	fi
145
+}
147 146
 
148
-if [ $? -ne 0 ] ; then
149
-	merr "Creating core database and grant privileges failed!"
147
+kamailio_db_grant () # pars: <database name>
148
+{
149
+	if [ $# -ne 1 ] ; then
150
+		merr "kamailio_db_grant function takes one param"
151
+		exit 1
152
+	fi
153
+
154
+	minfo "granting privileges to database $1 ..."
155
+
156
+	# Users: kamailio is the regular user, kamailioro only for reading
157
+	sql_query "" "GRANT ALL PRIVILEGES ON $1.* TO '${DBRWUSER}'@'$DBHOST' IDENTIFIED BY '$DBRWPW';
158
+		GRANT SELECT ON $1.* TO '${DBROUSER}'@'$DBHOST' IDENTIFIED BY '$DBROPW';"
159
+
160
+	if [ $? -ne 0 ] ; then
161
+		merr "granting privileges to database $1 failed!"
162
+		exit 1
163
+	fi
164
+
165
+	if [ "$DBHOST" != "localhost" ] ; then
166
+		sql_query "" "GRANT ALL PRIVILEGES ON $1.* TO '$DBRWUSER'@'localhost' IDENTIFIED  BY '$DBRWPW';
167
+			GRANT SELECT ON $1.* TO '$DBROUSER'@'localhost' IDENTIFIED BY '$DBROPW';"
168
+		if [ $? -ne 0 ] ; then
169
+			merr "granting localhost privileges to database $1 failed!"
170
+			exit 1
171
+		fi
172
+	fi
173
+}
174
+
175
+kamailio_create () # pars: <database name>
176
+{
177
+if [ $# -ne 1 ] ; then
178
+	merr "kamailio_create function takes one param"
150 179
 	exit 1
151 180
 fi
152 181
 
182
+kamailio_db_create $1
183
+
184
+kamailio_db_grant $1
185
+
153 186
 for TABLE in $STANDARD_MODULES; do
154 187
 	mdbg "Creating core table: $TABLE"
155 188
 	sql_query $1 < $DB_SCHEMA/$TABLE-create.sql
... ...
@@ -224,7 +257,7 @@ for TABLE in $EXTRA_MODULES; do
224 257
 	mdbg "Creating extra table: $TABLE"
225 258
 	sql_query $1 < $DB_SCHEMA/$TABLE-create.sql
226 259
 	if [ $? -ne 0 ] ; then
227
-		merr "Creating extra tables failed!"
260
+		merr "Creating extra tables failed at $TABLE!"
228 261
 		exit 1
229 262
 		fi
230 263
 	done