Fix database schema

This commit is contained in:
gpt-engineer-app[bot]
2025-10-17 14:25:03 +00:00
parent badb2cd865
commit 921abb63a1
8 changed files with 737 additions and 44 deletions

View File

@@ -19,7 +19,7 @@ export function useCoasterStats(rideId: string | undefined) {
queryFn: async () => {
if (!rideId) return [];
const { data, error } = await (supabase as any)
const { data, error} = await supabase
.from('ride_coaster_stats')
.select('*')
.eq('ride_id', rideId)
@@ -27,7 +27,7 @@ export function useCoasterStats(rideId: string | undefined) {
if (error) throw error;
return (data || []).map((stat: any) => ({
return (data || []).map((stat) => ({
id: stat.id,
ride_id: stat.ride_id,
stat_name: stat.stat_name,

View File

@@ -1,6 +1,7 @@
import { useState, useEffect, useRef, useCallback } from 'react';
import { supabase } from '@/integrations/supabase/client';
import { toast } from 'sonner';
import { getErrorMessage } from '@/lib/errorHandler';
import type { EntityType, EntityVersion } from '@/types/versioning';
interface FieldChange {
@@ -41,14 +42,42 @@ export function useEntityVersions(entityType: EntityType, entityId: string) {
const versionTable = `${entityType}_versions`;
const entityIdCol = `${entityType}_id`;
const { data, error } = await (supabase as any)
.from(versionTable)
.select(`
*,
profiles:created_by(username, display_name, avatar_url)
`)
.eq(entityIdCol, entityId)
.order('version_number', { ascending: false });
let data, error;
// Use explicit conditional branches for type safety
if (entityType === 'park') {
const result = await supabase
.from('park_versions')
.select(`*, profiles:created_by(username, display_name, avatar_url)`)
.eq('park_id', entityId)
.order('version_number', { ascending: false });
data = result.data;
error = result.error;
} else if (entityType === 'ride') {
const result = await supabase
.from('ride_versions')
.select(`*, profiles:created_by(username, display_name, avatar_url)`)
.eq('ride_id', entityId)
.order('version_number', { ascending: false });
data = result.data;
error = result.error;
} else if (entityType === 'company') {
const result = await supabase
.from('company_versions')
.select(`*, profiles:created_by(username, display_name, avatar_url)`)
.eq('company_id', entityId)
.order('version_number', { ascending: false });
data = result.data;
error = result.error;
} else {
const result = await supabase
.from('ride_model_versions')
.select(`*, profiles:created_by(username, display_name, avatar_url)`)
.eq('ride_model_id', entityId)
.order('version_number', { ascending: false });
data = result.data;
error = result.error;
}
if (error) throw error;
@@ -77,12 +106,12 @@ export function useEntityVersions(entityType: EntityType, entityId: string) {
setCurrentVersion(versionsWithProfiles.find(v => v.is_current) || null);
setLoading(false);
}
} catch (error: any) {
console.error('Error fetching versions:', error);
} catch (error) {
const errorMsg = getErrorMessage(error);
console.error('Error fetching versions:', errorMsg);
if (isMountedRef.current && currentRequestId === requestCounterRef.current) {
const errorMessage = error?.message || 'Failed to load version history';
toast.error(errorMessage);
toast.error(errorMsg);
setLoading(false);
}
}
@@ -109,11 +138,11 @@ export function useEntityVersions(entityType: EntityType, entityId: string) {
if (error) throw error;
return data;
} catch (error: any) {
console.error('Error comparing versions:', error);
} catch (error) {
const errorMsg = getErrorMessage(error);
console.error('Error comparing versions:', errorMsg);
if (isMountedRef.current) {
const errorMessage = error?.message || 'Failed to compare versions';
toast.error(errorMessage);
toast.error(errorMsg);
}
return null;
}
@@ -141,11 +170,11 @@ export function useEntityVersions(entityType: EntityType, entityId: string) {
await fetchVersions();
}
return data;
} catch (error: any) {
console.error('Error rolling back version:', error);
} catch (error) {
const errorMsg = getErrorMessage(error);
console.error('Error rolling back version:', errorMsg);
if (isMountedRef.current) {
const errorMessage = error?.message || 'Failed to rollback version';
toast.error(errorMessage);
toast.error(errorMsg);
}
return null;
}

View File

@@ -27,25 +27,47 @@ export function useTechnicalSpecifications(
: 'ride_model_technical_specifications';
const idColumn = entityType === 'ride' ? 'ride_id' : 'ride_model_id';
const { data, error } = await (supabase as any)
.from(tableName)
.select('*')
.eq(idColumn, entityId)
.order('display_order');
if (error) throw error;
return (data || []).map((spec: any) => ({
id: spec.id,
entity_type: entityType,
entity_id: entityId,
spec_name: spec.spec_name,
spec_value: spec.spec_value,
spec_unit: spec.spec_unit || null,
category: spec.category || null,
display_order: spec.display_order,
created_at: spec.created_at,
})) as TechnicalSpecification[];
if (entityType === 'ride') {
const { data, error } = await supabase
.from('ride_technical_specifications')
.select('*')
.eq('ride_id', entityId)
.order('display_order');
if (error) throw error;
return (data || []).map((spec) => ({
id: spec.id,
entity_type: 'ride' as const,
entity_id: entityId,
spec_name: spec.spec_name,
spec_value: spec.spec_value,
spec_unit: spec.unit || null,
category: spec.category || null,
display_order: spec.display_order,
created_at: spec.created_at,
})) as TechnicalSpecification[];
} else {
const { data, error } = await supabase
.from('ride_model_technical_specifications')
.select('*')
.eq('ride_model_id', entityId)
.order('display_order');
if (error) throw error;
return (data || []).map((spec) => ({
id: spec.id,
entity_type: 'ride_model' as const,
entity_id: entityId,
spec_name: spec.spec_name,
spec_value: spec.spec_value,
spec_unit: spec.unit || null,
category: spec.category || null,
display_order: spec.display_order,
created_at: spec.created_at,
})) as TechnicalSpecification[];
}
},
enabled: !!entityId
});

View File

@@ -2072,6 +2072,53 @@ export type Database = {
},
]
}
ride_coaster_stats: {
Row: {
category: string | null
created_at: string | null
description: string | null
display_order: number | null
id: string
ride_id: string
stat_name: string
stat_value: number
unit: string | null
updated_at: string | null
}
Insert: {
category?: string | null
created_at?: string | null
description?: string | null
display_order?: number | null
id?: string
ride_id: string
stat_name: string
stat_value: number
unit?: string | null
updated_at?: string | null
}
Update: {
category?: string | null
created_at?: string | null
description?: string | null
display_order?: number | null
id?: string
ride_id?: string
stat_name?: string
stat_value?: number
unit?: string | null
updated_at?: string | null
}
Relationships: [
{
foreignKeyName: "ride_coaster_stats_ride_id_fkey"
columns: ["ride_id"]
isOneToOne: false
referencedRelation: "rides"
referencedColumns: ["id"]
},
]
}
ride_model_submissions: {
Row: {
banner_image_id: string | null
@@ -2189,7 +2236,6 @@ export type Database = {
ride_model_id: string
slug: string
submission_id: string | null
technical_specs: Json | null
version_id: string
version_number: number
}
@@ -2206,7 +2252,6 @@ export type Database = {
ride_model_id: string
slug: string
submission_id?: string | null
technical_specs?: Json | null
version_id?: string
version_number: number
}
@@ -2223,7 +2268,6 @@ export type Database = {
ride_model_id?: string
slug?: string
submission_id?: string | null
technical_specs?: Json | null
version_id?: string
version_number?: number
}

View File

@@ -282,6 +282,55 @@ export interface AuditLogEntry {
created_at: string;
}
// Relational data structures (NO JSONB)
export interface RideCoasterStat {
id: string;
ride_id: string;
stat_name: string;
stat_value: number;
unit?: string | null;
category?: string | null;
description?: string | null;
display_order: number;
created_at: string;
updated_at: string;
}
export interface RideTechnicalSpecification {
id: string;
ride_id: string;
spec_name: string;
spec_value: string;
spec_type: string;
category?: string | null;
unit?: string | null;
display_order: number;
created_at: string;
}
export interface RideModelTechnicalSpecification {
id: string;
ride_model_id: string;
spec_name: string;
spec_value: string;
spec_type: string;
category?: string | null;
unit?: string | null;
display_order: number;
created_at: string;
}
export interface ListItem {
id: string;
list_id: string;
entity_type: 'park' | 'ride' | 'coaster';
entity_id: string;
position: number;
notes?: string | null;
created_at: string;
updated_at: string;
}
// User ride credit tracking
export interface UserRideCredit {
id: string;