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 8501 by niro, Fri Feb 5 14:31:53 2016 UTC revision 8509 by niro, Mon Feb 8 08:52:47 2016 UTC
# Line 125  sql_create_table() Line 125  sql_create_table()
125    
126  # TABLE_DEFINITION=(  # TABLE_DEFINITION=(
127  # "table_name"  # "table_name"
128  # "column1_name after_column column1_argvs"  # "column1_name column1_argvs"
129  # "column2_name after_column column2_argvs"  # "column2_name column2_argvs"
130  # "columnN_name after_column columnN_argvs"  # "columnN_name columnN_argvs"
131    #
132    # eg.    "description varchar(255) DEFAULT NULL AFTER value"
133  # )  # )
134    #
135  sql_add_column()  sql_add_column()
136  {  {
137   local table   local table
# Line 139  sql_add_column() Line 142  sql_add_column()
142   local column   local column
143   local autoincrement   local autoincrement
144   local primary   local primary
145     local current_primary
146     local current_autoincrement
147     local current_opts
148    
149   table="${TABLE_DEFINITION[0]}"   table="${TABLE_DEFINITION[0]}"
150   count="${#TABLE_DEFINITION[*]}"   count="${#TABLE_DEFINITION[*]}"
# Line 147  sql_add_column() Line 153  sql_add_column()
153   do   do
154   line=( ${TABLE_DEFINITION[${i}]} )   line=( ${TABLE_DEFINITION[${i}]} )
155   column="${line[0]}"   column="${line[0]}"
156   after="${line[1]}"   opts="${line[*]:1}"
  opts="${line[*]:2}"  
157    
158   case ${opts} in   case ${opts} in
159   *"PRIMARY KEY"*) opts="${opts//PRIMARY KEY}"; primary="${column}" ;;   *"PRIMARY KEY"*) opts="${opts//PRIMARY KEY}"; primary="${column}" ;;
# Line 160  sql_add_column() Line 165  sql_add_column()
165   *auto_increment*) opts="${opts//auto_increment}"; autoincrement="${column} ${opts}" ;;   *auto_increment*) opts="${opts//auto_increment}"; autoincrement="${column} ${opts}" ;;
166   esac   esac
167    
168   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};" || die "add column '${table}'"
169   done   done
170    
171     # delete an existing auto_increment first
172     if [[ -n ${autoincrement} ]]
173     then
174     i=( $(mysqldo 'SHOW COLUMNS FROM '${MCORE_SQL_DB}.${table}' WHERE `Extra`="auto_increment"') )
175     current_autoincrement="${i[0]}"
176     if [[ ${current_autoincrement} != ${column} ]] && [[ -n ${current_autoincrement} ]]
177     then
178     current_opts=( "${i[1]}" ) # type eg int(11)
179     # null or not
180     case "${i[2]}" in
181     NO|no) current_opts+=( "NOT NULL" ) ;;
182     YES|yes) current_opts+=( "DEFAULT ${i[4]}" ) ;; # should never match, but to be save
183     esac
184     mysqldo "ALTER TABLE ${MCORE_SQL_DB}.${table} CHANGE ${current_autoincrement} ${current_autoincrement} ${current_opts};" || die "removing auto_increment to table '${table}'"
185     fi
186     fi
187    
188   if [[ -n ${primary} ]]   if [[ -n ${primary} ]]
189   then   then
190     # only run this if the primary key is not already the same column
191     i=( $(mysqldo 'SHOW COLUMNS FROM '${MCORE_SQL_DB}.${table}' WHERE `Key`="PRI"') )
192     current_primary="${i[0]}"
193     if [[ -n ${current_primary} ]]
194     then
195     mysqldo "ALTER TABLE ${MCORE_SQL_DB}.${table} DROP PRIMARY KEY;" || die "adding primary key to table '${table}'"
196     fi
197   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}'"
198   fi   fi
199   if [[ -n ${autoincrement} ]]   if [[ -n ${autoincrement} ]]
# Line 190  sql_modify_column() Line 219  sql_modify_column()
219   local autoincrement   local autoincrement
220   local primary   local primary
221   local current_primary   local current_primary
222     local current_autoincrement
223     local current_opts
224    
225   table="${TABLE_DEFINITION[0]}"   table="${TABLE_DEFINITION[0]}"
226   count="${#TABLE_DEFINITION[*]}"   count="${#TABLE_DEFINITION[*]}"
# Line 213  sql_modify_column() Line 244  sql_modify_column()
244   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}'"
245   done   done
246    
247     # delete an existing auto_increment first
248     if [[ -n ${autoincrement} ]]
249     then
250     i=( $(mysqldo 'SHOW COLUMNS FROM '${MCORE_SQL_DB}.${table}' WHERE `Extra`="auto_increment"') )
251     echo "DEBUG: i='${i[*]}'"
252     current_autoincrement="${i[0]}"
253     if [[ ${current_autoincrement} != ${column} ]] && [[ -n ${current_autoincrement} ]]
254     then
255     current_opts=( "${i[1]}" ) # type eg int(11)
256     # null or not
257     case "${i[2]}" in
258     NO|no) current_opts+=( "NOT NULL" ) ;;
259     YES|yes) current_opts+=( "DEFAULT ${i[4]}" ) ;; # should never match, but to be save
260     esac
261     mysqldo "ALTER TABLE ${MCORE_SQL_DB}.${table} CHANGE ${current_autoincrement} ${current_autoincrement} ${current_opts};" || die "removing auto_increment to table '${table}'"
262     fi
263     fi
264    
265   if [[ -n ${primary} ]]   if [[ -n ${primary} ]]
266   then   then
267   # 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
268   i=( $(mysqldo 'SHOW COLUMNS FROM ${MCORE_SQL_DB}.${table} WHERE `Key`="PRI"') )   i=( $(mysqldo 'SHOW COLUMNS FROM '${MCORE_SQL_DB}.${table}' WHERE `Key`="PRI"') )
269   current_primary="${i[0]}"   current_primary="${i[0]}"
270   if [[ ${current_primary} != ${primary} ]]   if [[ -n ${current_primary} ]]
271   then   then
272   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}'"
273   fi   fi
274     mysqldo "ALTER TABLE ${MCORE_SQL_DB}.${table} ADD PRIMARY KEY (${primary});" || die "adding primary key to table '${table}'"
275   fi   fi
276   if [[ -n ${autoincrement} ]]   if [[ -n ${autoincrement} ]]
277   then   then
# Line 299  update_database() Line 349  update_database()
349    
350   if [[ ${current_sql_db_schema_version} = ${MCORE_SQL_DB_SCHEMA_VERSION} ]]   if [[ ${current_sql_db_schema_version} = ${MCORE_SQL_DB_SCHEMA_VERSION} ]]
351   then   then
352   echo "Database is already up to date"   echo "Database schema is already up to date"
353   return 0   return 0
354   fi   fi
355    

Legend:
Removed from v.8501  
changed lines
  Added in v.8509