embed-server --server-config=standalone-ha.xml

echo *** Begin Migration ***
echo

# Migrate from 1.8.1 to 1.9.1
if (outcome == failed) of /subsystem=infinispan/cache-container=keycloak/replicated-cache=work/:read-resource
  echo Adding replicated-cache=work to keycloak cache container...
  /subsystem=infinispan/cache-container=keycloak/replicated-cache=work/:add(mode=SYNC)
  echo
end-if
# realmVersions cache deprecated in 2.1.0
#if (outcome == failed) of /subsystem=infinispan/cache-container=keycloak/local-cache=realmVersions/:read-resource
#  echo Adding local-cache=realmVersions to keycloak cache container...
#  /subsystem=infinispan/cache-container=keycloak/local-cache=realmVersions/:add(indexing=NONE,start=LAZY)
#  /subsystem=infinispan/cache-container=keycloak/local-cache=realmVersions/component=transaction/:write-attribute(name=mode,value=BATCH)
#  echo
#end-if

# Migrate from 1.9.1 to 1.9.2
if (result == NONE) of /subsystem=infinispan/cache-container=keycloak/invalidation-cache=users/component=eviction/:read-attribute(name=strategy)
  echo Adding eviction strategy to keycloak users cache container...
  /subsystem=infinispan/cache-container=keycloak/invalidation-cache=users/component=eviction/:write-attribute(name=strategy,value=LRU)
  /subsystem=infinispan/cache-container=keycloak/invalidation-cache=users/component=eviction/:write-attribute(name=max-entries,value=10000)
  echo
end-if

# Migrate from 1.9.2 to 2.0.0
# NO CHANGES

# Migrate from 2.0.0 to 2.1.0
if (outcome == success) of /subsystem=infinispan/cache-container=keycloak/local-cache=realmVersions/:read-resource
  echo Removing deprecated cache 'realmVersions'
  /subsystem=infinispan/cache-container=keycloak/local-cache=realmVersions/:remove
  echo
end-if

# Migrate kecloak-server.json (deprecated in 2.2.0)
if (result == []) of /subsystem=keycloak-server/:read-children-names(child-type=spi)
  echo Migrating keycloak-server.json to server cofig xml...
  /subsystem=keycloak-server/:migrate-json
  echo
end-if

set persistenceProvider=jpa

# Migrate from 2.1.0 to 2.2.0
if (outcome == failed) of /extension=org.jboss.as.deployment-scanner/:read-resource
  echo Adding deployment-scanner extension...
  /extension=org.jboss.as.deployment-scanner/:add(module=org.jboss.as.deployment-scanner)
  echo
end-if
if (outcome == failed) of /subsystem=deployment-scanner/:read-resource
  echo Adding deployment-scanner...
  /subsystem=deployment-scanner/:add  
  echo
end-if
if (outcome == failed) of /subsystem=deployment-scanner/scanner=default/:read-resource
  echo Adding scanner=default
  /subsystem=deployment-scanner/scanner=default/:add(path=deployments,relative-to=jboss.server.base.dir,runtime-failure-causes-rollback=${jboss.deployment.scanner.rollback.on.failure:false},scan-interval=5000)
  echo
end-if
if (outcome == failed) of /subsystem=infinispan/cache-container=keycloak/distributed-cache=authorization/:read-resource
  echo Additing distributed-cache=authorization
  /subsystem=infinispan/cache-container=keycloak/distributed-cache=authorization/:add(mode=SYNC,owners=1)
  echo
end-if
if (result == update) of /subsystem=keycloak-server/spi=connectionsJpa/provider=default/:map-get(name=properties,key=databaseSchema)
  echo Updating connectionsJpa default properties...
  /subsystem=keycloak-server/spi=connectionsJpa/provider=default/:map-remove(name=properties,key=databaseSchema)
  /subsystem=keycloak-server/spi=connectionsJpa/provider=default/:map-put(name=properties,key=initializeEmpty,value=true)
  /subsystem=keycloak-server/spi=connectionsJpa/provider=default/:map-put(name=properties,key=migrationStrategy,value=update)
  /subsystem=keycloak-server/spi=connectionsJpa/provider=default/:map-put(name=properties,key=migrationExport,value=${jboss.home.dir}/keycloak-database-update.sql)
  echo
