mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-07-11 19:18:40 +00:00
Add project files:
- Add database initialization scripts - Add configuration files - Add documentation - Add public assets - Add source code structure - Update README
This commit is contained in:
171
db/init.sql
Normal file
171
db/init.sql
Normal file
@ -0,0 +1,171 @@
|
||||
-- Enable required extensions
|
||||
create extension if not exists "uuid-ossp"; -- For UUID generation
|
||||
create extension if not exists pg_cron; -- For scheduled jobs
|
||||
|
||||
-- Create the search_cache table
|
||||
create table public.search_cache (
|
||||
id uuid default uuid_generate_v4() primary key,
|
||||
query text not null,
|
||||
results jsonb not null,
|
||||
location text not null,
|
||||
category text not null,
|
||||
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
|
||||
updated_at timestamp with time zone default timezone('utc'::text, now()) not null,
|
||||
expires_at timestamp with time zone default timezone('utc'::text, now() + interval '7 days') not null
|
||||
);
|
||||
|
||||
-- Create indexes
|
||||
create index search_cache_query_idx on public.search_cache (query);
|
||||
create index search_cache_location_category_idx on public.search_cache (location, category);
|
||||
create index search_cache_expires_at_idx on public.search_cache (expires_at);
|
||||
|
||||
-- Enable RLS
|
||||
alter table public.search_cache enable row level security;
|
||||
|
||||
-- Create policies
|
||||
create policy "Allow public read access"
|
||||
on public.search_cache for select
|
||||
using (true);
|
||||
|
||||
create policy "Allow service write access"
|
||||
on public.search_cache for insert
|
||||
with check (true);
|
||||
|
||||
create policy "Allow service update access"
|
||||
on public.search_cache for update
|
||||
using (true)
|
||||
with check (true);
|
||||
|
||||
create policy "Allow delete expired records"
|
||||
on public.search_cache for delete
|
||||
using (expires_at < now());
|
||||
|
||||
-- Create function to clean up expired records
|
||||
create or replace function cleanup_expired_cache()
|
||||
returns void
|
||||
language plpgsql
|
||||
security definer
|
||||
as $$
|
||||
begin
|
||||
delete from public.search_cache
|
||||
where expires_at < now();
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Create a manual cleanup function since pg_cron might not be available
|
||||
create or replace function manual_cleanup()
|
||||
returns void
|
||||
language plpgsql
|
||||
security definer
|
||||
as $$
|
||||
begin
|
||||
delete from public.search_cache
|
||||
where expires_at < now();
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Create a view for cache statistics
|
||||
create or replace view cache_stats as
|
||||
select
|
||||
count(*) as total_entries,
|
||||
count(*) filter (where expires_at < now()) as expired_entries,
|
||||
count(*) filter (where expires_at >= now()) as valid_entries,
|
||||
min(created_at) as oldest_entry,
|
||||
max(created_at) as newest_entry,
|
||||
count(distinct category) as unique_categories,
|
||||
count(distinct location) as unique_locations
|
||||
from public.search_cache;
|
||||
|
||||
-- Grant permissions to access the view
|
||||
grant select on cache_stats to postgres;
|
||||
|
||||
-- Create table if not exists businesses
|
||||
create table if not exists businesses (
|
||||
id text primary key,
|
||||
name text not null,
|
||||
phone text,
|
||||
email text,
|
||||
address text,
|
||||
rating numeric,
|
||||
website text,
|
||||
logo text,
|
||||
source text,
|
||||
description text,
|
||||
latitude numeric,
|
||||
longitude numeric,
|
||||
last_updated timestamp with time zone default timezone('utc'::text, now()),
|
||||
search_count integer default 1,
|
||||
created_at timestamp with time zone default timezone('utc'::text, now())
|
||||
);
|
||||
|
||||
-- Create indexes for common queries
|
||||
create index if not exists businesses_name_idx on businesses (name);
|
||||
create index if not exists businesses_rating_idx on businesses (rating desc);
|
||||
create index if not exists businesses_search_count_idx on businesses (search_count desc);
|
||||
create index if not exists businesses_last_updated_idx on businesses (last_updated desc);
|
||||
|
||||
-- Create tables if they don't exist
|
||||
CREATE TABLE IF NOT EXISTS businesses (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
phone TEXT,
|
||||
email TEXT,
|
||||
address TEXT,
|
||||
rating INTEGER,
|
||||
website TEXT,
|
||||
logo TEXT,
|
||||
source TEXT,
|
||||
description TEXT,
|
||||
location JSONB,
|
||||
place_id TEXT,
|
||||
photos TEXT[],
|
||||
opening_hours TEXT[],
|
||||
distance JSONB,
|
||||
last_updated TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||
search_count INTEGER DEFAULT 0
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS searches (
|
||||
id SERIAL PRIMARY KEY,
|
||||
query TEXT NOT NULL,
|
||||
location TEXT NOT NULL,
|
||||
timestamp TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||
results_count INTEGER
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS cache (
|
||||
key TEXT PRIMARY KEY,
|
||||
value JSONB NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||
expires_at TIMESTAMP WITH TIME ZONE NOT NULL
|
||||
);
|
||||
|
||||
-- Create indexes
|
||||
CREATE INDEX IF NOT EXISTS idx_businesses_location ON businesses USING GIN (location);
|
||||
CREATE INDEX IF NOT EXISTS idx_businesses_search ON businesses USING GIN (to_tsvector('english', name || ' ' || COALESCE(description, '')));
|
||||
CREATE INDEX IF NOT EXISTS idx_cache_expires ON cache (expires_at);
|
||||
|
||||
-- Set up RLS (Row Level Security)
|
||||
ALTER TABLE businesses ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE searches ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE cache ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create policies
|
||||
CREATE POLICY "Allow anonymous select" ON businesses FOR SELECT USING (true);
|
||||
CREATE POLICY "Allow service role insert" ON businesses FOR INSERT WITH CHECK (true);
|
||||
CREATE POLICY "Allow service role update" ON businesses FOR UPDATE USING (true);
|
||||
|
||||
CREATE POLICY "Allow anonymous select" ON searches FOR SELECT USING (true);
|
||||
CREATE POLICY "Allow service role insert" ON searches FOR INSERT WITH CHECK (true);
|
||||
|
||||
CREATE POLICY "Allow anonymous select" ON cache FOR SELECT USING (true);
|
||||
CREATE POLICY "Allow service role all" ON cache USING (true);
|
||||
|
||||
-- Add place_id column to businesses table if it doesn't exist
|
||||
ALTER TABLE businesses ADD COLUMN IF NOT EXISTS place_id TEXT;
|
||||
CREATE INDEX IF NOT EXISTS idx_businesses_place_id ON businesses(place_id);
|
||||
|
||||
-- Create a unique constraint on place_id (excluding nulls)
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_businesses_place_id_unique
|
||||
ON businesses(place_id)
|
||||
WHERE place_id IS NOT NULL;
|
44
db/schema.sql
Normal file
44
db/schema.sql
Normal file
@ -0,0 +1,44 @@
|
||||
-- Create the businesses table
|
||||
create table businesses (
|
||||
id uuid primary key,
|
||||
name text not null,
|
||||
phone text,
|
||||
address text,
|
||||
city text,
|
||||
state text,
|
||||
zip text,
|
||||
category text[],
|
||||
rating numeric,
|
||||
review_count integer,
|
||||
license text,
|
||||
services text[],
|
||||
hours jsonb,
|
||||
website text,
|
||||
email text,
|
||||
verified boolean default false,
|
||||
last_updated timestamp with time zone,
|
||||
search_query text,
|
||||
search_location text,
|
||||
search_timestamp timestamp with time zone,
|
||||
reliability_score integer,
|
||||
|
||||
-- Create a composite index for deduplication
|
||||
constraint unique_business unique (phone, address)
|
||||
);
|
||||
|
||||
-- Create indexes for common queries
|
||||
create index idx_business_location on businesses (city, state);
|
||||
create index idx_business_category on businesses using gin (category);
|
||||
create index idx_search_query on businesses using gin (search_query gin_trgm_ops);
|
||||
create index idx_search_location on businesses using gin (search_location gin_trgm_ops);
|
||||
create index idx_reliability on businesses (reliability_score);
|
||||
|
||||
-- Enable full text search
|
||||
alter table businesses add column search_vector tsvector
|
||||
generated always as (
|
||||
setweight(to_tsvector('english', coalesce(name, '')), 'A') ||
|
||||
setweight(to_tsvector('english', coalesce(search_query, '')), 'B') ||
|
||||
setweight(to_tsvector('english', coalesce(search_location, '')), 'C')
|
||||
) stored;
|
||||
|
||||
create index idx_business_search on businesses using gin(search_vector);
|
15
db/verify.sql
Normal file
15
db/verify.sql
Normal file
@ -0,0 +1,15 @@
|
||||
-- Check if table exists
|
||||
SELECT EXISTS (
|
||||
SELECT FROM information_schema.tables
|
||||
WHERE table_schema = 'public'
|
||||
AND table_name = 'businesses'
|
||||
);
|
||||
|
||||
-- Check table structure
|
||||
SELECT column_name, data_type, is_nullable
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = 'public'
|
||||
AND table_name = 'businesses';
|
||||
|
||||
-- Check row count
|
||||
SELECT count(*) FROM businesses;
|
Reference in New Issue
Block a user