Skip to content

Commit

Permalink
Fix : data.sql sequence 맞추기
Browse files Browse the repository at this point in the history
  • Loading branch information
sejoon00 committed Jun 11, 2024
1 parent c273f17 commit 7a8b5cf
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/main/java/Chaeda_spring/ChaedaSpringApplication.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package Chaeda_spring;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
Expand All @@ -8,8 +10,30 @@
@SpringBootApplication
public class ChaedaSpringApplication {

@PersistenceContext
private EntityManager entityManager;

public static void main(String[] args) {
SpringApplication.run(ChaedaSpringApplication.class, args);
}

// @PostConstruct
// @Transactional
// public void init() {
// // 시퀀스가 존재하지 않으면 생성
// entityManager.createNativeQuery(
// "DO $$ " +
// "BEGIN " +
// "IF NOT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'member_id_seq') THEN " +
// "CREATE SEQUENCE member_id_seq INCREMENT BY 1; " +
// "END IF; " +
// "END $$;"
// ).executeUpdate();
//
// // 시퀀스 값 초기화
// entityManager.createNativeQuery(
// "SELECT setval('member_id_seq', COALESCE((SELECT MAX(id) FROM member), 1), false)"
// ).executeUpdate();
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//package Chaeda_spring.global;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.context.event.ApplicationReadyEvent;
//import org.springframework.context.event.EventListener;
//import org.springframework.core.io.ClassPathResource;
//import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
//import org.springframework.jdbc.datasource.init.ScriptException;
//import org.springframework.stereotype.Component;
//
//import javax.sql.DataSource;
//import java.sql.Connection;
//import java.sql.SQLException;
//
//@Component
//public class ResourceDatabaseInitializer {
//
// @Autowired
// private DataSource dataSource;
//
// @EventListener(ApplicationReadyEvent.class)
// public void initializeDatabase() {
// ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator(false, false, "UTF-8", new ClassPathResource("data.sql"));
// try (Connection connection = dataSource.getConnection()) {
// resourceDatabasePopulator.populate(connection);
// } catch (SQLException | ScriptException e) {
// e.printStackTrace();
// }
// }
//}
//
17 changes: 17 additions & 0 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,18 @@ VALUES (1, 'chaeda@konkuk.ac.kr', '$2a$10$ebqrHe.RCMF/eeVLCwjhqeC3ihDblknXX4qJ94
'chaeda@konkuk.ac.kr', 'MALE', '홍길동', '010-1234-5678', 'Student',
'STUDENT');

-- CREATE SEQUENCE member_id_seq;
--
-- ALTER TABLE member ALTER COLUMN id SET DEFAULT nextval('member_id_seq');
--
-- SELECT setval('member_id_seq', COALESCE((SELECT MAX(id) FROM member), 1), false);

INSERT INTO student (id, home_phone_num, parent_phone_num, school_name, notes, grade)
VALUES (1, '02-5318-6577', '010-5318-6577', '건국고등학교', null, 'HIGH_1')
;

SELECT setval('member_id_seq', COALESCE((SELECT MAX(id) FROM member), 1), false);

INSERT INTO math_problem
(id, problem_number, page_number, solved_students_count, incorrect_students_count, easy_num,
medium_difficulty_perceived_count, high_difficulty_perceived_count, textbook_id)
Expand Down Expand Up @@ -552,3 +559,13 @@ INSERT INTO accumulated_statistics_for_subconcept (student_id, solved_num, wrong
(1, 23, 4, 11, 7, 4, 50),
(1, 17, 3, 8, 6, 3, 52),
(1, 24, 6, 12, 9, 4, 54);
SELECT setval('member_id_seq', COALESCE((SELECT MAX(id) FROM member), 1), false);
SELECT setval('math_problem_id_seq', COALESCE((SELECT MAX(id) FROM math_problem), 1), false);
SELECT setval('math_problem_type_id_seq', COALESCE((SELECT MAX(id) FROM math_problem_type), 1), false);
SELECT setval('problem_type_mapping_id_seq', COALESCE((SELECT MAX(id) FROM problem_type_mapping), 1), false);
SELECT setval('solved_num_for_day_id_seq', COALESCE((SELECT MAX(id) FROM solved_num_for_day), 1), false);
SELECT setval('solved_num_for_month_id_seq', COALESCE((SELECT MAX(id) FROM solved_num_for_month), 1), false);
SELECT setval('solved_num_for_week_id_seq', COALESCE((SELECT MAX(id) FROM solved_num_for_week), 1), false);
SELECT setval('subconcept_statistics_for_week_id_seq', COALESCE((SELECT MAX(id) FROM subconcept_statistics_for_week), 1), false);
SELECT setval('subconcept_statistics_for_month_id_seq', COALESCE((SELECT MAX(id) FROM subconcept_statistics_for_month), 1), false);
SELECT setval('accumulated_statistics_for_subconcept_id_seq', COALESCE((SELECT MAX(id) FROM accumulated_statistics_for_subconcept), 1), false);

0 comments on commit 7a8b5cf

Please sign in to comment.