freealberta/influence/fix-campaigns-table.sh

296 lines
8.8 KiB
Bash

#!/bin/bash
# Fix Campaigns Table Script
# This script recreates the campaigns table with proper column options
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}$1${NC}"
}
print_success() {
echo -e "${GREEN}$1${NC}"
}
print_warning() {
echo -e "${YELLOW}$1${NC}"
}
print_error() {
echo -e "${RED}$1${NC}"
}
# Load environment variables
if [ -f ".env" ]; then
export $(cat .env | grep -v '^#' | xargs)
print_success "Environment variables loaded from .env"
else
print_error "No .env file found. Please create one based on .env.example"
exit 1
fi
# Validate required environment variables
if [ -z "$NOCODB_API_URL" ] || [ -z "$NOCODB_API_TOKEN" ] || [ -z "$NOCODB_PROJECT_ID" ]; then
print_error "Missing required environment variables: NOCODB_API_URL, NOCODB_API_TOKEN, NOCODB_PROJECT_ID"
exit 1
fi
print_status "Using NocoDB instance: $NOCODB_API_URL"
print_status "Project ID: $NOCODB_PROJECT_ID"
# Function to make API calls with proper error handling
make_api_call() {
local method="$1"
local url="$2"
local data="$3"
local description="$4"
print_status "Making $method request to: $url"
if [ -n "$description" ]; then
print_status "Purpose: $description"
fi
local response
local http_code
if [ "$method" = "DELETE" ]; then
response=$(curl -s -w "\n%{http_code}" -X DELETE \
-H "xc-token: $NOCODB_API_TOKEN" \
-H "Content-Type: application/json" \
"$url")
elif [ "$method" = "POST" ] && [ -n "$data" ]; then
response=$(curl -s -w "\n%{http_code}" -X POST \
-H "xc-token: $NOCODB_API_TOKEN" \
-H "Content-Type: application/json" \
-d "$data" \
"$url")
else
print_error "Invalid method or missing data for API call"
return 1
fi
# Extract HTTP code and response body
http_code=$(echo "$response" | tail -n1)
response_body=$(echo "$response" | head -n -1)
print_status "HTTP Status: $http_code"
if [[ "$http_code" -ge 200 && "$http_code" -lt 300 ]]; then
print_success "API call successful"
echo "$response_body"
return 0
else
print_error "API call failed with status $http_code"
print_error "Response: $response_body"
return 1
fi
}
# Function to delete the campaigns table
delete_campaigns_table() {
local table_id="$1"
print_warning "Deleting existing campaigns table (ID: $table_id)..."
make_api_call "DELETE" \
"$NOCODB_API_URL/db/meta/tables/$table_id" \
"" \
"Delete campaigns table" > /dev/null
}
# Function to create the campaigns table with proper options
create_campaigns_table() {
local base_id="$1"
print_status "Creating new campaigns table..."
local table_data='{
"table_name": "influence_campaigns",
"title": "Campaigns",
"columns": [
{
"column_name": "id",
"title": "ID",
"uidt": "ID",
"pk": true,
"ai": true,
"rqd": true
},
{
"column_name": "slug",
"title": "Campaign Slug",
"uidt": "SingleLineText",
"unique": true,
"rqd": true
},
{
"column_name": "title",
"title": "Campaign Title",
"uidt": "SingleLineText",
"rqd": true
},
{
"column_name": "description",
"title": "Description",
"uidt": "LongText"
},
{
"column_name": "email_subject",
"title": "Email Subject",
"uidt": "SingleLineText",
"rqd": true
},
{
"column_name": "email_body",
"title": "Email Body",
"uidt": "LongText",
"rqd": true
},
{
"column_name": "call_to_action",
"title": "Call to Action",
"uidt": "LongText"
},
{
"column_name": "status",
"title": "Status",
"uidt": "SingleSelect",
"colOptions": {
"options": [
{"title": "draft", "color": "#cfdffe"},
{"title": "active", "color": "#c2f5e8"},
{"title": "paused", "color": "#fee2d5"},
{"title": "archived", "color": "#ffeab6"}
]
},
"rqd": true,
"cdf": "draft"
},
{
"column_name": "allow_smtp_email",
"title": "Allow SMTP Email",
"uidt": "Checkbox",
"cdf": "true"
},
{
"column_name": "allow_mailto_link",
"title": "Allow Mailto Link",
"uidt": "Checkbox",
"cdf": "true"
},
{
"column_name": "collect_user_info",
"title": "Collect User Info",
"uidt": "Checkbox",
"cdf": "true"
},
{
"column_name": "show_email_count",
"title": "Show Email Count",
"uidt": "Checkbox",
"cdf": "true"
},
{
"column_name": "target_government_levels",
"title": "Target Government Levels",
"uidt": "MultiSelect",
"colOptions": {
"options": [
{"title": "Federal", "color": "#cfdffe"},
{"title": "Provincial", "color": "#d0f1fd"},
{"title": "Municipal", "color": "#c2f5e8"},
{"title": "School Board", "color": "#ffdaf6"}
]
}
},
{
"column_name": "created_at",
"title": "Created At",
"uidt": "DateTime",
"cdf": "now()"
},
{
"column_name": "updated_at",
"title": "Updated At",
"uidt": "DateTime",
"cdf": "now()"
}
]
}'
local response
response=$(make_api_call "POST" \
"$NOCODB_API_URL/db/meta/bases/$base_id/tables" \
"$table_data" \
"Create campaigns table with proper column options")
if [ $? -eq 0 ]; then
# Extract table ID from response
local table_id=$(echo "$response" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)
if [ -n "$table_id" ]; then
print_success "New campaigns table created with ID: $table_id"
echo "$table_id"
return 0
else
print_error "Could not extract table ID from response"
return 1
fi
else
return 1
fi
}
# Main execution
print_status "Starting campaigns table fix..."
# Check if campaigns table exists
if [ -n "$NOCODB_TABLE_CAMPAIGNS" ]; then
print_status "Found existing campaigns table ID: $NOCODB_TABLE_CAMPAIGNS"
# Delete existing table
if delete_campaigns_table "$NOCODB_TABLE_CAMPAIGNS"; then
print_success "Successfully deleted old campaigns table"
else
print_warning "Failed to delete old table, continuing anyway..."
fi
fi
# Create new table
NEW_TABLE_ID=$(create_campaigns_table "$NOCODB_PROJECT_ID")
if [ $? -eq 0 ] && [ -n "$NEW_TABLE_ID" ]; then
print_success "Successfully created new campaigns table!"
# Update .env file with new table ID
print_status "Updating .env file with new table ID..."
if grep -q "NOCODB_TABLE_CAMPAIGNS=" .env; then
# Replace existing NOCODB_TABLE_CAMPAIGNS
sed -i "s/NOCODB_TABLE_CAMPAIGNS=.*/NOCODB_TABLE_CAMPAIGNS=$NEW_TABLE_ID/" .env
print_success "Updated NOCODB_TABLE_CAMPAIGNS in .env file"
else
# Add new NOCODB_TABLE_CAMPAIGNS
echo "NOCODB_TABLE_CAMPAIGNS=$NEW_TABLE_ID" >> .env
print_success "Added NOCODB_TABLE_CAMPAIGNS to .env file"
fi
print_status ""
print_status "============================================================"
print_success "Campaigns table fix completed successfully!"
print_status "============================================================"
print_status ""
print_status "New table ID: $NEW_TABLE_ID"
print_status "Please restart your application to use the new table."
else
print_error "Failed to create new campaigns table"
exit 1
fi