Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Run migations on development server start

Conrad Irwin0f7b47af390e
diff --git a/crates/collab/src/lib.rs b/crates/collab/src/lib.rs
index 85216525b00..68fbb4e4d7d 100644
--- a/crates/collab/src/lib.rs
+++ b/crates/collab/src/lib.rs
@@ -103,6 +103,12 @@ pub struct Config {
pub zed_environment: String,
}
+impl Config {
+ pub fn is_development(&self) -> bool {
+ self.zed_environment == "development"
+ }
+}
+
#[derive(Default, Deserialize)]
pub struct MigrateConfig {
pub database_url: String,
diff --git a/crates/collab/src/main.rs b/crates/collab/src/main.rs
index 6fbb451fee7..87df7cac6fc 100644
--- a/crates/collab/src/main.rs
+++ b/crates/collab/src/main.rs
@@ -53,6 +53,25 @@ async fn main() -> Result<()> {
let config = envy::from_env::<Config>().expect("error loading config");
init_tracing(&config);
+ if config.is_development() {
+ // sanity check database url so even if we deploy a busted ZED_ENVIRONMENT to production
+ // we do not run
+ if config.database_url != "postgres://postgres@localhost/zed" {
+ panic!("about to run development migrations on a non-development database?")
+ }
+ let migrations_path = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/migrations"));
+ let db_options = db::ConnectOptions::new(config.database_url.clone());
+ let db = Database::new(db_options, Executor::Production).await?;
+
+ let migrations = db.migrate(&migrations_path, false).await?;
+ for (migration, duration) in migrations {
+ println!(
+ "Ran {} {} {:?}",
+ migration.version, migration.description, duration
+ );
+ }
+ }
+
let state = AppState::new(config).await?;
let listener = TcpListener::bind(&format!("0.0.0.0:{}", state.config.http_port))