Skip to content

Commit 211c06b

Browse files
committed
stuff
1 parent 7292ffa commit 211c06b

1 file changed

Lines changed: 63 additions & 8 deletions

File tree

  • crates/diesel_utils/src/schema_setup

crates/diesel_utils/src/schema_setup/mod.rs

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ use diesel::{
1010
RunQueryDsl,
1111
connection::SimpleConnection,
1212
dsl::exists,
13-
migration::{Migration, MigrationVersion},
13+
migration::{Migration, MigrationSource, MigrationVersion},
1414
pg::Pg,
1515
select,
1616
update,
1717
};
18-
use diesel_migrations::MigrationHarness;
18+
use diesel_migrations::{EmbeddedMigrations, MigrationHarness};
1919
use std::time::Instant;
2020
use tracing::debug;
2121

22+
// TODO: add duration logging to diesel
23+
2224
diesel::table! {
2325
pg_namespace (nspname) {
2426
nspname -> Text,
@@ -32,10 +34,62 @@ diesel::table! {
3234
}
3335
}
3436

35-
fn migrations() -> diesel_migrations::EmbeddedMigrations {
36-
// Using `const` here is required by the borrow checker
37-
const MIGRATIONS: diesel_migrations::EmbeddedMigrations = diesel_migrations::embed_migrations!();
38-
MIGRATIONS
37+
struct MigrationWrapper {
38+
embedded_migration: Box<dyn Migration<Pg>>,
39+
}
40+
41+
impl Migration<Pg> for MigrationWrapper {
42+
fn run(
43+
&self,
44+
conn: &mut dyn diesel::connection::BoxableConnection<Pg>,
45+
) -> diesel::migration::Result<()> {
46+
// Drop `r` schema, so migrations don't need to be made to work both with and without things in
47+
// it existing
48+
revert_replaceable_schema(conn)?;
49+
50+
self.embedded_migration.run(conn)
51+
}
52+
53+
fn revert(
54+
&self,
55+
conn: &mut dyn diesel::connection::BoxableConnection<Pg>,
56+
) -> diesel::migration::Result<()> {
57+
// Drop `r` schema, so migrations don't need to be made to work both with and without things in
58+
// it existing
59+
revert_replaceable_schema(conn)?;
60+
61+
self.embedded_migration.revert(conn)
62+
}
63+
64+
fn metadata(&self) -> &dyn diesel::migration::MigrationMetadata {
65+
self.embedded_migration.metadata()
66+
}
67+
68+
fn name(&self) -> &dyn diesel::migration::MigrationName {
69+
self.embedded_migration.name()
70+
}
71+
}
72+
73+
struct LemmyMigrations;
74+
75+
impl MigrationSource<Pg> for LemmyMigrations {
76+
fn migrations(&self) -> diesel::migration::Result<Vec<Box<dyn Migration<Pg>>>> {
77+
// Using `const` here is required by the borrow checker
78+
const MIGRATIONS: diesel_migrations::EmbeddedMigrations =
79+
diesel_migrations::embed_migrations!();
80+
81+
Ok(
82+
MIGRATIONS
83+
.migrations()?
84+
.into_iter()
85+
.map(|migration| -> Box<dyn Migration<Pg>> {
86+
Box::new(MigrationWrapper {
87+
embedded_migration: migration,
88+
})
89+
})
90+
.collect(),
91+
)
92+
}
3993
}
4094

4195
/// This SQL code sets up the `r` schema, which contains things that can be safely dropped and
@@ -192,7 +246,7 @@ pub fn run(options: Options, db_url: &str) -> anyhow::Result<Branch> {
192246
&& options.run
193247
&& options.limit.is_none()
194248
&& !conn
195-
.has_pending_migration(migrations())
249+
.has_pending_migration(LemmyMigrations)
196250
.map_err(convert_err)?
197251
{
198252
// The condition above implies that the migration that creates the previously_run_sql table was
@@ -211,6 +265,7 @@ pub fn run(options: Options, db_url: &str) -> anyhow::Result<Branch> {
211265
// Block concurrent attempts to run migrations until `conn` is closed, and disable the
212266
// trigger that prevents the Diesel CLI from running migrations
213267
options.print("Waiting for lock...");
268+
// TODO: maybe make diesel do something similar, and disable the trigger in some other way:
214269
conn.batch_execute("SELECT pg_advisory_lock(0);")?;
215270
options.print("Running Database migrations (This may take a long time)...");
216271

@@ -271,7 +326,7 @@ fn run_replaceable_schema(conn: &mut PgConnection) -> anyhow::Result<()> {
271326
})
272327
}
273328

274-
fn revert_replaceable_schema(conn: &mut PgConnection) -> anyhow::Result<()> {
329+
fn revert_replaceable_schema(conn: &mut (impl SimpleConnection + ?Sized)) -> anyhow::Result<()> {
275330
conn
276331
.batch_execute("DROP SCHEMA IF EXISTS r CASCADE;")
277332
.with_context(|| format!("Failed to revert SQL files in {REPLACEABLE_SCHEMA_PATH}"))?;

0 commit comments

Comments
 (0)