diff --git a/butane_core/src/db/mod.rs b/butane_core/src/db/mod.rs index 2a14970a..755e8271 100644 --- a/butane_core/src/db/mod.rs +++ b/butane_core/src/db/mod.rs @@ -107,7 +107,7 @@ impl ConnectionSpec { pub fn save(&self, path: &Path) -> Result<()> { let path = conn_complete_if_dir(path); let mut f = fs::File::create(path)?; - f.write_all(serde_json::to_string(self)?.as_bytes()) + f.write_all(serde_json::to_string_pretty(self)?.as_bytes()) .map_err(|e| e.into()) } /// Load a previously saved connection spec diff --git a/butane_core/src/migrations/fsmigrations.rs b/butane_core/src/migrations/fsmigrations.rs index 8480fdb0..80ab1365 100644 --- a/butane_core/src/migrations/fsmigrations.rs +++ b/butane_core/src/migrations/fsmigrations.rs @@ -68,7 +68,7 @@ impl FsMigration { } fn write_info(&self, info: &MigrationInfo) -> Result<()> { - self.write_contents("info.json", serde_json::to_string(info)?.as_bytes()) + self.write_contents("info.json", serde_json::to_string_pretty(info)?.as_bytes()) } fn write_sql(&self, name: &str, sql: &str) -> Result<()> { @@ -92,10 +92,16 @@ impl FsMigration { fn write_contents(&self, fname: &str, contents: &[u8]) -> Result<()> { self.ensure_dir()?; let path = self.root.join(fname); + let mut contents: Vec = contents.into(); + if contents[contents.len() - 1] != b'\n' { + contents.push(b'\n'); + } self.fs .write(&path)? - .write_all(contents) - .map_err(|e| e.into()) + .write_all(&contents) + .map_err(>::into)?; + + Ok(()) } fn ensure_dir(&self) -> Result<()> { @@ -124,7 +130,7 @@ impl MigrationMut for FsMigration { fn write_table(&mut self, table: &ATable) -> Result<()> { self.write_contents( &format!("{}.table", table.name), - serde_json::to_string(table)?.as_bytes(), + serde_json::to_string_pretty(table)?.as_bytes(), ) } @@ -269,7 +275,7 @@ impl FsMigrations { fn save_state(&mut self, state: &MigrationsState) -> Result<()> { let path = self.root.join("state.json"); let mut f = self.fs.write(&path)?; - f.write_all(serde_json::to_string(state)?.as_bytes()) + f.write_all(serde_json::to_string_pretty(state)?.as_bytes()) .map_err(|e| e.into()) } /// Detach the latest migration from the list of migrations, diff --git a/butane_core/tests/many.rs b/butane_core/tests/many.rs new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/butane_core/tests/many.rs @@ -0,0 +1 @@ + diff --git a/examples/getting_started/.butane/migrations/current/Blog.table b/examples/getting_started/.butane/migrations/current/Blog.table index 13a5d316..6dbe6909 100644 --- a/examples/getting_started/.butane/migrations/current/Blog.table +++ b/examples/getting_started/.butane/migrations/current/Blog.table @@ -1 +1,31 @@ -{"name":"Blog","columns":[{"name":"id","sqltype":{"KnownId":{"Ty":"BigInt"}},"nullable":false,"pk":true,"auto":true,"unique":false,"default":null},{"name":"name","sqltype":{"KnownId":{"Ty":"Text"}},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null}]} \ No newline at end of file +{ + "name": "Blog", + "columns": [ + { + "name": "id", + "sqltype": { + "KnownId": { + "Ty": "BigInt" + } + }, + "nullable": false, + "pk": true, + "auto": true, + "unique": false, + "default": null + }, + { + "name": "name", + "sqltype": { + "KnownId": { + "Ty": "Text" + } + }, + "nullable": false, + "pk": false, + "auto": false, + "unique": false, + "default": null + } + ] +} diff --git a/examples/getting_started/.butane/migrations/current/Post.table b/examples/getting_started/.butane/migrations/current/Post.table index 6b030e1d..fc624a57 100644 --- a/examples/getting_started/.butane/migrations/current/Post.table +++ b/examples/getting_started/.butane/migrations/current/Post.table @@ -1 +1,99 @@ -{"name":"Post","columns":[{"name":"id","sqltype":{"KnownId":{"Ty":"Int"}},"nullable":false,"pk":true,"auto":true,"unique":false,"default":null},{"name":"title","sqltype":{"KnownId":{"Ty":"Text"}},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null},{"name":"body","sqltype":{"KnownId":{"Ty":"Text"}},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null},{"name":"published","sqltype":{"KnownId":{"Ty":"Bool"}},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null},{"name":"blog","sqltype":{"Deferred":"PK:Blog"},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null,"reference":{"Deferred":{"Deferred":"PK:Blog"}}},{"name":"byline","sqltype":{"KnownId":{"Ty":"Text"}},"nullable":true,"pk":false,"auto":false,"unique":false,"default":null},{"name":"likes","sqltype":{"KnownId":{"Ty":"Int"}},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null}]} \ No newline at end of file +{ + "name": "Post", + "columns": [ + { + "name": "id", + "sqltype": { + "KnownId": { + "Ty": "Int" + } + }, + "nullable": false, + "pk": true, + "auto": true, + "unique": false, + "default": null + }, + { + "name": "title", + "sqltype": { + "KnownId": { + "Ty": "Text" + } + }, + "nullable": false, + "pk": false, + "auto": false, + "unique": false, + "default": null + }, + { + "name": "body", + "sqltype": { + "KnownId": { + "Ty": "Text" + } + }, + "nullable": false, + "pk": false, + "auto": false, + "unique": false, + "default": null + }, + { + "name": "published", + "sqltype": { + "KnownId": { + "Ty": "Bool" + } + }, + "nullable": false, + "pk": false, + "auto": false, + "unique": false, + "default": null + }, + { + "name": "blog", + "sqltype": { + "Deferred": "PK:Blog" + }, + "nullable": false, + "pk": false, + "auto": false, + "unique": false, + "default": null, + "reference": { + "Deferred": { + "Deferred": "PK:Blog" + } + } + }, + { + "name": "byline", + "sqltype": { + "KnownId": { + "Ty": "Text" + } + }, + "nullable": true, + "pk": false, + "auto": false, + "unique": false, + "default": null + }, + { + "name": "likes", + "sqltype": { + "KnownId": { + "Ty": "Int" + } + }, + "nullable": false, + "pk": false, + "auto": false, + "unique": false, + "default": null + } + ] +} diff --git a/examples/getting_started/.butane/migrations/current/Post_tags_Many.table b/examples/getting_started/.butane/migrations/current/Post_tags_Many.table index 6c5704f7..36afc8ce 100644 --- a/examples/getting_started/.butane/migrations/current/Post_tags_Many.table +++ b/examples/getting_started/.butane/migrations/current/Post_tags_Many.table @@ -1 +1,40 @@ -{"name":"Post_tags_Many","columns":[{"name":"owner","sqltype":{"KnownId":{"Ty":"Int"}},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null,"reference":{"Literal":{"table_name":"Post","column_name":"id"}}},{"name":"has","sqltype":{"Deferred":"PK:Tag"},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null,"reference":{"Deferred":{"Deferred":"PK:Tag"}}}]} \ No newline at end of file +{ + "name": "Post_tags_Many", + "columns": [ + { + "name": "owner", + "sqltype": { + "KnownId": { + "Ty": "Int" + } + }, + "nullable": false, + "pk": false, + "auto": false, + "unique": false, + "default": null, + "reference": { + "Literal": { + "table_name": "Post", + "column_name": "id" + } + } + }, + { + "name": "has", + "sqltype": { + "Deferred": "PK:Tag" + }, + "nullable": false, + "pk": false, + "auto": false, + "unique": false, + "default": null, + "reference": { + "Deferred": { + "Deferred": "PK:Tag" + } + } + } + ] +} diff --git a/examples/getting_started/.butane/migrations/current/Tag.table b/examples/getting_started/.butane/migrations/current/Tag.table index a5117916..784abd8b 100644 --- a/examples/getting_started/.butane/migrations/current/Tag.table +++ b/examples/getting_started/.butane/migrations/current/Tag.table @@ -1 +1,18 @@ -{"name":"Tag","columns":[{"name":"tag","sqltype":{"KnownId":{"Ty":"Text"}},"nullable":false,"pk":true,"auto":false,"unique":false,"default":null}]} \ No newline at end of file +{ + "name": "Tag", + "columns": [ + { + "name": "tag", + "sqltype": { + "KnownId": { + "Ty": "Text" + } + }, + "nullable": false, + "pk": true, + "auto": false, + "unique": false, + "default": null + } + ] +}