7.8 KiB
7.8 KiB
Database and Project Conventions
Database Naming Conventions
General Rules
- All database objects use snake_case
- All keywords stored in lowercase
- All names should be descriptive and clear
- Abbreviations avoided unless universally understood
Table Names
- Use plural form for table names (e.g.,
Keywords,Campaigns) - Compound names use underscores (e.g.,
Landing_Pages) - Junction tables combine both entity names (e.g.,
Keyword_Ad_Group_Assignments) - Historical/log tables append appropriate suffix (e.g.,
Import_History)
Column Names
- Use singular form
- Include units in name if applicable (e.g.,
budget_daily) - Use standard suffixes:
_idfor primary keys_atfor timestamps_datefor dates_typefor type indicators_statusfor status fields
Index Names
- Format:
idx_[table]_[column(s)] - Examples:
idx_keyword_searchidx_ad_group_campaignidx_keyword_assignments
View Names
- Prefix with
v_ - Describe the data being presented
- Examples:
v_active_keywords_per_ad_groupv_landing_page_usage
Function Names
- Use verb_noun format
- Describe the action being performed
- Examples:
update_keyword_metricscalculate_campaign_stats
Data Type Conventions
Text Data
VARCHAR(255)for standard text fieldsVARCHAR(2048)for URLsTEXTfor unlimited length fields- Always specify length for VARCHAR
Numeric Data
INTEGERfor whole numbersDECIMAL(10,2)for currencyDECIMAL(5,2)for percentages- Always specify precision and scale for DECIMAL
Date/Time Data
TIMESTAMPwithout timezone for all timestampsDATEfor date-only fields- Default
CURRENT_TIMESTAMPwhere appropriate
Boolean Data
- Use
BOOLEANtype (not INTEGER 0/1) - Default values specified where appropriate
Status Fields
Standard Status Values
- 'active'
- 'inactive'
- 'pending'
- 'archived'
- 'deleted'
Import Status Values
- 'SUCCESS'
- 'PARTIAL'
- 'FAILED'
Script Naming Conventions
Bash Scripts
- All scripts end in
.sh - Use kebab-case for names
- Format:
action-object[-modifier].sh - Examples:
create-database.shimport-keywords-gkwp.shupdate-functions.sh
SQL Files
- End in
.sql - Match associated script name where applicable
- Examples:
create-keywords-table.sqlcreate-import-views.sql
File Organization
Script Categories
- Keep related scripts in appropriate subdirectories
- Create new categories as needed
- Document new categories in project documentation
Configuration
- All sensitive data in
.databasefile - Configuration format:
DB_USER=postgres
DB_PASSWORD=your_password
DB_HOST=localhost
DB_PORT=5432
DB_NAME=keyword_management
Code Formatting
SQL Formatting
- Keywords in UPPER CASE
- Indentation: 2 spaces
- One statement per line
- Commas at the end of lines
- Proper line breaks for readability
Example:
SELECT
k.keyword,
k.gkwp_search_volume,
ag.ad_group_name
FROM
Keywords k
JOIN Keyword_Ad_Group_Assignments kaga
ON k.keyword_id = kaga.keyword_id
JOIN Ad_Groups ag
ON kaga.ad_group_id = ag.ad_group_id
WHERE
k.status = 'active'
AND ag.status = 'active';
Bash Script Formatting
- Two space indentation
- Clear section comments
- Consistent error handling
- Standard variable naming
Example:
#!/bin/bash
# Load database configuration
source .database
# Execute SQL command
psql -h "${DB_HOST}" \
-p "${DB_PORT}" \
-U "${DB_USER}" \
-d "${DB_NAME}" \
-f "sql/create-table.sql"
Documentation Conventions
Script Headers
- Purpose of script
- Usage example
- Dependencies
- Expected inputs/outputs
- Author and date
Example:
#!/bin/bash
# Purpose: Creates the Keywords table in the database
# Usage: ./create-keywords-table.sh
# Dependencies: .database configuration file
# Created: 2024-11-19
Comment Conventions
- SQL: -- for single line comments
- Bash: # for single line comments
- Include purpose for complex queries
- Document assumptions
- Explain non-obvious choices
Error Handling
Database Errors
- Proper error codes
- Meaningful error messages
- Transaction management
- Rollback procedures
Script Errors
- Exit codes for different scenarios
- Error logging
- User feedback
- Cleanup on failure
Logging Conventions
Log Format
- Timestamp
- Script name
- Action performed
- Status/Result
- Error details if applicable
Log Locations
- Script logs in
/logsdirectory - Database operation logs
- Error logs
- Import logs
Version Control
Commit Messages
- Clear and descriptive
- Reference task/ticket numbers
- Explain significant changes
- Document breaking changes
Branch Naming
- feature/description
- bugfix/description
- hotfix/description
These conventions ensure consistency across the project and make maintenance and collaboration easier.
Testing Conventions
Test Organization
- Test scripts stored in
/testsdirectory - Mirror
/bindirectory structure - Test files named with
.test.shsuffix - Example:
/bin └── create-functions/ └── create-keyword-function.sh /tests └── create-functions/ └── create-keyword-function.test.sh
Test Script Structure
#!/bin/bash
# Load test utilities
source ./tests/utils/test-framework.sh
# Test specific function creation
test_keyword_function() {
# Setup - capture initial state
initial_state=$(psql -t -c "SELECT count(*) FROM pg_proc WHERE proname = 'keyword_function';")
# Execute the script being tested
../bin/create-functions/create-keyword-function.sh
# Verify function exists
exists_check=$(psql -t -c "SELECT count(*) FROM pg_proc WHERE proname = 'keyword_function';")
assert_not_equals "$initial_state" "$exists_check" "Function should be created"
# Verify function signature
signature_check=$(psql -t -c "SELECT pg_get_functiondef('keyword_function'::regproc);")
assert_contains "$signature_check" "expected_signature" "Function signature should match"
# Test function behavior
result=$(psql -t -c "SELECT keyword_function('test_input');")
assert_equals "$result" "expected_output" "Function should produce expected results"
}
# Run tests
run_test_suite
Test Utilities
- Common test framework in
/tests/utils - Standard assertions
- Setup/teardown helpers
- Database state management
- Test reporting functions
Running Tests
- Individual test:
./tests/path/script.test.sh - Category tests:
./tests/run-category.sh create-functions - All tests:
./tests/run-all.sh
Test Reporting
- Standard output format
- Success/failure counts
- Execution time
- Detailed failure information
- State difference on failures
Database Test Environment
- Separate test database
- Reset to known state before tests
- Cleanup after test completion
- Isolation from production data
Continuous Integration
- Tests run before deployment
- Required passing tests for merges
- Test reports archived
- Coverage tracking
This approach offers several advantages:
- Separation of concerns
- Easier maintenance
- Clear test organization
- Reusable test utilities
- Proper test isolation
- Easy integration with CI/CD
Rather than building tests into operation scripts, we maintain parallel test scripts that:
- Mirror the structure of operational scripts
- Have access to shared test utilities
- Can be run individually or as suites
- Maintain clear relationships with what they test