Refactor code structure for improved readability and maintainability

This commit is contained in:
Poyraz
2026-06-16 10:04:41 +03:00
parent 72a0b38f3e
commit f352918950
12 changed files with 1706 additions and 7 deletions
+8
View File
@@ -26,6 +26,14 @@ This repository now ships only the web application. It does not bundle Supabase,
See `docs/04-supabase-kurulumu.md` for the expected Supabase-side resources. See `docs/04-supabase-kurulumu.md` for the expected Supabase-side resources.
For a fresh Supabase project, run the one-shot setup SQL:
```bash
psql "postgresql://postgres:[PASSWORD]@[HOST]:5432/postgres" -v ON_ERROR_STOP=1 -f supabase/setup.sql
```
You can also paste the full contents of `supabase/setup.sql` into Supabase SQL Editor and run it once.
## Environment Variables ## Environment Variables
Copy `.env.example` to `.env.local` for local development, or add the same values in Vercel, Coolify, Dokploy, or your hosting provider. Copy `.env.example` to `.env.local` for local development, or add the same values in Vercel, Coolify, Dokploy, or your hosting provider.
+58 -1
View File
@@ -22,6 +22,49 @@ Kurulumda genel yaklaşım şu:
Repo içinde `supabase/` ve `docs/database/` klasörleri tutulmaya devam eder. Bunları deployment sırasında otomatik çalıştıran bir script yoktur. Bu dosyalar benim SQL kayıtlarım, migration notlarım ve schema geçmişim olarak durur. Repo içinde `supabase/` ve `docs/database/` klasörleri tutulmaya devam eder. Bunları deployment sırasında otomatik çalıştıran bir script yoktur. Bu dosyalar benim SQL kayıtlarım, migration notlarım ve schema geçmişim olarak durur.
## Tek Seferlik Kurulum SQL'i
Yeni bir Supabase projesini Neta için hazırlamanın en kolay yolu `supabase/setup.sql` dosyasını çalıştırmak. Bu dosya `supabase/schema.sql` ve `supabase/migrations/0002..0011` arasındaki migration dosyalarının tek dosyada birleştirilmiş hâlidir.
Demo seed verisi bu dosyaya dahil değildir. Production kurulumda örnek veri istemediğim için seed ayrı tutulur.
### Supabase SQL Editor ile
Supabase panelinde şu bölümü açın:
```txt
SQL Editor -> New query
```
Sonra `supabase/setup.sql` dosyasının tamamını kopyalayıp tek seferde çalıştırın.
Bu işlem şunları kurar:
- Tablolar.
- Foreign key ve check constraint'leri.
- Index'ler.
- Trigger ve function'lar.
- RLS policy'leri.
- Storage bucket kayıtları ve storage policy'leri.
### Terminalden Tek Komut ile
Supabase connection string'iniz varsa aynı kurulumu tek `psql` komutuyla da çalıştırabilirsiniz:
```bash
psql "postgresql://postgres:[PASSWORD]@[HOST]:5432/postgres" -v ON_ERROR_STOP=1 -f supabase/setup.sql
```
Supabase hosted projelerde connection string'i şuradan alabilirsiniz:
```txt
Project Settings -> Database -> Connection string
```
Şifre, host ve port bilgilerini kendi projenize göre değiştirmelisiniz.
Bu komut başarılı bittikten sonra Supabase tarafı Neta uygulamasına hazır hâle gelir.
## Gerekli Tablolar ## Gerekli Tablolar
Neta şu public tabloları bekler: Neta şu public tabloları bekler:
@@ -104,7 +147,21 @@ Bu yüzden `SUPABASE_SERVICE_ROLE_KEY` zorunludur. Bu key asla browser'a gönder
## SQL Dosyaları Nasıl Kullanılmalı? ## SQL Dosyaları Nasıl Kullanılmalı?
Bu repodaki SQL dosyalarını otomatik migration sistemi gibi düşünmüyorum. Bunlar benim migration geçmişimi ve Supabase tarafında beklenen yapıyı belgeleyen kaynaklar. Yeni kurulum için en pratik dosya:
```txt
supabase/setup.sql
```
Bu dosya tek seferde çalıştırılacak kurulum dosyasıdır.
Parçalı dosyalar ise migration geçmişini ve geliştirme sürecini belgelemek için korunur:
```txt
supabase/schema.sql
supabase/migrations/
docs/database/
```
Yeni bir kurulum yaparken en güvenilir kaynak, çalışan Supabase projesinden aldığınız güncel SQL çıktısıdır. Supabase panelindeki "copy as SQL" çıktısı bu yüzden önemlidir. Yeni bir kurulum yaparken en güvenilir kaynak, çalışan Supabase projesinden aldığınız güncel SQL çıktısıdır. Supabase panelindeki "copy as SQL" çıktısı bu yüzden önemlidir.
+6
View File
@@ -19,6 +19,12 @@ Deploy'dan önce hazır olması gerekenler:
- Supabase tabloları, policy'leri, function'ları ve storage bucket'ları. - Supabase tabloları, policy'leri, function'ları ve storage bucket'ları.
- Environment variable değerleri. - Environment variable değerleri.
Supabase tarafını yeni kuruyorsanız önce `supabase/setup.sql` dosyasını Supabase SQL Editor'da tek seferde çalıştırın. Terminal kullanıyorsanız aynı işlem şu komutla yapılabilir:
```bash
psql "postgresql://postgres:[PASSWORD]@[HOST]:5432/postgres" -v ON_ERROR_STOP=1 -f supabase/setup.sql
```
Gerekli env değerleri: Gerekli env değerleri:
```env ```env
+8
View File
@@ -22,6 +22,14 @@ Coolify deploy için hazır olması gerekenler:
Supabase tarafında tablo, policy, function ve storage bucket'ları hazır olmalıdır. Supabase tarafında tablo, policy, function ve storage bucket'ları hazır olmalıdır.
Yeni bir Supabase projesi hazırlıyorsanız `supabase/setup.sql` dosyasını tek seferde çalıştırabilirsiniz:
```bash
psql "postgresql://postgres:[PASSWORD]@[HOST]:5432/postgres" -v ON_ERROR_STOP=1 -f supabase/setup.sql
```
Supabase panelinden ilerlemek isterseniz aynı dosyanın tamamını SQL Editor'a yapıştırıp çalıştırmanız yeterli.
## Uygulama Oluşturma ## Uygulama Oluşturma
Coolify içinde yeni bir application oluştururken: Coolify içinde yeni bir application oluştururken:
+8
View File
@@ -20,6 +20,14 @@ Başlamadan önce şunlar hazır olmalı:
- Supabase schema ve storage yapısı. - Supabase schema ve storage yapısı.
- Ortam değişkenleri. - Ortam değişkenleri.
Supabase tarafı hazır değilse önce `supabase/setup.sql` dosyasını çalıştırın. Terminalden tek komutla kurmak için:
```bash
psql "postgresql://postgres:[PASSWORD]@[HOST]:5432/postgres" -v ON_ERROR_STOP=1 -f supabase/setup.sql
```
SQL Editor kullanıyorsanız `supabase/setup.sql` içeriğini komple yapıştırıp çalıştırabilirsiniz.
## Dokploy'da Uygulama Oluşturma ## Dokploy'da Uygulama Oluşturma
Dokploy içinde yeni bir uygulama oluştururken GitHub reposunu seçin. Uygulamayı Node.js/Next.js app olarak yapılandırın. Dokploy içinde yeni bir uygulama oluştururken GitHub reposunu seçin. Uygulamayı Node.js/Next.js app olarak yapılandırın.
+3 -3
View File
@@ -33,10 +33,10 @@ They must also be documented and registered in this file, but they should only b
## Applying The Ordered SQL ## Applying The Ordered SQL
Use the migration helper from the repository root: For a fresh Supabase project, use the one-shot setup file from the repository root:
```bash ```bash
DATABASE_URL='postgresql://postgres:password@host:5432/postgres' sh ./scripts/apply-migrations.sh psql "postgresql://postgres:[PASSWORD]@[HOST]:5432/postgres" -v ON_ERROR_STOP=1 -f supabase/setup.sql
``` ```
The helper applies missing queries from `0001` through `0011` in the order listed above and records completed migrations in `neta_internal.schema_migrations`. It uses local `psql` when available, otherwise it runs `psql` through Docker. After migrations, it sends `NOTIFY pgrst, 'reload schema'` so PostgREST can see new RPC functions without a manual restart. The setup file applies `0001` through `0011` in the order listed above. It does not include optional seed data. If you are changing the schema later, keep adding ordered files under `supabase/migrations/` and update this table.
+19 -3
View File
@@ -2,6 +2,22 @@
This directory stores SQL files for the project's Supabase/PostgreSQL database. This directory stores SQL files for the project's Supabase/PostgreSQL database.
## One-Shot Setup
For a fresh Supabase project, use:
```text
supabase/setup.sql
```
You can paste the full file into Supabase SQL Editor and run it once, or run it from the repository root with `psql`:
```bash
psql "postgresql://postgres:[PASSWORD]@[HOST]:5432/postgres" -v ON_ERROR_STOP=1 -f supabase/setup.sql
```
The setup file is generated by concatenating `schema.sql` and migrations `0002` through `0011` in the canonical order. Demo seed data is intentionally not included.
## Current Baseline ## Current Baseline
- `schema.sql` is the first registered database SQL file. - `schema.sql` is the first registered database SQL file.
@@ -42,10 +58,10 @@ Do not overwrite already executed SQL without also creating a new ordered migrat
## Apply Migrations ## Apply Migrations
From the repository root: For a new project, prefer the one-shot setup file:
```bash ```bash
DATABASE_URL='postgresql://postgres:password@host:5432/postgres' bash ./scripts/apply-migrations.sh psql "postgresql://postgres:[PASSWORD]@[HOST]:5432/postgres" -v ON_ERROR_STOP=1 -f supabase/setup.sql
``` ```
The script applies all required schema files in canonical order. It requires either local `psql` or Docker. If `psql` is not installed, it runs `psql` through the `postgres:16-alpine` Docker image. The ordered migration files remain as the source history for future schema changes.
@@ -70,25 +70,41 @@ alter table public.invoices enable row level security;
alter table public.subscriptions enable row level security; alter table public.subscriptions enable row level security;
-- Proposals RLS -- Proposals RLS
drop policy if exists "Users can view their own proposals" on public.proposals;
create policy "Users can view their own proposals" on public.proposals for select using (auth.uid() = user_id); create policy "Users can view their own proposals" on public.proposals for select using (auth.uid() = user_id);
drop policy if exists "Users can insert their own proposals" on public.proposals;
create policy "Users can insert their own proposals" on public.proposals for insert with check (auth.uid() = user_id); create policy "Users can insert their own proposals" on public.proposals for insert with check (auth.uid() = user_id);
drop policy if exists "Users can update their own proposals" on public.proposals;
create policy "Users can update their own proposals" on public.proposals for update using (auth.uid() = user_id); create policy "Users can update their own proposals" on public.proposals for update using (auth.uid() = user_id);
drop policy if exists "Users can delete their own proposals" on public.proposals;
create policy "Users can delete their own proposals" on public.proposals for delete using (auth.uid() = user_id); create policy "Users can delete their own proposals" on public.proposals for delete using (auth.uid() = user_id);
-- Contracts RLS -- Contracts RLS
drop policy if exists "Users can view their own contracts" on public.contracts;
create policy "Users can view their own contracts" on public.contracts for select using (auth.uid() = user_id); create policy "Users can view their own contracts" on public.contracts for select using (auth.uid() = user_id);
drop policy if exists "Users can insert their own contracts" on public.contracts;
create policy "Users can insert their own contracts" on public.contracts for insert with check (auth.uid() = user_id); create policy "Users can insert their own contracts" on public.contracts for insert with check (auth.uid() = user_id);
drop policy if exists "Users can update their own contracts" on public.contracts;
create policy "Users can update their own contracts" on public.contracts for update using (auth.uid() = user_id); create policy "Users can update their own contracts" on public.contracts for update using (auth.uid() = user_id);
drop policy if exists "Users can delete their own contracts" on public.contracts;
create policy "Users can delete their own contracts" on public.contracts for delete using (auth.uid() = user_id); create policy "Users can delete their own contracts" on public.contracts for delete using (auth.uid() = user_id);
-- Invoices RLS -- Invoices RLS
drop policy if exists "Users can view their own invoices" on public.invoices;
create policy "Users can view their own invoices" on public.invoices for select using (auth.uid() = user_id); create policy "Users can view their own invoices" on public.invoices for select using (auth.uid() = user_id);
drop policy if exists "Users can insert their own invoices" on public.invoices;
create policy "Users can insert their own invoices" on public.invoices for insert with check (auth.uid() = user_id); create policy "Users can insert their own invoices" on public.invoices for insert with check (auth.uid() = user_id);
drop policy if exists "Users can update their own invoices" on public.invoices;
create policy "Users can update their own invoices" on public.invoices for update using (auth.uid() = user_id); create policy "Users can update their own invoices" on public.invoices for update using (auth.uid() = user_id);
drop policy if exists "Users can delete their own invoices" on public.invoices;
create policy "Users can delete their own invoices" on public.invoices for delete using (auth.uid() = user_id); create policy "Users can delete their own invoices" on public.invoices for delete using (auth.uid() = user_id);
-- Subscriptions RLS -- Subscriptions RLS
drop policy if exists "Users can view their own subscriptions" on public.subscriptions;
create policy "Users can view their own subscriptions" on public.subscriptions for select using (auth.uid() = user_id); create policy "Users can view their own subscriptions" on public.subscriptions for select using (auth.uid() = user_id);
drop policy if exists "Users can insert their own subscriptions" on public.subscriptions;
create policy "Users can insert their own subscriptions" on public.subscriptions for insert with check (auth.uid() = user_id); create policy "Users can insert their own subscriptions" on public.subscriptions for insert with check (auth.uid() = user_id);
drop policy if exists "Users can update their own subscriptions" on public.subscriptions;
create policy "Users can update their own subscriptions" on public.subscriptions for update using (auth.uid() = user_id); create policy "Users can update their own subscriptions" on public.subscriptions for update using (auth.uid() = user_id);
drop policy if exists "Users can delete their own subscriptions" on public.subscriptions;
create policy "Users can delete their own subscriptions" on public.subscriptions for delete using (auth.uid() = user_id); create policy "Users can delete their own subscriptions" on public.subscriptions for delete using (auth.uid() = user_id);
@@ -24,7 +24,11 @@ create table if not exists public.client_activities (
alter table public.client_activities enable row level security; alter table public.client_activities enable row level security;
-- Client Activities RLS -- Client Activities RLS
drop policy if exists "Users can view their own client activities" on public.client_activities;
create policy "Users can view their own client activities" on public.client_activities for select using (auth.uid() = user_id); create policy "Users can view their own client activities" on public.client_activities for select using (auth.uid() = user_id);
drop policy if exists "Users can insert their own client activities" on public.client_activities;
create policy "Users can insert their own client activities" on public.client_activities for insert with check (auth.uid() = user_id); create policy "Users can insert their own client activities" on public.client_activities for insert with check (auth.uid() = user_id);
drop policy if exists "Users can update their own client activities" on public.client_activities;
create policy "Users can update their own client activities" on public.client_activities for update using (auth.uid() = user_id); create policy "Users can update their own client activities" on public.client_activities for update using (auth.uid() = user_id);
drop policy if exists "Users can delete their own client activities" on public.client_activities;
create policy "Users can delete their own client activities" on public.client_activities for delete using (auth.uid() = user_id); create policy "Users can delete their own client activities" on public.client_activities for delete using (auth.uid() = user_id);
@@ -16,9 +16,13 @@ create table if not exists public.document_embeddings (
-- Enable RLS -- Enable RLS
alter table public.document_embeddings enable row level security; alter table public.document_embeddings enable row level security;
drop policy if exists "Users can view their own embeddings" on public.document_embeddings;
create policy "Users can view their own embeddings" on public.document_embeddings for select using (auth.uid() = user_id); create policy "Users can view their own embeddings" on public.document_embeddings for select using (auth.uid() = user_id);
drop policy if exists "Users can insert their own embeddings" on public.document_embeddings;
create policy "Users can insert their own embeddings" on public.document_embeddings for insert with check (auth.uid() = user_id); create policy "Users can insert their own embeddings" on public.document_embeddings for insert with check (auth.uid() = user_id);
drop policy if exists "Users can update their own embeddings" on public.document_embeddings;
create policy "Users can update their own embeddings" on public.document_embeddings for update using (auth.uid() = user_id); create policy "Users can update their own embeddings" on public.document_embeddings for update using (auth.uid() = user_id);
drop policy if exists "Users can delete their own embeddings" on public.document_embeddings;
create policy "Users can delete their own embeddings" on public.document_embeddings for delete using (auth.uid() = user_id); create policy "Users can delete their own embeddings" on public.document_embeddings for delete using (auth.uid() = user_id);
-- Create a function to similarity search for embeddings -- Create a function to similarity search for embeddings
@@ -23,6 +23,7 @@ CREATE TABLE IF NOT EXISTS public.project_revisions (
); );
-- Trigger to set updated_at -- Trigger to set updated_at
DROP TRIGGER IF EXISTS set_project_revisions_updated_at ON public.project_revisions;
CREATE TRIGGER set_project_revisions_updated_at CREATE TRIGGER set_project_revisions_updated_at
BEFORE UPDATE ON public.project_revisions BEFORE UPDATE ON public.project_revisions
FOR EACH ROW FOR EACH ROW
@@ -35,18 +36,21 @@ ALTER TABLE public.project_revisions ENABLE ROW LEVEL SECURITY;
-- PROFILES -- PROFILES
-- Clients can read their own profile -- Clients can read their own profile
DROP POLICY IF EXISTS "Clients can view own profile" ON public.profiles;
CREATE POLICY "Clients can view own profile" CREATE POLICY "Clients can view own profile"
ON public.profiles FOR SELECT ON public.profiles FOR SELECT
USING (auth.uid() = id); USING (auth.uid() = id);
-- CLIENTS -- CLIENTS
-- Clients can read their own client record -- Clients can read their own client record
DROP POLICY IF EXISTS "Clients can view own client record" ON public.clients;
CREATE POLICY "Clients can view own client record" CREATE POLICY "Clients can view own client record"
ON public.clients FOR SELECT ON public.clients FOR SELECT
USING (client_auth_id = auth.uid()); USING (client_auth_id = auth.uid());
-- PROJECTS -- PROJECTS
-- Clients can view projects where client_id matches their client record -- Clients can view projects where client_id matches their client record
DROP POLICY IF EXISTS "Clients can view own projects" ON public.projects;
CREATE POLICY "Clients can view own projects" CREATE POLICY "Clients can view own projects"
ON public.projects FOR SELECT ON public.projects FOR SELECT
USING ( USING (
@@ -57,6 +61,7 @@ USING (
-- TASKS -- TASKS
-- Clients can view tasks linked to their projects IF is_public_to_client is true -- Clients can view tasks linked to their projects IF is_public_to_client is true
DROP POLICY IF EXISTS "Clients can view public tasks of their projects" ON public.tasks;
CREATE POLICY "Clients can view public tasks of their projects" CREATE POLICY "Clients can view public tasks of their projects"
ON public.tasks FOR SELECT ON public.tasks FOR SELECT
USING ( USING (
@@ -70,6 +75,7 @@ USING (
-- PROJECT PLANNING SECTIONS (Milestones, Roadmap, etc.) -- PROJECT PLANNING SECTIONS (Milestones, Roadmap, etc.)
-- Clients can view planning sections of their projects -- Clients can view planning sections of their projects
DROP POLICY IF EXISTS "Clients can view planning sections of their projects" ON public.project_planning_sections;
CREATE POLICY "Clients can view planning sections of their projects" CREATE POLICY "Clients can view planning sections of their projects"
ON public.project_planning_sections FOR SELECT ON public.project_planning_sections FOR SELECT
USING ( USING (
@@ -82,6 +88,7 @@ USING (
-- PROJECT REVISIONS -- PROJECT REVISIONS
-- Freelancers can manage all revisions for their projects -- Freelancers can manage all revisions for their projects
DROP POLICY IF EXISTS "Freelancers can manage revisions" ON public.project_revisions;
CREATE POLICY "Freelancers can manage revisions" CREATE POLICY "Freelancers can manage revisions"
ON public.project_revisions FOR ALL ON public.project_revisions FOR ALL
USING ( USING (
@@ -91,6 +98,7 @@ USING (
); );
-- Clients can insert revisions for their projects -- Clients can insert revisions for their projects
DROP POLICY IF EXISTS "Clients can insert revisions" ON public.project_revisions;
CREATE POLICY "Clients can insert revisions" CREATE POLICY "Clients can insert revisions"
ON public.project_revisions FOR INSERT ON public.project_revisions FOR INSERT
WITH CHECK ( WITH CHECK (
@@ -101,6 +109,7 @@ WITH CHECK (
); );
-- Clients can view their own revisions -- Clients can view their own revisions
DROP POLICY IF EXISTS "Clients can view own revisions" ON public.project_revisions;
CREATE POLICY "Clients can view own revisions" CREATE POLICY "Clients can view own revisions"
ON public.project_revisions FOR SELECT ON public.project_revisions FOR SELECT
USING ( USING (
+1563
View File
File diff suppressed because it is too large Load Diff