-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor DB queries to RPC #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,8 @@ async function createWIRTransactionsTable() { | |
| // Table doesn't exist, create it | ||
| console.log('Creating market_wir_transactions table...'); | ||
|
|
||
| await supabaseAdmin.query(` | ||
| await supabaseAdmin.rpc('execute_sql', { | ||
| sql: ` | ||
| CREATE TABLE market_wir_transactions ( | ||
| id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
| user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, | ||
|
Comment on lines
+21
to
25
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential SQL Injection and Dependency on
|
||
|
|
@@ -48,7 +49,8 @@ async function createWIRTransactionsTable() { | |
| CREATE INDEX idx_wir_transactions_reference_id ON market_wir_transactions(reference_id); | ||
| CREATE INDEX idx_wir_transactions_created_at ON market_wir_transactions(created_at); | ||
| CREATE INDEX idx_wir_transactions_type ON market_wir_transactions(transaction_type); | ||
| `); | ||
| ` | ||
| }); | ||
|
|
||
| console.log('market_wir_transactions table created successfully!'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lack of Verification After Table CreationThe script logs a success message after executing the table creation command but does not verify whether the table and indexes were actually created. This could lead to situations where the script reports success, but the operation failed due to underlying SQL errors or permission issues. Recommendation:
|
||
| } else { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module.exports = require('./ScrapyardItem'); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,7 +80,8 @@ async function initializeDatabase() { | |
|
|
||
| if (error && error.code === '42P01') { // Table doesn't exist | ||
| console.log('Creating users table...'); | ||
| await supabaseAdmin.query(` | ||
| await supabaseAdmin.rpc('execute_sql', { | ||
| sql: ` | ||
| CREATE TABLE users ( | ||
| id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
| username TEXT UNIQUE NOT NULL, | ||
|
Comment on lines
80
to
87
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential Issue with UUID GenerationThe SQL command for creating the Suggested Change: |
||
|
|
@@ -111,7 +112,8 @@ async function initializeDatabase() { | |
| -- Create index on username and email | ||
| CREATE INDEX IF NOT EXISTS idx_users_username ON users(username); | ||
| CREATE INDEX IF NOT EXISTS idx_users_email ON users(email); | ||
| `); | ||
| ` | ||
| }); | ||
| } | ||
|
|
||
| // Create scrapyard_items table if not exists | ||
|
|
@@ -122,7 +124,8 @@ async function initializeDatabase() { | |
|
|
||
| if (itemsError && itemsError.code === '42P01') { // Table doesn't exist | ||
| console.log('Creating scrapyard_items table...'); | ||
| await supabaseAdmin.query(` | ||
| await supabaseAdmin.rpc('execute_sql', { | ||
| sql: ` | ||
| CREATE TABLE scrapyard_items ( | ||
| id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
| title TEXT NOT NULL, | ||
|
|
@@ -146,7 +149,8 @@ async function initializeDatabase() { | |
| -- Create indexes | ||
| CREATE INDEX IF NOT EXISTS idx_scrapyard_items_creator ON scrapyard_items(creator); | ||
| CREATE INDEX IF NOT EXISTS idx_scrapyard_items_category ON scrapyard_items(category); | ||
| `); | ||
| ` | ||
| }); | ||
| } | ||
|
|
||
| // Create sessions table if not exists | ||
|
|
@@ -157,15 +161,17 @@ async function initializeDatabase() { | |
|
|
||
| if (sessionsError && sessionsError.code === '42P01') { // Table doesn't exist | ||
| console.log('Creating sessions table...'); | ||
| await supabaseAdmin.query(` | ||
| await supabaseAdmin.rpc('execute_sql', { | ||
| sql: ` | ||
| CREATE TABLE sessions ( | ||
| sid varchar NOT NULL PRIMARY KEY, | ||
| sess json NOT NULL, | ||
| expired timestamp(6) with time zone NOT NULL | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS idx_sessions_expired ON sessions(expired); | ||
| `); | ||
| ` | ||
| }); | ||
| } | ||
|
|
||
| console.log('Database initialization complete'); | ||
|
Comment on lines
161
to
177
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enhancement for Session Table ManagementThe creation of the Suggested Enhancement: |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Security Concern: SQL Injection Risk
The use of raw SQL queries (
execute_sql) with template literals can potentially expose the application to SQL injection attacks if not properly sanitized or parameterized. Although this script is likely run in a controlled migration environment, it's a good practice to avoid forming SQL commands directly from input variables or using parameterized queries or stored procedures to mitigate such risks.Recommendation:
Consider using parameterized queries or stored procedures to enhance security, especially if any part of the SQL command can be influenced by external input in future modifications.