Open6

[メモ] Webアプリ開発で学ぶRust言語入門 4章

ki504178ki504178

4.1 データベース基礎

おっと VS Code + Remote Container やってきたのが裏目に出た予感。
まあローカル側でDBコンテナ立ち上げてそこのポートフォワードすればいけるか。

Docker で PostgreSQL 環境構築

データベース一覧を確認するコマンドが、よく見ても \1 にしか見えなくて、
何度叩いても invalid command になって??となったけど、
これ 1 の部分 l(エル)にも見えるし一覧表示だからきっとそうだと思って \l で実行したらいけた。

データベース作成とデータの格納

CLI で PostgreSQL 触るの久々。普段は DBeaver

誤字(記載ミス)

  • todos=# \c mydb # 作成済みデータベースの一覧表示
    • todos=# \c mydb # データベース接続
ki504178ki504178

sqlx とは

↑のほうで「クラス無いの不思議な気持ち」とか書いてたけど、
普通に struct を impl したやつがクラスやな。

Rust の SQL ライブラリ

2022年現在、有力なのは dieselsqlxで、sqlx の方が後発で人気急上昇中。

  • diesel

よくある O/Rマッパー+クエリビルダーっぽくて、.NET の EntityFramework みある。
async に非対応。

確かに diesel みたいなの使うと SQL 書かない、テーブルとのマッピングお任せできるので良い面もあるけど、
複雑な SQL 実現しようとして無事死亡したこともあるから注意が必要なのは同意。

sqlx とは

シンプルに SQL 書くやつで、SQLのコンパイル時チェック・マイクレーションのみ。

sqlx でマイグレーション

また Killed Fetch でインスコできなかったから、
Docker Desktop の Resouces を軒並み上乗せしたら Fetch はイケた。
ただ Fetch 後のビルドでまた落ちた。20分くらいかかってこれか。。。

エラー箇所

