Added migrations not up to date warning

This commit is contained in:
Dominic Grimm 2022-02-13 19:12:44 +01:00
parent 2eb5e16282
commit dc4ca94fc6
15 changed files with 231 additions and 3 deletions

View file

@ -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/>.
---
kind: pipeline
type: docker

View file

@ -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
URL=

View file

@ -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 {
}

View file

@ -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"
services:

View file

@ -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/
/lib/
/bin/

View file

@ -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
[*.cr]

View file

@ -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/
/lib/
/bin/

View file

@ -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

View file

@ -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
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

View file

@ -14,6 +14,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
require "retriable/core_ext/kernel"
require "./backend/*"
# Base module

View file

@ -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

View 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

View file

@ -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

View file

@ -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 <https://www.gnu.org/licenses/>.
docker-compose exec backend ./bin/backend "$@"

View file

@ -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 <https://www.gnu.org/licenses/>.
docker-compose exec backend ./bin/micrate "$@"