Skip to content

Commit cd023ef

Browse files
committed
Add notes for managing databases, ERPNext commands, and WordPress migration
1 parent 044d6a6 commit cd023ef

3 files changed

Lines changed: 188 additions & 0 deletions

File tree

docs/sysadmin/databases-notes.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Databases notes
2+
3+
## Managing Databases with Docker
4+
5+
When using databases within Docker containers, it's essential to manage data persistence and backups effectively.
6+
7+
### Get an sql prompt inside a running container
8+
9+
For MySQL/MariaDB:
10+
11+
```sh
12+
docker exec -it container_name mysql -u root -p
13+
docker exec -it container_name mariadb -u root -p
14+
```
15+
16+
## Tips and Tricks
17+
18+
### Cleaning Largest Tables
19+
20+
In order to identify the largest tables in your database, you can use the following SQL query:
21+
22+
```sql
23+
SELECT table_schema as `Database`, table_name AS `Table`, round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` FROM information_schema.TABLES ORDER BY (data_length + index_length) DESC;
24+
```
25+
26+
Create a stored procedure to clean old logs from a specific table (`tabName` in this example) in batches to avoid long locks:
27+
28+
```sql
29+
USE database_name; -- Get it with show databases;
30+
DELIMITER $$
31+
32+
CREATE OR REPLACE PROCEDURE clean_old_logs()
33+
BEGIN
34+
DECLARE rows_affected INT DEFAULT 1;
35+
36+
WHILE rows_affected > 0 DO
37+
DELETE FROM `tabName`
38+
WHERE creation < NOW() - INTERVAL 3 DAY
39+
LIMIT 500;
40+
41+
SET rows_affected = ROW_COUNT();
42+
END WHILE;
43+
END$$
44+
45+
DELIMITER ;
46+
47+
CALL clean_old_logs();
48+
```
49+
50+
Optimize the table you need after cleaning:
51+
52+
```sql
53+
OPTIMIZE TABLE `tabName`;
54+
```

docs/sysadmin/erpnext-notes.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Erpnext notes
2+
3+
I mainly use the Docker installation of ERPNext, so these notes are focused on that setup.
4+
5+
## General commands for Frappe/ERPNext with Docker
6+
7+
### Get a python prompt inside a running container
8+
9+
```sh
10+
sudo docker exec -it container_name_backend bench --site frontend console
11+
```
12+
13+
### When updating
14+
15+
If the image is updated, the database needs to be migrated.
16+
17+
```sh
18+
sudo docker exec -it container_name_backend bench --site frontend migrate
19+
```
20+
21+
### When frontend is not loading properly
22+
23+
```sh
24+
sudo docker exec -it container_name_backend bench --site frontend clear-cache
25+
sudo docker exec -it container_name_backend bench --site frontend clear-website-cache
26+
sudo docker exec -it container_name_redis_queue redis-cli flushall
27+
sudo docker exec -it container_name_redis_cache redis-cli flushall
28+
```
29+
30+
### Change variant template
31+
32+
Sometimes in ERPNext, an item variant may be incorrectly linked to the wrong template. The following Python scripts can be used within the Frappe framework to correct this issue.
33+
34+
**If the item has no links:**
35+
36+
```python
37+
def update_variant_template(variant_to_fix, correct_template):
38+
# Fetch the variant document
39+
doc = frappe.get_doc("Item", variant_to_fix)
40+
41+
# Update the variant_of field
42+
doc.variant_of = correct_template
43+
44+
# Save the changes
45+
doc.save()
46+
47+
# Commit to apply changes to the database
48+
frappe.db.commit()
49+
50+
print(f"Updated Item {variant_to_fix}: Now a variant of {correct_template}")
51+
```
52+
53+
**If the item has links and cannot be changed directly:**
54+
55+
```python
56+
def clone_variant_fix_template(old_variant, correct_template):
57+
# Fetch existing variant
58+
old_doc = frappe.get_doc("Item", old_variant)
59+
60+
# Generate a new unique item code
61+
new_item_code = make_autoname(f"{old_variant}-.#####")
62+
63+
# Create a new variant with the correct template
64+
new_doc = frappe.copy_doc(old_doc)
65+
new_doc.variant_of = correct_template
66+
new_doc.item_code = new_item_code # Assign a unique item code
67+
new_doc.insert()
68+
69+
# Deactivate old variant to prevent further usage
70+
old_doc.disabled = 1
71+
old_doc.save()
72+
73+
# Commit to apply changes to the database
74+
frappe.db.commit()
75+
76+
print(f"Created new variant {new_doc.name} ({new_item_code}) under {correct_template} and disabled {old_variant}.")
77+
```
78+
79+
**Example usage:**
80+
81+
```python
82+
# Update a single variant
83+
update_variant_template("variant_code", "template_code")
84+
85+
# Clone a variant and fix the template
86+
clone_variant_fix_template("variant_code", "template_code")
87+
```
88+
89+

docs/sysadmin/wordpress-notes.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Wordpress Notes
2+
3+
This document contains various notes and configurations related to managing and maintaining WordPress installations.
4+
5+
## Migration
6+
7+
When migrating a WordPress site from one server to another, consider the following steps:
8+
9+
1. **Backup Files and Database**: Ensure you have a complete backup of your WordPress files and database.
10+
- The files to back up include the entire WordPress directory, typically located at `/var/www/html/your-site`.
11+
- The database can be backed up using tools like `mysqldump`
12+
13+
```sh
14+
mysqldump -u username -p database_name > backup.sql
15+
```
16+
17+
2. **Transfer Files**: Use `rsync` or `scp` to transfer the WordPress files to the new server.
18+
19+
```sh
20+
rsync -avz /path/to/wordpress/ user@newserver:/path/to/destination/
21+
```
22+
23+
3. **Import Database**: On the new server, create a new database and import the backup.
24+
25+
```sh
26+
mysql -u username -p new_database_name < backup.sql
27+
```
28+
29+
4. **Update wp-config.php**: Modify the `wp-config.php` file on the new server to reflect the new database credentials.
30+
31+
5. **Update Site URL**: If the domain name has changed, update the site URL in the database. You can do this using wp-cli:
32+
33+
```sh
34+
# Migrate http to https
35+
docker run --rm --volumes-from wp-container --network container:wp-container wordpress:cli search-replace 'http://original.domain' 'https://new.domain'
36+
# Migrate https to https
37+
docker run --rm --volumes-from wp-container --network container:wp-container wordpress:cli search-replace 'https://original.domain' 'https://new.domain'
38+
# Migrate any remaining references
39+
docker run --rm --volumes-from wp-container --network container:wp-container wordpress:cli search-replace 'original.domain' 'new.domain'
40+
# Flush cache
41+
docker run --rm --volumes-from wp-container --network container:wp-container wordpress:cli cache flush
42+
```
43+
44+
Remember to recreate any cache of the extensions you are using.
45+

0 commit comments

Comments
 (0)