-- ============================================================
-- RECREARE PDB XEPDB1 - Oracle XE 21c
-- Scop: PDB curat cu SYSAUX mic (~500 MB vs 7.8 GB)
-- ============================================================
--
-- IMPORTANT: Inlocuieste PAROLA_SYS cu parola reala a user-ului SYS.
--   Daca nu stii parola, seteaz-o INAINTE de export:
--     sqlplus / as sysdba
--     ALTER USER SYS IDENTIFIED BY "ParolaNoua123";
--   Apoi foloseste "ParolaNoua123" peste tot unde apare PAROLA_SYS.
--
-- PREREQUISITE: Exporturile trebuie sa fi fost rulate cu succes!
--   1. expdp (Data Pump) - din CMD
--   2. export_pdb_complet.sql - din SQLPlus
--   3. Fisierele .dmp si .sql trebuie sa fie in C:\DMPDIR
--
-- TIMP ESTIMAT: ~30-45 minute (depinde de dimensiunea dump-urilor)
-- ============================================================

-- ************************************************************
-- PASUL 0: EXPORT (daca nu s-a facut deja)
-- ************************************************************
-- Conecteaza-te la PDB-ul VECHI si ruleaza exporturile:
--
-- A) Data Pump (din CMD, nu SQLPlus):
--    expdp \"sys/PAROLA_SYS@XEPDB1 AS SYSDBA\" schemas=CONTAFIN_ORACLE,FIRMA directory=DMPDIR dumpfile=schemas_export.dmp logfile=schemas_export.log
--    expdp \"sys/PAROLA_SYS@XEPDB1 AS SYSDBA\" tables=SYS.AUTH_SERII,SYS.AUTH_DETALII,SYS.INFO directory=DMPDIR dumpfile=sys_tables.dmp logfile=sys_tables_export.log
--
-- B) SQL export (din SQLPlus conectat la XEPDB1):
--    sqlplus sys/PAROLA_SYS@XEPDB1 AS SYSDBA
--    @C:\DMPDIR\import\export_pdb_complet.sql
--
-- VERIFICA: schemas_export.dmp si sys_tables.dmp exista in C:\DMPDIR
--           si log-urile nu contin erori fatale.
-- ************************************************************

-- ============================================================
-- PASUL 1: Drop PDB vechi
-- ============================================================
-- Conectare: sqlplus / as sysdba   (conecteaza la CDB$ROOT)
-- SAU:       sqlplus sys/PAROLA_SYS AS SYSDBA

ALTER SESSION SET CONTAINER = CDB$ROOT;
ALTER PLUGGABLE DATABASE XEPDB1 CLOSE IMMEDIATE;
DROP PLUGGABLE DATABASE XEPDB1 INCLUDING DATAFILES;

-- ============================================================
-- PASUL 2: Recreare PDB din seed
-- ============================================================
CREATE PLUGGABLE DATABASE XEPDB1 ADMIN USER PDBADMIN IDENTIFIED BY "RomfastRP2023"
  FILE_NAME_CONVERT = ('pdbseed', 'XEPDB1');

ALTER PLUGGABLE DATABASE XEPDB1 OPEN;
ALTER PLUGGABLE DATABASE XEPDB1 SAVE STATE;

-- ============================================================
-- PASUL 3: Conectare la PDB-ul nou + creare DMPDIR
-- ============================================================
ALTER SESSION SET CONTAINER = XEPDB1;

CREATE OR REPLACE DIRECTORY DMPDIR AS 'C:\DMPDIR';
GRANT READ, WRITE ON DIRECTORY DMPDIR TO SYS;

-- ============================================================
-- PASUL 4: FAZA 1 - Infrastructura (inainte de Data Pump)
-- ============================================================
-- Ruleaza import master-ul care creeaza:
--   tablespaces -> useri -> directories -> tabele SYS -> context

@C:\DMPDIR\import\11_import_master.sql

-- ============================================================
-- PASUL 5: DATA PUMP IMPORT (din CMD, NU din SQLPlus!)
-- ============================================================
-- Deschide CMD nou si ruleaza:
--
--   impdp \"sys/PAROLA_SYS@XEPDB1 AS SYSDBA\" directory=DMPDIR dumpfile=schemas_export.dmp logfile=schemas_import.log remap_tablespace=USERS:ROA
--   impdp \"sys/PAROLA_SYS@XEPDB1 AS SYSDBA\" directory=DMPDIR dumpfile=sys_tables.dmp logfile=sys_tables_import.log table_exists_action=APPEND
--
-- ASTEAPTA sa termine AMBELE importuri inainte de pasul 6!
-- Verifica log-urile: schemas_import.log si sys_tables_import.log
-- ============================================================

-- ============================================================
-- PASUL 6: FAZA 2 - Obiecte dependente (dupa Data Pump)
-- ============================================================
-- Conecteaza-te inapoi la PDB:
--   sqlplus sys/PAROLA_SYS@XEPDB1 AS SYSDBA
-- SAU daca esti deja conectat la CDB$ROOT:
--   ALTER SESSION SET CONTAINER = XEPDB1;

@C:\DMPDIR\import\11_import_master_faza2.sql

-- ============================================================
-- PASUL 7: Compilare obiecte invalide
-- ============================================================
EXEC DBMS_UTILITY.COMPILE_SCHEMA('CONTAFIN_ORACLE');
EXEC DBMS_UTILITY.COMPILE_SCHEMA('FIRMA');
EXEC DBMS_UTILITY.COMPILE_SCHEMA('SYS', FALSE);

-- ============================================================
-- PASUL 8: Verificare finala
-- ============================================================
PROMPT
PROMPT =============================================
PROMPT  VERIFICARE SPATIU
PROMPT =============================================
SELECT tablespace_name, ROUND(SUM(bytes)/1024/1024) AS allocated_mb
FROM dba_data_files GROUP BY tablespace_name ORDER BY allocated_mb DESC;

SELECT ROUND(SUM(bytes)/1024/1024) AS total_mb FROM dba_data_files;

PROMPT
PROMPT =============================================
PROMPT  OBIECTE INVALIDE (ar trebui sa fie 0)
PROMPT =============================================
SELECT owner, object_name, object_type, status
FROM dba_objects
WHERE status = 'INVALID'
AND owner IN ('SYS', 'CONTAFIN_ORACLE', 'FIRMA', 'ROMPETROLE')
ORDER BY owner, object_type, object_name;

PROMPT
PROMPT =============================================
PROMPT  AUTO TASKS (ar trebui DISABLED)
PROMPT =============================================
SELECT client_name, status FROM dba_autotask_client;

PROMPT
PROMPT =============================================
PROMPT  RECREARE PDB COMPLETA!
PROMPT  Total alocat ar trebui sa fie ~2-3 GB
PROMPT  (vs 11.5 GB inainte de recreare)
PROMPT =============================================
