Files
vault-para/400-archive/_duplicates/batch-2/Database.md
T
windyboyandClaude Sonnet 4.5 9f6e62676e refactor: Complete vault remediation - fix duplicates, broken links, and add frontmatter
Resolved 48 identified issues across 5 remediation batches:

Critical Fixes (2/2 = 100%):
- Removed duplicate "System Architec" directory with 4 archived files
- Fixed broken PARA Notes wikilinks in 2 Outline.md files

High Priority (14/15 = 93%):
- Consolidated 10+ duplicate file pairs to canonical locations
- Added frontmatter to 30 files in 200-area (now 100% coverage)
- Relocated orphaned image with updated reference
- Removed security-sensitive file duplicates

Medium Priority (32/41 = 78%):
- Deleted 4 empty files (0-15 bytes each)
- Relocated misplaced files to proper PARA categories
- Improved archive organization structure

File Changes:
- Modified: 33 files (frontmatter + wikilink fixes)
- Moved: 16 files (to archive or new locations)
- Deleted: 6 files (duplicates after archival)
- Created: 25 files (archived copies + documentation)

Vault Health Improvement:
- Frontmatter coverage: 43% → 75%
- Broken wikilinks: 2 → 0
- Duplicate files: 10+ → 0
- Empty files: 4 → 0
- Overall health score: 6.5/10 → 8.5/10

Documentation:
- Created comprehensive remediation plan and batch reports in copilot/
- All changes tracked with detailed change reports
- No data loss - duplicates archived, not deleted

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-30 14:36:42 +08:00

35 KiB
Raw Blame History

mysql:

/*M!999999\- enable the sandbox mode */ 
-- MariaDB dump 10.19-12.1.2-MariaDB, for debian-linux-gnu (x86_64)
--
-- Host: localhost    Database: homeassistant
-- ------------------------------------------------------
-- Server version	12.1.2-MariaDB-ubu2404

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*M!100616 SET @OLD_NOTE_VERBOSITY=@@NOTE_VERBOSITY, NOTE_VERBOSITY=0 */;

--
-- Table structure for table `event_data`
--

DROP TABLE IF EXISTS `event_data`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `event_data` (
  `data_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `hash` int(10) unsigned DEFAULT NULL,
  `shared_data` longtext DEFAULT NULL,
  PRIMARY KEY (`data_id`),
  KEY `ix_event_data_hash` (`hash`)
) ENGINE=InnoDB AUTO_INCREMENT=91 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `event_types`
--

DROP TABLE IF EXISTS `event_types`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `event_types` (
  `event_type_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `event_type` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`event_type_id`),
  UNIQUE KEY `ix_event_types_event_type` (`event_type`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `events`
--

DROP TABLE IF EXISTS `events`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `events` (
  `event_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `event_type` char(0) DEFAULT NULL,
  `event_data` char(0) DEFAULT NULL,
  `origin` char(0) DEFAULT NULL,
  `origin_idx` smallint(6) DEFAULT NULL,
  `time_fired` char(0) DEFAULT NULL,
  `time_fired_ts` double DEFAULT NULL,
  `context_id` char(0) DEFAULT NULL,
  `context_user_id` char(0) DEFAULT NULL,
  `context_parent_id` char(0) DEFAULT NULL,
  `data_id` bigint(20) DEFAULT NULL,
  `context_id_bin` tinyblob DEFAULT NULL,
  `context_user_id_bin` tinyblob DEFAULT NULL,
  `context_parent_id_bin` tinyblob DEFAULT NULL,
  `event_type_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`event_id`),
  KEY `ix_events_context_id_bin` (`context_id_bin`(16)),
  KEY `ix_events_time_fired_ts` (`time_fired_ts`),
  KEY `ix_events_event_type_id_time_fired_ts` (`event_type_id`,`time_fired_ts`),
  KEY `ix_events_data_id` (`data_id`),
  CONSTRAINT `1` FOREIGN KEY (`data_id`) REFERENCES `event_data` (`data_id`),
  CONSTRAINT `2` FOREIGN KEY (`event_type_id`) REFERENCES `event_types` (`event_type_id`)
) ENGINE=InnoDB AUTO_INCREMENT=183 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `migration_changes`
--

