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 8499 by niro, Fri Feb 5 14:03:20 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 160  sql_add_column() Line 163  sql_add_column()
163   *auto_increment*) opts="${opts//auto_increment}"; autoincrement="${column} ${opts}" ;;   *auto_increment*) opts="${opts//auto_increment}"; autoincrement="${column} ${opts}" ;;
164   esac   esac
165    
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 189  sql_modify_column() Line 217  sql_modify_column()
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[*]}"
# Line 209  sql_modify_column() Line 240  sql_modify_column()
240   *auto_increment*) opts="${opts//auto_increment}"; autoincrement="${column} ${opts}" ;;   *auto_increment*) opts="${opts//auto_increment}"; autoincrement="${column} ${opts}" ;;
241   esac   esac
242    
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} ]]
# Line 292  update_database() Line 348  update_database()
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    

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