USERS


Scripting a user from a development environment and promoting it to a UAT (User Acceptance Testing) environment involves generating the SQL script necessary to create the user and its associated objects, permissions, and roles in the UAT environment. Here's how you could do it manually. This process assumes that you have the necessary privileges in both environments to perform these actions.


1. **Generate User Creation Script**:

   - Firstly, you need to script the creation of the user. You'll need to decide whether you want to keep the same password or set a new one for the UAT environment.

     -- Run this in your dev environment

     CREATE USER test1 IDENTIFIED BY new_password

     DEFAULT TABLESPACE default_tablespace

     TEMPORARY TABLESPACE temp_tablespace

     QUOTA 100M ON users;


DECLARE

    v_user_count NUMBER;

BEGIN

    SELECT COUNT(*)

    INTO v_user_count

    FROM dba_users

    WHERE username = 'TEST1';  -- Change 'TEST1' to the name of the user you are checking/creating.


    IF v_user_count = 0 THEN

        EXECUTE IMMEDIATE 'CREATE USER test1 IDENTIFIED BY user_password

                           DEFAULT TABLESPACE default_tablespace

                           TEMPORARY TABLESPACE temp_tablespace';

        DBMS_OUTPUT.PUT_LINE('User test1 created.');

    ELSE

        DBMS_OUTPUT.PUT_LINE('User test1 already exists.');

    END IF;

END;

/


2. **Generate Role Grant Script**:

   - You'll need to list all the roles granted to `test1` and generate a script to grant these roles in UAT.

     -- Run this in your dev environment to get the roles

     SELECT 'GRANT ' || granted_role || ' TO test1;' FROM dba_role_privs WHERE grantee = 'TEST1';


3. **Generate Object Privileges Script**:

   - List all object privileges granted to `test1` and generate a script to grant these privileges in UAT.

     -- Run this in your dev environment to get the object privileges

     SELECT 'GRANT ' || privilege || ' ON ' || owner || '.' || table_name || ' TO test1;' 

     FROM dba_tab_privs WHERE grantee = 'TEST1';


4. **Generate System Privileges Script**:

   - List all system privileges granted to `test1` and generate a script to grant these privileges in UAT.

     -- Run this in your dev environment to get the system privileges

     SELECT 'GRANT ' || privilege || ' TO test1;' 

     FROM dba_sys_privs WHERE grantee = 'TEST1';


5. **Compile the Scripts**:

   - Combine all the generated scripts into a single SQL script file.


6. **Run the Script in UAT**:

   - Now you can run the compiled script in your UAT environment to create the `test1` user with the same roles and privileges it had in the development environment.

     ```bash

     sqlplus username/password@UAT @script.sql


Please make sure to review and adjust the scripts as necessary to comply with your organization's standards and the specific requirements of the UAT environment.

Note: This method is manual and assumes that the same roles, privileges, and tablespaces exist in both environments. There are also tools and solutions like Oracle Data Pump, SQL Developer, or third-party tools that can help with schema migration tasks which might automate some of these steps.

ROLES:

In order to create roles in UAT (User Acceptance Testing) that exist in DEV (Development), you would typically script out the roles from DEV and then run the scripts in UAT. Here's a manual approach on how you could achieve this. This process assumes you have the necessary privileges in both environments to perform these actions.


1. **List the Roles in DEV**:

   First, list all the roles in your DEV environment:

   SELECT role FROM dba_roles;

2. **Script out Role Creation and Granted Privileges**:

   For each role in DEV, you would want to script out the creation of the role and any system privileges and object privileges granted to that role.

   - **Role Creation**:

       CREATE ROLE role_name;

DECLARE

    v_role_count NUMBER;

BEGIN

    SELECT COUNT(*)

    INTO v_role_count

    FROM dba_roles

    WHERE role = 'YOUR_ROLE_NAME';  -- Replace 'YOUR_ROLE_NAME' with the name of the role you are checking.


    IF v_role_count = 0 THEN

        DBMS_OUTPUT.PUT_LINE('Role does not exist.');

    ELSE

        DBMS_OUTPUT.PUT_LINE('Role exists.');

    END IF;

END;

/


    - **System Privileges**:

     SELECT 'GRANT ' || privilege || ' TO ' || role || ';' FROM dba_sys_privs WHERE grantee = 'ROLE_NAME';


   - **Object Privileges**:

     SELECT 'GRANT ' || privilege || ' ON ' || owner || '.' || table_name || ' TO ' || role || ';' 

     FROM dba_tab_privs WHERE grantee = 'ROLE_NAME';


3. **Compile the Scripts**:

   Combine all the generated scripts from step 2 into a single SQL script file for each role or one big script file for all roles.


4. **Run the Script in UAT**:

   Now, you can run the compiled script in the UAT environment to create the roles and grant the privileges as they were in DEV.

   ```bash

   sqlplus username/password@UAT @script.sql


Please replace `'ROLE_NAME'`, `'role_name'`, `username`, `password`, and `@script.sql` with your actual values.

**Note**:

- Ensure you have the necessary privileges to execute these statements. Typically, you will need to be logged in as a user with `DBA` privileges or have the `CREATE ROLE` and `GRANT` system privileges.

- Be cautious about the permissions you are granting, especially in a production environment or UAT, as incorrect permissions can lead to security risks.

- This method is manual and assumes that the underlying objects and privileges are the same in both environments. There are also tools and solutions like Oracle Data Pump, SQL Developer, or third-party tools that can help with schema and role migration tasks which might automate some of these steps.