medal/db_apply_migrations.rs
1/* medal *\
2 * Copyright (C) 2022 Bundesweite Informatikwettbewerbe, Robert Czechowski *
3 * *
4 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero *
5 * General Public License as published by the Free Software Foundation, either version 3 of the License, or (at *
6 * your option) any later version. *
7 * *
8 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the *
9 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public *
10 * License for more details. *
11 * *
12 * You should have received a copy of the GNU Affero General Public License along with this program. If not, see *
13\* <http://www.gnu.org/licenses/>. */
14
15use std::fs;
16use std::io::Read;
17
18use db_conn::MedalConnection;
19
20pub fn test<C: MedalConnection>(conn: &mut C) {
21 let mut paths: Vec<_> =
22 fs::read_dir(format!("migrations/{}", conn.dbtype())).unwrap()
23 .map(|r| r.unwrap())
24 .filter(|r| {
25 r.path().display().to_string().ends_with(".sql")
26 })
27 .collect();
28 paths.sort_by_key(|dir| dir.path());
29
30 for path in paths {
31 let filename = path.file_name().into_string().unwrap();
32 if !conn.migration_already_applied(&filename) {
33 println!("Found: {}. Applying …", path.path().display());
34 let mut file = fs::File::open(path.path()).unwrap();
35 let mut contents = String::new();
36 file.read_to_string(&mut contents).unwrap();
37 conn.apply_migration(&filename, &contents);
38 }
39 /*else { // TODO: Show in high debug level only
40 println!("Found: {}. Already applied", path.path().display());
41 }*/
42 }
43}