Skip to main content

rorm_declaration/
migration.rs

1use serde::{Deserialize, Serialize};
2
3use crate::imr::{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 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 CreateIndex operation
108    #[serde(rename_all = "PascalCase")]
109    CreateIndex {
110        /// Name of the model to create the index on
111        model: String,
112        /// The index that should be created
113        index: Index,
114    },
115
116    /// Representation of a DeleteIndex operation
117    #[serde(rename_all = "PascalCase")]
118    DeleteIndex {
119        /// Name of the model the index was created on
120        model: String,
121        /// The index that should be deleted
122        index: Index,
123    },
124
125    /// Representation of a RawSQL operation
126    #[serde(rename_all = "PascalCase")]
127    RawSQL {
128        /// The provided raw sql does not change the structure of the database.
129        /// The migrator can assume that the layout stayed the same and will continue
130        /// generating new migrations based on `.models.json`
131        #[serde(default)]
132        structure_safe: bool,
133        /// SQL for sqlite
134        #[serde(rename = "SQLite")]
135        sqlite: String,
136        /// SQL for postgres
137        #[serde(rename = "Postgres")]
138        postgres: String,
139        /// SQL for mysql
140        #[serde(rename = "MySQL")]
141        mysql: String,
142    },
143}