Skip to content

Commit 7d8a057

Browse files
Fix dev import process (#851)
* Fix dev import process * Rm no-op change * Update README.md Co-authored-by: Willard Nilges <willardnilges@gmail.com> --------- Co-authored-by: Willard Nilges <willardnilges@gmail.com>
1 parent 010300e commit 7d8a057

File tree

5 files changed

+97
-32
lines changed

5 files changed

+97
-32
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,4 @@ loadenv.sh
175175

176176
# Spreadsheet data
177177
spreadsheet_data
178-
178+
data/

README.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,17 @@ You'll probably want an admin account
109109
python src/manage.py createsuperuser
110110
```
111111

112-
And if you have access to it, you can use `import_spreadsheet_dump.sh` to populate
112+
And if you have access to it, you can use `import_datadump.sh` script to populate
113113
your database.
114114

115115
> [!WARNING]
116116
> This is _real member data_. DO NOT share this database with anyone under any
117117
> circumstances.
118118
119119
```sh
120-
cp -R <path_to_data_dump> ./spreadsheet_data/
121-
./scripts/import_spreadsheet_dump.sh
120+
mkdir data/
121+
cp <path_to_data_dump>/full_dump.sql data/
122+
./scripts/import_datadump.sh
122123
```
123124

124125
If you want to do work with celery, you'll need to run a worker as well as a beat.
@@ -318,6 +319,29 @@ ADMIN_MAP_BASE_URL=http://localhost:3000
318319

319320
Follow this PR: https://github.com/nycmeshnet/meshdb/pull/617/files
320321

322+
### Making Exports for New Devs
323+
324+
To make importable data exports for new devs, first obtain a local copy of the data you want to
325+
share (see Backups below). Then:
326+
327+
Run the scramble script to obfuscate PII:
328+
```sh
329+
python src/manage.py scramble_members
330+
```
331+
332+
Clear the data from the historical tables (so that we don't leak the data we just scrambled via a diff)
333+
```sh
334+
scripts/clear_history_tables.sh
335+
```
336+
[!WARNING] be sure that you spot check the data to make sure the scramble process worked as expected.
337+
338+
Finally, create an importable datadump with:
339+
```sh
340+
scripts/create_importable_datadump.sh
341+
```
342+
343+
The file will be written to `data/full_dump.sql`, share this with the new devs
344+
321345
### Backups
322346

323347
**The Proper Way**
@@ -351,7 +375,7 @@ $ echo 'drop database meshdb; create database meshdb;' | docker exec -i meshdb-p
351375

352376
4. Restore the backup
353377
```
354-
root@eefdc57a46c2:/opt/meshdb# python manage.py dbrestore -i default-bd0acc253775-2024-03-31-163520.psql.bin
378+
root@eefdc57a46c2:/opt/meshdb# python manage.py dbrestore -i default-bd0acc253775-2024-03-31-163520.psql.bin --database default
355379
```
356380

357381
**The Quick 'n Dirty Way**

scripts/clear_history_tables.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
DOCKER_PG_COMMAND="docker exec -i meshdb-postgres-1 psql -U meshdb -d meshdb"
4+
tables=(
5+
"meshapi_historicalaccesspoint"
6+
"meshapi_historicalbuilding"
7+
"meshapi_historicalbuilding_nodes"
8+
"meshapi_historicaldevice"
9+
"meshapi_historicalinstall"
10+
"meshapi_historicallink"
11+
"meshapi_historicallos"
12+
"meshapi_historicalmember"
13+
"meshapi_historicalnode"
14+
"meshapi_historicalsector"
15+
)
16+
17+
set -ex
18+
19+
for table_name in "${tables[@]}"
20+
do
21+
echo "TRUNCATE ${table_name} CASCADE;" | $DOCKER_PG_COMMAND
22+
done
23+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
DOCKER_PG_COMMAND="docker exec -i meshdb-postgres-1 pg_dump -U meshdb"
4+
DATA_DIR="./data/"
5+
tables=(
6+
"meshapi_accesspoint"
7+
"meshapi_building"
8+
"meshapi_building_nodes"
9+
"meshapi_device"
10+
"meshapi_historicalaccesspoint"
11+
"meshapi_historicalbuilding"
12+
"meshapi_historicalbuilding_nodes"
13+
"meshapi_historicaldevice"
14+
"meshapi_historicalinstall"
15+
"meshapi_historicallink"
16+
"meshapi_historicallos"
17+
"meshapi_historicalmember"
18+
"meshapi_historicalnode"
19+
"meshapi_historicalsector"
20+
"meshapi_install"
21+
"meshapi_link"
22+
"meshapi_los"
23+
"meshapi_member"
24+
"meshapi_node"
25+
"meshapi_sector"
26+
)
27+
set -ex
28+
29+
# Make sure our files exist.
30+
if [ ! -d "$DATA_DIR" ]; then
31+
echo "$DATA_DIR missing!"
32+
exit 1
33+
fi
34+
35+
docker exec -i meshdb-postgres-1 pg_dump -U meshdb -d meshdb ${tables[@]/#/-t } > "$DATA_DIR/full_dump.sql"
36+
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

3-
DOCKER_PG_COMMAND="docker exec -i meshdb-postgres-1 psql -U meshdb"
4-
DATA_DIR="./spreadsheet_data/"
3+
DOCKER_PG_COMMAND="docker exec -i meshdb-postgres-1 psql -U meshdb -d meshdb"
4+
DATA_DIR="./data/"
55
tables=(
66
"meshapi_los"
77
"meshapi_link"
@@ -32,40 +32,22 @@ if [ ! -d "$DATA_DIR" ]; then
3232
exit 1
3333
fi
3434

35-
for table_name in "${tables[@]}"
36-
do
37-
if [ ! -e "spreadsheet_data/$table_name.sql" ]; then
38-
echo "$table_name.sql is missing!"
39-
exit 1
40-
fi
41-
done
42-
43-
# Don't need to create them.
44-
# XXX (willnilges): Do we want to have an option to dump the tables?
45-
#for table_name in "${tables[@]}"
46-
#do
47-
# docker exec -i meshdb_postgres_1 pg_dump -U meshdb --table="$table_name" > "$table_name.sql"
48-
#done
35+
if [ ! -e "$DATA_DIR/full_dump.sql" ]; then
36+
echo "full_dump.sql is missing!"
37+
exit 1
38+
fi
4939

5040
num_tables=${#tables[@]}
51-
52-
# Yeet
53-
# XXX (willnilges): Would it be better to use manage.py?
5441
for ((i = num_tables - 1; i >= 0; i--));
5542
do
5643
$DOCKER_PG_COMMAND -c "DROP TABLE IF EXISTS ${tables[i]} CASCADE"
5744
done
5845

46+
5947
# Import the new data
60-
for table_name in "${tables[@]}"
61-
do
62-
cat "spreadsheet_data/$table_name.sql" | $DOCKER_PG_COMMAND
63-
done
48+
cat "$DATA_DIR/full_dump.sql" | $DOCKER_PG_COMMAND
49+
6450

6551
# Fix the auto numbering sequence for installs
6652
max_install_number=$(($(${DOCKER_PG_COMMAND} -c "SELECT MAX(install_number) FROM meshapi_install" -At) + 1))
6753
${DOCKER_PG_COMMAND} -c "ALTER SEQUENCE meshapi_install_install_number_seq RESTART WITH ${max_install_number}"
68-
69-
70-
71-

0 commit comments

Comments
 (0)