How to configure multiple Databases #3537
Replies: 3 comments 5 replies
-
So with the help from others I found out how to simply manually control the migrations to process: var newsletterDatabaseConfig = Config.forPath("datasource.db");
var migrationConfig = new MigrationConfig();
migrationConfig.setDbUsername(newsletterDatabaseConfig.get("username"));
migrationConfig.setDbPassword(newsletterDatabaseConfig.get("password"));
migrationConfig.setDbUrl(newsletterDatabaseConfig.get("url"));
migrationConfig.setDbSchema("public");
var migrationRunner = new MigrationRunner(migrationConfig);
migrationRunner.run(); and turning off automatic migration ebean.migration.run= false But i could not yet figure out how to tell DbMigration to only migrate a single database instead of all of them. |
Beta Was this translation helpful? Give feedback.
-
Okay, looks like I found out how to specify a certain database to run db migrations for: public class GenerateDbMigration {
public static void main(String[] args) throws IOException {
DbMigration dbMigration = DbMigration.create();
dbMigration.setPlatform(Platform.POSTGRES);
dbMigration.setServer(DB.getDefault()); <<-------
dbMigration.generateMigration();
}
} So now it looks like everything works as expected now! Maybe this will help some other folks in the future |
Beta Was this translation helpful? Give feedback.
-
@rbygrave After further investigation by trying to repoduce the behaviour in an isolated example i think found the root cause for that strange behaviour. UsecaseBasically i have 4 databases of which the main database is the default database, and the 3 others are query only databases. For every database there is a entity with the same properties, but different superclass and different tablename:
Heres a sample entity for the ro1 datasource with its superclass: @Entity
@Table(name = "ro1_city")
public class DbRo1City extends DbRo1BaseEntity {
private String name;
private String countryCode;
private String district;
private long population;
private String localName;
} @DbName("ro1")
@MappedSuperclass
public class DbRo1BaseEntity extends Model { Root cause of my issue: Wrong configurationSo the issue was having the datasources defined in datasource.default=main. <<<----- "main"
datasource.main.username=main
datasource.main.password=main
datasource.main.url=jdbc:postgresql://localhost:8432/main
datasource.main.driver=org.postgresql.Driver
datasource.ro1.username=ro1
datasource.ro1.password=ro1
datasource.ro1.url=jdbc:postgresql://localhost:8433/ro1
datasource.ro1.driver=org.postgresql.Driver
datasource.ro2.username=ro2
datasource.ro2.password=ro2
datasource.ro2.url=jdbc:postgresql://localhost:8434/ro2
datasource.ro2.driver=org.postgresql.Driver
datasource.ro3.username=ro3
datasource.ro3.password=ro3
datasource.ro3.url=jdbc:postgresql://localhost:8435/ro3
datasource.ro3.driver=org.postgresql.Driver
.. B U T .. not having the default datasource mapped to a superclass via @DbName("db") <<<---- WRONG
@MappedSuperclass
public class DbMainBaseEntity extends Model { @DbName("ro1")
@MappedSuperclass
public class DbRo1BaseEntity extends Model { @DbName("ro2")
@MappedSuperclass
public class DbRo2BaseEntity extends Model { @DbName("ro3")
@MappedSuperclass
public class DbRo3BaseEntity extends Model { What ebean does when misconfiguredUnfortunatley this configuration issue will cause ebean to behave unexpectetly for generating and executing migrations Generated MigrationsActual behaviourGenerates create statements for every entity of every configured datasource:
Expected behaviourGenerates create statements only for the main datasource:
Executed MigrationsActual behaviour:Migrating every configured datasource
Expected behaviour:Migrating only the default datasource:
Trying to fix configuration issue does not seem to helpSo fixing the datasource in my superclass to match the default datasource like this .. @DbName("main") <<<---- CORRECT!
@MappedSuperclass
public class DbMainBaseEntity extends Model { ... will now generate the expected migration SQL ...
... BUT it will still try to run the migration against every configured datasource with that same
Application startup now fails, as the tables ´ro1_city
Overall ExpectationI would have expected to at least get an error that the default datasource is not defined or mapped properly. Feel free to checkout and test the example provided. I hope i did not make more stupid mistakes ^^ |
Beta Was this translation helpful? Give feedback.
-
Hello everyone,
i am struggling to setup ebean with multiple Databases, where only for one of those Databases should be actively migrated by ebean. All other databases should not be migrated and are only there for manual querying in readonly mode.
I thought the migrations are only run for/against the "default" database, but it runs for every database.
So thats how my
application.properties
look like:Sadly, running the migration creation will generate Migrations for all created entities, regardless of which Database they are from.
Is there something i am missing or is ebean designed to just run migrations against every database that is configured without any optout capability ?
Cheers,
Aleks
Beta Was this translation helpful? Give feedback.
All reactions