1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
use std::collections::HashMap;
use structopt::StructOpt;
use crate::{Settings, UrlMap, db::backend::Connectable};
use crate::db::migration::{Migration, DatabaseChange, CreateModelOption};
use crate::db::models::{Field, ModelMeta};
#[derive(StructOpt)]
/// Generate migrations
pub struct MakeMigrations {
}
#[derive(Debug)]
struct AppTablesState {
db: HashMap<&'static str, TableState>,
}
#[derive(Debug)]
struct TableState {
fields: Vec<Field>,
}
impl From<&[ModelMeta]> for AppTablesState {
fn from(models: &[ModelMeta]) -> Self {
let mut db = HashMap::new();
for model in models {
db.insert(model.name, TableState { fields: model.fields.into() });
}
Self { db }
}
}
impl From<&[Migration]> for AppTablesState {
fn from(migrations: &[Migration]) -> Self {
let mut db = HashMap::new();
for migration in migrations {
for change in migration.changes {
match change {
DatabaseChange::CreateModel { name, fields, options } => {
if db.contains_key(name) {
if options.contains(&CreateModelOption::IfNotExist) {
continue;
} else {
panic!("double-created table {}", name);
}
}
db.insert(*name, TableState { fields: (*fields).into() });
}
}
}
}
Self { db }
}
}
impl MakeMigrations {
pub fn execute(self, _urls: UrlMap, settings: Settings<impl Connectable>) {
for app in settings.installed_apps {
let expected_table_state = AppTablesState::from(app.models);
let actual_table_state = AppTablesState::from(app.migrations);
dbg!(expected_table_state, actual_table_state);
todo!();
}
}
}
|