Skip to content

Commit 9ffbc71

Browse files
aparajonsvc-squareup-copybara
authored andcommitted
Fix HibernateModule installSchemaMigrator issue that prevents
ServiceManager creation GitOrigin-RevId: 4476c19873e2879e5621cc78403be3307c376837
1 parent a4feecf commit 9ffbc71

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

misk-hibernate/src/main/kotlin/misk/hibernate/HibernateModule.kt

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,14 @@ open class HibernateModule @JvmOverloads constructor(
171171

172172
val transacterKey = Transacter::class.toKey(qualifier)
173173

174-
install(
175-
ServiceModule<SchemaMigratorService>(qualifier)
176-
.dependsOn<DataSourceService>(qualifier)
177-
.enhancedBy<ReadyService>()
178-
)
174+
// Only install SchemaMigratorService module if schema migrator is enabled
175+
if (installSchemaMigrator) {
176+
install(
177+
ServiceModule<SchemaMigratorService>(qualifier)
178+
.dependsOn<DataSourceService>(qualifier)
179+
.enhancedBy<ReadyService>()
180+
)
181+
}
179182

180183
bind(transacterKey).toProvider(getTransacterProvider()).asSingleton()
181184

@@ -291,12 +294,16 @@ open class HibernateModule @JvmOverloads constructor(
291294
}.asSingleton()
292295

293296
if (isWriter) {
294-
install(
295-
ServiceModule<TransacterService>(qualifier)
296-
.enhancedBy<SchemaMigratorService>(qualifier)
297-
.enhancedBy<ReadyService>()
298-
.dependsOn<DataSourceService>(qualifier)
299-
)
297+
val transacterServiceModule = ServiceModule<TransacterService>(qualifier)
298+
.dependsOn<DataSourceService>(qualifier)
299+
.enhancedBy<ReadyService>()
300+
301+
// Only enhance with SchemaMigratorService if it's installed
302+
if (installSchemaMigrator) {
303+
install(transacterServiceModule.enhancedBy<SchemaMigratorService>(qualifier))
304+
} else {
305+
install(transacterServiceModule)
306+
}
300307
} else {
301308
install(
302309
ServiceModule<TransacterService>(qualifier)

misk-hibernate/src/test/kotlin/misk/hibernate/HibernateModuleInstallSchemaMigratorTest.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package misk.hibernate
22

3+
import com.google.common.util.concurrent.ServiceManager
34
import com.google.inject.Guice
45
import com.google.inject.Key
56
import com.google.inject.util.Modules
@@ -88,6 +89,36 @@ internal class HibernateModuleInstallSchemaMigratorTest {
8889
assertThat(exception.message).contains("No implementation for")
8990
assertThat(exception.message).contains("SchemaMigratorService")
9091
}
92+
93+
@Test
94+
fun `installSchemaMigrator=false should allow ServiceManager to be created`() {
95+
val module = Modules.combine(
96+
deploymentModule,
97+
MiskTestingServiceModule(),
98+
HibernateModule(
99+
qualifier = TestDb::class,
100+
config = dataSourceConfig,
101+
readerQualifier = null,
102+
readerConfig = null,
103+
installSchemaMigrator = false
104+
),
105+
object : HibernateEntityModule(TestDb::class) {
106+
override fun configureHibernate() {
107+
addEntities(DbMovie::class)
108+
}
109+
}
110+
)
111+
112+
val injector = Guice.createInjector(module)
113+
114+
// ServiceManager should be created successfully without SchemaMigratorService
115+
val serviceManager = injector.getInstance(ServiceManager::class.java)
116+
assertThat(serviceManager).isNotNull()
117+
118+
// DataSourceService should still be bound
119+
val dataSourceService = injector.getInstance(Key.get(DataSourceService::class.java, TestDb::class.java))
120+
assertThat(dataSourceService).isInstanceOf(DataSourceService::class.java)
121+
}
91122
}
92123

93124
@com.google.inject.BindingAnnotation

0 commit comments

Comments
 (0)