rorm_declaration/migration.rs
1use serde::{Deserialize, Serialize};
2
3use crate::imr::{DbType, Field, Index};
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 SetFieldType operation
99 ///
100 /// It changes an existing column's type in place, preserving its data.
101 ///
102 /// The type has to be one which is fully described by itself. A
103 /// [`DbType::VarChar`] carries its maximum length and a [`DbType::Choices`]
104 /// its enum, so neither can be set through this operation.
105 #[serde(rename_all = "PascalCase")]
106 SetFieldType {
107 /// Name of the model
108 model: String,
109
110 /// Name of the column
111 name: String,
112
113 /// The type to change the column to
114 #[serde(rename = "DbType")]
115 db_type: DbType,
116 },
117
118 /// Representation of a SetFieldMaxLength operation
119 ///
120 /// It constrains a [`DbType::Text`] column to a maximum length. Postgres
121 /// enforces it with a check constraint; sqlite has no `varchar` and never
122 /// checks a string's length, so there it does nothing.
123 ///
124 /// A column which already has a maximum length has to
125 /// [drop](Operation::DropFieldMaxLength) it first: a constraint can't be
126 /// redefined, only replaced.
127 #[serde(rename_all = "PascalCase")]
128 SetFieldMaxLength {
129 /// Name of the model
130 model: String,
131
132 /// Name of the column
133 name: String,
134
135 /// The maximum number of characters the column may hold
136 max_length: i32,
137 },
138
139 /// Representation of a DropFieldMaxLength operation
140 ///
141 /// It removes the constraint [`Operation::SetFieldMaxLength`] created.
142 #[serde(rename_all = "PascalCase")]
143 DropFieldMaxLength {
144 /// Name of the model
145 model: String,
146
147 /// Name of the column
148 name: String,
149 },
150
151 /// Representation of a DeleteField operation
152 #[serde(rename_all = "PascalCase")]
153 DeleteField {
154 /// Name of the model
155 model: String,
156 /// Name of the field to delete
157 name: String,
158 },
159
160 /// Representation of a CreateIndex operation
161 #[serde(rename_all = "PascalCase")]
162 CreateIndex {
163 /// Name of the model to create the index on
164 model: String,
165 /// The index that should be created
166 index: Index,
167 },
168
169 /// Representation of a DeleteIndex operation
170 #[serde(rename_all = "PascalCase")]
171 DeleteIndex {
172 /// Name of the model the index was created on
173 model: String,
174 /// The index that should be deleted
175 index: Index,
176 },
177
178 /// Representation of a RawSQL operation
179 #[serde(rename_all = "PascalCase")]
180 RawSQL {
181 /// The provided raw sql does not change the structure of the database.
182 /// The migrator can assume that the layout stayed the same and will continue
183 /// generating new migrations based on `.models.json`
184 #[serde(default)]
185 structure_safe: bool,
186 /// SQL for sqlite
187 #[serde(rename = "SQLite")]
188 sqlite: String,
189 /// SQL for postgres
190 #[serde(rename = "Postgres")]
191 postgres: String,
192 /// SQL for mysql
193 #[serde(rename = "MySQL")]
194 mysql: String,
195 },
196}