Skip to content

Commit

Permalink
feat: don't run Tokenserver migrations on startup (#1286)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethowitz authored Apr 25, 2022
1 parent 37d2251 commit 1a197a6
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ python:
venv/bin/python -m pip install -r requirements.txt

run: python
PATH=./venv/bin:$(PATH) RUST_LOG=debug RUST_BACKTRACE=full cargo run -- --config config/local.toml
PATH="./venv/bin:$(PATH)" RUST_LOG=debug RUST_BACKTRACE=full cargo run -- --config config/local.toml

run_spanner:
GOOGLE_APPLICATION_CREDENTIALS=$(PATH_TO_SYNC_SPANNER_KEYS) GRPC_DEFAULT_SSL_ROOTS_FILE_PATH=$(PATH_TO_GRPC_CERT) make run
Expand Down
1 change: 1 addition & 0 deletions docker-compose.e2e.mysql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ services:
SYNC_TOKENSERVER__FXA_BROWSERID_ISSUER: "api-accounts.stage.mozaws.net"
SYNC_TOKENSERVER__FXA_EMAIL_DOMAIN: api-accounts.stage.mozaws.net
SYNC_TOKENSERVER__FXA_METRICS_HASH_SECRET: secret0
SYNC_TOKENSERVER__RUN_MIGRATIONS: "true"
TOKENSERVER_HOST: http://localhost:8000
entrypoint: >
/bin/sh -c "
Expand Down
1 change: 1 addition & 0 deletions docker-compose.e2e.spanner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ services:
SYNC_TOKENSERVER__FXA_BROWSERID_ISSUER: "api-accounts.stage.mozaws.net"
SYNC_TOKENSERVER__FXA_EMAIL_DOMAIN: api-accounts.stage.mozaws.net
SYNC_TOKENSERVER__FXA_METRICS_HASH_SECRET: secret0
SYNC_TOKENSERVER__RUN_MIGRATIONS: "true"
TOKENSERVER_HOST: http://localhost:8000
entrypoint: >
/bin/sh -c "
Expand Down
1 change: 1 addition & 0 deletions docker-compose.mysql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ services:
SYNC_MASTER_SECRET: secret0
SYNC_DATABASE_URL: mysql://test:test@sync-db:3306/syncstorage
SYNC_TOKENSERVER__DATABASE_URL: mysql://test:test@tokenserver-db:3306/tokenserver
SYNC_TOKENSERVER__RUN_MIGRATIONS: "true"

volumes:
sync_db_data:
Expand Down
23 changes: 12 additions & 11 deletions docker-compose.spanner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
sync-db-setup:
image: app:build
depends_on:
- sync-db
- sync-db
restart: "no"
entrypoint: "/app/scripts/prepare-spanner.sh"
environment:
Expand Down Expand Up @@ -39,19 +39,20 @@ services:
image: ${SYNCSTORAGE_RS_IMAGE:-syncstorage-rs:latest}
restart: always
ports:
- "8000:8000"
- "8000:8000"
depends_on:
- sync-db-setup
- sync-db-setup
environment:
SYNC_HOST: 0.0.0.0
SYNC_MASTER_SECRET: secret0
SYNC_DATABASE_URL: spanner://projects/test-project/instances/test-instance/databases/test-database
SYNC_SPANNER_EMULATOR_HOST: sync-db:9010
SYNC_TOKENSERVER__DATABASE_URL: mysql://test:test@tokenserver-db:3306/tokenserver
SYNC_HOST: 0.0.0.0
SYNC_MASTER_SECRET: secret0
SYNC_DATABASE_URL: spanner://projects/test-project/instances/test-instance/databases/test-database
SYNC_SPANNER_EMULATOR_HOST: sync-db:9010
SYNC_TOKENSERVER__DATABASE_URL: mysql://test:test@tokenserver-db:3306/tokenserver
SYNC_TOKENSERVER__RUN_MIGRATIONS: "true"

volumes:
tokenserver_db_data:

# Application runs off of port 8000.
# you can test if it's available with
# curl "http://localhost:8000/__heartbeat__"
# Application runs off of port 8000.
# you can test if it's available with
# curl "http://localhost:8000/__heartbeat__"
1 change: 1 addition & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ impl Settings {
s.set_default("tokenserver.fxa_oauth_request_timeout", 10)?;
s.set_default("tokenserver.node_type", "spanner")?;
s.set_default("tokenserver.statsd_label", "syncstorage.tokenserver")?;
s.set_default("tokenserver.run_migrations", cfg!(test))?;

// Set Cors defaults
s.set_default(
Expand Down
10 changes: 4 additions & 6 deletions src/tokenserver/db/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ use async_trait::async_trait;
use diesel::{
mysql::MysqlConnection,
r2d2::{ConnectionManager, Pool},
Connection,
};
#[cfg(test)]
use diesel_logger::LoggingConnection;
use std::time::Duration;

use super::models::{Db, DbResult, TokenserverDb};
use crate::db::{error::DbError, DbErrorKind};
use crate::diesel::Connection;
use crate::server::metrics::Metrics;
use crate::tokenserver::settings::Settings;

Expand All @@ -26,10 +25,7 @@ embed_migrations!("src/tokenserver/migrations");
pub fn run_embedded_migrations(database_url: &str) -> DbResult<()> {
let conn = MysqlConnection::establish(database_url)?;

#[cfg(test)]
embedded_migrations::run(&LoggingConnection::new(conn))?;
#[cfg(not(test))]
embedded_migrations::run(&conn)?;

Ok(())
}
Expand All @@ -47,7 +43,9 @@ impl TokenserverPool {
metrics: &Metrics,
_use_test_transactions: bool,
) -> DbResult<Self> {
run_embedded_migrations(&settings.database_url)?;
if settings.run_migrations {
run_embedded_migrations(&settings.database_url)?;
}

let manager = ConnectionManager::<MysqlConnection>::new(settings.database_url.clone());
let builder = Pool::builder()
Expand Down
3 changes: 3 additions & 0 deletions src/tokenserver/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub struct Settings {
pub node_type: NodeType,
/// The label to be used when reporting Metrics.
pub statsd_label: String,
/// Whether or not to run the Tokenserver migrations upon startup.
pub run_migrations: bool,
}

impl Default for Settings {
Expand All @@ -68,6 +70,7 @@ impl Default for Settings {
node_capacity_release_rate: None,
node_type: NodeType::Spanner,
statsd_label: "syncstorage.tokenserver".to_owned(),
run_migrations: cfg!(test),
}
}
}

0 comments on commit 1a197a6

Please sign in to comment.