0e53c9f66d
Portable run_rotate; moicen_start_huiwings_stack and ws_clazz updates; schema/models sync. Made-with: Cursor
慧添翼考试
启动数据库
$ pg_ctl -D /usr/local/var/postgres start
创建数据库
$ psql postgres
postgres=# create database htyws owner hty encoding utf8;
CREATE DATABASE
postgres=# \q
创建用户
postgres=# create user hty;
CREATE ROLE
改变数据库owner
postgres=# alter database htyws owner to hty;
ALTER DATABASE
postgres=#
改变tables的owner
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)
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
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
$ cargo install diesel_cli --no-default-features --features postgres
初始化项目过程(已完成)
创建.env文件:
$ echo "DATABASE_URL=postgres://weli@localhost/htyws" > .env
里面是diesel要使用的数据库相关信息,上面就放数据库连接地址。
$ diesel setup
Creating migrations directory at: /Users/weli/works/htyws/migrations
创建用户表(初步工作完成)
创建第一个migration,创建一张users表:
$ 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里面添加内容:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
nickname VARCHAR UNIQUE NOT NULL
)
执行migration:
$ diesel migration run 19:20:27
Running migration 2020-09-27-112838_create_users
数据库表可以看到已经创建:
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(在项目根目录执行):
$ diesel print-schema > src/schema.rs