1#![doc = include_str!("../README.md")]
2
3use crate::args::Args;
4use crate::migration::Migration;
5use charybdis_parser::schema::code_schema::CodeSchema;
6use charybdis_parser::schema::db_schema::DbSchema;
7use scylla::client::session::Session;
8
9pub mod args;
10pub mod migration;
11pub(crate) mod model;
12pub mod session;
13
14pub struct MigrationBuilder {
15 pub(crate) args: Args,
16}
17
18impl Default for MigrationBuilder {
19 fn default() -> Self {
20 Self::new()
21 }
22}
23
24impl MigrationBuilder {
25 pub fn new() -> Self {
26 Self { args: Args::default() }
27 }
28
29 pub async fn build(mut self, session: &'_ Session) -> Migration<'_> {
30 if self.args.keyspace.is_empty() {
31 self.args.keyspace = session
33 .get_keyspace()
34 .expect("No keyspace provided and no default keyspace set")
35 .to_string();
36 }
37
38 let current_db_schema = DbSchema::new(session, self.args.keyspace.clone()).await;
39 let current_code_schema: CodeSchema = self
40 .args
41 .code_schema_override_json
42 .as_ref()
43 .map(|json| serde_json::from_str(json).unwrap())
44 .unwrap_or_else(|| CodeSchema::new(&self.args.current_dir));
45
46 let migration = Migration::new(current_db_schema, current_code_schema, session, self.args);
47
48 migration
49 }
50
51 pub fn keyspace(mut self, keyspace: String) -> Self {
52 self.args.keyspace = keyspace;
53 self
54 }
55
56 pub fn current_dir(mut self, current_dir: String) -> Self {
57 self.args.current_dir = current_dir;
58 self
59 }
60
61 pub fn drop_and_replace(mut self, drop_and_replace: bool) -> Self {
62 self.args.drop_and_replace = drop_and_replace;
63 self
64 }
65
66 pub fn verbose(mut self, verbose: bool) -> Self {
67 self.args.verbose = verbose;
68 self
69 }
70
71 pub fn code_schema_override_json(mut self, code_schema_override_json: String) -> Self {
72 self.args.code_schema_override_json = Some(code_schema_override_json);
73 self
74 }
75}
76
77impl From<Args> for MigrationBuilder {
78 fn from(args: Args) -> Self {
79 Self { args }
80 }
81}