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
//! Conventional, reversible SeaORM identity schema shared with the installer.
use sea_orm_migration::prelude::*;
/// Creates users, sessions (including short-lived anonymous CSRF sessions), and reset tokens.
/// Applications normally register the migration generated by `capy make:auth`.
#[derive(DeriveMigrationName)]
pub struct IdentityMigration;
#[async_trait::async_trait]
impl MigrationTrait for IdentityMigration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Alias::new("users"))
.col(
ColumnDef::new(Alias::new("id"))
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(Alias::new("name"))
.string_len(100)
.not_null(),
)
.col(
ColumnDef::new(Alias::new("email"))
.string_len(254)
.not_null(),
)
.col(
ColumnDef::new(Alias::new("password_hash"))
.string()
.not_null(),
)
.col(ColumnDef::new(Alias::new("email_verified_at")).big_integer())
.col(
ColumnDef::new(Alias::new("created_at"))
.big_integer()
.not_null(),
)
.col(
ColumnDef::new(Alias::new("updated_at"))
.big_integer()
.not_null(),
)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("users_email_unique")
.table(Alias::new("users"))
.col(Alias::new("email"))
.unique()
.to_owned(),
)
.await?;
for table in ["sessions", "password_reset_tokens"] {
let mut statement = Table::create();
statement.table(Alias::new(table)).col(
ColumnDef::new(Alias::new("token_hash"))
.string_len(64)
.not_null()
.primary_key(),
);
let mut user = ColumnDef::new(Alias::new("user_id"));
user.big_integer();
if table == "password_reset_tokens" {
user.not_null();
}
statement
.col(&mut user)
.col(
ColumnDef::new(Alias::new("created_at"))
.big_integer()
.not_null(),
)
.col(
ColumnDef::new(Alias::new("expires_at"))
.big_integer()
.not_null(),
)
.foreign_key(
ForeignKey::create()
.name(format!("{table}_user_fk"))
.from(Alias::new(table), Alias::new("user_id"))
.to(Alias::new("users"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
);
if table == "sessions" {
statement
.col(
ColumnDef::new(Alias::new("csrf_token"))
.string_len(64)
.not_null(),
)
.col(
ColumnDef::new(Alias::new("last_seen_at"))
.big_integer()
.not_null(),
);
}
manager.create_table(statement.to_owned()).await?;
for column in ["user_id", "expires_at"] {
manager
.create_index(
Index::create()
.name(format!("{table}_{column}_idx"))
.table(Alias::new(table))
.col(Alias::new(column))
.to_owned(),
)
.await?;
}
}
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
for table in ["password_reset_tokens", "sessions", "users"] {
manager
.drop_table(Table::drop().table(Alias::new(table)).to_owned())
.await?;
}
Ok(())
}
}