Compiling sqlx-macros v0.6.2
<jemalloc>: MADV_DONTNEED does not work (memset will be used instead)
<jemalloc>: (This is the expected behaviour if you are running under QEMU)
error: linking with cc failed: exit status: 1
|
= note: "cc" "-Wl,--version-script=/tmp/rustc8QIqNJ/list" "-m64" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.sqlx_macros.92c9c040-cgu.0.rcgu.o" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.sqlx_macros.92c9c040-cgu.1.rcgu.o" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.sqlx_macros.92c9c040-cgu.10.rcgu.o" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.sqlx_macros.92c9c040-cgu.11.rcgu.o" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.sqlx_macros.92c9c040-cgu.12.rcgu.o" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.sqlx_macros.92c9c040-cgu.13.rcgu.o" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.sqlx_macros.92c9c040-cgu.14.rcgu.o" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.sqlx_macros.92c9c040-cgu.15.rcgu.o" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.sqlx_macros.92c9c040-cgu.2.rcgu.o" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.sqlx_macros.92c9c040-cgu.3.rcgu.o" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.sqlx_macros.92c9c040-cgu.4.rcgu.o" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.sqlx_macros.92c9c040-cgu.5.rcgu.o" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.sqlx_macros.92c9c040-cgu.6.rcgu.o" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.sqlx_macros.92c9c040-cgu.7.rcgu.o" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.sqlx_macros.92c9c040-cgu.8.rcgu.o" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.sqlx_macros.92c9c040-cgu.9.rcgu.o" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.2jknwk083t0b672t.rcgu.rmeta" "/tmp/cargo-installenDUMn/release/deps/sqlx_macros-30535d33bb51145b.dgpwadfr5brmuou.rcgu.o" "-Wl,--as-needed" "-L" "/tmp/cargo-installenDUMn/release/deps" "-L" "/tmp/cargo-installenDUMn/release/build/libsqlite3-sys-751cc545e242321a/out" "-L" "/usr/local/ssl/lib" "-L" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/tmp/cargo-installenDUMn/release/deps/libheck-e502cfc88723f475.rlib" "/tmp/cargo-installenDUMn/release/deps/libunicode_segmentation-7e19a52588cc2da8.rlib" "/tmp/cargo-installenDUMn/release/deps/libsyn-8f8a83fde0e25ba4.rlib" "/tmp/cargo-installenDUMn/release/deps/libsqlx_core-dad71d6db55696c5.rlib" "/tmp/cargo-installenDUMn/release/deps/libfutures_executor-c5a25065bad0cba3.rlib" "/tmp/cargo-installenDUMn/release/deps/libflume-28dfd6a01a1ee034.rlib" "/tmp/cargo-installenDUMn/release/deps/libspin-a8ad2ff36c99bcc8.rlib" "/tmp/cargo-installenDUMn/release/deps/libpin_project-5c694ce22349e858.rlib" "/tmp/cargo-installenDUMn/release/deps/libdotenvy-ec94c1470dd0e1fd.rlib" "/tmp/cargo-installenDUMn/release/deps/libcrc-68c42063956ab51b.rlib" "/tmp/cargo-installenDUMn/release/deps/libcrc_catalog-35c90ea9049ba8be.rlib" "/tmp/cargo-installenDUMn/release/deps/libhex-673c123d5dc1996b.rlib" "/tmp/cargo-installenDUMn/release/deps/libwhoami-653408da7b8096f7.rlib" "/tmp/cargo-installenDUMn/release/deps/libdirs-1ab0ab81d1d7daa3.rlib" "/tmp/cargo-installenDUMn/release/deps/libdirs_sys-4554bc7ce53d4781.rlib" "/tmp/cargo-installenDUMn/release/deps/libbase64-89c47c908e117bed.rlib" "/tmp/cargo-installenDUMn/release/deps/libtokio_stream-408e0d040ebd7691.rlib" "/tmp/cargo-installenDUMn/release/deps/libsqlformat-2ae8162f8b874f9e.rlib" "/tmp/cargo-installenDUMn/release/deps/libunicode_categories-6b12f0bf7fa3b2cc.rlib" "/tmp/cargo-installenDUMn/release/deps/libnom-0c815d4e26e75838.rlib" "/tmp/cargo-installenDUMn/release/deps/libitertools-0fb7000da29821fd.rlib" "/tmp/cargo-installenDUMn/release/deps/libthiserror-d58dbe1d87e01f03.rlib" "/tmp/cargo-installenDUMn/release/deps/libsha1-a5e51aeaf4467fb0.rlib" "/tmp/cargo-installenDUMn/release/deps/librsa-937119770398c0e9.rlib" "/tmp/cargo-installenDUMn/release/deps/libpkcs1-d3290950aedb5177.rlib" "/tmp/cargo-installenDUMn/release/deps/libpkcs8-4a2a2677880d3227.rlib" "/tmp/cargo-installenDUMn/release/deps/libspki-d5e9c3c4054ae2ad.rlib" "/tmp/cargo-installenDUMn/release/deps/libder-b1c8f2a0bbdcc4de.rlib" "/tmp/cargo-installenDUMn/release/deps/libpem_rfc7468-0cd66c05929a2a89.rlib" "/tmp/cargo-installenDUMn/release/deps/libbase64ct-02f571fc6bfea3b7.rlib" "/tmp/cargo-installenDUMn/release/deps/libconst_oid-6fa4c3a295ee6ef4.rlib" "/tmp/cargo-installenDUMn/release/deps/libcrypto_bigint-e8d10c07a8f2001b.rlib" "/tmp/cargo-installenDUMn/release/deps/libnum_bigint_dig-193bc256bcd3e147.rlib" "/tmp/cargo-installenDUMn/release/deps/liblibm-df5f4e0c18ae3467.rlib" "/tmp/cargo-installenDUMn/release/deps/libnum_iter-d8b36ab0c3bc3a23.rlib" "/tmp/cargo-installenDUMn/release/deps/libzeroize-c6764a3734fce806.rlib" "/tmp/cargo-installenDUMn/release/deps/libnum_integer-9c5adb523940b4c9.rlib" "/tmp/cargo-installenDUMn/release/deps/liblazy_static-8d359d62f7608e45.rlib" "/tmp/cargo-installenDUMn/release/deps/libspin-2e893dd8df858840.rlib" "/tmp/cargo-installenDUMn/release/deps/libindexmap-dd4b34ce36419e2a.rlib" "/tmp/cargo-installenDUMn/release/deps/liblibsqlite3_sys-218763df9159eca7.rlib" "/tmp/cargo-installenDUMn/release/deps/liburl-6f300796ccaf2b74.rlib" "/tmp/cargo-installenDUMn/release/deps/libidna-baf2c41a71eea816.rlib" "/tmp/cargo-installenDUMn/release/deps/libform_urlencoded-2ba48a65a00f379d.rlib" "/tmp/cargo-installenDUMn/release/deps/libpercent_encoding-90f13b8304eef29e.rlib" "/tmp/cargo-installenDUMn/release/deps/libmd5-787b525a819dcfd4.rlib" "/tmp/cargo-installenDUMn/release/deps/libbyteorder-91c82c873194f028.rlib" "/tmp/cargo-installenDUMn/release/deps/libatoi-d865f34aacc89ac9.rlib" "/tmp/cargo-installenDUMn/release/deps/libnum_traits-696d422538972bc4.rlib" "/tmp/cargo-installenDUMn/release/deps/libstringprep-821669b57d7f58f8.rlib" "/tmp/cargo-installenDUMn/release/deps/libunicode_normalization-487477c45a74aada.rlib" "/tmp/cargo-installenDUMn/release/deps/libtinyvec-c655c30157f94ce9.rlib" "/tmp/cargo-installenDUMn/release/deps/libtinyvec_macros-061a1e62793bf0d2.rlib" "/tmp/cargo-installenDUMn/release/deps/libunicode_bidi-9c62a000835267ae.rlib" "/tmp/cargo-installenDUMn/release/deps/librand-4e00a0044bc1c29d.rlib" "/tmp/cargo-installenDUMn/release/deps/librand_chacha-46053101bb16b37e.rlib" "/tmp/cargo-installenDUMn/release/deps/libppv_lite86-f671025d24e3af86.rlib" "/tmp/cargo-installenDUMn/release/deps/librand_core-e7836785f350e8d8.rlib" "/tmp/cargo-installenDUMn/release/deps/libhkdf-ebfaca1fd9f012fa.rlib" "/tmp/cargo-installenDUMn/release/deps/libhmac-b6818c470be9a2fd.rlib" "/tmp/cargo-installenDUMn/release/deps/libsha2-4821519e65e64559.rlib" "/tmp/cargo-installenDUMn/release/deps/libcpufeatures-4e49c726de360a28.rlib" "/tmp/cargo-installenDUMn/release/deps/libdigest-050d4b822a96b0b0.rlib" "/tmp/cargo-installenDUMn/release/deps/libsubtle-97fd5ad420c2c425.rlib" "/tmp/cargo-installenDUMn/release/deps/libblock_buffer-6f83e18021c8468e.rlib" "/tmp/cargo-installenDUMn/release/deps/libcrypto_common-9c32a87dcba39e40.rlib" "/tmp/cargo-installenDUMn/release/deps/libgeneric_array-6aaa1f3128bf47d2.rlib" "/tmp/cargo-installenDUMn/release/deps/libtypenum-a3a9d3f6779d2443.rlib" "/tmp/cargo-installenDUMn/release/deps/libsqlx_rt-4c36d69bda4ff9a2.rlib" "/tmp/cargo-installenDUMn/release/deps/libtokio_native_tls-31132581aaf99733.rlib" "/tmp/cargo-installenDUMn/release/deps/libnative_tls-0856120222c41db1.rlib" "/tmp/cargo-installenDUMn/release/deps/libopenssl_probe-db90a4d92ff1c53d.rlib" "/tmp/cargo-installenDUMn/release/deps/libopenssl-8a6d6ae78f21d997.rlib" "/tmp/cargo-installenDUMn/release/deps/libbitflags-3f675a874a12614b.rlib" "/tmp/cargo-installenDUMn/release/deps/libforeign_types-2db1ae40e0776503.rlib" "/tmp/cargo-installenDUMn/release/deps/libforeign_types_shared-f0aaee6e7afb4434.rlib" "/tmp/cargo-installenDUMn/release/deps/libopenssl_sys-721c4e4acd28ae51.rlib" "/tmp/cargo-installenDUMn/release/deps/libtokio-e18cfc54b93691dd.rlib" "/tmp/cargo-installenDUMn/release/deps/libnum_cpus-b0ba8af5d4cee9d0.rlib" "/tmp/cargo-installenDUMn/release/deps/libsocket2-589cfce35937e848.rlib" "/tmp/cargo-installenDUMn/release/deps/libmio-282e9009eede6fb9.rlib" "/tmp/cargo-installenDUMn/release/deps/libmemchr-5bb7da3b8113efde.rlib" "/tmp/cargo-installenDUMn/release/deps/libbytes-6a2c509cd11ec8ba.rlib" "/tmp/cargo-installenDUMn/release/deps/libhashlink-81a26e0873cbafa5.rlib" "/tmp/cargo-installenDUMn/release/deps/libhashbrown-1d7208515d19bf5b.rlib" "/tmp/cargo-installenDUMn/release/deps/libahash-9a5cceae871b17aa.rlib" "/tmp/cargo-installenDUMn/release/deps/libgetrandom-72b90395de68379a.rlib" "/tmp/cargo-installenDUMn/release/deps/libonce_cell-89b4927f2ec77609.rlib" "/tmp/cargo-installenDUMn/release/deps/libserde_json-4b669d6fe0d8946d.rlib" "/tmp/cargo-installenDUMn/release/deps/libryu-549a512462ca70ec.rlib" "/tmp/cargo-installenDUMn/release/deps/libitoa-589a5a473dc0d9c0.rlib" "/tmp/cargo-installenDUMn/release/deps/liblog-c25974f86f6e97ad.rlib" "/tmp/cargo-installenDUMn/release/deps/libcrossbeam_queue-95c1f2ba737b20ce.rlib" "/tmp/cargo-installenDUMn/release/deps/libcrossbeam_utils-fb85d422fc035e40.rlib" "/tmp/cargo-installenDUMn/release/deps/libfutures_intrusive-51f1381f1503129d.rlib" "/tmp/cargo-installenDUMn/release/deps/libparking_lot-3b1ef963fc68a357.rlib" "/tmp/cargo-installenDUMn/release/deps/libparking_lot_core-5cd605e7ee649f72.rlib" "/tmp/cargo-installenDUMn/release/deps/liblibc-9c3eb0d005773754.rlib" "/tmp/cargo-installenDUMn/release/deps/libsmallvec-2499e2b4302b2b1f.rlib" "/tmp/cargo-installenDUMn/release/deps/libinstant-2747334c81814410.rlib" "/tmp/cargo-installenDUMn/release/deps/libcfg_if-d76a01ad2bb5870b.rlib" "/tmp/cargo-installenDUMn/release/deps/liblock_api-ccc741f26cc83429.rlib" "/tmp/cargo-installenDUMn/release/deps/libscopeguard-4d3b41a768a6647a.rlib" "/tmp/cargo-installenDUMn/release/deps/libeither-32f0bf7f2218f324.rlib" "/tmp/cargo-installenDUMn/release/deps/libserde-737968eb079f4dc7.rlib" "/tmp/cargo-installenDUMn/release/deps/libevent_listener-612acb2b16157bc6.rlib" "/tmp/cargo-installenDUMn/release/deps/libfutures_util-6cffd21a425d934b.rlib" "/tmp/cargo-installenDUMn/release/deps/libslab-9bfaa30c0ebf721f.rlib" "/tmp/cargo-installenDUMn/release/deps/libpin_project_lite-b73d8c0f74d91250.rlib" "/tmp/cargo-installenDUMn/release/deps/libfutures_task-e50cf4139f364972.rlib" "/tmp/cargo-installenDUMn/release/deps/libpin_utils-03c59ff5d9b0eba9.rlib" "/tmp/cargo-installenDUMn/release/deps/libfutures_channel-8e7f21294c2e50dc.rlib" "/tmp/cargo-installenDUMn/release/deps/libfutures_sink-5e17f5c1cb41aad9.rlib" "/tmp/cargo-installenDUMn/release/deps/libfutures_core-d1d4e3d92cc2a9b7.rlib" "/tmp/cargo-installenDUMn/release/deps/libquote-4fd5fc8bf5649d4f.rlib" "/tmp/cargo-installenDUMn/release/deps/libproc_macro2-5e06fabd4fabd9e9.rlib" "/tmp/cargo-installenDUMn/release/deps/libunicode_ident-bf107af9aebb9a0e.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libproc_macro-0b705ff5175fb36a.rlib" "-Wl,--start-group" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-5670385a2fe8b60b.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-fd56ba6dbf7aaecc.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-b8acf8e5c2e85baa.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-1706edefffdca0a2.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-f483302e0b13708e.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-725b0718fc18e1ed.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-de685fcf2157e6fb.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-f613ac3eda05b9ff.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-3de98a7d049af6a1.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-c12183655bdce152.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-0fc7beea925de7e3.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-8c34825485bf59dc.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-ba5f7e926e729d81.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-643de0950163a839.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-539ea2f72ef89687.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-871432094bb4c885.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-ef1a8ee61f2e39bf.rlib" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-f1646747442c1c7b.rlib" "-Wl,--end-group" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-ff283b4bf550fa1c.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/root/.rustup/toolchains/1.64.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/tmp/cargo-installenDUMn/release/deps/libsqlx_macros-30535d33bb51145b.so" "-Wl,--gc-sections" "-shared" "-Wl,-zrelro,-znow" "-nodefaultlibs"
= note: /usr/bin/ld: /tmp/cargo-installenDUMn/release/deps/libopenssl_sys-721c4e4acd28ae51.rlib(t1_enc.o): relocation R_X86_64_PC32 against symbol stderr@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /tmp/cargo-installenDUMn/release/deps/libopenssl_sys-721c4e4acd28ae51.rlib(cryptlib.o): relocation R_X86_64_PC32 against symbol stderr@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /tmp/cargo-installenDUMn/release/deps/libopenssl_sys-721c4e4acd28ae51.rlib(ui_openssl.o): relocation R_X86_64_PC32 against symbol stdin@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /tmp/cargo-installenDUMn/release/deps/libopenssl_sys-721c4e4acd28ae51.rlib(txt_db.o): relocation R_X86_64_PC32 against symbol stderr@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: nonrepresentable section on output
collect2: error: ld returned 1 exit status

