Skip to content

Commit

Permalink
fix: migration should iterate files from current directory (where it …
Browse files Browse the repository at this point in the history
…runs)
  • Loading branch information
GoranBrkuljan committed Jan 15, 2025
1 parent d396423 commit 7f26e88
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 533 deletions.
14 changes: 12 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ jobs:
run: |
sudo sh -c "echo 2097152 >> /proc/sys/fs/aio-max-nr"
docker compose -f docker/docker-compose.yml up -d --wait
- name: Create Keyspace
- name: Create Charybdis Keyspace
run: |
docker exec scylla1 cqlsh -e \
"CREATE KEYSPACE charybdis WITH replication = {'class': 'NetworkTopologyStrategy', 'replication_factor': 2};"
- name: Create Reddit Example Keyspace
run: |
docker exec scylla1 cqlsh -e \
"CREATE KEYSPACE reddit_example WITH replication = {'class': 'NetworkTopologyStrategy', 'replication_factor': 2};"
- name: Cache Dependencies
uses: actions/cache@v4
with:
Expand Down Expand Up @@ -53,6 +57,12 @@ jobs:
- name: Install Charybdis Migration Tool
run: cargo install --path charybdis-migrate --force
- name: Run Charybdis Migration Tool
run: migrate --keyspace charybdis --host 127.0.0.1:9042 --drop-and-replace
run: |
cd charybdis
migrate --keyspace charybdis --host 127.0.0.1:9042 --drop-and-replace
cd ..
cd examples/actix-web/reddit-api/
migrate --keyspace reddit_example --host 127.0.0.1:9042 --drop-and-replace
cd ../../..
- name: Run tests
run: cargo test --verbose
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion charybdis-migrate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "charybdis-migrate"
rust-version = "1.75.0"
version = "0.7.11"
version = "0.7.12"
edition = "2021"
description = "Automatic Migration Tool for Charybdis ORM"
repository = "https://github.com/nodecosmos/charybdis"
Expand Down
18 changes: 5 additions & 13 deletions charybdis-migrate/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ pub struct Args {
#[arg(long, default_value = None)]
pub key: Option<String>,

#[arg(skip = get_project_root())]
pub project_root: String,
#[arg(skip = get_current_dir())]
pub current_dir: String,
}

impl Default for Args {
Expand All @@ -60,21 +60,13 @@ impl Default for Args {
ca: None,
cert: None,
key: None,
project_root: get_project_root(),
current_dir: get_current_dir(),
}
}
}

pub(crate) fn get_project_root() -> String {
pub(crate) fn get_current_dir() -> String {
let path = env::current_dir().expect("Failed to find project root: Could not get current directory");
let path_ancestors = path.as_path().ancestors();

for p in path_ancestors {
let has_cargo = read_dir(p).unwrap().any(|p| p.unwrap().file_name() == *"Cargo.lock");
if has_cargo {
return PathBuf::from(p).to_str().unwrap().to_string();
}
}

panic!("Failed to find project root: Ran out of places to find Cargo.toml");
PathBuf::from(path).to_str().unwrap().to_string()
}
6 changes: 3 additions & 3 deletions charybdis-migrate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl MigrationBuilder {
}

let current_db_schema = DbSchema::new(session, self.args.keyspace.clone()).await;
let current_code_schema = CodeSchema::new(&self.args.project_root);
let current_code_schema = CodeSchema::new(&self.args.current_dir);

let migration = Migration::new(current_db_schema, current_code_schema, session, self.args);

Expand All @@ -46,8 +46,8 @@ impl MigrationBuilder {
self
}

pub fn project_root(mut self, project_root: String) -> Self {
self.args.project_root = project_root;
pub fn current_dir(mut self, current_dir: String) -> Self {
self.args.current_dir = current_dir;
self
}

Expand Down
2 changes: 1 addition & 1 deletion charybdis-migrate/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<'a> Migration<'a> {
pub async fn write_schema_to_json(&self) {
DbSchema::new(self.session, self.args.keyspace.clone())
.await
.write_schema_to_json(&self.args.project_root);
.write_schema_to_json(&self.args.current_dir);
}

async fn run_udts(&self) {
Expand Down
10 changes: 5 additions & 5 deletions charybdis-parser/src/schema/code_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ pub struct CodeSchema {
}

impl CodeSchema {
pub fn new(project_root: &String) -> CodeSchema {
pub fn new(current_dir: &String) -> CodeSchema {
let mut current_code_schema = CodeSchema {
tables: SchemaObjects::new(),
udts: SchemaObjects::new(),
materialized_views: SchemaObjects::new(),
};

current_code_schema.get_models_from_code(project_root);
current_code_schema.get_models_from_code(current_dir);

current_code_schema
}

pub fn get_models_from_code(&mut self, project_root: &String) {
let project_root: PathBuf = PathBuf::from(project_root);
let walker = WalkDir::new(&project_root)
pub fn get_models_from_code(&mut self, current_dir: &String) {
let current_dir: PathBuf = PathBuf::from(current_dir);
let walker = WalkDir::new(&current_dir)
.into_iter()
.filter_entry(|e| !(e.file_type().is_dir() && e.file_name() == "target"));

Expand Down
6 changes: 3 additions & 3 deletions charybdis-parser/src/schema/db_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl DbSchema {
&mut self,
view_name: &String,
session: &Session,
) -> Result<(), crate::errors::DbSchemaParserError> {
) -> Result<(), DbSchemaParserError> {
let cql = r#"
SELECT column_name
FROM system_schema.columns
Expand Down Expand Up @@ -379,10 +379,10 @@ impl DbSchema {
})
}

pub fn write_schema_to_json(&self, project_root: &str) {
pub fn write_schema_to_json(&self, current_dir: &str) {
let json = self.get_current_schema_as_json();

let path = project_root.to_string() + "/current_schema.json";
let path = current_dir.to_string() + "/current_schema.json";

std::fs::write(path, json).unwrap_or_else(|e| {
panic!("Error writing schema to json: {}", e);
Expand Down
Loading

0 comments on commit 7f26e88

Please sign in to comment.