Skip to content

Commit

Permalink
Activating environments profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
galexandrade committed Mar 16, 2018
1 parent 01d8404 commit d86ac68
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 0 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile 'org.webjars:bootstrap:3.3.7-1'
compile 'org.webjars:jquery:3.3.1'
compile group: 'mysql', name: 'mysql-connector-java'
compileOnly('org.projectlombok:lombok')
runtime('org.springframework.boot:spring-boot-devtools')
runtime('com.h2database:h2')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

bootRun {
jvmArgs = ["-Dspring.profiles.active=default"]
}

jacocoTestReport {
reports {
xml.enabled true
Expand Down
12 changes: 12 additions & 0 deletions guru_database_create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
create table category (id bigint not null auto_increment, description varchar(255), primary key (id)) engine=InnoDB;
create table ingredient (id bigint not null auto_increment, amount decimal(19,2), description varchar(255), recipe_id bigint, uom_id bigint, primary key (id)) engine=InnoDB;
create table notes (id bigint not null auto_increment, recipe_notes longtext, recipe_id bigint, primary key (id)) engine=InnoDB;
create table recipe (id bigint not null auto_increment, cook_time integer, description varchar(255), difficulty varchar(255), directions longtext, image longblob, prep_time integer, servings integer, source varchar(255), url varchar(255), notes_id bigint, primary key (id)) engine=InnoDB;
create table recipe_category (recipe_id bigint not null, category_id bigint not null, primary key (recipe_id, category_id)) engine=InnoDB;
create table unit_of_measure (id bigint not null auto_increment, description varchar(255), primary key (id)) engine=InnoDB;
alter table ingredient add constraint FKj0s4ywmqqqw4h5iommigh5yja foreign key (recipe_id) references recipe (id);
alter table ingredient add constraint FK6iv5l89qmitedn5m2a71kta2t foreign key (uom_id) references unit_of_measure (id);
alter table notes add constraint FKdbfsiv21ocsbt63sd6fg0t3c8 foreign key (recipe_id) references recipe (id);
alter table recipe add constraint FK37al6kcbdasgfnut9xokktie9 foreign key (notes_id) references notes (id);
alter table recipe_category add constraint FKqsi87i8d4qqdehlv2eiwvpwb foreign key (category_id) references category (id);
alter table recipe_category add constraint FKcqlqnvfyarhieewfeayk3v25v foreign key (recipe_id) references recipe (id);
95 changes: 95 additions & 0 deletions src/main/java/com/recipe/app/bootstrap/BootStrapMySQL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.recipe.app.bootstrap;

import com.recipe.app.domain.Category;
import com.recipe.app.domain.UnitOfMeasure;
import com.recipe.app.repositories.CategoryRepository;
import com.recipe.app.repositories.UnitOfMeasureRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
* Created by alex.andrade on 15/03/2018.
*/
@Slf4j
@Component
@Profile({"dev", "prod"})
public class BootStrapMySQL implements ApplicationListener<ContextRefreshedEvent> {
private final CategoryRepository categoryRepository;
private final UnitOfMeasureRepository unitOfMeasureRepository;

public BootStrapMySQL(CategoryRepository categoryRepository,
UnitOfMeasureRepository unitOfMeasureRepository) {
this.categoryRepository = categoryRepository;
this.unitOfMeasureRepository = unitOfMeasureRepository;
}

@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

if (categoryRepository.count() == 0L){
log.debug("Loading Categories");
loadCategories();
}

if (unitOfMeasureRepository.count() == 0L){
log.debug("Loading UOMs");
loadUom();
}
}

private void loadCategories(){
Category cat1 = new Category();
cat1.setDescription("American");
categoryRepository.save(cat1);

Category cat2 = new Category();
cat2.setDescription("Italian");
categoryRepository.save(cat2);

Category cat3 = new Category();
cat3.setDescription("Mexican");
categoryRepository.save(cat3);

Category cat4 = new Category();
cat4.setDescription("Fast Food");
categoryRepository.save(cat4);
}

private void loadUom(){
UnitOfMeasure uom1 = new UnitOfMeasure();
uom1.setDescription("Teaspoon");
unitOfMeasureRepository.save(uom1);

UnitOfMeasure uom2 = new UnitOfMeasure();
uom2.setDescription("Tablespoon");
unitOfMeasureRepository.save(uom2);

UnitOfMeasure uom3 = new UnitOfMeasure();
uom3.setDescription("Cup");
unitOfMeasureRepository.save(uom3);

UnitOfMeasure uom4 = new UnitOfMeasure();
uom4.setDescription("Pinch");
unitOfMeasureRepository.save(uom4);

UnitOfMeasure uom5 = new UnitOfMeasure();
uom5.setDescription("Ounce");
unitOfMeasureRepository.save(uom5);

UnitOfMeasure uom6 = new UnitOfMeasure();
uom6.setDescription("Each");
unitOfMeasureRepository.save(uom6);

UnitOfMeasure uom7 = new UnitOfMeasure();
uom7.setDescription("Pint");
unitOfMeasureRepository.save(uom7);

UnitOfMeasure uom8 = new UnitOfMeasure();
uom8.setDescription("Dash");
unitOfMeasureRepository.save(uom8);
}

}
2 changes: 2 additions & 0 deletions src/main/java/com/recipe/app/bootstrap/RecipeBootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.recipe.app.repositories.UnitOfMeasureRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -20,6 +21,7 @@
*/
@Slf4j
@Component
@Profile("default")
public class RecipeBootstrap implements ApplicationListener<ContextRefreshedEvent> {
private final CategoryRepository categoryRepository;
private final RecipeRepository recipeRepository;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application-default.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.datasource.platform=h2
22 changes: 22 additions & 0 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
spring:
datasource:
url: jdbc:mysql://192.168.99.100:3306/sfg_dev
username: sfg_dev_user
password: sfg_dev_user
platform: mysql
jpa:
hibernate:
ddl-auto: validate
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
database: mysql
show-sql: true

#####Automatic load the database schema
# properties:
# javax:
# persistence:
# schema-generation:
# create-source: metadata
# scripts:
# action: create
# create-target: guru_database_create.sql
12 changes: 12 additions & 0 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
spring:
datasource:
url: jdbc:mysql://192.168.99.100:3306/sfg_prod
username: sfg_prod_user
password: sfg_prod_user
platform: mysql
jpa:
hibernate:
ddl-auto: validate
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
database: mysql
show-sql: false
File renamed without changes.

0 comments on commit d86ac68

Please sign in to comment.