Create Cassandra Migrations from Rust Structs
Custom derive to simplify cassandra table creation from rust structs
Installation
cassandra_macro = "0.1.0"
cassandra_macro_derive = "0.1.0"
Example with primary key
use cassandra_macro::Cassandra;
use cassandra_macro_derive::Cassandra;
#[table(keyspace = "fog")]
#[derive(Debug, Cassandra)]
pub struct TestRust {
#[column(type = "TEXT", primary_key)]
key_one: String,
}
fn main() {
let f = TestRust {
key_one: String::from("one")
};
println!("{}", f.create_table_cql()); println!("{}", f.drop_table_cql()); println!("{}", f.key_space()); println!("{}", f.table_name()); }
Example with compound key
use cassandra_macro::Cassandra;
use cassandra_macro_derive::Cassandra;
#[table(keyspace = "fog")]
#[derive(Debug, Cassandra)]
pub struct TestRust {
#[column(type = "UUID", compound_key(position = 2))]
key_one: String,
#[column(type = "TEXT", compound_key(position = 1))]
key_two: String,
}
fn main() {
let f = TestRust {
key_one: String::from("one"),
key_two: String::from("two")
};
println!("{}", f.create_table_cql()); println!("{}", f.drop_table_cql());
println!("{}", f.key_space());
println!("{}", f.table_name());
}
Example with compound key & cluster key & options
use cassandra_macro::Cassandra;
use cassandra_macro_derive::Cassandra;
#[table(keyspace = "fog", options = "comment='From RUST' | COMPACTION = { 'class' : 'SizeTieredCompactionStrategy' }")]
#[derive(Debug, Cassandra)]
pub struct TestRust {
#[column(type = "UUID", compound_key(position = 2))]
key_one: String,
#[column(type = "TEXT", compound_key(position = 1))]
key_two: String,
#[column(type = "TIMESTAMP", cluster_key(order = "DESC", position = 1))]
created: i64,
updated: i64, }
fn main() {
let f = TestRust {
key_one: String::from("one"),
key_two: String::from("two"),
created: 0,
updated: 0,
};
println!("{}", f.create_table_cql()); println!("{}", f.drop_table_cql());
println!("{}", f.key_space());
println!("{}", f.table_name());
}