1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Manual check if the column exists is needed, as Sqlite does not support
// ALTER TABLE IF COLUMN EXISTS. Without the check, the migration would fail
// on Sqlite with an "duplicate column" error.
manager
.create_table(
Table::create()
.table(CratesIoIden::Table)
.if_not_exists()
.col(
ColumnDef::new(CratesIoIden::Id)
.big_integer()
.not_null()
.primary_key()
.auto_increment(),
)
.col(
ColumnDef::new(CratesIoIden::Name)
.text()
.not_null()
.unique_key(),
)
.col(
ColumnDef::new(CratesIoIden::OriginalName)
.text()
.not_null()
.unique_key(),
)
.col(ColumnDef::new(CratesIoIden::ETag).string_len(64).not_null())
.col(ColumnDef::new(CratesIoIden::LastModified).text().not_null())
.col(ColumnDef::new(CratesIoIden::Description).text())
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(CratesIoIndexIden::Table)
.if_not_exists()
.col(
ColumnDef::new(CratesIoIndexIden::Id)
.big_integer()
.not_null()
.primary_key()
.auto_increment(),
)
.col(ColumnDef::new(CratesIoIndexIden::Name).text().not_null())
.col(ColumnDef::new(CratesIoIndexIden::Vers).text().not_null())
.col(ColumnDef::new(CratesIoIndexIden::Deps).json_binary())
.col(ColumnDef::new(CratesIoIndexIden::Cksum).text().not_null())
.col(ColumnDef::new(CratesIoIndexIden::Features).json_binary())
.col(ColumnDef::new(CratesIoIndexIden::Features2).json_binary())
.col(
ColumnDef::new(CratesIoIndexIden::Yanked)
.boolean()
.not_null()
.default(false),
)
.col(ColumnDef::new(CratesIoIndexIden::Links).text())
.col(
ColumnDef::new(CratesIoIndexIden::V)
.integer()
.not_null()
.default(1),
)
.col(
ColumnDef::new(CratesIoIndexIden::CratesIoFk)
.big_integer()
.not_null(),
)
.foreign_key(
ForeignKey::create()
.name("cratesio_fk")
.from(CratesIoIndexIden::Table, CratesIoIndexIden::CratesIoFk)
.to(CratesIoIden::Table, CratesIoIden::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::NoAction),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Drop tables in reverse order of creation
manager
.drop_table(Table::drop().table(CratesIoIndexIden::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(CratesIoIden::Table).to_owned())
.await
}
}
#[derive(Iden)]
pub enum CratesIoIden {
#[iden = "cratesio_crate"]
Table,
Id,
Name,
OriginalName,
Description,
ETag,
LastModified,
}
#[derive(Iden)]
pub enum CratesIoIndexIden {
#[iden = "cratesio_index"]
Table,
Id,
Name,
Vers,
Deps,
Cksum,
Features,
Features2,
Yanked,
Links,
V,
CratesIoFk,
}