Skip to content

Commit

Permalink
Stabilise embed format
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb committed Nov 7, 2023
1 parent aa1cb42 commit 393f177
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 22 deletions.
10 changes: 7 additions & 3 deletions butane_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,17 @@ pub fn embed(base_dir: &Path) -> Result<()> {
let json = serde_json::to_string(&mem_ms)?;

let src = format!(
"
use butane::migrations::MemMigrations;
"//! Butane migrations embedded in Rust.
use std::result::Result;
use butane::migrations::MemMigrations;
/// Load the butane migrations embedded in Rust.
pub fn get_migrations() -> Result<MemMigrations, butane::Error> {{
let json = r#\"{json}\"#;
MemMigrations::from_json(json)
}}"
}}
"
);

let mut f = std::fs::File::create(path)?;
Expand Down
4 changes: 0 additions & 4 deletions butane_cli/tests/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@ fn embed() {
.join("../examples/getting_started/.butane");
assert!(example_dir.exists());
butane_cli::embed(&example_dir).unwrap();
for filename in ["../src/butane_migrations.rs", "clistate.json"] {
let path = example_dir.join(filename);
std::fs::remove_file(path).unwrap();
}
}
16 changes: 8 additions & 8 deletions butane_core/src/migrations/adb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::{Error, Result, SqlType, SqlVal};
use serde::{de::Deserializer, de::Visitor, ser::Serializer, Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::{BTreeSet, HashMap};
use std::collections::{BTreeMap, BTreeSet, HashMap};

/// Suffix added to [`crate::many::Many`] tables.
pub const MANY_SUFFIX: &str = "_Many";
Expand Down Expand Up @@ -145,14 +145,14 @@ impl TypeResolver {
/// Abstract representation of a database schema.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct ADB {
tables: HashMap<String, ATable>,
extra_types: HashMap<TypeKey, DeferredSqlType>,
tables: BTreeMap<String, ATable>,
extra_types: BTreeMap<TypeKey, DeferredSqlType>,
}
impl ADB {
pub fn new() -> Self {
ADB {
tables: HashMap::new(),
extra_types: HashMap::new(),
tables: BTreeMap::new(),
extra_types: BTreeMap::new(),
}
}
pub fn tables(&self) -> impl Iterator<Item = &ATable> {
Expand All @@ -161,7 +161,7 @@ impl ADB {
pub fn get_table<'a>(&'a self, name: &str) -> Option<&'a ATable> {
self.tables.get(name)
}
pub fn types(&self) -> &HashMap<TypeKey, DeferredSqlType> {
pub fn types(&self) -> &BTreeMap<TypeKey, DeferredSqlType> {
&self.extra_types
}
pub fn replace_table(&mut self, table: ATable) {
Expand Down Expand Up @@ -460,8 +460,8 @@ impl AColumn {
/// Resolve a column constraints target.
fn resolve_reference_target(
&mut self,
extra_types: &HashMap<TypeKey, DeferredSqlType>,
tables: &HashMap<String, ATable>,
extra_types: &BTreeMap<TypeKey, DeferredSqlType>,
tables: &BTreeMap<String, ATable>,
) {
match &self.reference {
None | Some(ARef::Literal(_)) => {}
Expand Down
14 changes: 7 additions & 7 deletions butane_core/src/migrations/memmigrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use crate::query::BoolExpr;
use crate::{ConnectionMethods, DataObject, Result};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::collections::HashMap;
use std::collections::BTreeMap;

/// A migration stored in memory.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MemMigration {
name: String,
db: ADB,
from: Option<String>,
up: HashMap<String, String>,
down: HashMap<String, String>,
up: BTreeMap<String, String>,
down: BTreeMap<String, String>,
}

impl MemMigration {
Expand All @@ -22,8 +22,8 @@ impl MemMigration {
name,
db: ADB::new(),
from: None,
up: HashMap::new(),
down: HashMap::new(),
up: BTreeMap::new(),
down: BTreeMap::new(),
}
}
}
Expand Down Expand Up @@ -93,15 +93,15 @@ impl MigrationMut for MemMigration {
/// A collection of migrations stored in memory.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MemMigrations {
migrations: HashMap<String, MemMigration>,
migrations: BTreeMap<String, MemMigration>,
current: MemMigration,
latest: Option<String>,
}

impl MemMigrations {
pub fn new() -> Self {
MemMigrations {
migrations: HashMap::new(),
migrations: BTreeMap::new(),
current: MemMigration::new("current".to_string()),
latest: None,
}
Expand Down
1 change: 1 addition & 0 deletions examples/getting_started/.butane/clistate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"embedded":true}
10 changes: 10 additions & 0 deletions examples/getting_started/src/butane_migrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//! Butane migrations embedded in Rust.
use std::result::Result;

use butane::migrations::MemMigrations;

/// Load the butane migrations embedded in Rust.
pub fn get_migrations() -> Result<MemMigrations, butane::Error> {
let json = r#"{"migrations":{"20201229_144636751_init":{"name":"20201229_144636751_init","db":{"tables":{"Blog":{"name":"Blog","columns":[{"name":"id","sqltype":{"Known":"BigInt"},"nullable":false,"pk":true,"auto":true,"unique":false,"default":null},{"name":"name","sqltype":{"Known":"Text"},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null}]},"Post":{"name":"Post","columns":[{"name":"id","sqltype":{"Known":"Int"},"nullable":false,"pk":true,"auto":true,"unique":false,"default":null},{"name":"title","sqltype":{"Known":"Text"},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null},{"name":"body","sqltype":{"Known":"Text"},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null},{"name":"published","sqltype":{"Known":"Bool"},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null},{"name":"blog","sqltype":{"Known":"BigInt"},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null},{"name":"byline","sqltype":{"Known":"Text"},"nullable":true,"pk":false,"auto":false,"unique":false,"default":null}]},"Post_tags_Many":{"name":"Post_tags_Many","columns":[{"name":"owner","sqltype":{"Known":"Int"},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null},{"name":"has","sqltype":{"Known":"Text"},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null}]},"Tag":{"name":"Tag","columns":[{"name":"tag","sqltype":{"Known":"Text"},"nullable":false,"pk":true,"auto":false,"unique":false,"default":null}]}},"extra_types":{}},"from":null,"up":{"sqlite":"CREATE TABLE Tag (\ntag TEXT NOT NULL PRIMARY KEY\n);\nCREATE TABLE Post_tags_Many (\nowner INTEGER NOT NULL,\nhas TEXT NOT NULL\n);\nCREATE TABLE Post (\nid INTEGER NOT NULL PRIMARY KEY,\ntitle TEXT NOT NULL,\nbody TEXT NOT NULL,\npublished INTEGER NOT NULL,\nblog INTEGER NOT NULL,\nbyline TEXT \n);\nCREATE TABLE Blog (\nid INTEGER NOT NULL PRIMARY KEY,\nname TEXT NOT NULL\n);\nCREATE TABLE butane_migrations (\nname TEXT NOT NULL PRIMARY KEY\n);"},"down":{"sqlite":"DROP TABLE Blog;\nDROP TABLE Post_tags_Many;\nDROP TABLE Post;\nDROP TABLE Tag;"}},"20201229_171630604_likes":{"name":"20201229_171630604_likes","db":{"tables":{"Blog":{"name":"Blog","columns":[{"name":"id","sqltype":{"Known":"BigInt"},"nullable":false,"pk":true,"auto":true,"unique":false,"default":null},{"name":"name","sqltype":{"Known":"Text"},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null}]},"Post":{"name":"Post","columns":[{"name":"id","sqltype":{"Known":"Int"},"nullable":false,"pk":true,"auto":true,"unique":false,"default":null},{"name":"title","sqltype":{"Known":"Text"},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null},{"name":"body","sqltype":{"Known":"Text"},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null},{"name":"published","sqltype":{"Known":"Bool"},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null},{"name":"blog","sqltype":{"Known":"BigInt"},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null},{"name":"byline","sqltype":{"Known":"Text"},"nullable":true,"pk":false,"auto":false,"unique":false,"default":null},{"name":"likes","sqltype":{"Known":"Int"},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null}]},"Post_tags_Many":{"name":"Post_tags_Many","columns":[{"name":"owner","sqltype":{"Known":"Int"},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null},{"name":"has","sqltype":{"Known":"Text"},"nullable":false,"pk":false,"auto":false,"unique":false,"default":null}]},"Tag":{"name":"Tag","columns":[{"name":"tag","sqltype":{"Known":"Text"},"nullable":false,"pk":true,"auto":false,"unique":false,"default":null}]}},"extra_types":{}},"from":"20201229_144636751_init","up":{"sqlite":"ALTER TABLE Post ADD COLUMN likes INTEGER NOT NULL DEFAULT 0;"},"down":{"sqlite":"CREATE TABLE Post__butane_tmp (\nid INTEGER NOT NULL PRIMARY KEY,\ntitle TEXT NOT NULL,\nbody TEXT NOT NULL,\npublished INTEGER NOT NULL,\nblog INTEGER NOT NULL,\nbyline TEXT \n);\nINSERT INTO Post__butane_tmp SELECT id, title, body, published, blog, byline FROM Post;\nDROP TABLE Post;\nALTER TABLE Post__butane_tmp RENAME TO Post;"}}},"current":{"name":"current","db":{"tables":{},"extra_types":{}},"from":null,"up":{},"down":{}},"latest":"20201229_171630604_likes"}"#;
MemMigrations::from_json(json)
}
1 change: 1 addition & 0 deletions examples/getting_started/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Common helpers for the getting_started example CLI.
pub mod butane_migrations;
pub mod models;

use butane::db::{Connection, ConnectionSpec};
Expand Down

0 comments on commit 393f177

Please sign in to comment.