Table structure change deployment failure cases and solutions

Phenomenon: When the imported data and structure are upgraded, the structure changes, and the data in the django_migrations table is not recognized (the content does not match), which causes the migration to fail and the deployment fails during deployment.

Prerequisite: The data structure has not changed much, the new version of the table can only have more fields, not less, and the field types can adapt to the old version of the data.

Goal: The data should be retained and the data cannot be changed, but the data structure should use the latest data structure.
For example:
there are as many migration files in the migration directory of home_application in the project, the django_migrations table of the database will generate the corresponding number of data.

Replace the data in the django_migrations table with the contents of the latest django_migrations table. Compared with the previous version, the migration data records corresponding to the migration files added in this version are not stored in the database.

Project cases:

The latest version of django_migrations inserts sql statement:

INSERT INTO `django_migrations` VALUES (1, 'contenttypes', '0001_initial', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (2, 'contenttypes', '0002_remove_content_type_name', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (3, 'auth', '0001_initial', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (4, 'auth', '0002_alter_permission_name_max_length', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (5, 'auth', '0003_alter_user_email_max_length', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (6, 'auth', '0004_alter_user_username_opts', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (7, 'auth', '0005_alter_user_last_login_null', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (8, 'auth', '0006_require_contenttypes_0002', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (9, 'account', '0001_initial', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (10, 'account', '0002_initial_user_data', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (11, 'account', '0003_auto_20180911_0959', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (12, 'admin', '0001_initial', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (13, 'app_control', '0001_initial', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (14, 'app_control', '0002_initial_app_control', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (15, 'djcelery', '0001_initial', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (16, 'home_application', '0001_initial', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (17, 'home_application', '0002_auto_20181024_0920', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (18, 'home_application', '0003_auto_20181025_1117', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (19, 'home_application', '0004_auto_20181025_1202', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (20, 'home_application', '0005_runprocess_record_time', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (21, 'home_application', '0006_auto_20181031_1416', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (22, 'home_application', '0007_auto_20181119_0950', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (23, 'home_application', '0008_auto_20181119_0952', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (24, 'home_application', '0009_auto_20181120_1549', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (25, 'home_application', '0010_auto_20181120_1552', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (26, 'home_application', '0011_auto_20181122_0944', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (27, 'home_application', '0012_auto_20181122_1859', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (28, 'home_application', '0013_auto_20181122_1914', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (29, 'home_application', '0014_auto_20181122_1932', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (30, 'home_application', '0015_auto_20181219_1120', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (31, 'home_application', '0016_auto_20200107_1654', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (32, 'home_application', '0017_auto_20200119_1033', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (33, 'home_application', '0018_auto_20200120_1747', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (34, 'home_application', '0019_auto_20200218_1538', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (35, 'home_application', '0020_auto_20200219_1005', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (36, 'home_application', '0021_auto_20200220_1043', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (37, 'home_application', '0022_auto_20200220_1456', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (38, 'home_application', '0023_auto_20200220_1501', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (39, 'home_application', '0024_auto_20200221_0946', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (40, 'home_application', '0025_auto_20200304_1244', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (41, 'sessions', '0001_initial', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (42, 'sites', '0001_initial', '2020-03-04 19:12:52');

Compared with the previous version, a migration file has been added. This migration record data (for example, 40)
clears the old django_migrations table and inserts it (the added migration file record data is not stored in the database):

INSERT INTO `django_migrations` VALUES (1, 'contenttypes', '0001_initial', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (2, 'contenttypes', '0002_remove_content_type_name', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (3, 'auth', '0001_initial', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (4, 'auth', '0002_alter_permission_name_max_length', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (5, 'auth', '0003_alter_user_email_max_length', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (6, 'auth', '0004_alter_user_username_opts', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (7, 'auth', '0005_alter_user_last_login_null', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (8, 'auth', '0006_require_contenttypes_0002', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (9, 'account', '0001_initial', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (10, 'account', '0002_initial_user_data', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (11, 'account', '0003_auto_20180911_0959', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (12, 'admin', '0001_initial', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (13, 'app_control', '0001_initial', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (14, 'app_control', '0002_initial_app_control', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (15, 'djcelery', '0001_initial', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (16, 'home_application', '0001_initial', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (17, 'home_application', '0002_auto_20181024_0920', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (18, 'home_application', '0003_auto_20181025_1117', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (19, 'home_application', '0004_auto_20181025_1202', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (20, 'home_application', '0005_runprocess_record_time', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (21, 'home_application', '0006_auto_20181031_1416', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (22, 'home_application', '0007_auto_20181119_0950', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (23, 'home_application', '0008_auto_20181119_0952', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (24, 'home_application', '0009_auto_20181120_1549', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (25, 'home_application', '0010_auto_20181120_1552', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (26, 'home_application', '0011_auto_20181122_0944', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (27, 'home_application', '0012_auto_20181122_1859', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (28, 'home_application', '0013_auto_20181122_1914', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (29, 'home_application', '0014_auto_20181122_1932', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (30, 'home_application', '0015_auto_20181219_1120', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (31, 'home_application', '0016_auto_20200107_1654', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (32, 'home_application', '0017_auto_20200119_1033', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (33, 'home_application', '0018_auto_20200120_1747', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (34, 'home_application', '0019_auto_20200218_1538', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (35, 'home_application', '0020_auto_20200219_1005', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (36, 'home_application', '0021_auto_20200220_1043', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (37, 'home_application', '0022_auto_20200220_1456', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (38, 'home_application', '0023_auto_20200220_1501', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (39, 'home_application', '0024_auto_20200221_0946', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (40, 'sessions', '0001_initial', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (41, 'sites', '0001_initial', '2020-03-04 19:12:52');

When deploying in this way, the data table structure will be changed to the latest without causing conflicts in the contents of the django_migrations table, so the deployment is successful.
After the deployment is complete, there will be an additional migration file data record in the django_migrations table

42	home_application	0025_auto_20200304_1244	2020-03-04 19:47:37(新版本增加的迁移文件所生成记录)

solution:

The first solution: Synchronize the django_migrations table

Idea: Make an adaptation of the django_migrations table data-adapt to the new version of the migration file

1. Enter the project database and clear the django_migrations table data

2. Execute the following statement or run the relevant sql file:

INSERT INTO `django_migrations` VALUES (1, 'contenttypes', '0001_initial', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (2, 'contenttypes', '0002_remove_content_type_name', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (3, 'auth', '0001_initial', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (4, 'auth', '0002_alter_permission_name_max_length', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (5, 'auth', '0003_alter_user_email_max_length', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (6, 'auth', '0004_alter_user_username_opts', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (7, 'auth', '0005_alter_user_last_login_null', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (8, 'auth', '0006_require_contenttypes_0002', '2020-03-04 19:12:48');
INSERT INTO `django_migrations` VALUES (9, 'account', '0001_initial', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (10, 'account', '0002_initial_user_data', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (11, 'account', '0003_auto_20180911_0959', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (12, 'admin', '0001_initial', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (13, 'app_control', '0001_initial', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (14, 'app_control', '0002_initial_app_control', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (15, 'djcelery', '0001_initial', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (16, 'home_application', '0001_initial', '2020-03-04 19:12:49');
INSERT INTO `django_migrations` VALUES (17, 'home_application', '0002_auto_20181024_0920', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (18, 'home_application', '0003_auto_20181025_1117', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (19, 'home_application', '0004_auto_20181025_1202', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (20, 'home_application', '0005_runprocess_record_time', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (21, 'home_application', '0006_auto_20181031_1416', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (22, 'home_application', '0007_auto_20181119_0950', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (23, 'home_application', '0008_auto_20181119_0952', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (24, 'home_application', '0009_auto_20181120_1549', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (25, 'home_application', '0010_auto_20181120_1552', '2020-03-04 19:12:50');
INSERT INTO `django_migrations` VALUES (26, 'home_application', '0011_auto_20181122_0944', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (27, 'home_application', '0012_auto_20181122_1859', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (28, 'home_application', '0013_auto_20181122_1914', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (29, 'home_application', '0014_auto_20181122_1932', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (30, 'home_application', '0015_auto_20181219_1120', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (31, 'home_application', '0016_auto_20200107_1654', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (32, 'home_application', '0017_auto_20200119_1033', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (33, 'home_application', '0018_auto_20200120_1747', '2020-03-04 19:12:51');
INSERT INTO `django_migrations` VALUES (34, 'home_application', '0019_auto_20200218_1538', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (35, 'home_application', '0020_auto_20200219_1005', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (36, 'home_application', '0021_auto_20200220_1043', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (37, 'home_application', '0022_auto_20200220_1456', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (38, 'home_application', '0023_auto_20200220_1501', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (39, 'home_application', '0024_auto_20200221_0946', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (40, 'sessions', '0001_initial', '2020-03-04 19:12:52');
INSERT INTO `django_migrations` VALUES (41, 'sites', '0001_initial', '2020-03-04 19:12:52');

3. Redeploy the new project package

The second solution: Synchronize migration files

Idea: Make an adaptation of the migration files in the project-adapt to the previous version of django_migrations table data

1. Find the migration file corresponding to the migration data of the django_migrations table of the deployed project, compare it with the migration file in the project, and find the extra migration file.

2. Merge the extra migration file into the migration file corresponding to the latest piece of data in the django_migrations table,

3. Replace the merged migration file with the new version migration file, and delete the merged migration file—that is, the migration file found in the first step,

4. Re-generate the migration file in the project, package and deploy.

Guess you like

Origin blog.csdn.net/qq_42631707/article/details/104661486