


SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;


COMMENT ON SCHEMA "public" IS 'standard public schema';



CREATE EXTENSION IF NOT EXISTS "pg_stat_statements" WITH SCHEMA "extensions";






CREATE EXTENSION IF NOT EXISTS "pgcrypto" WITH SCHEMA "extensions";






CREATE EXTENSION IF NOT EXISTS "supabase_vault" WITH SCHEMA "vault";






CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA "extensions";





SET default_tablespace = '';

SET default_table_access_method = "heap";


CREATE TABLE IF NOT EXISTS "public"."companies" (
    "id" bigint NOT NULL,
    "name" "text" NOT NULL,
    "created_at" timestamp with time zone DEFAULT "now"()
);


ALTER TABLE "public"."companies" OWNER TO "postgres";


ALTER TABLE "public"."companies" ALTER COLUMN "id" ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME "public"."companies_id_seq"
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);



CREATE TABLE IF NOT EXISTS "public"."daily_stats" (
    "id" bigint NOT NULL,
    "plant_id" "text" NOT NULL,
    "date" "date" NOT NULL,
    "total_generation" double precision DEFAULT 0.0,
    "peak_kw" double precision DEFAULT 0.0,
    "generation_hours" double precision DEFAULT 0.0,
    "created_at" timestamp with time zone DEFAULT "now"()
);


ALTER TABLE "public"."daily_stats" OWNER TO "postgres";


COMMENT ON TABLE "public"."daily_stats" IS '일일 발전 통계 요약 테이블';



COMMENT ON COLUMN "public"."daily_stats"."total_generation" IS '당일 총 발전량 (kWh)';



COMMENT ON COLUMN "public"."daily_stats"."peak_kw" IS '당일 최고 출력 (kW)';



COMMENT ON COLUMN "public"."daily_stats"."generation_hours" IS '발전 시간 = 발전량 / 설비용량';



ALTER TABLE "public"."daily_stats" ALTER COLUMN "id" ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME "public"."daily_stats_id_seq"
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);



CREATE TABLE IF NOT EXISTS "public"."monthly_stats" (
    "id" bigint NOT NULL,
    "plant_id" "text" NOT NULL,
    "month" "text" NOT NULL,
    "total_generation" double precision DEFAULT 0,
    "currnet_last_date" "text",
    "updated_at" timestamp with time zone DEFAULT "timezone"('utc'::"text", "now"()) NOT NULL
);


ALTER TABLE "public"."monthly_stats" OWNER TO "postgres";


ALTER TABLE "public"."monthly_stats" ALTER COLUMN "id" ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME "public"."monthly_stats_id_seq"
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);



CREATE TABLE IF NOT EXISTS "public"."plants" (
    "id" "text" NOT NULL,
    "company_id" bigint,
    "name" "text" NOT NULL,
    "type" "text",
    "created_at" timestamp with time zone DEFAULT "now"(),
    "capacity" double precision DEFAULT 100.0,
    "initial_cumulative_kwh" double precision DEFAULT 0.0,
    "constructed_at" "date",
    "alerts_enabled" boolean DEFAULT true
);


ALTER TABLE "public"."plants" OWNER TO "postgres";


COMMENT ON COLUMN "public"."plants"."initial_cumulative_kwh" IS '시스템 도입 이전의 누적 발전량 (기초 자산)';



COMMENT ON COLUMN "public"."plants"."alerts_enabled" IS '발전소 이상 감지 알림 활성화 여부 (true: 알림 켬, false: 알림 끔)';



CREATE TABLE IF NOT EXISTS "public"."solar_logs" (
    "id" bigint NOT NULL,
    "plant_id" "text",
    "current_kw" double precision,
    "today_kwh" double precision,
    "status" "text",
    "created_at" timestamp with time zone DEFAULT "now"()
);


ALTER TABLE "public"."solar_logs" OWNER TO "postgres";


ALTER TABLE "public"."solar_logs" ALTER COLUMN "id" ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME "public"."solar_logs_id_seq"
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);



