diff --git a/map/build-nocodb.sh b/map/build-nocodb.sh index 816520b..eba4b7e 100755 --- a/map/build-nocodb.sh +++ b/map/build-nocodb.sh @@ -9,7 +9,7 @@ # 2. login - Simple authentication table with Email, Name, Admin fields # 3. settings - Configuration table with text fields only (no QR image storage) # -# Updated: July 2025 - Simplified walk sheet config (text-only, no image storage) +# Updated: July 2025 - Always creates a new base, does not touch existing data set -e # Exit on any error @@ -139,24 +139,14 @@ create_project() { make_api_call "POST" "/meta/bases" "$project_data" "Creating project: $project_name" "v2" } -# Function to create a table or get existing table ID +# Function to create a table create_table() { local base_id=$1 local table_name=$2 local table_data=$3 local description=$4 - # First check if table already exists - local existing_id - existing_id=$(get_table_id_by_name "$base_id" "$table_name") - - if [[ -n "$existing_id" ]]; then - print_success "Table '$table_name' already exists with ID: $existing_id" - echo "$existing_id" - return 0 - fi - - # Table doesn't exist, create it + # Always create new table (no checking for existing) local response response=$(make_api_call "POST" "/meta/bases/$base_id/tables" "$table_data" "Creating table: $table_name ($description)" "v2") @@ -178,16 +168,6 @@ create_table() { fi } -# Function to create columns for a table -create_column() { - local project_id=$1 - local table_id=$2 - local column_data=$3 - local column_name=$4 - - make_api_call "POST" "/meta/projects/$project_id/tables/$table_id/columns" "$column_data" "Creating column: $column_name" -} - # Function to test API connectivity test_api_connectivity() { print_status "Testing API connectivity..." @@ -222,49 +202,11 @@ test_api_connectivity() { fi } -# Function to extract project ID from existing URLs -extract_project_from_urls() { - print_status "Checking for existing project IDs in URLs..." - - local project_id="" - - # Try to extract from view URL - if [[ -n "$NOCODB_VIEW_URL" ]]; then - project_id=$(echo "$NOCODB_VIEW_URL" | sed -n 's/.*\/nc\/\([^\/]*\)\/.*/\1/p') - if [[ -n "$project_id" ]]; then - print_success "Found project ID from VIEW_URL: $project_id" - echo "$project_id" - return 0 - fi - fi - - # Try to extract from login sheet - if [[ -n "$NOCODB_LOGIN_SHEET" ]]; then - project_id=$(echo "$NOCODB_LOGIN_SHEET" | sed -n 's/.*\/nc\/\([^\/]*\)\/.*/\1/p') - if [[ -n "$project_id" ]]; then - print_success "Found project ID from LOGIN_SHEET: $project_id" - echo "$project_id" - return 0 - fi - fi - - # Try to extract from settings sheet - if [[ -n "$NOCODB_SETTINGS_SHEET" ]]; then - project_id=$(echo "$NOCODB_SETTINGS_SHEET" | sed -n 's/.*\/nc\/\([^\/]*\)\/.*/\1/p') - if [[ -n "$project_id" ]]; then - print_success "Found project ID from SETTINGS_SHEET: $project_id" - echo "$project_id" - return 0 - fi - fi - - print_warning "No existing project ID found in URLs" - return 1 -} - -# Function to get or create project -get_or_create_project() { - local project_name="Map Viewer Project" +# Function to create new project with timestamp +create_new_project() { + # Generate unique project name with timestamp + local timestamp=$(date +"%Y%m%d_%H%M%S") + local project_name="Map Viewer Project - $timestamp" # First test API connectivity if ! test_api_connectivity; then @@ -272,40 +214,26 @@ get_or_create_project() { exit 1 fi - # Since the URLs are empty, we need to create new tables - # Check if we have any existing bases first - print_status "Checking for existing bases..." + print_status "Creating new base: $project_name" + print_warning "This script will create a new base and will NOT touch any existing data" - local bases_response - bases_response=$(make_api_call "GET" "/meta/bases" "" "Fetching existing bases" "v2") + local new_base_response + new_base_response=$(create_project "$project_name") if [[ $? -eq 0 ]]; then - # Try to find existing project (look for our project name or use first available) - local existing_base_id - existing_base_id=$(echo "$bases_response" | grep -o '"id":"[^"]*"' | head -1 | sed 's/"id":"//;s/"//') + local new_base_id + new_base_id=$(echo "$new_base_response" | grep -o '"id":"[^"]*"' | head -1 | sed 's/"id":"//;s/"//') - if [ -n "$existing_base_id" ]; then - print_success "Using existing base with ID: $existing_base_id" - echo "$existing_base_id" + if [ -n "$new_base_id" ]; then + print_success "Created new base '$project_name' with ID: $new_base_id" + echo "$new_base_id" return 0 else - print_status "No existing base found, creating new base..." - local new_base_response - new_base_response=$(create_project "$project_name") - local new_base_id - new_base_id=$(echo "$new_base_response" | grep -o '"id":"[^"]*"' | head -1 | sed 's/"id":"//;s/"//') - - if [ -n "$new_base_id" ]; then - print_success "Created new base with ID: $new_base_id" - echo "$new_base_id" - return 0 - else - print_error "Failed to create or find base" - exit 1 - fi + print_error "Failed to extract base ID from response" + exit 1 fi else - print_error "Failed to fetch bases from API" + print_error "Failed to create new base" exit 1 fi } @@ -646,7 +574,7 @@ create_default_admin() { make_api_call "POST" "/tables/$login_table_id/records" "$admin_data" "Creating default admin user" "v2" print_warning "Default admin user created:" - print_warning " Email: admin@example.com" + print_warning " Email: admin@thebunkerops.ca" print_warning " Name: Administrator" print_warning " Admin: true" print_warning " Note: This is a simplified login table for demonstration." @@ -681,69 +609,22 @@ create_default_start_location() { make_api_call "POST" "/tables/$settings_table_id/records" "$start_location_data" "Creating default settings row" "v2" } -# Function to get table ID from table name -get_table_id() { - local project_id=$1 - local table_name=$2 - - local tables_response - tables_response=$(make_api_call "GET" "/meta/projects/$project_id/tables" "" "Fetching tables for project") - - local table_id - table_id=$(echo "$tables_response" | grep -A 5 -B 5 "\"table_name\":\"$table_name\"" | grep -o '"id":"[^"]*"' | head -1 | sed 's/"id":"//;s/"//') - - if [ -n "$table_id" ]; then - echo "$table_id" - else - print_error "Could not find table ID for table: $table_name" - return 1 - fi -} - -# Function to get table ID from table name -get_table_id_by_name() { - local base_id=$1 - local table_name=$2 - - print_status "Checking if table '$table_name' exists..." - - local tables_response - tables_response=$(make_api_call "GET" "/meta/bases/$base_id/tables" "" "Fetching tables for base" "v2") - - if [[ $? -eq 0 ]]; then - # Parse JSON to find table by name - local table_id - table_id=$(echo "$tables_response" | grep -o '"id":"[^"]*","table_name":"'"$table_name"'"' | grep -o '"id":"[^"]*"' | head -1 | sed 's/"id":"//;s/"//') - - if [ -n "$table_id" ]; then - print_success "Found existing table '$table_name' with ID: $table_id" - echo "$table_id" - return 0 - else - print_status "Table '$table_name' does not exist" - return 1 - fi - else - print_error "Failed to fetch tables for base" - return 1 - fi -} - # Main execution main() { print_status "Starting NocoDB Auto-Setup..." print_status "================================" - # Get or create project - print_status "Getting or creating base..." - BASE_ID=$(get_or_create_project) + # Always create a new project + print_status "Creating new base..." + print_warning "This script creates a NEW base and does NOT modify existing data" + BASE_ID=$(create_new_project) if [ -z "$BASE_ID" ]; then - print_error "Failed to get or create base" + print_error "Failed to create new base" exit 1 fi - print_status "Working with base ID: $BASE_ID" + print_status "Working with new base ID: $BASE_ID" # Create tables print_status "Creating tables..." @@ -769,81 +650,28 @@ main() { # Create default settings row (includes both start location and walk sheet config) create_default_start_location "$BASE_ID" "$SETTINGS_TABLE_ID" - # Create default walk sheet configuration - # create_default_walk_sheet_config "$BASE_ID" "$SETTINGS_TABLE_ID" - print_status "================================" print_success "NocoDB Auto-Setup completed successfully!" print_status "================================" + print_status "Base ID: $BASE_ID" + print_status "" print_status "Next steps:" - print_status "1. Login to your NocoDB instance and verify the tables were created" - print_status "2. Find the table URLs in NocoDB and update your .env file:" - print_status " - Go to each table > Details > Copy the view URL" - print_status " - Update NOCODB_VIEW_URL, NOCODB_LOGIN_SHEET, and NOCODB_SETTINGS_SHEET" - print_status "3. Set up proper authentication for the admin user (admin@example.com)" - print_status "4. Start adding your location data" + print_status "1. Login to your NocoDB instance at: $BASE_URL" + print_status "2. Find your new base and navigate to each table" + print_status "3. For each table, copy the view URL and update your .env file:" + print_status " - NOCODB_VIEW_URL (for locations table)" + print_status " - NOCODB_LOGIN_SHEET (for login table)" + print_status " - NOCODB_SETTINGS_SHEET (for settings table)" + print_status "4. The default admin user is: admin@thebunkerops.ca" + print_status "5. Start adding your location data!" - print_warning "Important: Please update your .env file with the actual table URLs from NocoDB!" - print_warning "The current .env file has empty URLs - you need to populate them with the correct table URLs." + print_warning "" + print_warning "IMPORTANT: This script created a NEW base. Your existing data was NOT modified." + print_warning "Please update your .env file with the new table URLs from the newly created base." } # Check if script is being run directly if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" -fi - - if [ -z "$BASE_ID" ]; then - print_error "Failed to get or create base" - exit 1 - fi - - print_status "Working with base ID: $BASE_ID" - - # Create tables - print_status "Creating tables..." - - # Create locations table - LOCATIONS_TABLE_ID=$(create_locations_table "$BASE_ID") - - # Create login table - LOGIN_TABLE_ID=$(create_login_table "$BASE_ID") - - # Create settings table - SETTINGS_TABLE_ID=$(create_settings_table "$BASE_ID") - - # Wait a moment for tables to be fully created - sleep 3 - - # Create default data - print_status "Setting up default data..." - - # Create default admin user - create_default_admin "$BASE_ID" "$LOGIN_TABLE_ID" - - # Create default start location - create_default_start_location "$BASE_ID" "$SETTINGS_TABLE_ID" - - # Create default walk sheet configuration - create_default_walk_sheet_config "$BASE_ID" "$SETTINGS_TABLE_ID" - - print_status "================================" - print_success "NocoDB Auto-Setup completed successfully!" - print_status "================================" - - print_status "Next steps:" - print_status "1. Login to your NocoDB instance and verify the tables were created" - print_status "2. Find the table URLs in NocoDB and update your .env file:" - print_status " - Go to each table > Details > Copy the view URL" - print_status " - Update NOCODB_VIEW_URL, NOCODB_LOGIN_SHEET, and NOCODB_SETTINGS_SHEET" - print_status "3. Set up proper authentication for the admin user (admin@example.com)" - print_status "4. Start adding your location data" - - print_warning "Important: Please update your .env file with the actual table URLs from NocoDB!" - print_warning "The current .env file has empty URLs - you need to populate them with the correct table URLs." -} - -# Check if script is being run directly -if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then - main "$@" -fi +fi \ No newline at end of file