Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilise embed format #161

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions butane_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,20 @@ pub fn embed(base_dir: &Path) -> Result<()> {
copy_migration(&m, &mut new_m)?;
mem_ms.add_migration(new_m)?;
}
let json = serde_json::to_string(&mem_ms)?;
let json = serde_json::to_string_pretty(&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}
Loading
Loading