Magellan Linux

Diff of /alx-src/branches/alx-web-070/scripts/sql-schema/mcore-sql-schema.sh

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 8503 by niro, Fri Feb 5 14:34:11 2016 UTC revision 8504 by niro, Fri Feb 5 15:14:21 2016 UTC
# Line 139  sql_add_column() Line 139  sql_add_column()
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[*]}"
# Line 163  sql_add_column() Line 166  sql_add_column()
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} ]]
# Line 190  sql_modify_column() Line 218  sql_modify_column()
218   local autoincrement   local autoincrement
219   local primary   local primary
220   local current_primary   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[*]}"
# Line 213  sql_modify_column() Line 243  sql_modify_column()
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   # 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"') )   i=( $(mysqldo 'SHOW COLUMNS FROM '${MCORE_SQL_DB}.${table}' WHERE `Key`="PRI"') )
268   current_primary="${i[0]}"   current_primary="${i[0]}"
269   if [[ ${current_primary} != ${primary} ]]   if [[ -n ${current_primary} ]]
270   then   then
271   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}'"
272   fi   fi
273     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} ]]
276   then   then

Legend:
Removed from v.8503  
changed lines
  Added in v.8504