Remove dependency on pgcrypto
authorMagnus Hagander <magnus@hagander.net>
Mon, 19 Aug 2024 11:15:05 +0000 (13:15 +0200)
committerMagnus Hagander <magnus@hagander.net>
Mon, 19 Aug 2024 11:15:05 +0000 (13:15 +0200)
pgcrypto was only used to set tokens in some migrations,
so in practice it should not actually be UPDATEing any
rows anymore (there should either be no rows, or they're
all updated by now). Once the migration has completed,
all new tokens are set from the python code.

So update the migrations to use the build-in random() function instead,
which is "good enough for this usage", and thereby dropping the
dependency on pgcrypto making new installs easier.

postgresqleu/confreg/migrations/0012_mandatory_tokens.py
postgresqleu/confreg/migrations/0039_tickets.py
tools/devsetup/dev_setup.sh

index ce3f885808444b25baa76042347894dda4c80cab..9ec95ce8bdf2a010762741add20c20e704482fc0 100644 (file)
@@ -12,7 +12,12 @@ class Migration(migrations.Migration):
 
     operations = [
         migrations.RunSQL(
-            "UPDATE confreg_conferenceregistration SET regtoken=encode(pgcrypto.digest(pgcrypto.gen_random_bytes(250), 'sha256'), 'hex') WHERE regtoken IS NULL"
+            """WITH t AS (
+  SELECT r.id, string_agg(lpad(to_hex((random() * 255)::int)::text, 2, '0'),'') AS rnd
+  FROM confreg_conferenceregistration r CROSS JOIN generate_series(1,32) g(g)
+  WHERE r.regtoken IS NULL GROUP BY r.id
+)
+UPDATE confreg_conferenceregistration rr SET regtoken=rnd FROM t WHERE t.id=rr.id""",
         ),
         migrations.AlterField(
             model_name='conferenceregistration',
@@ -20,7 +25,12 @@ class Migration(migrations.Migration):
             field=models.TextField(unique=True),
         ),
         migrations.RunSQL(
-            "UPDATE confreg_speaker SET speakertoken=encode(pgcrypto.digest(pgcrypto.gen_random_bytes(250), 'sha256'), 'hex') WHERE speakertoken IS NULL"
+            """WITH t AS (
+  SELECT s.id, string_agg(lpad(to_hex((random() * 255)::int)::text, 2, '0'),'') AS rnd
+  FROM confreg_speaker s CROSS JOIN generate_series(1,32) g(g)
+  WHERE s.speakertoken IS NULL GROUP BY s.id
+)
+UPDATE confreg_speaker ss SET speakertoken=rnd FROM t WHERE t.id=ss.id""",
         ),
         migrations.AlterField(
             model_name='speaker',
index 1ea1b85dffcfa5a97c6c4d9553200c0c8a7918b4..e772c2e8956cec77b83f9c3d5931c21bbf8d7615 100644 (file)
@@ -55,10 +55,20 @@ class Migration(migrations.Migration):
             field=models.TextField(blank=True, null=True, unique=True),
         ),
         migrations.RunSQL(
-            "UPDATE confreg_conferenceregistration SET idtoken=encode(pgcrypto.digest(pgcrypto.gen_random_bytes(250), 'sha256'), 'hex') WHERE idtoken IS NULL"
+            """WITH t AS (
+  SELECT r.id, string_agg(lpad(to_hex((random() * 255)::int)::text, 2, '0'),'') AS rnd
+  FROM confreg_conferenceregistration r CROSS JOIN generate_series(1,32) g(g)
+  WHERE r.idtoken IS NULL GROUP BY r.id
+)
+UPDATE confreg_conferenceregistration rr SET idtoken=rnd FROM t WHERE t.id=rr.id""",
         ),
         migrations.RunSQL(
-            "UPDATE confreg_conferenceregistration SET publictoken=encode(pgcrypto.digest(pgcrypto.gen_random_bytes(250), 'sha256'), 'hex') WHERE publictoken IS NULL"
+            """WITH t AS (
+  SELECT r.id, string_agg(lpad(to_hex((random() * 255)::int)::text, 2, '0'),'') AS rnd
+  FROM confreg_conferenceregistration r CROSS JOIN generate_series(1,32) g(g)
+  WHERE r.publictoken IS NULL GROUP BY r.id
+)
+UPDATE confreg_conferenceregistration rr SET publictoken=rnd FROM t WHERE t.id=rr.id""",
         ),
         migrations.AlterField(
             model_name='conferenceregistration',
index 125f5af5f80f92a38fcef3859e1f091f4ec5a2d5..de104b95f35e11e5a589425fbf2fb4c2983b0d0f 100755 (executable)
@@ -20,9 +20,6 @@ echo "                 User: $PGU"
 echo "Verifying postgres connection..."
 psql -w "host=$PGH port=$PGP dbname=$PGD user=$PGU" -c "SELECT 1" >/dev/null
 
-echo "Verifying that pgcrypto is installed in the pgcrypto schema..."
-psql -w "host=$PGH port=$PGP dbname=$PGD user=$PGU" -c "CREATE SCHEMA IF NOT EXISTS pgcrypto; CREATE EXTENSION IF NOT EXISTS pgcrypto SCHEMA pgcrypto; SELECT pgcrypto.gen_random_uuid()"
-
 # Start from script directory to be safe!
 cd "${0%/*}"