DROP TABLE IF EXISTS `migration_changes`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `migration_changes` (
  `migration_id` varchar(255) NOT NULL,
  `version` smallint(6) NOT NULL,
  PRIMARY KEY (`migration_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `recorder_runs`
--

DROP TABLE IF EXISTS `recorder_runs`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `recorder_runs` (
  `run_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `start` datetime(6) NOT NULL,
  `end` datetime(6) DEFAULT NULL,
  `closed_incorrect` tinyint(1) NOT NULL,
  `created` datetime(6) NOT NULL,
  PRIMARY KEY (`run_id`),
  KEY `ix_recorder_runs_start_end` (`start`,`end`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `schema_changes`
--

DROP TABLE IF EXISTS `schema_changes`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `schema_changes` (
  `change_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `schema_version` int(11) DEFAULT NULL,
  `changed` datetime(6) NOT NULL,
  PRIMARY KEY (`change_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `state_attributes`
--

DROP TABLE IF EXISTS `state_attributes`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `state_attributes` (
  `attributes_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `hash` int(10) unsigned DEFAULT NULL,
  `shared_attrs` longtext DEFAULT NULL,
  PRIMARY KEY (`attributes_id`),
  KEY `ix_state_attributes_hash` (`hash`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `states`
--

DROP TABLE IF EXISTS `states`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `states` (
  `state_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `entity_id` char(0) DEFAULT NULL,
  `state` varchar(255) DEFAULT NULL,
  `attributes` char(0) DEFAULT NULL,
  `event_id` smallint(6) DEFAULT NULL,
  `last_changed` char(0) DEFAULT NULL,
  `last_changed_ts` double DEFAULT NULL,
  `last_reported_ts` double DEFAULT NULL,
  `last_updated` char(0) DEFAULT NULL,
  `last_updated_ts` double DEFAULT NULL,
  `old_state_id` bigint(20) DEFAULT NULL,
  `attributes_id` bigint(20) DEFAULT NULL,
  `context_id` char(0) DEFAULT NULL,
  `context_user_id` char(0) DEFAULT NULL,
  `context_parent_id` char(0) DEFAULT NULL,
  `origin_idx` smallint(6) DEFAULT NULL,
  `context_id_bin` tinyblob DEFAULT NULL,
  `context_user_id_bin` tinyblob DEFAULT NULL,
  `context_parent_id_bin` tinyblob DEFAULT NULL,
  `metadata_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`state_id`),
  KEY `ix_states_attributes_id` (`attributes_id`),
  KEY `ix_states_last_updated_ts` (`last_updated_ts`),
  KEY `ix_states_context_id_bin` (`context_id_bin`(16)),
  KEY `ix_states_metadata_id_last_updated_ts` (`metadata_id`,`last_updated_ts`),
  KEY `ix_states_old_state_id` (`old_state_id`),
  CONSTRAINT `1` FOREIGN KEY (`old_state_id`) REFERENCES `states` (`state_id`),
  CONSTRAINT `2` FOREIGN KEY (`attributes_id`) REFERENCES `state_attributes` (`attributes_id`),
  CONSTRAINT `3` FOREIGN KEY (`metadata_id`) REFERENCES `states_meta` (`metadata_id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `states_meta`
--

DROP TABLE IF EXISTS `states_meta`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `states_meta` (
  `metadata_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `entity_id` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`metadata_id`),
  UNIQUE KEY `ix_states_meta_entity_id` (`entity_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `statistics`
--

DROP TABLE IF EXISTS `statistics`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `statistics` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `created` char(0) DEFAULT NULL,
  `created_ts` double DEFAULT NULL,
  `metadata_id` bigint(20) DEFAULT NULL,
  `start` char(0) DEFAULT NULL,
  `start_ts` double DEFAULT NULL,
  `mean` double DEFAULT NULL,
  `mean_weight` double DEFAULT NULL,
  `min` double DEFAULT NULL,
  `max` double DEFAULT NULL,
  `last_reset` char(0) DEFAULT NULL,
  `last_reset_ts` double DEFAULT NULL,
  `state` double DEFAULT NULL,
  `sum` double DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ix_statistics_statistic_id_start_ts` (`metadata_id`,`start_ts`),
  KEY `ix_statistics_start_ts` (`start_ts`),
  CONSTRAINT `1` FOREIGN KEY (`metadata_id`) REFERENCES `statistics_meta` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `statistics_meta`
--

DROP TABLE IF EXISTS `statistics_meta`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `statistics_meta` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `statistic_id` varchar(255) DEFAULT NULL,
  `source` varchar(32) DEFAULT NULL,
  `unit_of_measurement` varchar(255) DEFAULT NULL,
  `unit_class` varchar(255) DEFAULT NULL,
  `has_mean` tinyint(1) DEFAULT NULL,
  `has_sum` tinyint(1) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `mean_type` smallint(6) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ix_statistics_meta_statistic_id` (`statistic_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `statistics_runs`
--

DROP TABLE IF EXISTS `statistics_runs`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `statistics_runs` (
  `run_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `start` datetime(6) NOT NULL,
  PRIMARY KEY (`run_id`),
  KEY `ix_statistics_runs_start` (`start`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `statistics_short_term`
--

DROP TABLE IF EXISTS `statistics_short_term`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `statistics_short_term` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `created` char(0) DEFAULT NULL,
  `created_ts` double DEFAULT NULL,
  `metadata_id` bigint(20) DEFAULT NULL,
  `start` char(0) DEFAULT NULL,
  `start_ts` double DEFAULT NULL,
  `mean` double DEFAULT NULL,
  `mean_weight` double DEFAULT NULL,
  `min` double DEFAULT NULL,
  `max` double DEFAULT NULL,
  `last_reset` char(0) DEFAULT NULL,
  `last_reset_ts` double DEFAULT NULL,
  `state` double DEFAULT NULL,
  `sum` double DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ix_statistics_short_term_statistic_id_start_ts` (`metadata_id`,`start_ts`),
  KEY `ix_statistics_short_term_start_ts` (`start_ts`),
  CONSTRAINT `1` FOREIGN KEY (`metadata_id`) REFERENCES `statistics_meta` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*M!100616 SET NOTE_VERBOSITY=@OLD_NOTE_VERBOSITY */;

-- Dump completed on 2025-12-08 14:28:27


postgresql:

--
-- PostgreSQL database dump
--

\restrict 9zFMerZ7o6L03AnfBUNASNm8ofDuUqBhBkWGUYVYq1dGJZ5sz9TULNvRDy8ig5C

-- Dumped from database version 17.7 (Debian 17.7-3.pgdg13+1)
-- Dumped by pg_dump version 17.7 (Debian 17.7-3.pgdg13+1)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET transaction_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;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: event_data; Type: TABLE; Schema: public; Owner: homeassistant
--

CREATE TABLE public.event_data (
    data_id bigint NOT NULL,
    hash bigint,
    shared_data text
);


ALTER TABLE public.event_data OWNER TO homeassistant;

--
-- Name: event_data_data_id_seq; Type: SEQUENCE; Schema: public; Owner: homeassistant
--

ALTER TABLE public.event_data ALTER COLUMN data_id ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME public.event_data_data_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);


--
-- Name: event_types; Type: TABLE; Schema: public; Owner: homeassistant
--

CREATE TABLE public.event_types (
    event_type_id bigint NOT NULL,
    event_type character varying(64)
);


ALTER TABLE public.event_types OWNER TO homeassistant;

--
-- Name: event_types_event_type_id_seq; Type: SEQUENCE; Schema: public; Owner: homeassistant
--

ALTER TABLE public.event_types ALTER COLUMN event_type_id ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME public.event_types_event_type_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);


--
-- Name: events; Type: TABLE; Schema: public; Owner: homeassistant
--

CREATE TABLE public.events (
    event_id bigint NOT NULL,
    event_type character(1),
    event_data character(1),
    origin character(1),
    origin_idx smallint,
    time_fired timestamp with time zone,
    time_fired_ts double precision,
    context_id character(1),
    context_user_id character(1),
    context_parent_id character(1),
    data_id bigint,
    context_id_bin bytea,
    context_user_id_bin bytea,
    context_parent_id_bin bytea,
    event_type_id bigint
);


ALTER TABLE public.events OWNER TO homeassistant;

--
-- Name: events_event_id_seq; Type: SEQUENCE; Schema: public; Owner: homeassistant
--

ALTER TABLE public.events ALTER COLUMN event_id ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME public.events_event_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);


--
-- Name: migration_changes; Type: TABLE; Schema: public; Owner: homeassistant
--

CREATE TABLE public.migration_changes (
    migration_id character varying(255) NOT NULL,
    version smallint NOT NULL
);


ALTER TABLE public.migration_changes OWNER TO homeassistant;

--
-- Name: recorder_runs; Type: TABLE; Schema: public; Owner: homeassistant
--

CREATE TABLE public.recorder_runs (
    run_id bigint NOT NULL,
    start timestamp with time zone NOT NULL,
    "end" timestamp with time zone,
    closed_incorrect boolean NOT NULL,
    created timestamp with time zone NOT NULL
);


ALTER TABLE public.recorder_runs OWNER TO homeassistant;

--
-- Name: recorder_runs_run_id_seq; Type: SEQUENCE; Schema: public; Owner: homeassistant
--

ALTER TABLE public.recorder_runs ALTER COLUMN run_id ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME public.recorder_runs_run_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);


--
-- Name: schema_changes; Type: TABLE; Schema: public; Owner: homeassistant
--

CREATE TABLE public.schema_changes (
    change_id bigint NOT NULL,
    schema_version integer,
    changed timestamp with time zone NOT NULL
);


ALTER TABLE public.schema_changes OWNER TO homeassistant;

--
-- Name: schema_changes_change_id_seq; Type: SEQUENCE; Schema: public; Owner: homeassistant
--

ALTER TABLE public.schema_changes ALTER COLUMN change_id ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME public.schema_changes_change_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);


--
-- Name: state_attributes; Type: TABLE; Schema: public; Owner: homeassistant
--

CREATE TABLE public.state_attributes (
    attributes_id bigint NOT NULL,
    hash bigint,
    shared_attrs text
);


ALTER TABLE public.state_attributes OWNER TO homeassistant;

--
-- Name: state_attributes_attributes_id_seq; Type: SEQUENCE; Schema: public; Owner: homeassistant
--

ALTER TABLE public.state_attributes ALTER COLUMN attributes_id ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME public.state_attributes_attributes_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);


--
-- Name: states; Type: TABLE; Schema: public; Owner: homeassistant
--

CREATE TABLE public.states (
    state_id bigint NOT NULL,
    entity_id character(1),
    state character varying(255),
    attributes character(1),
    event_id smallint,
    last_changed timestamp with time zone,
    last_changed_ts double precision,
    last_reported_ts double precision,
    last_updated timestamp with time zone,
    last_updated_ts double precision,
    old_state_id bigint,
    attributes_id bigint,
    context_id character(1),
    context_user_id character(1),
    context_parent_id character(1),
    origin_idx smallint,
    context_id_bin bytea,
    context_user_id_bin bytea,
    context_parent_id_bin bytea,
    metadata_id bigint
);


ALTER TABLE public.states OWNER TO homeassistant;

--
-- Name: states_meta; Type: TABLE; Schema: public; Owner: homeassistant
--

CREATE TABLE public.states_meta (
    metadata_id bigint NOT NULL,
    entity_id character varying(255)
);


ALTER TABLE public.states_meta OWNER TO homeassistant;

--
-- Name: states_meta_metadata_id_seq; Type: SEQUENCE; Schema: public; Owner: homeassistant
--

ALTER TABLE public.states_meta ALTER COLUMN metadata_id ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME public.states_meta_metadata_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);


--
-- Name: states_state_id_seq; Type: SEQUENCE; Schema: public; Owner: homeassistant
--

ALTER TABLE public.states ALTER COLUMN state_id ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME public.states_state_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);


--
-- Name: statistics; Type: TABLE; Schema: public; Owner: homeassistant
--

CREATE TABLE public.statistics (
    id bigint NOT NULL,
    created timestamp with time zone,
    created_ts double precision,
    metadata_id bigint,
    start timestamp with time zone,
    start_ts double precision,
    mean double precision,
    mean_weight double precision,
    min double precision,
    max double precision,
    last_reset timestamp with time zone,
    last_reset_ts double precision,
    state double precision,
    sum double precision
);


ALTER TABLE public.statistics OWNER TO homeassistant;

--
-- Name: statistics_id_seq; Type: SEQUENCE; Schema: public; Owner: homeassistant
--

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


--
-- Name: statistics_meta; Type: TABLE; Schema: public; Owner: homeassistant
--

CREATE TABLE public.statistics_meta (
    id bigint NOT NULL,
    statistic_id character varying(255),
    source character varying(32),
    unit_of_measurement character varying(255),
    unit_class character varying(255),
    has_mean boolean,
    has_sum boolean,
    name character varying(255),
    mean_type smallint NOT NULL
);


ALTER TABLE public.statistics_meta OWNER TO homeassistant;

--
-- Name: statistics_meta_id_seq; Type: SEQUENCE; Schema: public; Owner: homeassistant
--

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


--
-- Name: statistics_runs; Type: TABLE; Schema: public; Owner: homeassistant
--

CREATE TABLE public.statistics_runs (
    run_id bigint NOT NULL,
    start timestamp with time zone NOT NULL
);


ALTER TABLE public.statistics_runs OWNER TO homeassistant;

--
-- Name: statistics_runs_run_id_seq; Type: SEQUENCE; Schema: public; Owner: homeassistant
--

ALTER TABLE public.statistics_runs ALTER COLUMN run_id ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME public.statistics_runs_run_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);


--
-- Name: statistics_short_term; Type: TABLE; Schema: public; Owner: homeassistant
--

CREATE TABLE public.statistics_short_term (
    id bigint NOT NULL,
    created timestamp with time zone,
    created_ts double precision,
    metadata_id bigint,
    start timestamp with time zone,
    start_ts double precision,
    mean double precision,
    mean_weight double precision,
    min double precision,
    max double precision,
    last_reset timestamp with time zone,
    last_reset_ts double precision,
    state double precision,
    sum double precision
);


ALTER TABLE public.statistics_short_term OWNER TO homeassistant;

--
-- Name: statistics_short_term_id_seq; Type: SEQUENCE; Schema: public; Owner: homeassistant
--

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


--
-- Name: event_data event_data_pkey; Type: CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.event_data
    ADD CONSTRAINT event_data_pkey PRIMARY KEY (data_id);


--
-- Name: event_types event_types_pkey; Type: CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.event_types
    ADD CONSTRAINT event_types_pkey PRIMARY KEY (event_type_id);


--
-- Name: events events_pkey; Type: CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.events
    ADD CONSTRAINT events_pkey PRIMARY KEY (event_id);


--
-- Name: migration_changes migration_changes_pkey; Type: CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.migration_changes
    ADD CONSTRAINT migration_changes_pkey PRIMARY KEY (migration_id);


--
-- Name: recorder_runs recorder_runs_pkey; Type: CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.recorder_runs
    ADD CONSTRAINT recorder_runs_pkey PRIMARY KEY (run_id);


--
-- Name: schema_changes schema_changes_pkey; Type: CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.schema_changes
    ADD CONSTRAINT schema_changes_pkey PRIMARY KEY (change_id);


--
-- Name: state_attributes state_attributes_pkey; Type: CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.state_attributes
    ADD CONSTRAINT state_attributes_pkey PRIMARY KEY (attributes_id);


--
-- Name: states_meta states_meta_pkey; Type: CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.states_meta
    ADD CONSTRAINT states_meta_pkey PRIMARY KEY (metadata_id);


--
-- Name: states states_pkey; Type: CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.states
    ADD CONSTRAINT states_pkey PRIMARY KEY (state_id);


--
-- Name: statistics_meta statistics_meta_pkey; Type: CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.statistics_meta
    ADD CONSTRAINT statistics_meta_pkey PRIMARY KEY (id);


--
-- Name: statistics statistics_pkey; Type: CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.statistics
    ADD CONSTRAINT statistics_pkey PRIMARY KEY (id);


--
-- Name: statistics_runs statistics_runs_pkey; Type: CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.statistics_runs
    ADD CONSTRAINT statistics_runs_pkey PRIMARY KEY (run_id);


--
-- Name: statistics_short_term statistics_short_term_pkey; Type: CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.statistics_short_term
    ADD CONSTRAINT statistics_short_term_pkey PRIMARY KEY (id);


--
-- Name: ix_event_data_hash; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE INDEX ix_event_data_hash ON public.event_data USING btree (hash);


--
-- Name: ix_event_types_event_type; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE UNIQUE INDEX ix_event_types_event_type ON public.event_types USING btree (event_type);


--
-- Name: ix_events_context_id_bin; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE INDEX ix_events_context_id_bin ON public.events USING btree (context_id_bin);


--
-- Name: ix_events_data_id; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE INDEX ix_events_data_id ON public.events USING btree (data_id);


--
-- Name: ix_events_event_type_id_time_fired_ts; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE INDEX ix_events_event_type_id_time_fired_ts ON public.events USING btree (event_type_id, time_fired_ts);


--
-- Name: ix_events_time_fired_ts; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE INDEX ix_events_time_fired_ts ON public.events USING btree (time_fired_ts);


--
-- Name: ix_recorder_runs_start_end; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE INDEX ix_recorder_runs_start_end ON public.recorder_runs USING btree (start, "end");


--
-- Name: ix_state_attributes_hash; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE INDEX ix_state_attributes_hash ON public.state_attributes USING btree (hash);


--
-- Name: ix_states_attributes_id; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE INDEX ix_states_attributes_id ON public.states USING btree (attributes_id);


--
-- Name: ix_states_context_id_bin; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE INDEX ix_states_context_id_bin ON public.states USING btree (context_id_bin);


--
-- Name: ix_states_last_updated_ts; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE INDEX ix_states_last_updated_ts ON public.states USING btree (last_updated_ts);


--
-- Name: ix_states_meta_entity_id; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE UNIQUE INDEX ix_states_meta_entity_id ON public.states_meta USING btree (entity_id);


--
-- Name: ix_states_metadata_id_last_updated_ts; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE INDEX ix_states_metadata_id_last_updated_ts ON public.states USING btree (metadata_id, last_updated_ts);


--
-- Name: ix_states_old_state_id; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE INDEX ix_states_old_state_id ON public.states USING btree (old_state_id);


--
-- Name: ix_statistics_meta_statistic_id; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE UNIQUE INDEX ix_statistics_meta_statistic_id ON public.statistics_meta USING btree (statistic_id);


--
-- Name: ix_statistics_runs_start; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE INDEX ix_statistics_runs_start ON public.statistics_runs USING btree (start);


--
-- Name: ix_statistics_short_term_start_ts; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE INDEX ix_statistics_short_term_start_ts ON public.statistics_short_term USING btree (start_ts);


--
-- Name: ix_statistics_short_term_statistic_id_start_ts; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE UNIQUE INDEX ix_statistics_short_term_statistic_id_start_ts ON public.statistics_short_term USING btree (metadata_id, start_ts);


--
-- Name: ix_statistics_start_ts; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE INDEX ix_statistics_start_ts ON public.statistics USING btree (start_ts);


--
-- Name: ix_statistics_statistic_id_start_ts; Type: INDEX; Schema: public; Owner: homeassistant
--

CREATE UNIQUE INDEX ix_statistics_statistic_id_start_ts ON public.statistics USING btree (metadata_id, start_ts);


--
-- Name: events events_data_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.events
    ADD CONSTRAINT events_data_id_fkey FOREIGN KEY (data_id) REFERENCES public.event_data(data_id);


--
-- Name: events events_event_type_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.events
    ADD CONSTRAINT events_event_type_id_fkey FOREIGN KEY (event_type_id) REFERENCES public.event_types(event_type_id);


--
-- Name: states states_attributes_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.states
    ADD CONSTRAINT states_attributes_id_fkey FOREIGN KEY (attributes_id) REFERENCES public.state_attributes(attributes_id);


--
-- Name: states states_metadata_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.states
    ADD CONSTRAINT states_metadata_id_fkey FOREIGN KEY (metadata_id) REFERENCES public.states_meta(metadata_id);


--
-- Name: states states_old_state_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.states
    ADD CONSTRAINT states_old_state_id_fkey FOREIGN KEY (old_state_id) REFERENCES public.states(state_id);


--
-- Name: statistics statistics_metadata_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.statistics
    ADD CONSTRAINT statistics_metadata_id_fkey FOREIGN KEY (metadata_id) REFERENCES public.statistics_meta(id) ON DELETE CASCADE;


--
-- Name: statistics_short_term statistics_short_term_metadata_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: homeassistant
--

ALTER TABLE ONLY public.statistics_short_term
    ADD CONSTRAINT statistics_short_term_metadata_id_fkey FOREIGN KEY (metadata_id) REFERENCES public.statistics_meta(id) ON DELETE CASCADE;


--
-- PostgreSQL database dump complete
--

\unrestrict 9zFMerZ7o6L03AnfBUNASNm8ofDuUqBhBkWGUYVYq1dGJZ5sz9TULNvRDy8ig5C


经过对你提供的两份 SQL 文件(MySQL 源 vs PostgreSQL 目标)的逐行比对和逻辑推演,我为你整理了一份最终版的“排雷清单”。

这是从 MySQL 迁移到 PostgreSQL 时,百分之百会遇到的具体技术障碍。如果脚本不处理这些问题,迁移一定会失败。


🔴 第一类:致命错误 (会导致迁移直接中断)

1. 空字符串撞击时间戳 (char(0) vs TIMESTAMP)

这是最隐蔽也是最致命的问题。

  • 位置states 表 (last_updated, last_changed), events 表 (time_fired), statistics 表 (start, created)。
  • MySQL 现状:字段类型是 char(0)。这意味着里面存的数据全是 空字符串 ""
  • Postgres 现状:字段类型是 timestamp with time zone
  • 冲突点Postgres 极其严格,它认为 "" 不是一个合法的时间。
  • 报错信息ERROR: invalid input syntax for type timestamp: ""
  • 必需对策:脚本必须检测:如果源数据是 "" 且目标是时间列,强制写入 NULL

2. Null 字节攻击 (\x00 in Text)

  • 位置state_attributes (shared_attrs), event_data (shared_data)。
  • MySQL 现状LONGTEXT 类型。MySQL 允许文本中包含二进制 \0 (Null Byte) 字符。
  • Postgres 现状TEXT 类型。Postgres 的 Text 类型底层是 C 语言字符串,严禁包含 \0,否则会截断或报错。
  • 冲突点:如果你的某个智能家居设备(比如乱码的 Zigbee 设备)上报过含有特殊字符的数据,迁移到这就挂了。
  • 报错信息ERROR: invalid byte sequence for encoding "UTF8": 0x00
  • 必需对策:Python 脚本在读取字符串后,必须执行 .replace('\0', '') 清洗数据。

3. 布尔值类型不匹配

  • 位置recorder_runs (closed_incorrect), statistics_meta (has_mean, has_sum)。
  • MySQL 现状tinyint(1),存储 01
  • Postgres 现状boolean,存储 falsetrue
  • 冲突点:虽然部分驱动能转换,但在使用 COPY 或严格 SQL 模式时,直接插入整数 1 到布尔字段会失败。
  • 报错信息column "xxx" is of type boolean but expression is of type integer
  • 必需对策:脚本必须显式将 0/1 转换为 Python 的 False/True 对象。

🟠 第二类:逻辑隐患 (迁移能成功,但 HA 运行不正常)

4. 时区丢失 (Timezone Naive)

  • 位置recorder_runs 表的 startend 字段。
  • MySQL 现状datetime(6)。这是“无时区”时间,比如 2023-01-01 12:00:00
  • Postgres 现状timestamp with time zone
  • 隐患:Postgres 收到这个时间后,会困惑“这是 UTC 还是北京时间?”。通常它会默认按服务器本地时间处理。如果你的 MySQL 里存的是 UTCHA 默认存 UTC),而 Postgres 把它当成北京时间存进去,时间就偏了 8 小时
  • 表现:历史记录图表的时间轴对不上。
  • 必需对策:脚本读取到 datetime 对象后,必须手动加上 tzinfo=timezone.utc

5. 自增序列未同步 (Sequence out of sync)

  • 位置:所有表的主键 (state_id, event_id, run_id 等)。
  • MySQL 机制:插入 ID=1000,下一次自动生成 1001
  • Postgres 机制:插入 ID=1000,序列生成器(Sequence不会自动跳到 1000,它可能还在 1
  • 隐患:迁移完几百万条数据后,HA 启动,试图插入一条新状态,Postgres 分配 ID=2,结果数据库里已经有 ID=2 了。
  • 报错信息duplicate key value violates unique constraint
  • 必需对策:迁移结束后,必须运行 setval SQL 命令,重置所有序列。

🟢 第三类:无需担心的差异 (自动兼容)

以下差异在你的 SQL 文件中存在,但不会导致问题,脚本可以直接处理:

  1. 整数精度
    • MySQL int(10) unsigned (Hash值) -> Postgres bigint
    • 结论:安全。bigint 范围比 int unsigned 大,直接存没问题。
  2. 双精度时间戳
    • MySQL double (_ts 字段) -> Postgres double precision
    • 结论:安全。这是纯数字,不涉及编码和时区,直接复制。
  3. 废弃字段的字符长度
    • MySQL entity_id char(0) -> Postgres entity_id character(1)
    • 结论:安全。MySQL 里的 NULL"" 都可以存入 Postgres 的 character(1)(只要脚本把空串处理为 NULL 最好,或者存入空串也没问题,因为 Postgres 的 char(1) 能存空串)。

📝 最终结论

你不需要手动修改 SQL,也不需要担心这些差异。

我之前提供的 migrate_ha_final.py 脚本,是基于上述所有“坑”编写的。它内置了以下逻辑闭环:

  1. clean_value 函数:专门拦截 char(0)NULL
  2. clean_value 函数:专门清洗 \0
  3. clean_value 函数:专门附加 UTC 时区。
  4. 脚本末尾:专门执行 setval 修复序列。

你可以放心使用该脚本进行迁移。