aboutsummaryrefslogtreecommitdiff
path: root/src/cli
diff options
context:
space:
mode:
Diffstat (limited to 'src/cli')
-rw-r--r--src/cli/make_migrations.rs44
-rw-r--r--src/cli/migrate.rs11
-rw-r--r--src/cli/mod.rs2
-rw-r--r--src/cli/run_server.rs2
4 files changed, 37 insertions, 22 deletions
diff --git a/src/cli/make_migrations.rs b/src/cli/make_migrations.rs
index 83e760c..b0cd341 100644
--- a/src/cli/make_migrations.rs
+++ b/src/cli/make_migrations.rs
@@ -5,14 +5,13 @@ use std::path::PathBuf;
use quote::{quote, ToTokens};
use structopt::StructOpt;
-use crate::{Settings, UrlMap, db::backend::Connectable};
-use crate::db::migration::{Migration, DatabaseChange, CreateModelOption};
+use crate::db::migration::{CreateModelOption, DatabaseChange, Migration};
use crate::db::models::{Field, ModelMeta};
+use crate::{db::backend::Connectable, Settings, UrlMap};
#[derive(StructOpt)]
/// Generate migrations
-pub struct MakeMigrations {
-}
+pub struct MakeMigrations {}
#[derive(Debug)]
struct AppTablesState {
@@ -35,7 +34,7 @@ impl AppTablesState {
Field::CharField { name, max_length: None } => quote! { Field::CharField { name: #name, max_length: None } },
Field::DateTimeField { name } => quote! { Field::DateTimeField { name: #name } },
-
+
Field::IntField { name } => quote! { Field::IntField { name: #name } },
}
}).collect::<Vec<_>>();
@@ -58,7 +57,12 @@ 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() });
+ db.insert(
+ model.name,
+ TableState {
+ fields: model.fields.into(),
+ },
+ );
}
Self { db }
}
@@ -70,7 +74,11 @@ impl From<&[Migration]> for AppTablesState {
for migration in migrations {
for change in migration.changes {
match change {
- DatabaseChange::CreateModel { name, fields, options } => {
+ DatabaseChange::CreateModel {
+ name,
+ fields,
+ options,
+ } => {
if db.contains_key(name) {
if options.contains(&CreateModelOption::IfNotExist) {
continue;
@@ -78,7 +86,12 @@ impl From<&[Migration]> for AppTablesState {
panic!("double-created table {}", name);
}
}
- db.insert(*name, TableState { fields: (*fields).into() });
+ db.insert(
+ *name,
+ TableState {
+ fields: (*fields).into(),
+ },
+ );
}
}
}
@@ -92,10 +105,17 @@ impl MakeMigrations {
for app in settings.installed_apps {
let expected_table_state = AppTablesState::from(app.models);
let actual_table_state = AppTablesState::from(app.migrations);
- let next_id = app.migrations.iter().map(|m| m.id).max().map_or(1, |x| x + 1);
+ let next_id = app
+ .migrations
+ .iter()
+ .map(|m| m.id)
+ .max()
+ .map_or(1, |x| x + 1);
let name = "auto"; // TODO names
let changes = actual_table_state.changes_to(expected_table_state);
- if changes.is_empty() { continue; }
+ if changes.is_empty() {
+ continue;
+ }
let migration = quote! {
Migration {
id: #next_id,
@@ -111,9 +131,7 @@ impl MakeMigrations {
};
let file_name = format!("m_{:04}_{}.rs", next_id, name);
let file_text = file.into_token_stream().to_string();
- let app_folder = app.name.split("::")
- .skip(1)
- .collect::<PathBuf>();
+ let app_folder = app.name.split("::").skip(1).collect::<PathBuf>();
// TODO don't explode if running in a weird place
let file_path = PathBuf::from("src")
.join(app_folder)
diff --git a/src/cli/migrate.rs b/src/cli/migrate.rs
index a6efbb5..acc59d3 100644
--- a/src/cli/migrate.rs
+++ b/src/cli/migrate.rs
@@ -1,14 +1,13 @@
use structopt::StructOpt;
-use crate::{Settings, UrlMap};
use crate::db::backend::Connectable;
-use crate::db::migration::{DatabaseChange, CreateModelOption};
+use crate::db::migration::{CreateModelOption, DatabaseChange};
use crate::db::models::Field;
+use crate::{Settings, UrlMap};
#[derive(StructOpt)]
/// Perform database migrations
-pub struct Migrate {
-}
+pub struct Migrate {}
const CREATE_MIGRATION_TABLE: DatabaseChange = DatabaseChange::CreateModel {
name: "Migration",
@@ -24,9 +23,7 @@ const CREATE_MIGRATION_TABLE: DatabaseChange = DatabaseChange::CreateModel {
name: "migration_name",
max_length: None,
},
- Field::DateTimeField {
- name: "applied_at",
- }
+ Field::DateTimeField { name: "applied_at" },
],
options: &[CreateModelOption::IfNotExist],
};
diff --git a/src/cli/mod.rs b/src/cli/mod.rs
index 851812f..5bff8ae 100644
--- a/src/cli/mod.rs
+++ b/src/cli/mod.rs
@@ -1,6 +1,6 @@
use structopt::StructOpt;
-use crate::{Settings, UrlMap, db::backend::Connectable};
+use crate::{db::backend::Connectable, Settings, UrlMap};
mod make_migrations;
mod migrate;
diff --git a/src/cli/run_server.rs b/src/cli/run_server.rs
index 66e6fe1..d20ac14 100644
--- a/src/cli/run_server.rs
+++ b/src/cli/run_server.rs
@@ -1,6 +1,6 @@
use structopt::StructOpt;
-use crate::{Settings, UrlMap, db::backend::Connectable};
+use crate::{db::backend::Connectable, Settings, UrlMap};
#[derive(StructOpt)]
/// Run an HTTP server