The post has been translated automatically. Original language: Russian
Topic
To change the database schema on an empty project is one line. Changing it on a running service with live users is a separate task, where an error costs several minutes of unavailability or lost data.
What is dangerous on large tables
Adding a column with a default value. In older versions of the DBMS, this is a rewrite of the entire table under lock. New ones are often safe, but you need to check your version.
Creating an index in the usual way. The table is locked for writing until the end of the operation. Almost everywhere there is an option with parallel construction, and it is needed.
Changing the column type. As a rule, it means a complete rewrite of the data.
Deleting or renaming a column at the same time as the code is being deployed. At the time of the rollout, some instances are running on the old version and accessing what is no longer there.
The fourth point is the most underestimated. The problem is not the migration itself, but the fact that the code and schema are not changing at the same time.
A multi-step deployment scheme
To rename a column, the sequence looks like this.
Add a new column without deleting anything.
Embed code that writes to both places and reads from the old one.
Fill in a new column for existing rows in batches rather than a single query.
Switch the reading to a new column and make sure everything is working.
Stop writing to the old one and only then delete it, in a separate release.
Five steps instead of one, but at each stage you can roll back and at no point is there an incompatibility between the code and the schema.
Rules that save nerves
Fill in data with batch files with pauses. One UPDATE of ten million rows creates a long transaction, bloats the log and interferes with the rest of the requests.
Set the timeout to lock. Migrations that couldn't take the lock quickly should drop instead of queuing up and stopping all traffic.
Check migration against copies of real data. On a test database of thousands of lines, everything is always fast.
Keep migrations reversible where possible, and know the rollback plan in advance if not.
Do not combine schema changes and logic changes in one release. If there is a problem, it will be unclear what to roll back.
The main thing
Secure migration is not about tricky SQL, but about the fact that the schema and code are always compatible in both directions. Once this condition is met, the deployment ceases to be an event that the team prepares for in the evening after the traffic.
Do you do migrations during business hours or still at night?
Тёмная тема выглядит как простая замена белого фона на чёрный, но внутри приложения это отдельный набор правил. Разработчики не прописывают цвет для каждой кнопки и каждой надписи вручную. Вместо этого создаётся список названий: цвет фона, цвет основного текста, цвет второстепенного текста, цвет границ. У каждого названия есть два значения, для светлого режима и для тёмного. Приложение спрашивает у телефона, какой режим выбран в настройках, и подставляет нужный набор значений.
Самое сложное в тёмной теме это не сами цвета, а контраст. Серый текст, который хорошо читается на белом, на чёрном фоне почти растворяется. Поэтому цвета подбирают парами и проверяют, чтобы разница между текстом и фоном оставалась достаточной в обоих режимах. Ещё один момент это картинки и логотипы с прозрачным фоном. Чёрный логотип на белом листе виден, а на тёмном экране пропадает, и для таких элементов готовят второй вариант.
Есть и практическая польза. На экранах, где каждая точка светится отдельно, тёмный фон расходует меньше заряда, потому что часть точек просто не горит.
А вы сами чем пользуетесь чаще, светлой темой или тёмной?