diff options
| author | mo khan <mo@mokhan.ca> | 2025-06-11 17:27:59 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-06-11 17:27:59 -0600 |
| commit | 19ca22e604f9bcdf6b25f973f81b2486b0dcb789 (patch) | |
| tree | f410c79c8531d30fe9c6944742eb49633a9d7b0a | |
| parent | 9fc51f0a8312d87b65adb661fc1c6757662d9479 (diff) | |
refactor: use the migrations runner
| -rw-r--r-- | src/database.rs | 155 |
1 files changed, 6 insertions, 149 deletions
diff --git a/src/database.rs b/src/database.rs index 5251dac..a91579b 100644 --- a/src/database.rs +++ b/src/database.rs @@ -103,164 +103,21 @@ impl Database { pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> { let conn = Connection::open(path)?; let db = Self { conn }; - db.initialize_schema()?; + db.run_migrations()?; Ok(db) } pub fn new_in_memory() -> Result<Self> { let conn = Connection::open_in_memory()?; let db = Self { conn }; - db.initialize_schema()?; + db.run_migrations()?; Ok(db) } - fn initialize_schema(&self) -> Result<()> { - // OAuth Clients table - self.conn.execute( - "CREATE TABLE IF NOT EXISTS oauth_clients ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - client_id TEXT NOT NULL UNIQUE, - client_secret_hash TEXT NOT NULL, - client_name TEXT NOT NULL, - redirect_uris TEXT NOT NULL, -- JSON array - scopes TEXT NOT NULL, -- space-separated - grant_types TEXT NOT NULL, -- space-separated - response_types TEXT NOT NULL, -- space-separated - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - is_active BOOLEAN NOT NULL DEFAULT 1 - )", - [], - )?; - - // Authorization Codes table - self.conn.execute( - "CREATE TABLE IF NOT EXISTS auth_codes ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - code TEXT NOT NULL UNIQUE, - client_id TEXT NOT NULL, - user_id TEXT NOT NULL, - redirect_uri TEXT NOT NULL, - scope TEXT, - expires_at TEXT NOT NULL, - created_at TEXT NOT NULL, - is_used BOOLEAN NOT NULL DEFAULT 0, - code_challenge TEXT, - code_challenge_method TEXT, - FOREIGN KEY (client_id) REFERENCES oauth_clients (client_id) - )", - [], - )?; - - // Access Tokens table - self.conn.execute( - "CREATE TABLE IF NOT EXISTS access_tokens ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - token_id TEXT NOT NULL UNIQUE, - client_id TEXT NOT NULL, - user_id TEXT NOT NULL, - scope TEXT, - expires_at TEXT NOT NULL, - created_at TEXT NOT NULL, - is_revoked BOOLEAN NOT NULL DEFAULT 0, - token_hash TEXT NOT NULL, - FOREIGN KEY (client_id) REFERENCES oauth_clients (client_id) - )", - [], - )?; - - // Refresh Tokens table - self.conn.execute( - "CREATE TABLE IF NOT EXISTS refresh_tokens ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - token_id TEXT NOT NULL UNIQUE, - access_token_id INTEGER NOT NULL, - client_id TEXT NOT NULL, - user_id TEXT NOT NULL, - scope TEXT, - expires_at TEXT NOT NULL, - created_at TEXT NOT NULL, - is_revoked BOOLEAN NOT NULL DEFAULT 0, - token_hash TEXT NOT NULL, - FOREIGN KEY (client_id) REFERENCES oauth_clients (client_id), - FOREIGN KEY (access_token_id) REFERENCES access_tokens (id) - )", - [], - )?; - - // RSA Keys table - self.conn.execute( - "CREATE TABLE IF NOT EXISTS rsa_keys ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - kid TEXT NOT NULL UNIQUE, - private_key_pem TEXT NOT NULL, - public_key_pem TEXT NOT NULL, - created_at TEXT NOT NULL, - is_current BOOLEAN NOT NULL DEFAULT 0 - )", - [], - )?; - - // Audit Log table - self.conn.execute( - "CREATE TABLE IF NOT EXISTS audit_logs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - event_type TEXT NOT NULL, - client_id TEXT, - user_id TEXT, - ip_address TEXT, - user_agent TEXT, - details TEXT, -- JSON - created_at TEXT NOT NULL, - success BOOLEAN NOT NULL - )", - [], - )?; - - // Rate Limiting table - self.conn.execute( - "CREATE TABLE IF NOT EXISTS rate_limits ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - identifier TEXT NOT NULL, -- client_id or IP - endpoint TEXT NOT NULL, - count INTEGER NOT NULL DEFAULT 1, - window_start TEXT NOT NULL, - created_at TEXT NOT NULL, - UNIQUE (identifier, endpoint, window_start) - )", - [], - )?; - - // Create indexes for performance - self.conn.execute( - "CREATE INDEX IF NOT EXISTS idx_auth_codes_client_id ON auth_codes (client_id)", - [], - )?; - self.conn.execute( - "CREATE INDEX IF NOT EXISTS idx_auth_codes_expires_at ON auth_codes (expires_at)", - [], - )?; - self.conn.execute( - "CREATE INDEX IF NOT EXISTS idx_access_tokens_client_id ON access_tokens (client_id)", - [], - )?; - self.conn.execute( - "CREATE INDEX IF NOT EXISTS idx_access_tokens_expires_at ON access_tokens (expires_at)", - [], - )?; - self.conn.execute( - "CREATE INDEX IF NOT EXISTS idx_refresh_tokens_client_id ON refresh_tokens (client_id)", - [], - )?; - self.conn.execute( - "CREATE INDEX IF NOT EXISTS idx_audit_logs_created_at ON audit_logs (created_at)", - [], - )?; - self.conn.execute( - "CREATE INDEX IF NOT EXISTS idx_rate_limits_identifier ON rate_limits (identifier, endpoint)", - [], - )?; - + fn run_migrations(&self) -> Result<()> { + // Use the migration system instead of duplicated schema + let migration_runner = crate::migrations::MigrationRunner::new(&self.conn); + migration_runner.run_migrations()?; Ok(()) } |
