# Scope error while migrating in test container

**URL:** <https://forum.liquibase.org/t/scope-error-while-migrating-in-test-container/4808>\
**Category:** General Discussion\
**Created:** [November 18, 2020, 7:15am UTC](https://forum.liquibase.org/t/scope-error-while-migrating-in-test-container/4808 "2020-11-18T07:15:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DenisNovac](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@DenisNovac](https://forum.liquibase.org/u/DenisNovac)\
**Post date:** [November 18, 2020, 7:15am UTC](https://forum.liquibase.org/t/scope-error-while-migrating-in-test-container/4808/1 "2020-11-18T07:15:42Z")

</div>

Hi everyone. I am getting this errors while migrating my database inside test container. I am using PostgreSQL 13, test container com.dimafeng/testcontainers-scala-postgresql 0.38.6 and Liquibase 4.1.1.

```
INFO: Error getting default schema
java.lang.NullPointerException
	at java.base/java.util.TreeMap.rotateRight(TreeMap.java:2240)
	at java.base/java.util.TreeMap.fixAfterInsertion(TreeMap.java:2272)
	at java.base/java.util.TreeMap.put(TreeMap.java:580)
	at liquibase.util.SmartMap.put(SmartMap.java:77)
	at liquibase.Scope.getSingleton(Scope.java:311)
	at liquibase.database.AbstractJdbcDatabase.getConnectionSchemaName(AbstractJdbcDatabase.java:360)
	at liquibase.database.AbstractJdbcDatabase.getDefaultSchemaName(AbstractJdbcDatabase.java:325)
	at liquibase.changelog.ChangeLogParameters.<init>(ChangeLogParameters.java:61)
	at liquibase.Liquibase.<init>(Liquibase.java:123)

/** some production classes here

   at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
   at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
   at java.base/java.lang.Thread.run(Thread.java:834)

```

And then:

```
INFO: Successfully acquired change log lock
нояб. 17, 2020 11:53:50 PM liquibase.lockservice
INFO: Successfully acquired change log lock
нояб. 17, 2020 11:53:52 PM liquibase.changelog
INFO: Creating database history table with name: public.databasechangelog
нояб. 17, 2020 11:53:52 PM liquibase.changelog
INFO: Creating database history table with name: public.databasechangelog
нояб. 17, 2020 11:53:52 PM liquibase.changelog
INFO: Reading from public.databasechangelog
нояб. 17, 2020 11:53:52 PM liquibase.changelog
INFO: Reading from public.databasechangelog
нояб. 17, 2020 11:53:52 PM liquibase.lockservice
INFO: Successfully released change log lock
нояб. 17, 2020 11:53:52 PM liquibase.lockservice
INFO: Successfully released change log lock

[info] liquibase.exception.LiquibaseException: java.lang.RuntimeException: Cannot end scope aikoqztxww when currently at scope spqahgmasy
[info] at liquibase.Liquibase.runInScope(Liquibase.java:2282)
[info] at liquibase.Liquibase.update(Liquibase.java:215)
[info] at liquibase.Liquibase.update(Liquibase.java:201)
[info] at liquibase.Liquibase.update(Liquibase.java:197)
[info] at liquibase.Liquibase.update(Liquibase.java:189)
/** Some production again
[info] ...
[info] Cause: java.lang.RuntimeException: Cannot end scope aikoqztxww when currently at scope spqahgmasy
[info] at liquibase.Scope.exit(Scope.java:209)
[info] at liquibase.Scope.child(Scope.java:171)
[info] at liquibase.Scope.child(Scope.java:159)
[info] at liquibase.Scope.child(Scope.java:138)
[info] at liquibase.Liquibase.runInScope(Liquibase.java:2277)
[info] at liquibase.Liquibase.update(Liquibase.java:215)
[info] at liquibase.Liquibase.update(Liquibase.java:201)
[info] at liquibase.Liquibase.update(Liquibase.java:197)
[info] at liquibase.Liquibase.update(Liquibase.java:189)
/** And some production here

```

I tried to google this “scope” thing but no luck. The only thing i found is this report on wildfly: [https://github.com/jamesnetherton/wildfly-liquibase/issues/82](https://github.com/jamesnetherton/wildfly-liquibase/issues/82)

It seems to happens only when i have two test suit in app with database container in each.

---

<div class="post-metadata">

**Author:** ![DenisNovac](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@DenisNovac](https://forum.liquibase.org/u/DenisNovac)\
**Post date:** [November 18, 2020, 9:53am UTC](https://forum.liquibase.org/t/scope-error-while-migrating-in-test-container/4808/2 "2020-11-18T09:53:59Z")

</div>

It looks like somehow there was one Liquibase instance which tried to migrate two databases concurrently.

---

<div class="post-metadata">

**Author:** ![poznas](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@poznas](https://forum.liquibase.org/u/poznas)\
**Post date:** [November 25, 2020, 4:11pm UTC](https://forum.liquibase.org/t/scope-error-while-migrating-in-test-container/4808/3 "2020-11-25T16:11:54Z")

</div>

We are getting the same error while running multiple schema upgrades in parallel

the static property of `Scope.class` : `private static ScopeManager scopeManager` seems to be not thread safe.

```auto
executor.invokeAll(
     schemas.map { 
          Callable { createNewLiquibaseInstanceAndRun(it) }
     }
)

```

---

<div class="post-metadata">

**Author:** ![DenisNovac](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@DenisNovac](https://forum.liquibase.org/u/DenisNovac)\
**Post date:** [December 3, 2020, 3:54am UTC](https://forum.liquibase.org/t/scope-error-while-migrating-in-test-container/4808/4 "2020-12-03T03:54:47Z")

</div>

We just removed parallel test execution for now.

---

<div class="post-metadata">

**Author:** ![DenisNovac](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@DenisNovac](https://forum.liquibase.org/u/DenisNovac)\
**Post date:** [May 24, 2023, 9:10am UTC](https://forum.liquibase.org/t/scope-error-while-migrating-in-test-container/4808/5 "2023-05-24T09:10:53Z")

</div>

Should’ve just add semaphore 🫠
