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