Skip to content

Commit 46a5a84

Browse files
committed
update
1 parent 643645a commit 46a5a84

File tree

1 file changed

+31
-106
lines changed

1 file changed

+31
-106
lines changed

README.md

Lines changed: 31 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ A QGIS plugin for managing PostgreSQL database templates and creating portable p
1111

1212
### 📦 Portable Project Archives
1313
- **PostgreSQL to Geopackage**: Convert all PostgreSQL layers to a single portable geopackage
14-
- **Complete Project Export**: Copy all project files including DCIM folders and media
15-
- **QField Compatible**: Create archives ready for mobile data collection with QField
16-
- **Progress Tracking**: Real-time progress feedback during export process
14+
- **Complete Project Export**: Copy all project files including DCIM folders and media and updates source references
1715

1816
## Installation
1917

@@ -47,10 +45,10 @@ A QGIS plugin for managing PostgreSQL database templates and creating portable p
4745

4846
#### Deploying from Template
4947
1. Ensure your connection is configured
50-
2. Go to the **Deploy Template** tab
48+
2. Go to the **Create Template** tab
5149
3. Select the template database
5250
4. Enter new database name
53-
5. Click **Deploy Database**
51+
5. Click **Create Database**
5452

5553
### Creating Portable Archives
5654

@@ -76,70 +74,55 @@ The archive process copies ALL files from your project directory, including DCIM
7674
- PostgreSQL database with appropriate permissions
7775
- Python 3.6+ (included with QGIS)
7876

79-
## Supported Formats
80-
81-
### Input
82-
- PostgreSQL databases
83-
- QGIS projects (.qgs files)
84-
- All standard QGIS vector layers
85-
86-
### Output
87-
- PostgreSQL templates
88-
- Geopackage (.gpkg)
89-
- Portable QGIS projects
90-
9177
## Technical Details
9278

93-
### SQL Commands for Template Creation
79+
### Manual Template Creation
9480

95-
When creating a clean template from a database, the plugin executes the following SQL commands in sequence:
81+
If you prefer to create templates manually, you can execute these SQL commands directly in any PostgreSQL client (DBeaver, pgAdmin, etc.):
9682

97-
#### 1. Disconnect Active Users
9883
```sql
99-
-- Terminate all active connections to the source database
84+
-- 1. Connect to postgres database and terminate active connections to source database
10085
SELECT pg_terminate_backend(pid)
10186
FROM pg_stat_activity
10287
WHERE datname = 'source_database_name'
10388
AND pid != pg_backend_pid();
104-
```
10589

106-
#### 2. Create Template Database (Full Copy with Template Flag)
107-
```sql
108-
-- Drop existing template if it exists
109-
DROP DATABASE "template_database_name";
110-
111-
-- Create the new template database as a complete copy of the source
112-
-- and mark it as a template in the same command
90+
-- 2. Create template database
11391
CREATE DATABASE "template_database_name"
11492
WITH TEMPLATE "source_database_name"
11593
IS_TEMPLATE = true;
116-
```
11794

118-
#### 3. Truncate All Data from Template
119-
```sql
120-
-- Connect to the template database and get all user tables
121-
SELECT schemaname, tablename
122-
FROM pg_tables
123-
WHERE schemaname NOT IN ('information_schema', 'pg_catalog', 'pg_toast')
124-
ORDER BY schemaname, tablename;
125-
126-
-- Truncate each table individually with CASCADE
127-
TRUNCATE TABLE "schema_name"."table_name" CASCADE;
95+
-- 3. Connect to the template database
96+
97+
-- 4. Truncate all user tables to remove data while preserving structure
98+
DO $$
99+
DECLARE
100+
table_record RECORD;
101+
BEGIN
102+
FOR table_record IN
103+
SELECT schemaname, tablename
104+
FROM pg_tables
105+
WHERE schemaname NOT IN ('information_schema', 'pg_catalog', 'pg_toast')
106+
ORDER BY schemaname, tablename
107+
LOOP
108+
EXECUTE format('TRUNCATE TABLE %I.%I CASCADE;',
109+
table_record.schemaname,
110+
table_record.tablename);
111+
RAISE NOTICE 'Cleared data from %.%',
112+
table_record.schemaname,
113+
table_record.tablename;
114+
END LOOP;
115+
END $$;
128116
```
129117

130-
#### 4. Template Deployment Commands
131-
When deploying a new database from a template:
118+
**Template Deployment:**
132119
```sql
133-
-- Drop existing database if it exists
134-
DROP DATABASE "new_database_name";
135-
136120
-- Create new database from template
137121
CREATE DATABASE "new_database_name"
138122
WITH TEMPLATE "template_database_name";
139123
```
140124

141-
#### 5. Template Deletion Commands
142-
When deleting a template:
125+
**Template Deletion:**
143126
```sql
144127
-- Deactivate template status first
145128
UPDATE pg_database
@@ -150,52 +133,6 @@ WHERE datname = 'template_database_name';
150133
DROP DATABASE "template_database_name";
151134
```
152135

153-
### Manual Template Creation
154-
155-
If you prefer to create templates manually, you can use these commands directly in PostgreSQL:
156-
157-
```bash
158-
# Connect to PostgreSQL
159-
psql -h hostname -U username -d postgres
160-
161-
# 1. Terminate active connections to source database
162-
SELECT pg_terminate_backend(pid)
163-
FROM pg_stat_activity
164-
WHERE datname = 'source_database_name'
165-
AND pid != pg_backend_pid();
166-
167-
# 2. Create template database
168-
CREATE DATABASE "template_database_name"
169-
WITH TEMPLATE "source_database_name"
170-
IS_TEMPLATE = true;
171-
172-
# 3. Connect to template database
173-
\c template_database_name
174-
175-
# 4. Get list of all user tables
176-
SELECT schemaname, tablename
177-
FROM pg_tables
178-
WHERE schemaname NOT IN ('information_schema', 'pg_catalog', 'pg_toast')
179-
ORDER BY schemaname, tablename;
180-
181-
# 5. Truncate each table (repeat for each table found)
182-
TRUNCATE TABLE "schema_name"."table_name" CASCADE;
183-
```
184-
185-
## Use Cases
186-
187-
### Template Management
188-
- **Development Workflows**: Create clean database templates for new projects
189-
- **Testing**: Deploy consistent test databases
190-
- **Team Collaboration**: Share database structures without sensitive data
191-
- **Backup & Recovery**: Maintain structural backups
192-
193-
### Portable Archives
194-
- **Project Sharing**: Share projects without database dependencies
195-
- **Offline Work**: Convert online projects for offline use
196-
- **Data Distribution**: Package projects for easy distribution
197-
198-
199136
## Troubleshooting
200137

201138
### Common Issues
@@ -222,16 +159,4 @@ TRUNCATE TABLE "schema_name"."table_name" CASCADE;
222159

223160
## License
224161

225-
This project is licensed under the GNU General Public License v3.0 - see the [LICENSE](https://opensource.org/license/mit) file for details.
226-
227-
228-
## Changelog
229-
230-
### v1.0.0
231-
- Initial release
232-
- PostgreSQL template management
233-
- Portable project archive creation
234-
- QField compatibility
235-
- Progress tracking and user feedback
236-
237-
---
162+
This project is licensed under the GNU General Public License v3.0 - see the [LICENSE](https://opensource.org/license/mit) file for details.

0 commit comments

Comments
 (0)