From 7f26e887846253726b093b6685a345071db9fdb5 Mon Sep 17 00:00:00 2001 From: Goran Brkuljan Date: Wed, 15 Jan 2025 20:20:40 +0100 Subject: [PATCH] fix: migration should iterate files from current directory (where it runs) --- .github/workflows/build.yml | 14 +- Cargo.lock | 14 +- charybdis-migrate/Cargo.toml | 2 +- charybdis-migrate/src/args.rs | 18 +- charybdis-migrate/src/lib.rs | 6 +- charybdis-migrate/src/migration.rs | 2 +- charybdis-parser/src/schema/code_schema.rs | 10 +- charybdis-parser/src/schema/db_schema.rs | 6 +- current_schema.json | 496 ------------------ .../actix-web/reddit-api/src/models/post.rs | 7 +- 10 files changed, 42 insertions(+), 533 deletions(-) delete mode 100644 current_schema.json diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6880bdd..27564d8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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: @@ -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 diff --git a/Cargo.lock b/Cargo.lock index d30c004..c313735 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -466,7 +466,7 @@ name = "charybdis" version = "0.7.11" dependencies = [ "bigdecimal", - "charybdis-migrate 0.7.11", + "charybdis-migrate 0.7.12", "charybdis_macros 0.7.11", "chrono", "colored", @@ -485,7 +485,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "218e5df24a1126d32f77664244aa589ea5bca70ed32e51528a3cc45148ec6fd6" dependencies = [ "bigdecimal", - "charybdis-migrate 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", + "charybdis-migrate 0.7.11", "charybdis_macros 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", "chrono", "colored", @@ -499,8 +499,10 @@ dependencies = [ [[package]] name = "charybdis-migrate" version = "0.7.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e94b49003b4bc27397b8b2fb71677366c99df75c592365188f057ea43f16873f" dependencies = [ - "charybdis_parser 0.7.11", + "charybdis_parser 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", "clap", "colored", "openssl", @@ -512,11 +514,9 @@ dependencies = [ [[package]] name = "charybdis-migrate" -version = "0.7.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e94b49003b4bc27397b8b2fb71677366c99df75c592365188f057ea43f16873f" +version = "0.7.12" dependencies = [ - "charybdis_parser 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", + "charybdis_parser 0.7.11", "clap", "colored", "openssl", diff --git a/charybdis-migrate/Cargo.toml b/charybdis-migrate/Cargo.toml index 0149025..4c68adf 100644 --- a/charybdis-migrate/Cargo.toml +++ b/charybdis-migrate/Cargo.toml @@ -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" diff --git a/charybdis-migrate/src/args.rs b/charybdis-migrate/src/args.rs index e7ca325..49781da 100644 --- a/charybdis-migrate/src/args.rs +++ b/charybdis-migrate/src/args.rs @@ -43,8 +43,8 @@ pub struct Args { #[arg(long, default_value = None)] pub key: Option, - #[arg(skip = get_project_root())] - pub project_root: String, + #[arg(skip = get_current_dir())] + pub current_dir: String, } impl Default for Args { @@ -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() } diff --git a/charybdis-migrate/src/lib.rs b/charybdis-migrate/src/lib.rs index 6426c15..112fc6a 100644 --- a/charybdis-migrate/src/lib.rs +++ b/charybdis-migrate/src/lib.rs @@ -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); @@ -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 } diff --git a/charybdis-migrate/src/migration.rs b/charybdis-migrate/src/migration.rs index b1db9d1..705e015 100644 --- a/charybdis-migrate/src/migration.rs +++ b/charybdis-migrate/src/migration.rs @@ -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) { diff --git a/charybdis-parser/src/schema/code_schema.rs b/charybdis-parser/src/schema/code_schema.rs index 268ac16..5c3c1f3 100644 --- a/charybdis-parser/src/schema/code_schema.rs +++ b/charybdis-parser/src/schema/code_schema.rs @@ -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(¤t_dir) .into_iter() .filter_entry(|e| !(e.file_type().is_dir() && e.file_name() == "target")); diff --git a/charybdis-parser/src/schema/db_schema.rs b/charybdis-parser/src/schema/db_schema.rs index d14aeff..1b599bd 100644 --- a/charybdis-parser/src/schema/db_schema.rs +++ b/charybdis-parser/src/schema/db_schema.rs @@ -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 @@ -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); diff --git a/current_schema.json b/current_schema.json deleted file mode 100644 index 2b79da0..0000000 --- a/current_schema.json +++ /dev/null @@ -1,496 +0,0 @@ -{ - "tables": { - "posts": { - "fields": [ - [ - "community_id", - "uuid", - false - ], - [ - "created_at", - "timestamp", - false - ], - [ - "creator", - "profile", - false - ], - [ - "creator_id", - "uuid", - false - ], - [ - "description", - "text", - false - ], - [ - "id", - "uuid", - false - ], - [ - "is_archived", - "boolean", - false - ], - [ - "title", - "text", - false - ], - [ - "updated_at", - "timestamp", - false - ] - ], - "field_names": [ - "creator_id", - "id", - "creator", - "created_at", - "community_id", - "description", - "is_archived", - "title", - "updated_at" - ], - "types_by_name": { - "description": "text", - "id": "uuid", - "title": "text", - "is_archived": "boolean", - "creator": "profile", - "creator_id": "uuid", - "updated_at": "timestamp", - "community_id": "uuid", - "created_at": "timestamp" - }, - "type_name": "", - "table_name": "", - "base_table": "", - "partition_keys": [ - "community_id" - ], - "clustering_keys": [ - "created_at", - "id" - ], - "static_columns": [], - "global_secondary_indexes": [], - "local_secondary_indexes": [], - "table_options": null - }, - "communities": { - "fields": [ - [ - "created_at", - "timestamp", - false - ], - [ - "creator", - "profile", - false - ], - [ - "creator_id", - "uuid", - false - ], - [ - "description", - "text", - false - ], - [ - "id", - "uuid", - false - ], - [ - "title", - "text", - false - ], - [ - "updated_at", - "timestamp", - false - ] - ], - "field_names": [ - "title", - "created_at", - "creator", - "creator_id", - "id", - "description", - "updated_at" - ], - "types_by_name": { - "updated_at": "timestamp", - "creator": "profile", - "creator_id": "uuid", - "created_at": "timestamp", - "description": "text", - "id": "uuid", - "title": "text" - }, - "type_name": "", - "table_name": "", - "base_table": "", - "partition_keys": [ - "id" - ], - "clustering_keys": [], - "static_columns": [], - "global_secondary_indexes": [], - "local_secondary_indexes": [], - "table_options": null - }, - "users": { - "fields": [ - [ - "address", - "address", - false - ], - [ - "bio", - "text", - false - ], - [ - "created_at", - "timestamp", - false - ], - [ - "email", - "text", - false - ], - [ - "first_name", - "text", - false - ], - [ - "id", - "uuid", - false - ], - [ - "last_name", - "text", - false - ], - [ - "password", - "text", - false - ], - [ - "updated_at", - "timestamp", - false - ], - [ - "username", - "text", - false - ] - ], - "field_names": [ - "first_name", - "email", - "updated_at", - "id", - "created_at", - "last_name", - "password", - "bio", - "address", - "username" - ], - "types_by_name": { - "first_name": "text", - "last_name": "text", - "id": "uuid", - "address": "address", - "password": "text", - "bio": "text", - "created_at": "timestamp", - "updated_at": "timestamp", - "username": "text", - "email": "text" - }, - "type_name": "", - "table_name": "", - "base_table": "", - "partition_keys": [ - "id" - ], - "clustering_keys": [], - "static_columns": [], - "global_secondary_indexes": [ - [ - "users_username_idx", - "username" - ] - ], - "local_secondary_indexes": [], - "table_options": null - } - }, - "udts": { - "address": { - "fields": [ - [ - "street", - "text", - false - ], - [ - "city", - "text", - false - ], - [ - "state", - "text", - false - ], - [ - "zip", - "text", - false - ], - [ - "country", - "text", - false - ] - ], - "field_names": [ - "street", - "country", - "zip", - "city", - "state" - ], - "types_by_name": { - "state": "text", - "street": "text", - "zip": "text", - "country": "text", - "city": "text" - }, - "type_name": "", - "table_name": "", - "base_table": "", - "partition_keys": [], - "clustering_keys": [], - "static_columns": [], - "global_secondary_indexes": [], - "local_secondary_indexes": [], - "table_options": null - }, - "profile": { - "fields": [ - [ - "first_name", - "text", - false - ], - [ - "last_name", - "text", - false - ], - [ - "username", - "text", - false - ], - [ - "email", - "text", - false - ] - ], - "field_names": [ - "first_name", - "email", - "username", - "last_name" - ], - "types_by_name": { - "email": "text", - "last_name": "text", - "first_name": "text", - "username": "text" - }, - "type_name": "", - "table_name": "", - "base_table": "", - "partition_keys": [], - "clustering_keys": [], - "static_columns": [], - "global_secondary_indexes": [], - "local_secondary_indexes": [], - "table_options": null - } - }, - "materialized_views": { - "user_by_email": { - "fields": [ - [ - "email", - "text", - false - ], - [ - "id", - "uuid", - false - ], - [ - "username", - "text", - false - ] - ], - "field_names": [ - "id", - "username", - "email" - ], - "types_by_name": { - "username": "text", - "email": "text", - "id": "uuid" - }, - "type_name": "", - "table_name": "", - "base_table": "", - "partition_keys": [ - "email" - ], - "clustering_keys": [ - "id" - ], - "static_columns": [], - "global_secondary_indexes": [], - "local_secondary_indexes": [], - "table_options": null - }, - "posts_by_creator": { - "fields": [ - [ - "community_id", - "uuid", - false - ], - [ - "created_at", - "timestamp", - false - ], - [ - "creator_id", - "uuid", - false - ], - [ - "id", - "uuid", - false - ], - [ - "title", - "text", - false - ] - ], - "field_names": [ - "id", - "community_id", - "creator_id", - "title", - "created_at" - ], - "types_by_name": { - "created_at": "timestamp", - "creator_id": "uuid", - "community_id": "uuid", - "id": "uuid", - "title": "text" - }, - "type_name": "", - "table_name": "", - "base_table": "", - "partition_keys": [ - "creator_id" - ], - "clustering_keys": [ - "community_id", - "created_at", - "id" - ], - "static_columns": [], - "global_secondary_indexes": [], - "local_secondary_indexes": [], - "table_options": null - }, - "users_username_idx_index": { - "fields": [ - [ - "id", - "uuid", - false - ], - [ - "idx_token", - "bigint", - false - ], - [ - "username", - "text", - false - ] - ], - "field_names": [ - "username", - "id", - "idx_token" - ], - "types_by_name": { - "idx_token": "bigint", - "username": "text", - "id": "uuid" - }, - "type_name": "", - "table_name": "", - "base_table": "", - "partition_keys": [ - "username" - ], - "clustering_keys": [ - "id", - "idx_token" - ], - "static_columns": [], - "global_secondary_indexes": [], - "local_secondary_indexes": [], - "table_options": null - } - }, - "keyspace_name": "reddit_example" -} \ No newline at end of file diff --git a/examples/actix-web/reddit-api/src/models/post.rs b/examples/actix-web/reddit-api/src/models/post.rs index f21f27f..d2e7b0a 100644 --- a/examples/actix-web/reddit-api/src/models/post.rs +++ b/examples/actix-web/reddit-api/src/models/post.rs @@ -3,7 +3,7 @@ use crate::models::udts::Profile; use charybdis::callbacks::Callbacks; use charybdis::macros::charybdis_model; use charybdis::scylla::CachingSession; -use charybdis::types::{Text, Timestamp, Uuid}; +use charybdis::types::{Boolean, Text, Timestamp, Uuid}; use serde::{Deserialize, Serialize}; #[charybdis_model( @@ -11,6 +11,9 @@ use serde::{Deserialize, Serialize}; partition_keys = [community_id], clustering_keys = [created_at, id], global_secondary_indexes = [], + table_options = " + CLUSTERING ORDER BY (created_at DESC) + " )] #[derive(Serialize, Deserialize, Default, Clone, Debug)] #[serde(rename_all = "camelCase")] @@ -24,7 +27,7 @@ pub struct Post { pub updated_at: Timestamp, pub creator_id: Uuid, pub creator: Profile, - pub is_archived: bool, + pub is_archived: Boolean, } impl Callbacks for Post {