Skip to content

Commit

Permalink
doc: update the example to use lazylock instead of lazy_static
Browse files Browse the repository at this point in the history
LazyLock was introduced in the latest version of the Rust compiler,
1.80. It’s similar enough to lazy_static that folks still using it will
figure out how to. In any case, an old example is linked in the README.
  • Loading branch information
cljoly committed Feb 1, 2025
1 parent 8889349 commit fc29b7d
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 49 deletions.
7 changes: 1 addition & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,9 @@ Please see the [examples](https://github.com/cljoly/rusqlite_migrate/tree/master
- `async` migrations in the [`quick_start_async.rs`][quick_start_async] file
- migrations with multiple SQL statements (using for instance `r#"…"` or `include_str!(…)`)
- migrations defined [from a directory][from_dir] with SQL files
- use of [lazy_static][]
- use of [`LazyLock`][lazy_lock] (or [lazy_static][] with older versions of Rust)
- migrations to [previous versions (downward migrations)][generic_example]

[quick_start_async]: https://github.com/cljoly/rusqlite_migration/blob/master/examples/async/src/main.rs
[from_dir]: https://github.com/cljoly/rusqlite_migration/tree/master/examples/from-directory
[lazy_static]: https://github.com/cljoly/rusqlite_migration/blob/f3d19847065b890efe73c27393b2980d1571f871/examples/simple/src/main.rs#L18
[generic_example]: https://github.com/cljoly/rusqlite_migration/blob/master/examples/simple/src/main.rs

I’ve also made a [cheatsheet of SQLite pragma for improved performance and consistency][cheat].

### Built-in tests
Expand Down Expand Up @@ -176,6 +171,7 @@ Thanks to [Migadu](https://www.migadu.com/) for offering a discounted service to
[safety-dance]: https://github.com/rust-secure-code/safety-dance/
[cio]: https://crates.io/crates/rusqlite_migration
[cio_reverse]: https://crates.io/crates/rusqlite_migration/reverse_dependencies
[lazy_lock]: https://doc.rust-lang.org/std/sync/struct.LazyLock.html
[lrs_reverse]: https://lib.rs/crates/rusqlite_migration/rev
[gh_reverse]: https://github.com/cljoly/rusqlite_migration/network/dependents?dependent_type=REPOSITORY
[contributing]: https://cj.rs/docs/contribute/
Expand All @@ -190,3 +186,7 @@ Thanks to [Migadu](https://www.migadu.com/) for offering a discounted service to
[cheat]: https://cj.rs/blog/sqlite-pragma-cheatsheet-for-performance-and-consistency/
[docs]: https://docs.rs/rusqlite_migration
[msrv]: https://github.com/rusqlite/rusqlite?tab=readme-ov-file#minimum-supported-rust-version-msrv
[quick_start_async]: https://github.com/cljoly/rusqlite_migration/blob/master/examples/async/src/main.rs
[from_dir]: https://github.com/cljoly/rusqlite_migration/tree/master/examples/from-directory
[lazy_static]: https://github.com/cljoly/rusqlite_migration/blob/f3d19847065b890efe73c27393b2980d1571f871/examples/simple/src/main.rs#L18
[generic_example]: https://github.com/cljoly/rusqlite_migration/blob/master/examples/simple/src/main.rs
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
doc-valid-idents = ["SQLite", "rusqlite_migration", "serde_rusqlite", "lazy_static", ".."]
doc-valid-idents = ["SQLite", "rusqlite_migration", "serde_rusqlite", ".."]
1 change: 0 additions & 1 deletion examples/async/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ log = "0.4"
simple-logging = "2.0.2"
env_logger = "0.11"
anyhow = "1"
lazy_static = "1.5.0"
mktemp = "0.5"
tokio-rusqlite = "0.6.0"
tokio = { version = "1.43.0", features = ["full"] }
Expand Down
10 changes: 5 additions & 5 deletions examples/async/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::LazyLock;

use anyhow::Result;
use lazy_static::lazy_static;
use rusqlite::params;
use rusqlite_migration::{AsyncMigrations, M};
use tokio_rusqlite::Connection;
Expand All @@ -16,8 +17,7 @@ mod tests {
}

// Define migrations. These are applied atomically.
lazy_static! {
static ref MIGRATIONS: AsyncMigrations =
static MIGRATIONS: LazyLock<AsyncMigrations> = LazyLock::new(|| {
AsyncMigrations::new(vec![
M::up(include_str!("../../friend_car.sql")),
// PRAGMA are better applied outside of migrations, see below for details.
Expand All @@ -34,8 +34,8 @@ lazy_static! {
// migrations here, like so:
// M::up("CREATE INDEX UX_friend_email ON friend(email);"),
// M::up("CREATE INDEX UX_friend_name ON friend(name);"),
]);
}
])
});

pub async fn init_db() -> Result<Connection> {
let mut async_conn = Connection::open("./my_db.db3").await?;
Expand Down
1 change: 0 additions & 1 deletion examples/from-directory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ log = "0.4"
simple-logging = "2.0.2"
env_logger = "0.11"
anyhow = "1"
lazy_static = "1.5.0"
mktemp = "0.5"
include_dir = "0.7.4"

Expand Down
9 changes: 4 additions & 5 deletions examples/from-directory/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use std::sync::LazyLock;

use anyhow::Result;
use include_dir::{include_dir, Dir};
use lazy_static::lazy_static;
use rusqlite::{params, Connection};
use rusqlite_migration::Migrations;

static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations");

// Define migrations. These are applied atomically.
lazy_static! {
static ref MIGRATIONS: Migrations<'static> =
Migrations::from_directory(&MIGRATIONS_DIR).unwrap();
}
static MIGRATIONS: LazyLock<Migrations<'static>> =
LazyLock::new(|| Migrations::from_directory(&MIGRATIONS_DIR).unwrap());

pub fn init_db() -> Result<Connection> {
let mut conn = Connection::open("./my_db.db3")?;
Expand Down
1 change: 0 additions & 1 deletion examples/simple/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ log = "0.4"
simple-logging = "2.0.2"
env_logger = "0.11"
anyhow = "1"
lazy_static = "1.5.0"
mktemp = "0.5"

[dependencies.rusqlite]
Expand Down
41 changes: 20 additions & 21 deletions examples/simple/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::LazyLock;

use anyhow::Result;
use lazy_static::lazy_static;
use rusqlite::{params, Connection};
use rusqlite_migration::{Migrations, M};

Expand All @@ -15,26 +16,24 @@ mod tests {
}

// Define migrations. These are applied atomically.
lazy_static! {
static ref MIGRATIONS: Migrations<'static> =
Migrations::new(vec![
M::up(include_str!("../../friend_car.sql")),
// PRAGMA are better applied outside of migrations, see below for details.
M::up(r#"
ALTER TABLE friend ADD COLUMN birthday TEXT;
ALTER TABLE friend ADD COLUMN comment TEXT;
"#),

// This migration can be reverted
M::up("CREATE TABLE animal(name TEXT);")
.down("DROP TABLE animal;")

// In the future, if the need to change the schema arises, put
// migrations here, like so:
// M::up("CREATE INDEX UX_friend_email ON friend(email);"),
// M::up("CREATE INDEX UX_friend_name ON friend(name);"),
]);
}
static MIGRATIONS: LazyLock<Migrations<'static>> = LazyLock::new(|| {
Migrations::new(vec![
M::up(include_str!("../../friend_car.sql")),
// PRAGMA are better applied outside of migrations, see below for details.
M::up(
r#"
ALTER TABLE friend ADD COLUMN birthday TEXT;
ALTER TABLE friend ADD COLUMN comment TEXT;
"#,
),
// This migration can be reverted
M::up("CREATE TABLE animal(name TEXT);").down("DROP TABLE animal;"),
// In the future, if the need to change the schema arises, put
// migrations here, like so:
// M::up("CREATE INDEX UX_friend_email ON friend(email);"),
// M::up("CREATE INDEX UX_friend_name ON friend(name);"),
])
});

pub fn init_db() -> Result<Connection> {
let mut conn = Connection::open("./my_db.db3")?;
Expand Down
1 change: 0 additions & 1 deletion rusqlite_migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ anyhow = "1"
env_logger = "0.11"
iai = "0.1"
insta = "1.41.1"
lazy_static = "1.5.0"
mktemp = "0.5"
mutants = "0.0.3"
simple-logging = "2.0.2"
Expand Down
1 change: 0 additions & 1 deletion rusqlite_migration_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ tokio-test = "0.4.4"
simple-logging = "2.0.2"
env_logger = "0.11"
anyhow = "1"
lazy_static = "1.5.0"
mktemp = "0.5"
include_dir = "0.7.4"

Expand Down

0 comments on commit fc29b7d

Please sign in to comment.