Sqlx
Install for Postgres
cargo install --version=0.5.7 sqlx-cli --no-default-features --features postgres
Cargo.toml example for Postgres
[dependencies.sqlx]
version = "0.5.7"
default-features = false
features = [
"runtime-actix-rustls",
"macros",
"postgres",
"uuid",
"chrono",
"migrate",
]
runtime-actix-rustls
useactix
as runtime andrustls
asTLS
backendmacros
gives access tosqlx::query
andsqlx::query_as!
postgres
makes all the postgres types availableuuid
Allows generating uuid's from theuuid
cratechrono
allowstimestamptz
to be mapped toDateTime<T>
migrate
manage migrations from rust code
Environment variables
DATABASE_URL
postgres://postgres:password@localhost:5432/newsletter
Command examples
sqlx database create
create the database in the connection stringsqlx migrate add create_subscriptions_table
: sqlx migrate add will create a sql file placeholder inmigrations
folder, add your script there:
-- Add migration script here
CREATE TABLE subscriptions(
id uuid NOT NULL,
PRIMARY KEY (id),
email TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
subscribed_at timestamptz NOT NULL
);
sqlx migrate run
run all the migrations inmigrations
folder