rorm_declaration/
migration.rs

1use serde::{Deserialize, Serialize};
2
3use crate::imr::Field;
4
5/**
6The presentation of a migration file
7*/
8#[derive(Serialize, Deserialize, Debug, Clone)]
9#[serde(rename_all = "PascalCase")]
10pub struct MigrationFile {
11    /// The migration of the migration file
12    pub migration: Migration,
13}
14
15/**
16Representation for a migration.
17*/
18#[derive(Serialize, Deserialize, Debug, Clone)]
19#[serde(rename_all = "PascalCase")]
20pub struct Migration {
21    /// Hash of the migration
22    pub hash: String,
23
24    /// Marks the migration initial state
25    pub initial: bool,
26
27    /// ID of the migration, derived from filename
28    #[serde(skip)]
29    pub id: u16,
30
31    /// Name of the migration, derived from filename
32    #[serde(skip)]
33    pub name: String,
34
35    /// Migration this migration depends on
36    pub dependency: Option<u16>,
37
38    /// List of migrations this migration replaces
39    pub replaces: Vec<u16>,
40
41    /// The operations to execute
42    pub operations: Vec<Operation>,
43}
44
45/**
46The representation for all possible database operations
47*/
48#[derive(Serialize, Deserialize, Debug, Clone)]
49#[serde(tag = "Type")]
50pub enum Operation {
51    /// Representation of a CreateModel operation
52    #[serde(rename_all = "PascalCase")]
53    CreateModel {
54        /// Name of the model
55        name: String,
56        /// List of fields associated to the model
57        fields: Vec<Field>,
58    },
59
60    /// Representation of a RenameModel operation
61    #[serde(rename_all = "PascalCase")]
62    RenameModel {
63        /// Old name of the model
64        old: String,
65        /// New name of the model
66        new: String,
67    },
68
69    /// Representation of a DeleteModel operation
70    #[serde(rename_all = "PascalCase")]
71    DeleteModel {
72        /// Name of the model
73        name: String,
74    },
75
76    /// Representation of a CreateField operation
77    #[serde(rename_all = "PascalCase")]
78    CreateField {
79        /// Name of the model
80        model: String,
81        /// The field that should be created
82        field: Field,
83    },
84
85    /// Representation of a RenameField operation
86    #[serde(rename_all = "PascalCase")]
87    RenameField {
88        /// Name of the table the column lives in
89        table_name: String,
90
91        /// Old name of the column
92        old: String,
93
94        /// New name of the column
95        new: String,
96    },
97
98    /// Representation of a DeleteField operation
99    #[serde(rename_all = "PascalCase")]
100    DeleteField {
101        /// Name of the model
102        model: String,
103        /// Name of the field to delete
104        name: String,
105    },
106
107    /// Representation of a RawSQL operation
108    #[serde(rename_all = "PascalCase")]
109    RawSQL {
110        /// The provided raw sql does not change the structure of the database.
111        /// The migrator can assume that the layout stayed the same and will continue
112        /// generating new migrations based on `.models.json`
113        #[serde(default)]
114        structure_safe: bool,
115        /// SQL for sqlite
116        #[serde(rename = "SQLite")]
117        sqlite: String,
118        /// SQL for postgres
119        #[serde(rename = "Postgres")]
120        postgres: String,
121        /// SQL for mysql
122        #[serde(rename = "MySQL")]
123        mysql: String,
124    },
125}