Skip to content

Commit 9253e74

Browse files
committedFeb 3, 2019
add default roles on startup
1 parent d2a51d7 commit 9253e74

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed
 

‎Readme.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ I've written a complete tutorial series for this application on The CalliCoder B
5151
mvn package
5252
java -jar target/polls-0.0.1-SNAPSHOT.jar
5353
```
54-
5. **Add the default Roles**
54+
5. **Default Roles**
5555

56-
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.
56+
The spring boot app uses role based authorization powered by spring security. To add the default roles in the database, I have added the following script in `src/main/resources/data.sql` file. Spring boot will automatically execute this script on startup -
5757

5858
```sql
59-
INSERT INTO roles(name) VALUES('ROLE_USER');
60-
INSERT INTO roles(name) VALUES('ROLE_ADMIN');
59+
INSERT IGNORE INTO roles(name) VALUES('ROLE_USER');
60+
INSERT IGNORE INTO roles(name) VALUES('ROLE_ADMIN');
6161
```
6262

6363
Any new user who signs up to the app is assigned the `ROLE_USER` by default.

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

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ spring.jpa.hibernate.ddl-auto = update
1616
## Hibernate Logging
1717
logging.level.org.hibernate.SQL= DEBUG
1818

19+
# Initialize the datasource with available DDL and DML scripts
20+
spring.datasource.initialization-mode=always
21+
1922
## Jackson Properties
2023
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS= false
2124
spring.jackson.time-zone= UTC
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');

‎polling-app-server/src/main/resources/db/migration/V1__schema.sql

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ CREATE TABLE `users` (
44
`username` varchar(15) NOT NULL,
55
`email` varchar(40) NOT NULL,
66
`password` varchar(100) NOT NULL,
7-
`created_at` datetime DEFAULT NULL,
8-
`updated_at` datetime DEFAULT NULL,
7+
`created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
8+
`updated_at` timestamp DEFAULT CURRENT_TIMESTAMP,
99
PRIMARY KEY (`id`),
1010
UNIQUE KEY `uk_users_username` (`username`),
1111
UNIQUE KEY `uk_users_email` (`email`)
@@ -34,8 +34,8 @@ CREATE TABLE `polls` (
3434
`id` bigint(20) NOT NULL AUTO_INCREMENT,
3535
`question` varchar(140) NOT NULL,
3636
`expiration_date_time` datetime NOT NULL,
37-
`created_at` datetime DEFAULT NULL,
38-
`updated_at` datetime DEFAULT NULL,
37+
`created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
38+
`updated_at` timestamp DEFAULT CURRENT_TIMESTAMP,
3939
`created_by` bigint(20) DEFAULT NULL,
4040
`updated_by` bigint(20) DEFAULT NULL,
4141
PRIMARY KEY (`id`)
@@ -57,8 +57,8 @@ CREATE TABLE `votes` (
5757
`user_id` bigint(20) NOT NULL,
5858
`poll_id` bigint(20) NOT NULL,
5959
`choice_id` bigint(20) NOT NULL,
60-
`created_at` datetime DEFAULT NULL,
61-
`updated_at` datetime DEFAULT NULL,
60+
`created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
61+
`updated_at` timestamp DEFAULT CURRENT_TIMESTAMP,
6262
PRIMARY KEY (`id`),
6363
KEY `fk_votes_user_id` (`user_id`),
6464
KEY `fk_votes_poll_id` (`poll_id`),

‎polling-app-server/src/main/resources/default-roles.sql

-2
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.