@@ -145,12 +145,9 @@ MFG_DB_SCHEMA="id integer primary key,
145145 os_image_sha256 char(64) DEFAULT NULL,
146146 connect_registered integer DEFAULT NULL,
147147 connect_device_id varchar(255) DEFAULT NULL,
148- eeprom_device varchar(64) DEFAULT NULL,
149148 eeprom_size integer DEFAULT NULL,
150- eeprom_sha256 char(64) DEFAULT NULL,
151149 eeprom_jedec char(6) DEFAULT NULL,
152150 eeprom_unique_id varchar(32) DEFAULT NULL,
153- eeprom_spi_speed integer DEFAULT NULL,
154151 bootloader_build_timestamp integer DEFAULT NULL,
155152 provision_ts timestamp default current_timestamp"
156153
@@ -164,35 +161,47 @@ if [ "$MFG_TABLE_EXISTS" -eq 0 ]; then
164161 # Table doesn't exist, create it
165162 sqlite3 " $MFG_DB_PATH " " CREATE TABLE devices($MFG_DB_SCHEMA );" > /dev/null 2>&1
166163else
167- # Table exists, check if migration is needed
168- # Get current schema
169- CURRENT_COLUMNS=$( sqlite3 " $MFG_DB_PATH " " PRAGMA table_info(devices);" | awk -F' |' ' {print $2}' | tr ' \n' ' ,' )
170-
171- # Check if any expected columns are missing
172- for COL in id boardname serial eth_mac wifi_mac bt_mac mmc_size mmc_cid rpi_duid board_revision processor memory manufacturer secure jtag_locked eeprom_write_protected pubkey_programmed devkey_revoked signed_boot_enabled os_image_filename os_image_sha256 connect_registered connect_device_id eeprom_device eeprom_size eeprom_sha256 eeprom_jedec eeprom_unique_id eeprom_spi_speed bootloader_build_timestamp provision_ts; do
173- if ! echo " $CURRENT_COLUMNS " | grep -q " $COL " ; then
174- echo " Migration needed: column $COL is missing from manufacturing.db devices table"
175-
176- # Create a new table with correct schema
177- sqlite3 " $MFG_DB_PATH " << EOF
178- CREATE TABLE devices_new($MFG_DB_SCHEMA );
179-
180- -- Copy data from old table to new table
181- INSERT INTO devices_new($( echo " $CURRENT_COLUMNS " | sed ' s/,$//' ) )
182- SELECT $( echo " $CURRENT_COLUMNS " | sed ' s/,$//' )
183- FROM devices;
184-
185- -- Drop old table
186- DROP TABLE devices;
187-
188- -- Rename new table to original name
189- ALTER TABLE devices_new RENAME TO devices;
164+ # Table exists, check if migration is needed. The expected column set,
165+ # in schema order. Keep this in sync with MFG_DB_SCHEMA above.
166+ EXPECTED_COLS=" id boardname serial eth_mac wifi_mac bt_mac mmc_size mmc_cid rpi_duid board_revision processor memory manufacturer secure jtag_locked eeprom_write_protected pubkey_programmed devkey_revoked signed_boot_enabled os_image_filename os_image_sha256 connect_registered connect_device_id eeprom_size eeprom_jedec eeprom_unique_id bootloader_build_timestamp provision_ts"
167+
168+ # Current columns, one per line.
169+ CURRENT_COLUMNS=$( sqlite3 " $MFG_DB_PATH " " PRAGMA table_info(devices);" | awk -F' |' ' {print $2}' )
170+
171+ # Migration is needed whenever the on-disk column set differs from the
172+ # expected set in either direction -- a missing column (added in this
173+ # release) or an extra column (dropped in this release).
174+ CURRENT_SORTED=$( printf ' %s\n' $CURRENT_COLUMNS | sort)
175+ EXPECTED_SORTED=$( printf ' %s\n' $EXPECTED_COLS | sort)
176+
177+ if [ " $CURRENT_SORTED " != " $EXPECTED_SORTED " ]; then
178+ echo " Migration needed: manufacturing.db devices schema differs from expected"
179+
180+ # Carry over only the columns present in BOTH the old and new schema.
181+ # Added columns are left at their DEFAULT; dropped columns (and their
182+ # data) are discarded.
183+ COMMON_COLS=" "
184+ for COL in $EXPECTED_COLS ; do
185+ if printf ' %s\n' " $CURRENT_COLUMNS " | grep -qx " $COL " ; then
186+ COMMON_COLS=" ${COMMON_COLS: +$COMMON_COLS ,} $COL "
187+ fi
188+ done
189+
190+ sqlite3 " $MFG_DB_PATH " << EOF
191+ CREATE TABLE devices_new($MFG_DB_SCHEMA );
192+
193+ -- Copy the intersection of old and new columns.
194+ INSERT INTO devices_new($COMMON_COLS )
195+ SELECT $COMMON_COLS
196+ FROM devices;
197+
198+ -- Replace the old table with the migrated one.
199+ DROP TABLE devices;
200+ ALTER TABLE devices_new RENAME TO devices;
190201EOF
191-
192- echo " Migration completed successfully for manufacturing.db"
193- break
194- fi
195- done
202+
203+ echo " Migration completed successfully for manufacturing.db"
204+ fi
196205fi
197206
198207# DEBHELPER#
0 commit comments