@@ -88,6 +88,93 @@ The archive process copies ALL files from your project directory, including DCIM
8888- Geopackage (.gpkg)
8989- Portable QGIS projects
9090
91+ ## Technical Details
92+
93+ ### SQL Commands for Template Creation
94+
95+ When creating a clean template from a database, the plugin executes the following SQL commands in sequence:
96+
97+ #### 1. Disconnect Active Users
98+ ``` sql
99+ -- Terminate all active connections to the source database
100+ SELECT pg_terminate_backend(pg_stat_activity .pid )
101+ FROM pg_stat_activity
102+ WHERE pg_stat_activity .datname = ' source_database_name'
103+ AND pid <> pg_backend_pid();
104+ ```
105+
106+ #### 2. Create Template Database
107+ ``` sql
108+ -- Create the new template database with special template settings
109+ CREATE DATABASE template_database_name
110+ WITH TEMPLATE template0
111+ ENCODING ' UTF8'
112+ LC_COLLATE = ' en_US.UTF-8'
113+ LC_CTYPE = ' en_US.UTF-8'
114+ IS_TEMPLATE = true;
115+ ```
116+
117+ #### 3. Copy Schema Structure (Without Data)
118+ ``` sql
119+ -- Connect to template database and copy schema structure
120+ -- This includes tables, views, functions, triggers, etc.
121+ -- But excludes all data from tables
122+
123+ -- Copy table structures
124+ CREATE TABLE new_table (LIKE source_table INCLUDING ALL);
125+
126+ -- Copy views
127+ CREATE VIEW new_view AS SELECT * FROM source_view;
128+
129+ -- Copy functions and procedures
130+ -- (Function definitions are copied from source database)
131+
132+ -- Copy triggers
133+ -- (Trigger definitions are copied and recreated)
134+
135+ -- Copy constraints and indexes
136+ -- (Included with INCLUDING ALL clause)
137+ ```
138+
139+ #### 4. Set Template Permissions
140+ ``` sql
141+ -- Prevent connections to template database during creation
142+ UPDATE pg_database
143+ SET datallowconn = false
144+ WHERE datname = ' template_database_name' ;
145+
146+ -- Re-enable connections after setup is complete
147+ UPDATE pg_database
148+ SET datallowconn = true
149+ WHERE datname = ' template_database_name' ;
150+
151+ -- Set appropriate ownership and permissions
152+ ALTER DATABASE template_database_name OWNER TO template_owner;
153+ ```
154+
155+ #### 5. Template Deployment Commands
156+ When deploying a new database from a template:
157+ ``` sql
158+ -- Create new database from template
159+ CREATE DATABASE new_database_name
160+ WITH TEMPLATE template_database_name
161+ OWNER database_owner;
162+
163+ -- Grant appropriate permissions
164+ GRANT ALL PRIVILEGES ON DATABASE new_database_name TO database_user;
165+ ```
166+
167+ ### Manual Template Creation
168+
169+ If you prefer to create templates manually, you can use these commands directly in PostgreSQL:
170+
171+ ``` bash
172+ # Connect to PostgreSQL
173+ psql -h hostname -U username -d postgres
174+
175+ # Execute the SQL commands above in sequence
176+ ```
177+
91178## Use Cases
92179
93180### Template Management
@@ -140,4 +227,4 @@ This project is licensed under the GNU General Public License v3.0 - see the [LI
140227- QField compatibility
141228- Progress tracking and user feedback
142229
143- ---
230+ ---
0 commit comments