First integrate Darwin to your project and create create.sql
SQL script
in a platform specific folder. Let's have new component adam
with initial version 1.0
:
Create a file on your classpath /META-INF/adam/sql/mysql/create.sql
with example content:
create table ADAM (
id integer not null auto_increment,
preciousContent varchar(255) not null,
constraint CNPK_ADAM primary key (id)
) engine=InnoDB;
Create Darwin bean in your Spring configuration:
@Configuration
@Import(DarwinConfiguration.class)
public class YourNameOfConfigFile {
@Bean
public Darwin adamSchema(ApplicationContext applicationContext) {
return new DarwinBuilder(applicationContext, "adam", "1.0")
.withResourcePath("classpath:/META-INF/adam/sql/")
.build();
}
}
... and you're done - Darwin creates ADAM
table for you once Spring tries to instantiate adamSchema
bean. If you
require ADAM
table to exist in order to be used in other bean setup you should force Darwin
bean to instantiate first
using Spring DependsOn
annotation.
Create a file on your classpath /META-INF/adam/sql/mysql/patch_1.1.sql
with example content:
alter table ADAM add column additionalContent varchar(64) null;
Update version in your Darwin bean declaration in your Spring configuration:
@Configuration
@Import(DarwinConfiguration.class)
public class YourNameOfConfigFile {
@Bean
public Darwin adamSchema(ApplicationContext applicationContext) {
return new DarwinBuilder(applicationContext, "adam", "1.1")
.withResourcePath("classpath:/META-INF/adam/sql/")
.build();
}
}
... and you're done - once Darwin
bean is instantiated again, it finds out, that version of the model is 1.1
, but
there is older version (1.0
) in the database and it automatically finds and applies patch_1.1.sql
script. When it's
finished it writes down that database now contains model with version 1.1
.