diff --git a/.drone.yml b/.drone.yml index f192654..767e355 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,3 +1,19 @@ +# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes. +# Copyright (C) 2022 Dominic Grimm + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + --- kind: pipeline type: docker diff --git a/.example.env b/.example.env index c38cd7f..87525cc 100644 --- a/.example.env +++ b/.example.env @@ -1,3 +1,19 @@ +# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes. +# Copyright (C) 2022 Dominic Grimm + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + # General URL= diff --git a/config/nginx/nginx.conf b/config/nginx/nginx.conf index 8dcf12f..e7dc28d 100644 --- a/config/nginx/nginx.conf +++ b/config/nginx/nginx.conf @@ -1,3 +1,17 @@ +# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes. +# Copyright (C) 2022 Dominic Grimm + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . events { } diff --git a/docker-compose.yml b/docker-compose.yml index 71ba61b..027a437 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,3 +1,19 @@ +# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes. +# Copyright (C) 2022 Dominic Grimm + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + version: "3" services: diff --git a/docker/backend/.dockerignore b/docker/backend/.dockerignore index 06163ad..3bdd927 100644 --- a/docker/backend/.dockerignore +++ b/docker/backend/.dockerignore @@ -1,3 +1,19 @@ +# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes. +# Copyright (C) 2022 Dominic Grimm + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + /docs/ /lib/ /bin/ diff --git a/docker/backend/.editorconfig b/docker/backend/.editorconfig index 163eb75..a31eb2b 100644 --- a/docker/backend/.editorconfig +++ b/docker/backend/.editorconfig @@ -1,3 +1,19 @@ +# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes. +# Copyright (C) 2022 Dominic Grimm + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + root = true [*.cr] diff --git a/docker/backend/.gitignore b/docker/backend/.gitignore index 7c3e445..0599719 100644 --- a/docker/backend/.gitignore +++ b/docker/backend/.gitignore @@ -1,3 +1,19 @@ +# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes. +# Copyright (C) 2022 Dominic Grimm + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + /docs/ /lib/ /bin/ diff --git a/docker/backend/shard.lock b/docker/backend/shard.lock index 0cefeae..d643463 100644 --- a/docker/backend/shard.lock +++ b/docker/backend/shard.lock @@ -96,6 +96,10 @@ shards: git: https://github.com/stefanwille/crystal-redis.git version: 2.8.3 + retriable: + git: https://github.com/sija/retriable.cr.git + version: 0.2.4 + router: git: https://github.com/tbrand/router.cr.git version: 0.2.8 diff --git a/docker/backend/shard.yml b/docker/backend/shard.yml index a37e96b..94d1140 100644 --- a/docker/backend/shard.yml +++ b/docker/backend/shard.yml @@ -1,3 +1,19 @@ +# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes. +# Copyright (C) 2022 Dominic Grimm + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + name: backend version: 0.1.0 @@ -48,3 +64,5 @@ dependencies: git: https://git.dergrimm.net/dergrimm/ldap_escape.git shard: github: maiha/shard.cr + retriable: + github: Sija/retriable.cr diff --git a/docker/backend/src/backend.cr b/docker/backend/src/backend.cr index 9efb84a..71f704f 100644 --- a/docker/backend/src/backend.cr +++ b/docker/backend/src/backend.cr @@ -14,6 +14,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +require "retriable/core_ext/kernel" + require "./backend/*" # Base module diff --git a/docker/backend/src/backend/db.cr b/docker/backend/src/backend/db.cr index 11e440d..532a9d9 100644 --- a/docker/backend/src/backend/db.cr +++ b/docker/backend/src/backend/db.cr @@ -22,6 +22,23 @@ require "./db/*" module Backend # Database model definitions module Db + extend self + Granite::Connections << Granite::Adapter::Pg.new(name: "pg", url: Backend.config.db.url) + + # Checks if database schema is up to date + def schema_up_to_date_compare : Int32? + migrations = Dir["db/migrations/*.sql"].map { |f| Path[f].basename }.sort! + latest_migration = migrations.try(&.last.match(/\d+/).try(&.to_a.first.not_nil!.to_u64)) + if latest_migration.nil? + return nil + end + + begin + MicrateDbVersion.order(tstamp: :desc).limit(1).assembler.select.run.first.version_id <=> latest_migration + rescue PQ::PQError + nil + end + end end end diff --git a/docker/backend/src/backend/db/micrate_db_version.cr b/docker/backend/src/backend/db/micrate_db_version.cr new file mode 100644 index 0000000..c5183b3 --- /dev/null +++ b/docker/backend/src/backend/db/micrate_db_version.cr @@ -0,0 +1,29 @@ +# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes. +# Copyright (C) 2022 Dominic Grimm + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +module Backend + module Db + # Micrate DB migrator model / configuration + class MicrateDbVersion < Granite::Base + table micrate_db_version + + column id : Int32, primary: true + column version_id : Int64 + column is_applied : Bool + column tstamp : Time? + end + end +end diff --git a/docker/backend/src/backend/run.cr b/docker/backend/src/backend/run.cr index dc53f50..b650a46 100644 --- a/docker/backend/src/backend/run.cr +++ b/docker/backend/src/backend/run.cr @@ -22,21 +22,37 @@ module Backend {% if flag?(:development) %} Log.warn { "Backend is running in development mode! Do not use this in production!" } {% end %} + + Log.info { "Checking if DB schema is up to date..." } + retry(backoff: false, base_interval: 10.seconds, multiplier: 1.0) do + case retry(on: DB::ConnectionRefused, backoff: false) do + Db.schema_up_to_date_compare + end + when nil + Log.fatal { "No database schema is applied. Please run `bash scripts/micrate.sh up` urgently!" } + raise Exception.new + when -1 + Log.warn { "Database schema is not up to date. Please run `bash scripts/micrate.sh up`." } + when 0 + Log.info { "Database schema is up to date." } + else + Log.warn { "Database schema is maybe up to date but not consistent. Please run `bash scripts/micrate.sh up` to be safe." } + end + end + Log.info { "Starting services..." } - channel = Channel(Nil).new(SERVICES.size) - SERVICES.each do |service| spawn do service.call channel.send(nil) end end - SERVICES.size.times do channel.receive end Fiber.yield + Log.info { "Backend services started." } end end diff --git a/scripts/backend.sh b/scripts/backend.sh index a4d78e7..5217e48 100644 --- a/scripts/backend.sh +++ b/scripts/backend.sh @@ -1,3 +1,19 @@ #!/usr/bin/env bash +# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes. +# Copyright (C) 2022 Dominic Grimm + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + docker-compose exec backend ./bin/backend "$@" diff --git a/scripts/micrate.sh b/scripts/micrate.sh index 5057671..e442e1c 100644 --- a/scripts/micrate.sh +++ b/scripts/micrate.sh @@ -1,3 +1,19 @@ #!/usr/bin/env bash +# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes. +# Copyright (C) 2022 Dominic Grimm + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + docker-compose exec backend ./bin/micrate "$@"