mentorenwahl/docker/backend/src/backend/db.cr
Dominic Grimm 3a19d1d8db
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
Fixed file license headers
2022-03-07 14:06:02 +01:00

45 lines
1.5 KiB
Crystal

# 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/>.
require "granite"
require "granite/adapter/pg"
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