Skip to content

Commit 11b851a

Browse files
author
Rajeev Kumar Singh
committed
flyway migrations
1 parent da4dfd3 commit 11b851a

File tree

5 files changed

+79
-5
lines changed

5 files changed

+79
-5
lines changed

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ I've written a complete tutorial series for this application on The CalliCoder B
5353
mvn package
5454
java -jar target/polls-0.0.1-SNAPSHOT.jar
5555
```
56-
5. **Add the default Roles**
56+
5. ~~**Add the default Roles**~~ (It's not needed now. I've added flyway migrations)
5757

5858
The spring boot app uses role based authorization powered by spring security. Please execute the following sql queries in the database to insert the `USER` and `ADMIN` roles.
5959

polling-app-server/pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@
5858
</dependency>
5959

6060

61+
<!-- Flyway database management -->
6162
<dependency>
62-
<groupId>org.springframework.boot</groupId>
63-
<artifactId>spring-boot-starter-test</artifactId>
64-
<scope>test</scope>
63+
<groupId>org.flywaydb</groupId>
64+
<artifactId>flyway-core</artifactId>
6565
</dependency>
66+
6667
<dependency>
6768
<groupId>org.springframework.security</groupId>
6869
<artifactId>spring-security-test</artifactId>

polling-app-server/src/main/resources/application.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ spring.datasource.password= callicoder
1010
## Hibernate Properties
1111
# The SQL dialect makes Hibernate generate better SQL for the chosen database
1212
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
13-
spring.jpa.hibernate.ddl-auto = update
13+
spring.jpa.hibernate.ddl-auto = validate
1414

1515
## Hibernate Logging
1616
logging.level.org.hibernate.SQL= DEBUG
1717

18+
## Flyway Properties
19+
spring.flyway.baseline-on-migrate=true
1820

1921
## Jackson Properties
2022
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS= false
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
CREATE TABLE `users` (
2+
`id` bigint(20) NOT NULL AUTO_INCREMENT,
3+
`name` varchar(40) NOT NULL,
4+
`username` varchar(15) NOT NULL,
5+
`email` varchar(40) NOT NULL,
6+
`password` varchar(100) NOT NULL,
7+
`created_at` datetime DEFAULT NULL,
8+
`updated_at` datetime DEFAULT NULL,
9+
PRIMARY KEY (`id`),
10+
UNIQUE KEY `uk_users_username` (`username`),
11+
UNIQUE KEY `uk_users_email` (`email`)
12+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
13+
14+
15+
CREATE TABLE `roles` (
16+
`id` bigint(20) NOT NULL AUTO_INCREMENT,
17+
`name` varchar(60) NOT NULL,
18+
PRIMARY KEY (`id`),
19+
UNIQUE KEY `uk_roles_name` (`name`)
20+
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
21+
22+
23+
CREATE TABLE `user_roles` (
24+
`user_id` bigint(20) NOT NULL,
25+
`role_id` bigint(20) NOT NULL,
26+
PRIMARY KEY (`user_id`,`role_id`),
27+
KEY `fk_user_roles_role_id` (`role_id`),
28+
CONSTRAINT `fk_user_roles_role_id` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`),
29+
CONSTRAINT `fk_user_roles_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
30+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
31+
32+
33+
CREATE TABLE `polls` (
34+
`id` bigint(20) NOT NULL AUTO_INCREMENT,
35+
`question` varchar(140) NOT NULL,
36+
`expiration_date_time` datetime NOT NULL,
37+
`created_at` datetime DEFAULT NULL,
38+
`updated_at` datetime DEFAULT NULL,
39+
`created_by` bigint(20) DEFAULT NULL,
40+
`updated_by` bigint(20) DEFAULT NULL,
41+
PRIMARY KEY (`id`)
42+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
43+
44+
45+
CREATE TABLE `choices` (
46+
`id` bigint(20) NOT NULL AUTO_INCREMENT,
47+
`text` varchar(40) NOT NULL,
48+
`poll_id` bigint(20) NOT NULL,
49+
PRIMARY KEY (`id`),
50+
KEY `fk_choices_poll_id` (`poll_id`),
51+
CONSTRAINT `fk_choices_poll_id` FOREIGN KEY (`poll_id`) REFERENCES `polls` (`id`)
52+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
53+
54+
55+
CREATE TABLE `votes` (
56+
`id` bigint(20) NOT NULL AUTO_INCREMENT,
57+
`user_id` bigint(20) NOT NULL,
58+
`poll_id` bigint(20) NOT NULL,
59+
`choice_id` bigint(20) NOT NULL,
60+
`created_at` datetime DEFAULT NULL,
61+
`updated_at` datetime DEFAULT NULL,
62+
PRIMARY KEY (`id`),
63+
KEY `fk_votes_user_id` (`user_id`),
64+
KEY `fk_votes_poll_id` (`poll_id`),
65+
KEY `fk_votes_choice_id` (`choice_id`),
66+
CONSTRAINT `fk_votes_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
67+
CONSTRAINT `fk_votes_poll_id` FOREIGN KEY (`poll_id`) REFERENCES `polls` (`id`),
68+
CONSTRAINT `fk_votes_choice_id` FOREIGN KEY (`choice_id`) REFERENCES `choices` (`id`)
69+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
INSERT IGNORE INTO roles(name) VALUES('ROLE_USER');
2+
INSERT IGNORE INTO roles(name) VALUES('ROLE_ADMIN');

0 commit comments

Comments
 (0)