Skip to content

Commit 675eacf

Browse files
committed
feat: Refactor SQL queries and models to use 'auth' schema
- Updated SQL queries in `auth.sql` and `auth.sql.go` to reference the 'auth' schema for all tables, ensuring proper namespace usage. - Renamed data models in `models.go` to include 'Auth' prefix for clarity and consistency. - Adjusted repository and service methods to utilize the updated models, enhancing type safety and readability. - Modified migration scripts to create and drop tables within the 'auth' schema, ensuring proper database structure. - Updated mock repository methods to reflect changes in model names, maintaining test integrity. Signed-off-by: Christian Melgarejo <cmelgarejo@users.noreply.github.com>
1 parent 795ecb6 commit 675eacf

21 files changed

Lines changed: 457 additions & 284 deletions

cmd/visualize/analyzer/analyzer.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,9 @@ func analyzeEventConnections(projectRoot, modulesDir string, graph *Graph) error
358358
// Find publisher
359359
if publisher, ok := eventPublishers[eventName]; ok && publisher != moduleName {
360360
graph.Connections = append(graph.Connections, Connection{
361-
From: publisher,
362-
To: moduleName,
363-
Type: "event",
361+
From: publisher,
362+
To: moduleName,
363+
Type: "event",
364364
Event: eventName,
365365
})
366366
}
@@ -501,4 +501,3 @@ func updateModuleEvents(graph *Graph, moduleName, eventName string) {
501501
}
502502
}
503503
}
504-

modules/auth/internal/db/query/auth.sql

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,158 +3,158 @@
33
-- ========================
44

55
-- name: CreateUser :exec
6-
INSERT INTO users (id, email, phone) VALUES ($1, $2, $3);
6+
INSERT INTO auth.users (id, email, phone) VALUES ($1, $2, $3);
77

88
-- name: GetUserByID :one
9-
SELECT * FROM users WHERE id = $1 LIMIT 1;
9+
SELECT * FROM auth.users WHERE id = $1 LIMIT 1;
1010

1111
-- name: GetUserByEmail :one
12-
SELECT * FROM users WHERE email = $1 LIMIT 1;
12+
SELECT * FROM auth.users WHERE email = $1 LIMIT 1;
1313

1414
-- name: GetUserByPhone :one
15-
SELECT * FROM users WHERE phone = $1 LIMIT 1;
15+
SELECT * FROM auth.users WHERE phone = $1 LIMIT 1;
1616

1717
-- name: UpdateUserProfile :exec
18-
UPDATE users SET display_name = $2, avatar_url = $3, updated_at = CURRENT_TIMESTAMP WHERE id = $1;
18+
UPDATE auth.users SET display_name = $2, avatar_url = $3, updated_at = CURRENT_TIMESTAMP WHERE id = $1;
1919

2020
-- ========================
2121
-- Magic Codes (Passwordless)
2222
-- ========================
2323

2424
-- name: CreateMagicCode :exec
25-
INSERT INTO magic_codes (code, user_email, user_phone, expires_at) VALUES ($1, $2, $3, $4);
25+
INSERT INTO auth.magic_codes (code, user_email, user_phone, expires_at) VALUES ($1, $2, $3, $4);
2626

2727
-- name: GetValidMagicCodeByEmail :one
28-
SELECT * FROM magic_codes
28+
SELECT * FROM auth.magic_codes
2929
WHERE user_email = $1 AND code = $2 AND expires_at > $3
3030
ORDER BY created_at DESC LIMIT 1;
3131

3232
-- name: GetValidMagicCodeByPhone :one
33-
SELECT * FROM magic_codes
33+
SELECT * FROM auth.magic_codes
3434
WHERE user_phone = $1 AND code = $2 AND expires_at > $3
3535
ORDER BY created_at DESC LIMIT 1;
3636

3737
-- name: DeleteMagicCodesByEmail :exec
38-
DELETE FROM magic_codes WHERE user_email = $1;
38+
DELETE FROM auth.magic_codes WHERE user_email = $1;
3939

4040
-- name: DeleteMagicCodesByPhone :exec
41-
DELETE FROM magic_codes WHERE user_phone = $1;
41+
DELETE FROM auth.magic_codes WHERE user_phone = $1;
4242

4343
-- name: CleanupExpiredMagicCodes :exec
44-
DELETE FROM magic_codes WHERE expires_at < CURRENT_TIMESTAMP;
44+
DELETE FROM auth.magic_codes WHERE expires_at < CURRENT_TIMESTAMP;
4545

4646
-- ========================
4747
-- Sessions
4848
-- ========================
4949

