Merge pull request 'Warn migration not up to date' (#57) from migrator-warn-not-latest into main
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: mentorenwahl/mentorenwahl#57
This commit is contained in:
commit
d583ca58ff
15 changed files with 216 additions and 4 deletions
16
.example.env
16
.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 <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
# General
|
# General
|
||||||
URL=
|
URL=
|
||||||
|
|
||||||
|
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
events {
|
events {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
version: "3"
|
version: "3"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
|
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
/docs/
|
/docs/
|
||||||
/lib/
|
/lib/
|
||||||
/bin/
|
/bin/
|
||||||
|
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
root = true
|
root = true
|
||||||
|
|
||||||
[*.cr]
|
[*.cr]
|
||||||
|
|
16
docker/backend/.gitignore
vendored
16
docker/backend/.gitignore
vendored
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
/docs/
|
/docs/
|
||||||
/lib/
|
/lib/
|
||||||
/bin/
|
/bin/
|
||||||
|
|
|
@ -37,7 +37,7 @@ FROM alpine as runner
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
RUN adduser -S backend -u 1001
|
RUN adduser -S backend -u 1001
|
||||||
COPY --from=builder /app/bin ./bin
|
COPY --from=builder /app/bin ./bin
|
||||||
COPY ./db ./db
|
COPY --chown=root ./db ./db
|
||||||
USER backend
|
USER backend
|
||||||
EXPOSE 80
|
EXPOSE 80
|
||||||
ENTRYPOINT [ "./bin/backend" ]
|
ENTRYPOINT [ "./bin/backend" ]
|
||||||
|
|
|
@ -96,6 +96,10 @@ shards:
|
||||||
git: https://github.com/stefanwille/crystal-redis.git
|
git: https://github.com/stefanwille/crystal-redis.git
|
||||||
version: 2.8.3
|
version: 2.8.3
|
||||||
|
|
||||||
|
retriable:
|
||||||
|
git: https://github.com/sija/retriable.cr.git
|
||||||
|
version: 0.2.4
|
||||||
|
|
||||||
router:
|
router:
|
||||||
git: https://github.com/tbrand/router.cr.git
|
git: https://github.com/tbrand/router.cr.git
|
||||||
version: 0.2.8
|
version: 0.2.8
|
||||||
|
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
name: backend
|
name: backend
|
||||||
version: 0.1.0
|
version: 0.1.0
|
||||||
|
|
||||||
|
@ -48,3 +64,5 @@ dependencies:
|
||||||
git: https://git.dergrimm.net/dergrimm/ldap_escape.git
|
git: https://git.dergrimm.net/dergrimm/ldap_escape.git
|
||||||
shard:
|
shard:
|
||||||
github: maiha/shard.cr
|
github: maiha/shard.cr
|
||||||
|
retriable:
|
||||||
|
github: Sija/retriable.cr
|
||||||
|
|
|
@ -14,6 +14,8 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
require "retriable/core_ext/kernel"
|
||||||
|
|
||||||
require "./backend/*"
|
require "./backend/*"
|
||||||
|
|
||||||
# Base module
|
# Base module
|
||||||
|
|
|
@ -22,6 +22,23 @@ require "./db/*"
|
||||||
module Backend
|
module Backend
|
||||||
# Database model definitions
|
# Database model definitions
|
||||||
module Db
|
module Db
|
||||||
|
extend self
|
||||||
|
|
||||||
Granite::Connections << Granite::Adapter::Pg.new(name: "pg", url: Backend.config.db.url)
|
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
|
||||||
end
|
end
|
||||||
|
|
29
docker/backend/src/backend/db/micrate_db_version.cr
Normal file
29
docker/backend/src/backend/db/micrate_db_version.cr
Normal file
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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
|
|
@ -22,21 +22,37 @@ module Backend
|
||||||
{% if flag?(:development) %}
|
{% if flag?(:development) %}
|
||||||
Log.warn { "Backend is running in development mode! Do not use this in production!" }
|
Log.warn { "Backend is running in development mode! Do not use this in production!" }
|
||||||
{% end %}
|
{% 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..." }
|
Log.info { "Starting services..." }
|
||||||
|
|
||||||
channel = Channel(Nil).new(SERVICES.size)
|
channel = Channel(Nil).new(SERVICES.size)
|
||||||
|
|
||||||
SERVICES.each do |service|
|
SERVICES.each do |service|
|
||||||
spawn do
|
spawn do
|
||||||
service.call
|
service.call
|
||||||
channel.send(nil)
|
channel.send(nil)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
SERVICES.size.times do
|
SERVICES.size.times do
|
||||||
channel.receive
|
channel.receive
|
||||||
end
|
end
|
||||||
Fiber.yield
|
Fiber.yield
|
||||||
|
|
||||||
Log.info { "Backend services started." }
|
Log.info { "Backend services started." }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,19 @@
|
||||||
#!/usr/bin/env bash
|
#!/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 <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
docker-compose exec backend ./bin/backend "$@"
|
docker-compose exec backend ./bin/backend "$@"
|
||||||
|
|
|
@ -1,3 +1,19 @@
|
||||||
#!/usr/bin/env bash
|
#!/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 <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
docker-compose exec backend ./bin/micrate "$@"
|
docker-compose exec backend ./bin/micrate "$@"
|
||||||
|
|
Loading…
Reference in a new issue