error: could not compile sqlx-macros due to previous error
warning: build failed, waiting for other jobs to finish...
error: failed to compile sqlx-cli v0.6.2, intermediate artifacts can be found at /tmp/cargo-installenDUMn

解消できないから大人しくローカルに Rust 環境構築しよ。
そしてローカルに構築したらビルドとか爆速になって草。今までの待ち時間はなんだったのか。。。
Lima とか使えば VS Code + Remote Container でも早いのだろうか。

  • make dev

cargo watch -x run のところで error: no such subcommand: watchで落ちた。
cargo install cargo-watchしたら無事起動できたけど、
これインストールする手順てあったかな?

ki504178ki504178

4.3 axum と sqlx

TodoRepository の非同期対応

お!なんとなく理解してきたのか、それぞれのファイルの修正がどう影響するのかを少し考えながら、
サンプルコード見なくてもコード書けるところ出てきた。

TodoRepositoryForDb の作成

っぱ、Respository (というか I/O 層)は抽象化してなんぼよね。

あれ?Memory版実装してたときは impl の中でメソッド推論されてメソッド定義丸っと補完してくれてたけど、
なんか今回の実装だと推論しかされない。。。便利だったのに何故?

  • TodoRepositoryForMemory の移動

impl した CreateTodo, Todotest_utils に移動してるけどいんだっけ?と思ったけど、
よく考えたら Memory 版でしか使ってないから移動しても問題ないのか。にゃるほど。
そして pub mod test_utils#[cfg(test)] 付いてるからその中のテストmod testには不要と。

ki504178ki504178

4.4 todo の CRUD

ここらへんは SQL に馴染んでれば特に違和感なく理解できそう。

sqlx

  • query, query_as, bind

誤字

  • query と query_as 違いは、
    • query と query_as 違いは、
ki504178ki504178

4.5 sqlx のテスト

#[cfg(test)] でコンパイル対象を制御できるっての見た時も思ったけど、
更に細やかに制御を簡単に実装できるのはマジで良き。

テスト実装

Repository 単位でシナリオテストしたの初めてな気もするなぁ(ただの感想

feature で DB なしのテストケースのみを実行

  • feature

クレート公開時やコンパイル時に設定を入れておくと、特定の feature フラグが有効になっている場合のみコード生成できる。