【備忘】Spring Session に関連するテーブル(spring_session 等)が存在しない旨のエラーへの対応

「org.postgresql.util.PSQLException: ERROR: リレーション"spring_session"は存在しません」

以下から、対応するschema-<DB>.sql (今回の場合はPostgreSQLなのでschema-postgresql.sql) を拝借してDBに直接流してしまう。

github.com

Stacktrace: リレーション"spring_session"は存在しません

2022-02-06 14:30:00.017 ERROR 14184 --- [pool-2-thread-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [DELETE FROM SPRING_SESSION WHERE EXPIRY_TIME < ?]; nested exception is org.postgresql.util.PSQLException: ERROR: リレーション"spring_session"は存在しません
  位置: 13
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:239) ~[spring-jdbc-5.3.1.jar:5.3.1]
SQLErrorCodeSQLExceptionTranslator.java:239
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70) ~[spring-jdbc-5.3.1.jar:5.3.1]
AbstractFallbackSQLExceptionTranslator.java:70
    at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1541) ~[spring-jdbc-5.3.1.jar:5.3.1]
JdbcTemplate.java:1541
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:667) ~[spring-jdbc-5.3.1.jar:5.3.1]
JdbcTemplate.java:667
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:960) ~[spring-jdbc-5.3.1.jar:5.3.1]
JdbcTemplate.java:960
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:1015) ~[spring-jdbc-5.3.1.jar:5.3.1]
JdbcTemplate.java:1015
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:1025) ~[spring-jdbc-5.3.1.jar:5.3.1]
JdbcTemplate.java:1025
    at org.springframework.session.jdbc.JdbcIndexedSessionRepository.lambda$cleanUpExpiredSessions$8(JdbcIndexedSessionRepository.java:556) ~[spring-session-jdbc-2.4.1.jar:2.4.1]
JdbcIndexedSessionRepository.java:556
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140) ~[spring-tx-5.3.1.jar:5.3.1]
TransactionTemplate.java:140
    at org.springframework.session.jdbc.JdbcIndexedSessionRepository.cleanUpExpiredSessions(JdbcIndexedSessionRepository.java:556) ~[spring-session-jdbc-2.4.1.jar:2.4.1]
JdbcIndexedSessionRepository.java:556
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.3.1.jar:5.3.1]
DelegatingErrorHandlingRunnable.java:54
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:95) ~[spring-context-5.3.1.jar:5.3.1]
ReschedulingRunnable.java:95
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na]
Executors.java:515
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
FutureTask.java:264
    at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[na:na]
ScheduledThreadPoolExecutor.java:304
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
ThreadPoolExecutor.java:1128
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
ThreadPoolExecutor.java:628
    at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Thread.java:834
Caused by: org.postgresql.util.PSQLException: ERROR: リレーション"spring_session"は存在しません
  位置: 13
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2553) ~[postgresql-42.2.18.jar:42.2.18]
QueryExecutorImpl.java:2553
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2285) ~[postgresql-42.2.18.jar:42.2.18]
QueryExecutorImpl.java:2285
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:323) ~[postgresql-42.2.18.jar:42.2.18]
QueryExecutorImpl.java:323
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:473) ~[postgresql-42.2.18.jar:42.2.18]
PgStatement.java:473
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:393) ~[postgresql-42.2.18.jar:42.2.18]
PgStatement.java:393
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:164) ~[postgresql-42.2.18.jar:42.2.18]
PgPreparedStatement.java:164
    at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:130) ~[postgresql-42.2.18.jar:42.2.18]
PgPreparedStatement.java:130
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-3.4.5.jar:na]
ProxyPreparedStatement.java:61
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.4.5.jar:na]
    at org.springframework.jdbc.core.JdbcTemplate.lambda$update$2(JdbcTemplate.java:965) ~[spring-jdbc-5.3.1.jar:5.3.1]
JdbcTemplate.java:965
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:651) ~[spring-jdbc-5.3.1.jar:5.3.1]
JdbcTemplate.java:651
    ... 14 common frames omitted

DBの載せ替えで、「あれ、どこだっけ」ってよく探すのでメモ。

# これ、正規手順だっけ? ・x・