139 |
local column |
local column |
140 |
local autoincrement |
local autoincrement |
141 |
local primary |
local primary |
142 |
|
local current_primary |
143 |
|
local current_autoincrement |
144 |
|
local current_opts |
145 |
|
|
146 |
table="${TABLE_DEFINITION[0]}" |
table="${TABLE_DEFINITION[0]}" |
147 |
count="${#TABLE_DEFINITION[*]}" |
count="${#TABLE_DEFINITION[*]}" |
166 |
mysqldo "ALTER TABLE ${MCORE_SQL_DB}.${table} ADD ${column} ${opts} AFTER ${after_column};" || die "add column '${table}'" |
mysqldo "ALTER TABLE ${MCORE_SQL_DB}.${table} ADD ${column} ${opts} AFTER ${after_column};" || die "add column '${table}'" |
167 |
done |
done |
168 |
|
|
169 |
|
# delete an existing auto_increment first |
170 |
|
if [[ -n ${autoincrement} ]] |
171 |
|
then |
172 |
|
i=( $(mysqldo 'SHOW COLUMNS FROM '${MCORE_SQL_DB}.${table}' WHERE `Extra`="auto_increment"') ) |
173 |
|
echo "DEBUG: i='${i[*]}'" |
174 |
|
current_autoincrement="${i[0]}" |
175 |
|
if [[ ${current_autoincrement} != ${column} ]] && [[ -n ${current_autoincrement} ]] |
176 |
|
then |
177 |
|
current_opts=( "${i[1]}" ) # type eg int(11) |
178 |
|
# null or not |
179 |
|
case "${i[2]}" in |
180 |
|
NO|no) current_opts+=( "NOT NULL" ) ;; |
181 |
|
YES|yes) current_opts+=( "DEFAULT ${i[4]}" ) ;; # should never match, but to be save |
182 |
|
esac |
183 |
|
mysqldo "ALTER TABLE ${MCORE_SQL_DB}.${table} CHANGE ${current_autoincrement} ${current_autoincrement} ${current_opts};" || die "removing auto_increment to table '${table}'" |
184 |
|
fi |
185 |
|
fi |
186 |
|
|
187 |
if [[ -n ${primary} ]] |
if [[ -n ${primary} ]] |
188 |
then |
then |
189 |
|
# only run this if the primary key is not already the same column |
190 |
|
i=( $(mysqldo 'SHOW COLUMNS FROM '${MCORE_SQL_DB}.${table}' WHERE `Key`="PRI"') ) |
191 |
|
current_primary="${i[0]}" |
192 |
|
if [[ -n ${current_primary} ]] |
193 |
|
then |
194 |
|
mysqldo "ALTER TABLE ${MCORE_SQL_DB}.${table} DROP PRIMARY KEY;" || die "adding primary key to table '${table}'" |
195 |
|
fi |
196 |
mysqldo "ALTER TABLE ${MCORE_SQL_DB}.${table} ADD PRIMARY KEY (${primary});" || die "adding primary key to table '${table}'" |
mysqldo "ALTER TABLE ${MCORE_SQL_DB}.${table} ADD PRIMARY KEY (${primary});" || die "adding primary key to table '${table}'" |
197 |
fi |
fi |
198 |
if [[ -n ${autoincrement} ]] |
if [[ -n ${autoincrement} ]] |
217 |
local column |
local column |
218 |
local autoincrement |
local autoincrement |
219 |
local primary |
local primary |
220 |
|
local current_primary |
221 |
|
local current_autoincrement |
222 |
|
local current_opts |
223 |
|
|
224 |
table="${TABLE_DEFINITION[0]}" |
table="${TABLE_DEFINITION[0]}" |
225 |
count="${#TABLE_DEFINITION[*]}" |
count="${#TABLE_DEFINITION[*]}" |
243 |
mysqldo "ALTER TABLE ${MCORE_SQL_DB}.${table} MODIFY ${column} ${opts};" || die "modify table '${table}'" |
mysqldo "ALTER TABLE ${MCORE_SQL_DB}.${table} MODIFY ${column} ${opts};" || die "modify table '${table}'" |
244 |
done |
done |
245 |
|
|
246 |
|
# delete an existing auto_increment first |
247 |
|
if [[ -n ${autoincrement} ]] |
248 |
|
then |
249 |
|
i=( $(mysqldo 'SHOW COLUMNS FROM '${MCORE_SQL_DB}.${table}' WHERE `Extra`="auto_increment"') ) |
250 |
|
echo "DEBUG: i='${i[*]}'" |
251 |
|
current_autoincrement="${i[0]}" |
252 |
|
if [[ ${current_autoincrement} != ${column} ]] && [[ -n ${current_autoincrement} ]] |
253 |
|
then |
254 |
|
current_opts=( "${i[1]}" ) # type eg int(11) |
255 |
|
# null or not |
256 |
|
case "${i[2]}" in |
257 |
|
NO|no) current_opts+=( "NOT NULL" ) ;; |
258 |
|
YES|yes) current_opts+=( "DEFAULT ${i[4]}" ) ;; # should never match, but to be save |
259 |
|
esac |
260 |
|
mysqldo "ALTER TABLE ${MCORE_SQL_DB}.${table} CHANGE ${current_autoincrement} ${current_autoincrement} ${current_opts};" || die "removing auto_increment to table '${table}'" |
261 |
|
fi |
262 |
|
fi |
263 |
|
|
264 |
if [[ -n ${primary} ]] |
if [[ -n ${primary} ]] |
265 |
then |
then |
266 |
|
# only run this if the primary key is not already the same column |
267 |
|
i=( $(mysqldo 'SHOW COLUMNS FROM '${MCORE_SQL_DB}.${table}' WHERE `Key`="PRI"') ) |
268 |
|
current_primary="${i[0]}" |
269 |
|
if [[ -n ${current_primary} ]] |
270 |
|
then |
271 |
|
mysqldo "ALTER TABLE ${MCORE_SQL_DB}.${table} DROP PRIMARY KEY;" || die "adding primary key to table '${table}'" |
272 |
|
fi |
273 |
mysqldo "ALTER TABLE ${MCORE_SQL_DB}.${table} ADD PRIMARY KEY (${primary});" || die "adding primary key to table '${table}'" |
mysqldo "ALTER TABLE ${MCORE_SQL_DB}.${table} ADD PRIMARY KEY (${primary});" || die "adding primary key to table '${table}'" |
274 |
fi |
fi |
275 |
if [[ -n ${autoincrement} ]] |
if [[ -n ${autoincrement} ]] |
348 |
|
|
349 |
if [[ ${current_sql_db_schema_version} = ${MCORE_SQL_DB_SCHEMA_VERSION} ]] |
if [[ ${current_sql_db_schema_version} = ${MCORE_SQL_DB_SCHEMA_VERSION} ]] |
350 |
then |
then |
351 |
echo "Database is already up to date" |
echo "Database schema is already up to date" |
352 |
return 0 |
return 0 |
353 |
fi |
fi |
354 |
|
|