-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
myndla-api: Add initial database setup
- Loading branch information
Showing
9 changed files
with
171 additions
and
4 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
myndla-api/src/main/resources/no/ndla/myndlaapi/db/migration/V1__user_tables.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE my_ndla_users ( | ||
id BIGSERIAL PRIMARY KEY, | ||
feide_id text, | ||
document jsonb | ||
); |
35 changes: 35 additions & 0 deletions
35
myndla-api/src/main/resources/no/ndla/myndlaapi/db/migration/V2__folder_tables.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
CREATE TABLE resources ( | ||
id uuid NOT NULL DEFAULT gen_random_uuid(), | ||
feide_id text NULL, | ||
"path" text NULL, | ||
resource_type text NULL, | ||
created timestamp NULL, | ||
"document" jsonb NULL, | ||
CONSTRAINT resources_pkey PRIMARY KEY (id) | ||
); | ||
|
||
CREATE TABLE folders ( | ||
id uuid NOT NULL DEFAULT gen_random_uuid(), | ||
parent_id uuid NULL, | ||
feide_id text NULL, | ||
"rank" int4 NULL, | ||
"name" text NOT NULL, | ||
status text NOT NULL, | ||
created timestamp NOT NULL DEFAULT now(), | ||
updated timestamp NOT NULL DEFAULT now(), | ||
shared timestamp NULL, | ||
description text NULL, | ||
CONSTRAINT folders_pkey PRIMARY KEY (id), | ||
CONSTRAINT fk_parent_id FOREIGN KEY (parent_id) REFERENCES folders(id) | ||
); | ||
CREATE INDEX folders_feide_id_idx ON folders USING btree (feide_id); | ||
CREATE INDEX folders_parent_id_idx ON folders USING btree (parent_id); | ||
|
||
CREATE TABLE folder_resources ( | ||
folder_id uuid NOT NULL, | ||
resource_id uuid NOT NULL, | ||
"rank" int4 NULL, | ||
CONSTRAINT folder_resource_pkey PRIMARY KEY (folder_id, resource_id), | ||
CONSTRAINT folder_resources_folder_id_fkey FOREIGN KEY (folder_id) REFERENCES folders(id) ON DELETE CASCADE, | ||
CONSTRAINT folder_resources_resource_id_fkey FOREIGN KEY (resource_id) REFERENCES resources(id) ON DELETE CASCADE | ||
); |
33 changes: 33 additions & 0 deletions
33
myndla-api/src/main/resources/no/ndla/myndlaapi/db/migration/V3__forum_tables.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
CREATE TABLE categories ( | ||
id BIGSERIAL PRIMARY KEY, | ||
title text, | ||
description text | ||
); | ||
|
||
CREATE TABLE topics ( | ||
id BIGSERIAL PRIMARY KEY, | ||
title text, | ||
content text, | ||
category_id BIGINT REFERENCES categories(id) | ||
); | ||
|
||
CREATE TABLE posts ( | ||
id BIGSERIAL PRIMARY KEY, | ||
title text, | ||
content text, | ||
topic_id BIGINT REFERENCES topics(id) | ||
); | ||
|
||
CREATE TABLE topic_follows ( | ||
id BIGSERIAL PRIMARY KEY, | ||
user_id BIGINT REFERENCES my_ndla_users(id), | ||
topic_id BIGINT REFERENCES topics(id) | ||
); | ||
|
||
CREATE TABLE notifications ( | ||
id BIGSERIAL PRIMARY KEY, | ||
user_id BIGINT REFERENCES my_ndla_users(id), | ||
post_id BIGINT REFERENCES posts(id), | ||
topic_id BIGINT REFERENCES topics(id), | ||
is_read BOOLEAN DEFAULT FALSE | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
myndla-api/src/main/scala/no/ndla/myndlaapi/DBMigrator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Part of NDLA article-api. | ||
* Copyright (C) 2016 NDLA | ||
* | ||
* See LICENSE | ||
* | ||
*/ | ||
|
||
package no.ndla.myndlaapi | ||
|
||
import no.ndla.myndlaapi.integration.DataSource | ||
import org.flywaydb.core.Flyway | ||
|
||
trait DBMigrator { | ||
this: Props with DataSource => | ||
val migrator: DBMigrator | ||
|
||
class DBMigrator { | ||
|
||
def migrate(): Unit = { | ||
val flyway = Flyway | ||
.configure() | ||
.javaMigrations( | ||
) | ||
.locations("no/ndla/myndlaapi/db/migration") | ||
.dataSource(dataSource) | ||
// Seems like flyway uses datasource.getConnection().getScheme() which is null if the scheme does not exist. | ||
// Therefore we simply override it with dataSource.getScheme. | ||
// https://github.com/flyway/flyway/issues/2182 | ||
.schemas(dataSource.getSchema) | ||
.load() | ||
flyway.migrate(): Unit | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
myndla-api/src/main/scala/no/ndla/myndlaapi/integration/DataSource.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Part of NDLA article-api. | ||
* Copyright (C) 2016 NDLA | ||
* | ||
* See LICENSE | ||
* | ||
*/ | ||
|
||
package no.ndla.myndlaapi.integration | ||
|
||
import com.zaxxer.hikari.{HikariConfig, HikariDataSource} | ||
import no.ndla.myndlaapi.Props | ||
import scalikejdbc.{ConnectionPool, DataSourceConnectionPool} | ||
|
||
trait DataSource { | ||
this: Props => | ||
val dataSource: HikariDataSource | ||
|
||
object DataSource { | ||
import props._ | ||
|
||
def getHikariDataSource: HikariDataSource = { | ||
val dataSourceConfig = new HikariConfig() | ||
dataSourceConfig.setUsername(MetaUserName) | ||
dataSourceConfig.setPassword(MetaPassword) | ||
dataSourceConfig.setJdbcUrl(s"jdbc:postgresql://$MetaServer:$MetaPort/$MetaResource") | ||
dataSourceConfig.setDriverClassName("org.postgresql.Driver") | ||
dataSourceConfig.setSchema(MetaSchema) | ||
dataSourceConfig.setMaximumPoolSize(MetaMaxConnections) | ||
new HikariDataSource(dataSourceConfig) | ||
} | ||
|
||
def connectToDatabase(): Unit = ConnectionPool.singleton(new DataSourceConnectionPool(dataSource)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters