@@ -8,19 +8,20 @@ defmodule DatabaseMigration do
88 the result in the same COPY format.
99
1010 Functionality:
11- 1. Reads a PostgreSQL dump file containing COPY statements and their associated data.
12- 2. Processes each COPY section (extract, transform, load).
13- 3. Applies transformations based on table names.
14- 4. Outputs the transformed data in COPY format.
15- 5. Discards COPY sections for tables not in the allowed list.
11+ 1. Dumps the PostgreSQL database from MIGRATION_URL environment variable
12+ 2. Reads a PostgreSQL dump file containing COPY statements and their associated data.
13+ 3. Processes each COPY section (extract, transform, load).
14+ 4. Applies transformations based on table names.
15+ 5. Outputs the transformed data in COPY format.
16+ 6. Discards COPY sections for tables not in the allowed list.
1617
1718 Usage:
18- - Set the input_file to your PostgreSQL dump file path.
19- - Set the output_file to your desired output file path.
19+ - Set the MIGRATION_URL environment variable to the source database URL
2020 - Run the script using: elixir scripts/database_migration.exs
2121 """
2222 alias Algora.Accounts.Identity
2323 alias Algora.Accounts.User
24+ alias Algora.Admin
2425 alias Algora.Bounties.Attempt
2526 alias Algora.Bounties.Bounty
2627 alias Algora.Bounties.Claim
@@ -1339,26 +1340,52 @@ defmodule DatabaseMigration do
13391340 result
13401341 end
13411342
1342- def run! do
1343- input_file = ".local/db/v1-data-2025-02-13.sql"
1344- output_file = ".local/db/v2-data-2025-02-13.sql"
1343+ defp dump_database! ( output_path ) do
1344+ { output , exit_code } =
1345+ System . cmd (
1346+ "pg_dump" ,
1347+ [
1348+ System . fetch_env! ( "MIGRATION_URL" ) ,
1349+ "-N" ,
1350+ "supabase_functions" ,
1351+ "-a" ,
1352+ "-f" ,
1353+ output_path
1354+ ] ,
1355+ stderr_to_stdout: true
1356+ )
1357+
1358+ if exit_code != 0 do
1359+ Logger . error ( output )
1360+ raise "Failed to dump database with exit code: #{ exit_code } "
1361+ end
1362+
1363+ :ok
1364+ end
13451365
1366+ def run! do
13461367 System . put_env ( "MIGRATION" , "true" )
13471368
1348- if File . exists? ( input_file ) or File . exists? ( output_file ) do
1349- IO . puts ( "⏳ Starting migration..." )
1369+ pwd = ".local/db"
1370+ File . mkdir_p! ( pwd )
13501371
1351- { total_time , _ } =
1352- :timer . tc ( fn ->
1353- :ok = time_step ( "Processing dump" , fn -> process_dump ( input_file , output_file ) end )
1354- :ok = time_step ( "Clearing tables" , fn -> clear_tables! ( ) end )
1355- { :ok , _ } = time_step ( "Importing new data" , fn -> psql ( [ "-f" , output_file ] ) end )
1356- :ok = time_step ( "Backfilling repositories" , fn -> Algora.Admin . backfill_repos! ( ) end )
1357- :ok = time_step ( "Backfilling claims" , fn -> Algora.Admin . backfill_claims! ( ) end )
1358- end )
1372+ timestamp = Calendar . strftime ( DateTime . utc_now ( ) , "%Y-%m-%d-%H-%M-%S" )
1373+ input_path = Path . join ( pwd , "v1-data-#{ timestamp } .sql" )
1374+ output_path = Path . join ( pwd , "v2-data-#{ timestamp } .sql" )
13591375
1360- IO . puts ( "✅ Migration completed successfully in #{ total_time / 1_000_000 } seconds" )
1361- end
1376+ IO . puts ( "⏳ Starting migration..." )
1377+
1378+ { total_time , _ } =
1379+ :timer . tc ( fn ->
1380+ :ok = time_step ( "Dumping database" , fn -> dump_database! ( input_path ) end )
1381+ :ok = time_step ( "Processing dump" , fn -> process_dump ( input_path , output_path ) end )
1382+ :ok = time_step ( "Clearing tables" , fn -> clear_tables! ( ) end )
1383+ { :ok , _ } = time_step ( "Importing new data" , fn -> psql ( [ "-f" , output_path ] ) end )
1384+ :ok = time_step ( "Backfilling repositories" , fn -> Admin . backfill_repos! ( ) end )
1385+ :ok = time_step ( "Backfilling claims" , fn -> Admin . backfill_claims! ( ) end )
1386+ end )
1387+
1388+ IO . puts ( "✅ Migration completed successfully in #{ total_time / 1_000_000 } seconds" )
13621389 end
13631390end
13641391
0 commit comments