Mbugua Mnjala
Mbugua Mnjala

Reputation: 19

How do I set up Baserow to migrate data from one table to another?

I want to set up a system in Baserow such that a record added to one table is automatically copied to another table.

For example, once someone pays for the service the user data is to be copied to another table where the payment ID of the user is concatenated with the last 3 characters of their name and that is saved as the user ID.

My lambda script logic makes no sense.

My Python script that accesses the Baserow API to copy data does not access the other table.

import os
import json
import requests

def lambda_handler(event, context):
    # Fetch data from the database
    data = fetch_data_from_database()

    # Write the data to Baserow
    write_to_baserow(data)

    return {
        'statusCode': 200,
        'body': json.dumps(data)
    }

def fetch_data_from_database():
    url = "https://base.example.com/api/database/rows/table/666/?user_field_names=true"
    headers = {
        "Authorization": f"Token {os.environ['DATABASE_TOKEN']}"
    }

    try:
        response = requests.request("GET", url, headers=headers)
        response.raise_for_status()
        data = response.json()
    except requests.exceptions.RequestException as e:
        print(f"Error fetching data from the database: {e}")
        raise

    # Extract the relevant fields
    results = []
    for item in data['results']:
        results.append({
           # Sensitive data
          
          })

    return results

def write_to_baserow(data):
    baserow_url = os.environ['BASEROW_URL']
    baserow_api_key = os.environ['BASEROW_API_KEY']
    destination_table_id = os.environ['DESTINATION_TABLE_ID']

    headers = {
        "Authorization": f"Token {baserow_api_key}",
        "Content-Type": "application/json",
    }

    url = f"{baserow_url}/api/database/rows/table/{destination_table_id}/"          
    for row in data:
        response = requests.post(url, headers=headers, json=row)
        response.raise_for_status()              

Upvotes: 1

Views: 152

Answers (0)

Related Questions