5050
-- name: CreateSession :exec
51-
INSERT INTO sessions (id, user_id, refresh_token_hash, user_agent, ip_address, expires_at)
51+
INSERT INTO auth.sessions (id, user_id, refresh_token_hash, user_agent, ip_address, expires_at)
5252
VALUES ($1, $2, $3, $4, $5, $6);
5353

5454
-- name: GetSessionByID :one
55-
SELECT * FROM sessions WHERE id = $1 AND revoked_at IS NULL LIMIT 1;
55+
SELECT * FROM auth.sessions WHERE id = $1 AND revoked_at IS NULL LIMIT 1;
5656

5757
-- name: GetSessionByRefreshTokenHash :one
58-
SELECT * FROM sessions WHERE refresh_token_hash = $1 AND revoked_at IS NULL AND expires_at > CURRENT_TIMESTAMP LIMIT 1;
58+
SELECT * FROM auth.sessions WHERE refresh_token_hash = $1 AND revoked_at IS NULL AND expires_at > CURRENT_TIMESTAMP LIMIT 1;
5959

6060
-- name: GetSessionsByUserID :many
61-
SELECT * FROM sessions WHERE user_id = $1 AND revoked_at IS NULL AND expires_at > CURRENT_TIMESTAMP ORDER BY last_active_at DESC;
61+
SELECT * FROM auth.sessions WHERE user_id = $1 AND revoked_at IS NULL AND expires_at > CURRENT_TIMESTAMP ORDER BY last_active_at DESC;
6262

6363
-- name: UpdateSessionActivity :exec
64-
UPDATE sessions SET last_active_at = CURRENT_TIMESTAMP WHERE id = $1;
64+
UPDATE auth.sessions SET last_active_at = CURRENT_TIMESTAMP WHERE id = $1;
6565

6666
-- name: RevokeSession :exec
67-
UPDATE sessions SET revoked_at = CURRENT_TIMESTAMP WHERE id = $1;
67+
UPDATE auth.sessions SET revoked_at = CURRENT_TIMESTAMP WHERE id = $1;
6868

6969
-- name: RevokeAllUserSessions :execrows
70-
UPDATE sessions SET revoked_at = CURRENT_TIMESTAMP WHERE user_id = $1 AND revoked_at IS NULL AND ($2 = '' OR id != $2);
70+
UPDATE auth.sessions SET revoked_at = CURRENT_TIMESTAMP WHERE user_id = $1 AND revoked_at IS NULL AND ($2 = '' OR id != $2);
7171

7272
-- name: CleanupExpiredSessions :exec
73-
DELETE FROM sessions WHERE expires_at < CURRENT_TIMESTAMP - INTERVAL '7 days';
73+
DELETE FROM auth.sessions WHERE expires_at < CURRENT_TIMESTAMP - INTERVAL '7 days';
7474

7575
-- ========================
7676
-- Token Blacklist
7777
-- ========================
7878

7979
-- name: BlacklistToken :exec
80-
INSERT INTO token_blacklist (token_hash, user_id, expires_at, reason)
80+
INSERT INTO auth.token_blacklist (token_hash, user_id, expires_at, reason)
8181
VALUES ($1, $2, $3, $4)
8282
ON CONFLICT (token_hash) DO NOTHING;
8383

8484
-- name: IsTokenBlacklisted :one
85-
SELECT EXISTS(SELECT 1 FROM token_blacklist WHERE token_hash = $1 AND expires_at > CURRENT_TIMESTAMP);
85+
SELECT EXISTS(SELECT 1 FROM auth.token_blacklist WHERE token_hash = $1 AND expires_at > CURRENT_TIMESTAMP);
8686

8787
-- name: CleanupExpiredBlacklistEntries :exec
88-
DELETE FROM token_blacklist WHERE expires_at < CURRENT_TIMESTAMP;
88+
DELETE FROM auth.token_blacklist WHERE expires_at < CURRENT_TIMESTAMP;
8989

9090
-- ========================
9191
-- Pending Contact Changes
9292
-- ========================
9393

9494
-- name: CreatePendingContactChange :exec
95-
INSERT INTO pending_contact_changes (id, user_id, change_type, new_value, verification_code, expires_at)
95+
INSERT INTO auth.pending_contact_changes (id, user_id, change_type, new_value, verification_code, expires_at)
9696
VALUES ($1, $2, $3, $4, $5, $6);
9797

9898
-- name: GetPendingContactChange :one
99-
SELECT * FROM pending_contact_changes
99+
SELECT * FROM auth.pending_contact_changes
100100
WHERE user_id = $1 AND change_type = $2 AND verification_code = $3 AND expires_at > CURRENT_TIMESTAMP
101101
LIMIT 1;
102102

