uploader-bot/scripts/init-db.sql

57 lines
2.4 KiB
SQL

-- PostgreSQL Database Initialization Script for MY Uploader Bot
-- This script sets up the initial database structure and users
-- Create database if not exists (done by PostgreSQL container initialization)
-- Create user and grant privileges are also handled by container environment variables
-- Set up database encoding and collation (handled by POSTGRES_INITDB_ARGS)
-- Create extensions if needed
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "pg_trgm";
CREATE EXTENSION IF NOT EXISTS "btree_gin";
-- Create schemas for better organization
CREATE SCHEMA IF NOT EXISTS my_network;
CREATE SCHEMA IF NOT EXISTS content;
CREATE SCHEMA IF NOT EXISTS users;
CREATE SCHEMA IF NOT EXISTS analytics;
-- Grant permissions on schemas
GRANT USAGE, CREATE ON SCHEMA my_network TO my_user;
GRANT USAGE, CREATE ON SCHEMA content TO my_user;
GRANT USAGE, CREATE ON SCHEMA users TO my_user;
GRANT USAGE, CREATE ON SCHEMA analytics TO my_user;
-- Set default search path
ALTER DATABASE my_uploader_db SET search_path TO public, my_network, content, users, analytics;
-- Create initial tables structure (if not handled by ORM migrations)
-- These will be created by the application's migration system
-- Example: Create a simple health check table
CREATE TABLE IF NOT EXISTS health_check (
id SERIAL PRIMARY KEY,
status VARCHAR(50) NOT NULL DEFAULT 'ok',
timestamp TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
details JSONB
);
-- Insert initial health check record
INSERT INTO health_check (status, details)
VALUES ('initialized', '{"message": "Database initialized successfully", "version": "1.0.0"}')
ON CONFLICT DO NOTHING;
-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_health_check_timestamp ON health_check(timestamp);
CREATE INDEX IF NOT EXISTS idx_health_check_status ON health_check(status);
-- Log successful initialization
INSERT INTO health_check (status, details)
VALUES ('ready', '{"message": "PostgreSQL initialization completed", "timestamp": "' || NOW() || '"}');
COMMENT ON DATABASE my_uploader_db IS 'MY Uploader Bot - Main database for content management and MY Network protocol';
COMMENT ON SCHEMA my_network IS 'MY Network protocol related tables and functions';
COMMENT ON SCHEMA content IS 'Content storage and management tables';
COMMENT ON SCHEMA users IS 'User management and authentication tables';
COMMENT ON SCHEMA analytics IS 'Analytics and metrics tables';