Study/django

mysql에서 postgresql로 마이그레이션 시도를 하다 #2

bluebamus 2023. 10. 11.

 - 이후 수정 사항 :

   - mysql에 정의된 bool 처리를 위해 설정된 int4 속성을 postgresql에서 bool 값으로 모두 변경

   - 문제 없이 동작하는거 확인

 

 - 발생한 문제 :

   -  admin 페이지에서 데이터를 수정하면 django_admin_log 테이블의 not null 제약조건 위반 에러 발생

   - mysql dll과 postgresql의 dll을 확인해 보자

      - mysql

CREATE TABLE `django_admin_log` (
  `id` int NOT NULL AUTO_INCREMENT,
  `action_time` datetime(6) NOT NULL,
  `object_id` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  `object_repr` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `action_flag` smallint unsigned NOT NULL,
  `change_message` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `content_type_id` int DEFAULT NULL,
  `user_id` bigint NOT NULL,
  PRIMARY KEY (`id`),
  KEY `django_admin_log_content_type_id_c4bce8eb_fk_django_co` (`content_type_id`),
  KEY `django_admin_log_user_id_c564eba6_fk_abstractuser_user_id` (`user_id`),
  CONSTRAINT `django_admin_log_content_type_id_c4bce8eb_fk_django_co` FOREIGN KEY (`content_type_id`) REFERENCES `django_content_type` (`id`),
  CONSTRAINT `django_admin_log_user_id_c564eba6_fk_abstractuser_user_id` FOREIGN KEY (`user_id`) REFERENCES `abstractuser_user` (`id`),
  CONSTRAINT `django_admin_log_chk_1` CHECK ((`action_flag` >= 0))
) ENGINE=InnoDB AUTO_INCREMENT=194 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

       - postgresql

CREATE TABLE django_portfolio_blog.django_admin_log (
	id int4 NOT NULL,
	action_time timestamp NOT NULL,
	object_id text NULL,
	object_repr varchar(200) NULL,
	action_flag int2 NOT NULL,
	change_message text NULL,
	content_type_id int4 NULL,
	user_id int8 NOT NULL
);

   - AUTO_INCREMENT의 설정이 안되어 있어서 당연히 문제가 발생하는 것이었다. 그런이 이뿐만 아니래 외래키, index 등 기타 설정들이 하나도 되어 있지 않아 있는 것이 확인되었다.

 

 

 - 이대로 사용할 수 없기 때문에, 빈 스키마에 migrations을 시도하고 데이터만 덤프해서 사용하는 것으로 테스트 하기로 한다.

   - makemigrations -> migrate 이후 모든 설정이 제대로 되어 있는 것을 확인하였다. 

   - auto increase나 기타 필드 설정이 mysql과 많이 다른 것을 확인할 수 있었다.

   - 역시나 mysql에서 다운받은 데이터를 밀어 넣기에 bool 값이 문제가 된다 하나씩 비교 대조하여 업데이트 하면 넣어진다.

   - 데이터가 거의 없는 상황이기에 설정된 메뉴와 필수 항목만 옮기고 나머지는 초기화 하기로 결정함

   - 이전 방법에 대해서는 문제 없이 수행할 수 있을것 같다.

 

   * 블로그 글만 보면 쉽게 이전이 될 것 같지만, 외래키나 필드에 대한 설정은 쉽게 되지 않는 것을 확인할 수 있었다.

     역시 직접 해봐야 실체를 알 수 있다.

댓글