103103
-- name: DeletePendingContactChange :exec
104-
DELETE FROM pending_contact_changes WHERE id = $1;
104+
DELETE FROM auth.pending_contact_changes WHERE id = $1;
105105

106106
-- name: DeleteExpiredPendingContactChanges :exec
107-
DELETE FROM pending_contact_changes WHERE expires_at < CURRENT_TIMESTAMP;
107+
DELETE FROM auth.pending_contact_changes WHERE expires_at < CURRENT_TIMESTAMP;
108108

109109
-- ========================
110110
-- External OAuth Accounts
111111
-- ========================
112112

113113
-- name: CreateExternalAccount :exec
114-
INSERT INTO user_external_accounts (id, user_id, provider, provider_user_id, email, name, avatar_url, access_token, refresh_token, token_expires_at, raw_data)
114+
INSERT INTO auth.user_external_accounts (id, user_id, provider, provider_user_id, email, name, avatar_url, access_token, refresh_token, token_expires_at, raw_data)
115115
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11);
116116

117117
-- name: GetExternalAccountByProviderAndUserID :one
118-
SELECT * FROM user_external_accounts WHERE provider = $1 AND provider_user_id = $2 LIMIT 1;
118+
SELECT * FROM auth.user_external_accounts WHERE provider = $1 AND provider_user_id = $2 LIMIT 1;
119119

120120
-- name: GetExternalAccountsByUserID :many
121-
SELECT * FROM user_external_accounts WHERE user_id = $1 ORDER BY created_at DESC;
121+
SELECT * FROM auth.user_external_accounts WHERE user_id = $1 ORDER BY created_at DESC;
122122

123123
-- name: GetExternalAccountByProviderAndEmail :one
124-
SELECT * FROM user_external_accounts WHERE provider = $1 AND email = $2 LIMIT 1;
124+
SELECT * FROM auth.user_external_accounts WHERE provider = $1 AND email = $2 LIMIT 1;
125125

126126
-- name: UpdateExternalAccountTokens :exec
127-
UPDATE user_external_accounts
127+
UPDATE auth.user_external_accounts
128128
SET access_token = $3, refresh_token = $4, token_expires_at = $5, updated_at = CURRENT_TIMESTAMP
129129
WHERE provider = $1 AND provider_user_id = $2;
130130

131131
-- name: UpdateExternalAccountProfile :exec
132-
UPDATE user_external_accounts
132+
UPDATE auth.user_external_accounts
133133
SET name = $3, avatar_url = $4, email = $5, raw_data = $6, updated_at = CURRENT_TIMESTAMP
134134
WHERE provider = $1 AND provider_user_id = $2;
135135

136136
-- name: DeleteExternalAccount :exec
137-
DELETE FROM user_external_accounts WHERE id = $1 AND user_id = $2;
137+
DELETE FROM auth.user_external_accounts WHERE id = $1 AND user_id = $2;
138138

139139
-- name: DeleteExternalAccountByProvider :exec
140-
DELETE FROM user_external_accounts WHERE user_id = $1 AND provider = $2;
140+
DELETE FROM auth.user_external_accounts WHERE user_id = $1 AND provider = $2;
141141

142142
-- name: CountExternalAccountsByUserID :one
143-
SELECT COUNT(*) FROM user_external_accounts WHERE user_id = $1;
143+
SELECT COUNT(*) FROM auth.user_external_accounts WHERE user_id = $1;
144144

145145
-- ========================
146146
-- OAuth State Tokens
147147
-- ========================
148148

149149
-- name: CreateOAuthState :exec
150-
INSERT INTO oauth_states (state, provider, redirect_url, user_id, action, expires_at)
150+
INSERT INTO auth.oauth_states (state, provider, redirect_url, user_id, action, expires_at)
151151
VALUES ($1, $2, $3, $4, $5, $6);
152152

153153
-- name: GetOAuthState :one
154-
SELECT * FROM oauth_states WHERE state = $1 AND expires_at > CURRENT_TIMESTAMP LIMIT 1;
154+
SELECT * FROM auth.oauth_states WHERE state = $1 AND expires_at > CURRENT_TIMESTAMP LIMIT 1;
155155

156156
-- name: DeleteOAuthState :exec
157-
DELETE FROM oauth_states WHERE state = $1;
157+
DELETE FROM auth.oauth_states WHERE state = $1;
158158

159159
-- name: CleanupExpiredOAuthStates :exec
160-
DELETE FROM oauth_states WHERE expires_at < CURRENT_TIMESTAMP;
160+
DELETE FROM auth.oauth_states WHERE expires_at < CURRENT_TIMESTAMP;

0 commit comments

Comments
 (0)