44c320d8fa
Track required workspace crates, scripts, and historical diesel migrations so the repository contains the complete runnable backend baseline. Made-with: Cursor
148 lines
3.1 KiB
Markdown
148 lines
3.1 KiB
Markdown
# 慧添翼考试
|
||
|
||
## 启动数据库
|
||
|
||
```bash
|
||
$ pg_ctl -D /usr/local/var/postgres start
|
||
```
|
||
|
||
## 创建数据库
|
||
|
||
```bash
|
||
$ psql postgres
|
||
postgres=# create database htyws owner hty encoding utf8;
|
||
CREATE DATABASE
|
||
postgres=# \q
|
||
```
|
||
|
||
## 创建用户
|
||
|
||
```bash
|
||
postgres=# create user hty;
|
||
CREATE ROLE
|
||
```
|
||
|
||
## 改变数据库owner
|
||
|
||
```bash
|
||
postgres=# alter database htyws owner to hty;
|
||
ALTER DATABASE
|
||
postgres=#
|
||
```
|
||
|
||
## 改变tables的owner
|
||
|
||
```bash
|
||
htyws=# \dt
|
||
List of relations
|
||
Schema | Name | Type | Owner
|
||
--------+----------------------------+-------+-------
|
||
public | __diesel_schema_migrations | table | htyws
|
||
public | rewards | table | weli
|
||
public | users | table | weli
|
||
(3 rows)
|
||
```
|
||
|
||
```bash
|
||
htyws=# alter table __diesel_schema_migrations owner to htyws;
|
||
ALTER TABLE
|
||
htyws=# alter table rewards owner to htyws;
|
||
ALTER TABLE
|
||
htyws=# alter table users owner to htyws;
|
||
ALTER TABLE
|
||
```
|
||
|
||
```bash
|
||
htyws=# \dt
|
||
List of relations
|
||
Schema | Name | Type | Owner
|
||
--------+----------------------------+-------+-------
|
||
public | __diesel_schema_migrations | table | htyws
|
||
public | rewards | table | htyws
|
||
public | users | table | htyws
|
||
(3 rows)
|
||
|
||
htyws=#
|
||
```
|
||
|
||
## 安装diesel_cli
|
||
|
||
```bash
|
||
$ cargo install diesel_cli --no-default-features --features postgres
|
||
```
|
||
|
||
## 初始化项目过程(已完成)
|
||
|
||
创建`.env`文件:
|
||
|
||
```bash
|
||
$ echo "DATABASE_URL=postgres://weli@localhost/htyws" > .env
|
||
```
|
||
|
||
里面是diesel要使用的数据库相关信息,上面就放数据库连接地址。
|
||
|
||
```bash
|
||
$ diesel setup
|
||
Creating migrations directory at: /Users/weli/works/htyws/migrations
|
||
```
|
||
|
||
## 创建用户表(初步工作完成)
|
||
|
||
创建第一个migration,创建一张`users`表:
|
||
|
||
```bash
|
||
$ diesel migration generate create_users
|
||
Creating migrations/2020-09-27-112838_create_users/up.sql
|
||
Creating migrations/2020-09-27-112838_create_users/down.sql
|
||
```
|
||
|
||
在`up.sql`里面添加内容:
|
||
|
||
```sql
|
||
CREATE TABLE users (
|
||
id SERIAL PRIMARY KEY,
|
||
nickname VARCHAR UNIQUE NOT NULL
|
||
)
|
||
```
|
||
|
||
执行migration:
|
||
|
||
```bash
|
||
$ diesel migration run 19:20:27
|
||
Running migration 2020-09-27-112838_create_users
|
||
```
|
||
|
||
数据库表可以看到已经创建:
|
||
|
||
```bash
|
||
postgres=# \c htyws
|
||
You are now connected to database "htyws" as user "weli".
|
||
htyws=# \dt
|
||
List of relations
|
||
Schema | Name | Type | Owner
|
||
--------+----------------------------+-------+-------
|
||
public | __diesel_schema_migrations | table | weli
|
||
public | users | table | weli
|
||
(2 rows)
|
||
|
||
htyws=# select * from users;
|
||
id | nickname
|
||
----+----------
|
||
(0 rows)
|
||
|
||
htyws=#
|
||
```
|
||
|
||
## 生成/更新schema文件(每次重构要做)
|
||
|
||
使用`diesel`的命令更新`schema.rs`(在项目根目录执行):
|
||
|
||
```bash
|
||
$ diesel print-schema > src/schema.rs
|
||
```
|
||
|
||
|
||
|
||
|
||
|