end-if
if (outcome == failed) of /subsystem=keycloak-server/spi=userFederatedStorage/:read-resource
  echo Adding spi=userFederatedStorage...
  /subsystem=keycloak-server/spi=userFederatedStorage/:add(default-provider=$persistenceProvider)
  echo
end-if
if (outcome == failed) of /subsystem=keycloak-server/spi=jta-lookup/:read-resource
  echo Adding spi=jta-lookup...
  /subsystem=keycloak-server/spi=jta-lookup/:add(default-provider=${keycloak.jta.lookup.provider:jboss})
  /subsystem=keycloak-server/spi=jta-lookup/provider=jboss/:add(enabled=true)
  echo
end-if

# Migrate from 2.2.0 to 2.2.1
# NO CHANGES

# Migrate from 2.2.1 to 2.3.0
if (outcome == failed) of /subsystem=infinispan/cache-container=keycloak/local-cache=keys/:read-resource
  echo Adding local-cache=keys to keycloak cache container...
  /subsystem=infinispan/cache-container=keycloak/local-cache=keys/:add(indexing=NONE,start=LAZY)
  echo
end-if
if (result == undefined) of /subsystem=infinispan/cache-container=keycloak/local-cache=keys/component=eviction/:read-attribute(name=strategy,include-defaults=false)
  echo Updating eviction and expiration in local-cache=keys...
  /subsystem=infinispan/cache-container=keycloak/local-cache=keys/component=eviction/:write-attribute(name=strategy,value=LRU)
  /subsystem=infinispan/cache-container=keycloak/local-cache=keys/component=eviction/:write-attribute(name=max-entries,value=1000)
  /subsystem=infinispan/cache-container=keycloak/local-cache=keys/component=expiration/:write-attribute(name=max-idle,value=3600000)
  echo
end-if
if (outcome == failed) of /subsystem=keycloak-server/spi=publicKeyStorage/:read-resource  
  echo Adding spi=publicKeyStorage...
  /subsystem=keycloak-server/spi=publicKeyStorage/:add
  /subsystem=keycloak-server/spi=publicKeyStorage/provider=infinispan/:add(properties={minTimeBetweenRequests => "10"},enabled=true)
  echo
end-if

# Migrate from 2.3.0 to 2.4.0
if (outcome == success) of /subsystem=infinispan/cache-container=keycloak/invalidation-cache=users/:read-resource
  echo Replacing invalidation-cache=users with local-cache=users
  /subsystem=infinispan/cache-container=keycloak/invalidation-cache=users/:remove
  /subsystem=infinispan/cache-container=keycloak/local-cache=users/:add
  echo
end-if
if (result == undefined) of /subsystem=infinispan/cache-container=keycloak/local-cache=users/component=eviction/:read-attribute(name=strategy,include-defaults=false)
  echo Updating eviction in local-cache=users...
  /subsystem=infinispan/cache-container=keycloak/local-cache=users/component=eviction/:write-attribute(name=strategy,value=LRU)
  /subsystem=infinispan/cache-container=keycloak/local-cache=users/component=eviction/:write-attribute(name=max-entries,value=10000)
  echo
end-if
if (outcome == success) of /subsystem=infinispan/cache-container=keycloak/invalidation-cache=realms/:read-resource
  echo Replacing invalidation-cache=realms with local-cache=realms
  /subsystem=infinispan/cache-container=keycloak/invalidation-cache=realms/:remove
  /subsystem=infinispan/cache-container=keycloak/local-cache=realms/:add
  echo
end-if


