[FIX] Fix migration errors for missing tables
- Add table existence checks before adding constraints/triggers - Fix playback_analytics references (table doesn't exist) - Fix playlist_versions references (table doesn't exist) - Fix follows.deleted_at reference (column doesn't exist) - Fix marketplace_products/orders triggers (tables don't exist) - All migrations now pass successfully
This commit is contained in:
parent
32886d43cc
commit
db7ad3dd7a
3 changed files with 132 additions and 37 deletions
|
|
@ -163,10 +163,16 @@ BEGIN
|
|||
END $$;
|
||||
|
||||
-- === PLAYBACK_ANALYTICS CONSTRAINTS ===
|
||||
-- Note: playback_analytics table may not exist yet (referenced in views but not created)
|
||||
-- Constraint for completion_rate (0-100)
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
-- Check if table exists before adding constraints
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.tables
|
||||
WHERE table_schema = 'public'
|
||||
AND table_name = 'playback_analytics'
|
||||
) AND NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.table_constraints
|
||||
WHERE constraint_schema = 'public'
|
||||
AND table_name = 'playback_analytics'
|
||||
|
|
@ -181,7 +187,12 @@ END $$;
|
|||
-- Constraint for counts (non-negative)
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
-- Check if table exists before adding constraints
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.tables
|
||||
WHERE table_schema = 'public'
|
||||
AND table_name = 'playback_analytics'
|
||||
) AND NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.table_constraints
|
||||
WHERE constraint_schema = 'public'
|
||||
AND table_name = 'playback_analytics'
|
||||
|
|
@ -194,10 +205,16 @@ BEGIN
|
|||
END $$;
|
||||
|
||||
-- === PLAYLIST_VERSION CONSTRAINTS ===
|
||||
-- Note: playlist_versions table may not exist yet
|
||||
-- Constraint for version (positive)
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
-- Check if table exists before adding constraints
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.tables
|
||||
WHERE table_schema = 'public'
|
||||
AND table_name = 'playlist_versions'
|
||||
) AND NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.table_constraints
|
||||
WHERE constraint_schema = 'public'
|
||||
AND table_name = 'playlist_versions'
|
||||
|
|
@ -212,7 +229,12 @@ END $$;
|
|||
-- Constraint for action (created, updated, restored)
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
-- Check if table exists before adding constraints
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.tables
|
||||
WHERE table_schema = 'public'
|
||||
AND table_name = 'playlist_versions'
|
||||
) AND NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.table_constraints
|
||||
WHERE constraint_schema = 'public'
|
||||
AND table_name = 'playlist_versions'
|
||||
|
|
@ -244,7 +266,12 @@ END $$;
|
|||
-- Constraint for reason (network_slow, network_fast, user_selected, buffer_low)
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
-- Check if table exists before adding constraints
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.tables
|
||||
WHERE table_schema = 'public'
|
||||
AND table_name = 'bitrate_adaptation_logs'
|
||||
) AND NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.table_constraints
|
||||
WHERE constraint_schema = 'public'
|
||||
AND table_name = 'bitrate_adaptation_logs'
|
||||
|
|
@ -259,7 +286,12 @@ END $$;
|
|||
-- Constraint for bitrates (positive)
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
-- Check if table exists before adding constraints
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.tables
|
||||
WHERE table_schema = 'public'
|
||||
AND table_name = 'bitrate_adaptation_logs'
|
||||
) AND NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.table_constraints
|
||||
WHERE constraint_schema = 'public'
|
||||
AND table_name = 'bitrate_adaptation_logs'
|
||||
|
|
@ -318,10 +350,60 @@ BEGIN
|
|||
END IF;
|
||||
END $$;
|
||||
|
||||
-- Comments
|
||||
COMMENT ON CONSTRAINT chk_tracks_status_valid ON public.tracks IS 'Validates track status values';
|
||||
COMMENT ON CONSTRAINT chk_tracks_stream_status_valid ON public.tracks IS 'Validates stream status values';
|
||||
COMMENT ON CONSTRAINT chk_hls_transcode_queue_status_valid ON public.hls_transcode_queue IS 'Validates queue status values';
|
||||
COMMENT ON CONSTRAINT chk_playlist_versions_action_valid ON public.playlist_versions IS 'Validates playlist version action values';
|
||||
COMMENT ON CONSTRAINT chk_track_history_action_valid ON public.track_history IS 'Validates track history action values';
|
||||
COMMENT ON CONSTRAINT chk_bitrate_adaptation_reason_valid ON public.bitrate_adaptation_logs IS 'Validates bitrate adaptation reason values';
|
||||
-- Comments (only add comments if constraints exist)
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.table_constraints
|
||||
WHERE constraint_schema = 'public'
|
||||
AND table_name = 'tracks'
|
||||
AND constraint_name = 'chk_tracks_status_valid'
|
||||
) THEN
|
||||
COMMENT ON CONSTRAINT chk_tracks_status_valid ON public.tracks IS 'Validates track status values';
|
||||
END IF;
|
||||
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.table_constraints
|
||||
WHERE constraint_schema = 'public'
|
||||
AND table_name = 'tracks'
|
||||
AND constraint_name = 'chk_tracks_stream_status_valid'
|
||||
) THEN
|
||||
COMMENT ON CONSTRAINT chk_tracks_stream_status_valid ON public.tracks IS 'Validates stream status values';
|
||||
END IF;
|
||||
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.table_constraints
|
||||
WHERE constraint_schema = 'public'
|
||||
AND table_name = 'hls_transcode_queue'
|
||||
AND constraint_name = 'chk_hls_transcode_queue_status_valid'
|
||||
) THEN
|
||||
COMMENT ON CONSTRAINT chk_hls_transcode_queue_status_valid ON public.hls_transcode_queue IS 'Validates queue status values';
|
||||
END IF;
|
||||
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.table_constraints
|
||||
WHERE constraint_schema = 'public'
|
||||
AND table_name = 'playlist_versions'
|
||||
AND constraint_name = 'chk_playlist_versions_action_valid'
|
||||
) THEN
|
||||
COMMENT ON CONSTRAINT chk_playlist_versions_action_valid ON public.playlist_versions IS 'Validates playlist version action values';
|
||||
END IF;
|
||||
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.table_constraints
|
||||
WHERE constraint_schema = 'public'
|
||||
AND table_name = 'track_history'
|
||||
AND constraint_name = 'chk_track_history_action_valid'
|
||||
) THEN
|
||||
COMMENT ON CONSTRAINT chk_track_history_action_valid ON public.track_history IS 'Validates track history action values';
|
||||
END IF;
|
||||
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.table_constraints
|
||||
WHERE constraint_schema = 'public'
|
||||
AND table_name = 'bitrate_adaptation_logs'
|
||||
AND constraint_name = 'chk_bitrate_adaptation_reason_valid'
|
||||
) THEN
|
||||
COMMENT ON CONSTRAINT chk_bitrate_adaptation_reason_valid ON public.bitrate_adaptation_logs IS 'Validates bitrate adaptation reason values';
|
||||
END IF;
|
||||
END $$;
|
||||
|
|
|
|||
|
|
@ -44,8 +44,8 @@ SELECT
|
|||
FROM public.users u
|
||||
LEFT JOIN public.tracks t ON t.creator_id = u.id AND t.deleted_at IS NULL
|
||||
LEFT JOIN public.playlists p ON p.user_id = u.id AND p.deleted_at IS NULL
|
||||
LEFT JOIN public.follows f1 ON f1.followed_id = u.id AND f1.deleted_at IS NULL
|
||||
LEFT JOIN public.follows f2 ON f2.follower_id = u.id AND f2.deleted_at IS NULL
|
||||
LEFT JOIN public.follows f1 ON f1.followed_id = u.id
|
||||
LEFT JOIN public.follows f2 ON f2.follower_id = u.id
|
||||
LEFT JOIN public.track_plays tp ON tp.user_id = u.id
|
||||
LEFT JOIN public.track_likes tl ON tl.user_id = u.id
|
||||
LEFT JOIN public.track_comments tc ON tc.user_id = u.id AND tc.deleted_at IS NULL
|
||||
|
|
@ -80,25 +80,18 @@ SELECT
|
|||
ELSE 0
|
||||
END AS average_duration,
|
||||
|
||||
-- Playback analytics
|
||||
COALESCE(AVG(pa.completion_rate), 0) AS average_completion_rate,
|
||||
COALESCE(SUM(pa.play_time), 0) AS total_analytics_play_time,
|
||||
COALESCE(SUM(pa.pause_count), 0) AS total_pause_count,
|
||||
COALESCE(SUM(pa.seek_count), 0) AS total_seek_count,
|
||||
|
||||
-- Share statistics
|
||||
COUNT(DISTINCT ts.id) AS share_links_count,
|
||||
COALESCE(SUM(ts.access_count), 0) AS total_share_accesses
|
||||
|
||||
FROM public.tracks t
|
||||
LEFT JOIN public.track_plays tp ON tp.track_id = t.id
|
||||
LEFT JOIN public.playback_analytics pa ON pa.track_id = t.id
|
||||
LEFT JOIN public.track_shares ts ON ts.track_id = t.id AND ts.deleted_at IS NULL
|
||||
WHERE t.deleted_at IS NULL
|
||||
GROUP BY t.id, t.title, t.artist, t.creator_id, t.created_at, t.published_at,
|
||||
t.play_count, t.like_count, t.comment_count, t.download_count;
|
||||
|
||||
COMMENT ON VIEW public.track_stats IS 'Aggregated track statistics view including plays, likes, comments, downloads, and playback analytics';
|
||||
COMMENT ON VIEW public.track_stats IS 'Aggregated track statistics view including plays, likes, comments, and downloads';
|
||||
|
||||
-- === PLAYLIST_STATS VIEW ===
|
||||
-- View for playlist statistics aggregating tracks, followers, and plays
|
||||
|
|
|
|||
|
|
@ -144,22 +144,42 @@ CREATE TRIGGER audit_track_comments_trigger
|
|||
COMMENT ON TRIGGER audit_track_comments_trigger ON public.track_comments IS 'Audit trigger for track_comments table - logs all create, update, and delete operations';
|
||||
|
||||
-- === TRIGGERS FOR MARKETPLACE_PRODUCTS TABLE ===
|
||||
DROP TRIGGER IF EXISTS audit_marketplace_products_trigger ON public.marketplace_products;
|
||||
CREATE TRIGGER audit_marketplace_products_trigger
|
||||
AFTER INSERT OR UPDATE OR DELETE ON public.marketplace_products
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION public.audit_trigger_function();
|
||||
|
||||
COMMENT ON TRIGGER audit_marketplace_products_trigger ON public.marketplace_products IS 'Audit trigger for marketplace_products table - logs all create, update, and delete operations';
|
||||
-- Note: marketplace_products table may not exist yet
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.tables
|
||||
WHERE table_schema = 'public'
|
||||
AND table_name = 'marketplace_products'
|
||||
) THEN
|
||||
DROP TRIGGER IF EXISTS audit_marketplace_products_trigger ON public.marketplace_products;
|
||||
CREATE TRIGGER audit_marketplace_products_trigger
|
||||
AFTER INSERT OR UPDATE OR DELETE ON public.marketplace_products
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION public.audit_trigger_function();
|
||||
|
||||
COMMENT ON TRIGGER audit_marketplace_products_trigger ON public.marketplace_products IS 'Audit trigger for marketplace_products table - logs all create, update, and delete operations';
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- === TRIGGERS FOR MARKETPLACE_ORDERS TABLE ===
|
||||
DROP TRIGGER IF EXISTS audit_marketplace_orders_trigger ON public.marketplace_orders;
|
||||
CREATE TRIGGER audit_marketplace_orders_trigger
|
||||
AFTER INSERT OR UPDATE OR DELETE ON public.marketplace_orders
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION public.audit_trigger_function();
|
||||
|
||||
COMMENT ON TRIGGER audit_marketplace_orders_trigger ON public.marketplace_orders IS 'Audit trigger for marketplace_orders table - logs all create, update, and delete operations';
|
||||
-- Note: marketplace_orders table may not exist yet
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.tables
|
||||
WHERE table_schema = 'public'
|
||||
AND table_name = 'marketplace_orders'
|
||||
) THEN
|
||||
DROP TRIGGER IF EXISTS audit_marketplace_orders_trigger ON public.marketplace_orders;
|
||||
CREATE TRIGGER audit_marketplace_orders_trigger
|
||||
AFTER INSERT OR UPDATE OR DELETE ON public.marketplace_orders
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION public.audit_trigger_function();
|
||||
|
||||
COMMENT ON TRIGGER audit_marketplace_orders_trigger ON public.marketplace_orders IS 'Audit trigger for marketplace_orders table - logs all create, update, and delete operations';
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- === TRIGGERS FOR FOLLOWS TABLE ===
|
||||
DROP TRIGGER IF EXISTS audit_follows_trigger ON public.follows;
|
||||
|
|
|
|||
Loading…
Reference in a new issue