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 8497 by niro, Fri Feb 5 13:48:23 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 279  create_database() Line 335  create_database()
335  {  {
336   create_database_schema   create_database_schema
337    
338   # update database version   # update database schema version
339   mysqldo "INSERT INTO ${MCORE_SQL_DB}.meta(version) VALUES ('${MCORE_SQL_DB_SCHEMA_VERSION}') ON DUPLICATE KEY UPDATE version='${MCORE_SQL_DB_SCHEMA_VERSION}';" || die "updating version"   mysqldo "INSERT INTO ${MCORE_SQL_DB}.meta_schema(version) VALUES ('${MCORE_SQL_DB_SCHEMA_VERSION}') ON DUPLICATE KEY UPDATE version='${MCORE_SQL_DB_SCHEMA_VERSION}';" || die "updating schema version"
340  }  }
341    
342  update_database()  update_database()
# Line 288  update_database() Line 344  update_database()
344   local current_sql_db_schema_version   local current_sql_db_schema_version
345   local i   local i
346    
347   current_sql_db_schema_version=$(mysqldo "SELECT MAX(schema_version) from meta;")   current_sql_db_schema_version=$(mysqldo "SELECT MAX(version) from meta_schema;")
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    
# Line 306  update_database() Line 362  update_database()
362   fi   fi
363   done   done
364    
365   # update database version   # update database schema version
366   mysqldo "INSERT INTO ${MCORE_SQL_DB}.meta(version) VALUES ('${MCORE_SQL_DB_SCHEMA_VERSION}') ON DUPLICATE KEY UPDATE version='${MCORE_SQL_DB_SCHEMA_VERSION}';" || die "updating version"   mysqldo "INSERT INTO ${MCORE_SQL_DB}.meta_schema(version) VALUES ('${MCORE_SQL_DB_SCHEMA_VERSION}') ON DUPLICATE KEY UPDATE version='${MCORE_SQL_DB_SCHEMA_VERSION}';" || die "updating schema version"
367  }  }
368    
369  fill_database()  fill_database()
370  {  {
371     local current_sql_db_values_version
372     local i
373    
374     current_sql_db_values_version=$(mysqldo "SELECT MAX(version) from meta_values;")
375    
376     if [[ ${current_sql_db_values_version} = ${MCORE_SQL_DB_VALUES_VERSION} ]]
377     then
378     echo "Database values are already up to date"
379     return 0
380     fi
381    
382   create_database_values   create_database_values
383     # update database values version
384     mysqldo "INSERT INTO ${MCORE_SQL_DB}.meta_values(version) VALUES ('${MCORE_SQL_DB_VALUES_VERSION}') ON DUPLICATE KEY UPDATE version='${MCORE_SQL_DB_VALUES_VERSION}';" || die "updating values version"
385  }  }
386    
387  case $1 in  case $1 in

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