# Migrate from 2.4.0 to 2.5.0
if (result == NONE) of /subsystem=infinispan/cache-container=keycloak/local-cache=realms/component=eviction/:read-attribute(name=strategy)  
  echo Adding eviction strategy to keycloak realms cache...
  /subsystem=infinispan/cache-container=keycloak/local-cache=realms/component=eviction/:write-attribute(name=strategy,value=LRU)
  /subsystem=infinispan/cache-container=keycloak/local-cache=realms/component=eviction/:write-attribute(name=max-entries,value=10000)
  echo
end-if

# Migrate from 2.5.0 to 2.5.1
# NO CHANGES

# Migrate 2.5.1 to 2.5.4
if (result != REPEATABLE_READ) of /subsystem=infinispan/cache-container=ejb/local-cache=persistent/component=locking/:read-attribute(name=isolation)
  echo Changing ejb cache locking to REPEATABLE_READ
  /subsystem=infinispan/cache-container=ejb/local-cache=persistent/component=locking/:write-attribute(name=isolation,value=REPEATABLE_READ)
  echo
end-if

if (outcome == success) of /subsystem=infinispan/cache-container=hibernate/local-cache=immutable-entity/:read-resource
   echo Removing Hibernate immutable-entity cache
   /subsystem=infinispan/cache-container=hibernate/local-cache=immutable-entity/:remove
end-if


# Migrate from 2.5.4 to 3.0.0 
if (result == jpa) of /subsystem=keycloak-server/spi=eventsStore/:read-attribute(name=default-provider,include-defaults=false)
   echo Removing default provider for eventsStore
   /subsystem=keycloak-server/spi=eventsStore/:undefine-attribute(name=default-provider)
   echo
end-if

if ((outcome == success) && (result.default-provider == jpa) && (result.provider == undefined)) of /subsystem=keycloak-server/spi=realm/:read-resource(recursive=false,include-defaults=false)
   echo Removing declaration for user SPI
   /subsystem=keycloak-server/spi=realm/:remove
   echo
end-if

if ((outcome == success) && (result.default-provider == jpa) && (result.provider == undefined)) of /subsystem=keycloak-server/spi=user/:read-resource(recursive=false,include-defaults=false)
   echo Removing declaration for user SPI
   /subsystem=keycloak-server/spi=user/:remove
   echo
end-if

if ((outcome == success) && (result.default-provider == jpa) && (result.provider == undefined)) of /subsystem=keycloak-server/spi=userFederatedStorage/:read-resource(recursive=false,include-defaults=false)
   echo Removing declaration for userFederatedStorage SPI
   /subsystem=keycloak-server/spi=userFederatedStorage/:remove
   echo
end-if

if ((outcome == success) && (result.default-provider == jpa) && (result.provider == undefined)) of /subsystem=keycloak-server/spi=authorizationPersister/:read-resource(recursive=false,include-defaults=false)
   echo Removing declaration for authorizationPersister SPI
   /subsystem=keycloak-server/spi=authorizationPersister/:remove
   echo
end-if

if (outcome == failed) of /subsystem=keycloak-server/spi=userCache/:read-resource
   echo Adding userCache SPI
   /subsystem=keycloak-server/spi=userCache/:add
   /subsystem=keycloak-server/spi=userCache/provider=default/:add(enabled=true)
   echo
end-if

if (outcome == failed) of /subsystem=keycloak-server/spi=realmCache/:read-resource
   echo Adding realmCache SPI
   /subsystem=keycloak-server/spi=realmCache/:add
   /subsystem=keycloak-server/spi=realmCache/provider=default/:add(enabled=true)
   echo
end-if

if ((result.default-provider == undefined) && (result.provider.default.enabled == true)) of /subsystem=keycloak-server/spi=connectionsInfinispan/:read-resource(recursive=true,include-defaults=false)
   echo Adding 'default' as default provider for connectionsInfinispan
   /subsystem=keycloak-server/spi=connectionsInfinispan/:write-attribute(name=default-provider,value=default)
   echo
end-if
echo *** End Migration ***