From 978f1be26185c27f8fbb2ffbf91e967fc5d3076f Mon Sep 17 00:00:00 2001
From: Tamas Bunth <tamas.bunth@collabora.co.uk>
Date: Mon, 5 Mar 2018 22:45:13 +0100
Subject: [PATCH] Use schema.sql instead of ddl-auto

---
 src/main/kotlin/mobildata/web/UserHandler.kt | 10 +++---
 src/main/resources/application.properties    |  6 ++--
 src/main/resources/schema.sql                | 38 ++++++++++++++++++++
 3 files changed, 46 insertions(+), 8 deletions(-)
 create mode 100644 src/main/resources/schema.sql

diff --git a/src/main/kotlin/mobildata/web/UserHandler.kt b/src/main/kotlin/mobildata/web/UserHandler.kt
index e1fb137..ae54146 100644
--- a/src/main/kotlin/mobildata/web/UserHandler.kt
+++ b/src/main/kotlin/mobildata/web/UserHandler.kt
@@ -20,14 +20,14 @@ class UserHandler(val repository: UserRepository,
                   val service: UserService) {
 
     @PostMapping("/api/users/login")
-    fun login(@Valid @RequestBody login: Login, errors: Errors): Any {
+    fun login(@Valid @RequestBody login: Login, errors: Errors): User {
         InvalidRequest.check(errors)
 
         try {
             service.login(login)?.let {
-                return view(service.updateToken(it))
+                return service.updateToken(it)
             }
-            return ForbiddenRequestException()
+            throw ForbiddenRequestException() // FIXME
         } catch (e: InvalidLoginException) {
             val errors = org.springframework.validation.BindException(this, "")
             errors.addError(FieldError("", e.field, e.error))
@@ -57,7 +57,7 @@ class UserHandler(val repository: UserRepository,
 
     @ApiKeySecured
     @PutMapping("/api/user")
-    fun updateUser(@Valid @RequestBody user: UpdateUser, errors: Errors): Any {
+    fun updateUser(@Valid @RequestBody user: UpdateUser, errors: Errors): User {
         InvalidRequest.check(errors)
 
         val currentUser = service.currentUser()
@@ -88,7 +88,7 @@ class UserHandler(val repository: UserRepository,
             u.token = service.newToken(u)
         }
 
-        return view(repository.save(u))
+        return repository.save(u)
     }
 
     private fun checkUserAvailability(errors: BindException, email: String?, username: String?) {
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 583c14d..bce5f66 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -2,15 +2,15 @@ spring.datasource.url = jdbc:postgresql://localhost:5432/mobildata
 spring.datasource.driverClassName=org.postgresql.Driver
 spring.datasource.username=postgres
 spring.datasource.password=postgres
+spring.datasource.initialization-mode=always
 
 spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
 
-# Every time the modell changes, there should be a run with "generate-ddl=true
-
+# The following is commented out, because currently schema.sql is in use.
 ## create schema automatically. It can be more fine-grained with the commented stuff below.
 # spring.jpa.generate-ddl=true
 ## recreate the database on startup
-spring.jpa.hibernate.ddl-auto=create-drop
+# spring.jpa.hibernate.ddl-auto=create-drop
 
 # restart server:
 # sudo service postgresql restart
diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql
new file mode 100644
index 0000000..9fde62b
--- /dev/null
+++ b/src/main/resources/schema.sql
@@ -0,0 +1,38 @@
+alter table if exists "user_user"
+  drop constraint FKl0s7rdd5k82i0xbgm2igjaetj;
+
+alter table if exists "user_user"
+  drop constraint FK8w8vorfaxyv2c87ga05luvkls;
+
+drop table if exists "user" cascade;
+
+drop table if exists "user_user" cascade;
+
+drop sequence if exists hibernate_sequence;
+create sequence hibernate_sequence start 1 increment 1;
+
+create table "user" (
+  id int8 not null,
+  bio varchar(255),
+  email varchar(255),
+  image varchar(255),
+  password varchar(255),
+  token varchar(255),
+  username varchar(255),
+  primary key (id)
+);
+
+create table "user_user" (
+  "user_id" int8 not null,
+  "follows_id" int8 not null
+);
+
+alter table "user_user"
+  add constraint FKl0s7rdd5k82i0xbgm2igjaetj
+foreign key ("follows_id")
+references "user";
+
+alter table "user_user"
+  add constraint FK8w8vorfaxyv2c87ga05luvkls
+foreign key ("user_id")
+references "user";
\ No newline at end of file
-- 
GitLab