ALTER TABLE ONLY "public"."companies"
    ADD CONSTRAINT "companies_pkey" PRIMARY KEY ("id");



ALTER TABLE ONLY "public"."daily_stats"
    ADD CONSTRAINT "daily_stats_pkey" PRIMARY KEY ("id");



ALTER TABLE ONLY "public"."daily_stats"
    ADD CONSTRAINT "daily_stats_plant_date_unique" UNIQUE ("plant_id", "date");



ALTER TABLE ONLY "public"."monthly_stats"
    ADD CONSTRAINT "monthly_stats_pkey" PRIMARY KEY ("id");



ALTER TABLE ONLY "public"."monthly_stats"
    ADD CONSTRAINT "monthly_stats_plant_month_key" UNIQUE ("plant_id", "month");



ALTER TABLE ONLY "public"."plants"
    ADD CONSTRAINT "plants_pkey" PRIMARY KEY ("id");



ALTER TABLE ONLY "public"."solar_logs"
    ADD CONSTRAINT "solar_logs_pkey" PRIMARY KEY ("id");



CREATE INDEX "idx_daily_stats_date" ON "public"."daily_stats" USING "btree" ("date");



CREATE INDEX "idx_daily_stats_plant_date" ON "public"."daily_stats" USING "btree" ("plant_id", "date");



CREATE INDEX "idx_daily_stats_plant_id" ON "public"."daily_stats" USING "btree" ("plant_id");



ALTER TABLE ONLY "public"."daily_stats"
    ADD CONSTRAINT "daily_stats_plant_id_fkey" FOREIGN KEY ("plant_id") REFERENCES "public"."plants"("id") ON DELETE CASCADE;



ALTER TABLE ONLY "public"."plants"
    ADD CONSTRAINT "plants_company_id_fkey" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id");



ALTER TABLE ONLY "public"."solar_logs"
    ADD CONSTRAINT "solar_logs_plant_id_fkey" FOREIGN KEY ("plant_id") REFERENCES "public"."plants"("id");



CREATE POLICY "Allow anon insert update" ON "public"."daily_stats" TO "anon" USING (true) WITH CHECK (true);



CREATE POLICY "Allow anonymous read access on daily_stats" ON "public"."daily_stats" FOR SELECT TO "anon" USING (true);



CREATE POLICY "Allow anonymous read access on monthly_stats" ON "public"."monthly_stats" FOR SELECT TO "anon" USING (true);



CREATE POLICY "Allow anonymous read access on plants" ON "public"."plants" FOR SELECT TO "anon" USING (true);



CREATE POLICY "Allow anonymous read access on solar_logs" ON "public"."solar_logs" FOR SELECT TO "anon" USING (true);



CREATE POLICY "Allow authenticated full access on daily_stats" ON "public"."daily_stats" TO "authenticated" USING (true) WITH CHECK (true);



CREATE POLICY "Allow authenticated full access on monthly_stats" ON "public"."monthly_stats" TO "authenticated" USING (true) WITH CHECK (true);



CREATE POLICY "Allow authenticated full access on plants" ON "public"."plants" TO "authenticated" USING (true) WITH CHECK (true);



CREATE POLICY "Allow authenticated full access on solar_logs" ON "public"."solar_logs" TO "authenticated" USING (true) WITH CHECK (true);



CREATE POLICY "Allow public read access" ON "public"."companies" FOR SELECT TO "authenticated", "anon" USING (true);



CREATE POLICY "Allow service_role full access on daily_stats" ON "public"."daily_stats" TO "service_role" USING (true) WITH CHECK (true);



CREATE POLICY "Allow service_role full access on monthly_stats" ON "public"."monthly_stats" TO "service_role" USING (true) WITH CHECK (true);



CREATE POLICY "Allow service_role full access on solar_logs" ON "public"."solar_logs" TO "service_role" USING (true) WITH CHECK (true);



CREATE POLICY "Public Select" ON "public"."monthly_stats" FOR SELECT USING (true);



CREATE POLICY "Service Role Insert" ON "public"."monthly_stats" FOR INSERT WITH CHECK (true);



CREATE POLICY "Service Role Update" ON "public"."monthly_stats" FOR UPDATE USING (true);



ALTER TABLE "public"."companies" ENABLE ROW LEVEL SECURITY;


ALTER TABLE "public"."daily_stats" ENABLE ROW LEVEL SECURITY;


ALTER TABLE "public"."monthly_stats" ENABLE ROW LEVEL SECURITY;


ALTER TABLE "public"."plants" ENABLE ROW LEVEL SECURITY;


ALTER TABLE "public"."solar_logs" ENABLE ROW LEVEL SECURITY;




ALTER PUBLICATION "supabase_realtime" OWNER TO "postgres";


GRANT USAGE ON SCHEMA "public" TO "postgres";
GRANT USAGE ON SCHEMA "public" TO "anon";
GRANT USAGE ON SCHEMA "public" TO "authenticated";
GRANT USAGE ON SCHEMA "public" TO "service_role";





































































































































































GRANT ALL ON TABLE "public"."companies" TO "anon";
GRANT ALL ON TABLE "public"."companies" TO "authenticated";
GRANT ALL ON TABLE "public"."companies" TO "service_role";



GRANT ALL ON SEQUENCE "public"."companies_id_seq" TO "anon";
GRANT ALL ON SEQUENCE "public"."companies_id_seq" TO "authenticated";
GRANT ALL ON SEQUENCE "public"."companies_id_seq" TO "service_role";



GRANT ALL ON TABLE "public"."daily_stats" TO "anon";
GRANT ALL ON TABLE "public"."daily_stats" TO "authenticated";
GRANT ALL ON TABLE "public"."daily_stats" TO "service_role";



GRANT ALL ON SEQUENCE "public"."daily_stats_id_seq" TO "anon";
GRANT ALL ON SEQUENCE "public"."daily_stats_id_seq" TO "authenticated";
GRANT ALL ON SEQUENCE "public"."daily_stats_id_seq" TO "service_role";



GRANT ALL ON TABLE "public"."monthly_stats" TO "anon";
GRANT ALL ON TABLE "public"."monthly_stats" TO "authenticated";
GRANT ALL ON TABLE "public"."monthly_stats" TO "service_role";



GRANT ALL ON SEQUENCE "public"."monthly_stats_id_seq" TO "anon";
GRANT ALL ON SEQUENCE "public"."monthly_stats_id_seq" TO "authenticated";
GRANT ALL ON SEQUENCE "public"."monthly_stats_id_seq" TO "service_role";



GRANT ALL ON TABLE "public"."plants" TO "anon";
GRANT ALL ON TABLE "public"."plants" TO "authenticated";
GRANT ALL ON TABLE "public"."plants" TO "service_role";



GRANT ALL ON TABLE "public"."solar_logs" TO "anon";
GRANT ALL ON TABLE "public"."solar_logs" TO "authenticated";
GRANT ALL ON TABLE "public"."solar_logs" TO "service_role";



GRANT ALL ON SEQUENCE "public"."solar_logs_id_seq" TO "anon";
GRANT ALL ON SEQUENCE "public"."solar_logs_id_seq" TO "authenticated";
GRANT ALL ON SEQUENCE "public"."solar_logs_id_seq" TO "service_role";









ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON SEQUENCES TO "postgres";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON SEQUENCES TO "anon";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON SEQUENCES TO "authenticated";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON SEQUENCES TO "service_role";






ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON FUNCTIONS TO "postgres";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON FUNCTIONS TO "anon";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON FUNCTIONS TO "authenticated";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON FUNCTIONS TO "service_role";






ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON TABLES TO "postgres";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON TABLES TO "anon";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON TABLES TO "authenticated";
ALTER DEFAULT PRIVILEGES FOR ROLE "postgres" IN SCHEMA "public" GRANT ALL ON TABLES TO "service_role";































