-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
248 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule Migration_0_0_1 do | ||
@moduledoc "DB.transaction_exists? used to catch it in MigrateTest mock" | ||
|
||
alias Archethic.DB | ||
|
||
def pre_start() do | ||
DB.transaction_exists?("pre_start 0.0.1", :storage) | ||
end | ||
|
||
def pre_upgrade() do | ||
raise "error" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Migration_0_0_2 do | ||
@moduledoc "DB.transaction_exists? used to catch it in MigrateTest mock" | ||
|
||
alias Archethic.DB | ||
|
||
def pre_start() do | ||
DB.transaction_exists?("pre_start 0.0.2", :storage) | ||
end | ||
|
||
def pre_upgrade() do | ||
DB.transaction_exists?("pre_upgrade 0.0.2", :storage) | ||
end | ||
|
||
def post_upgrade() do | ||
DB.transaction_exists?("post_upgrade 0.0.2", :storage) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env bash | ||
|
||
release_remote_ctl eval --mfa "Mix.Tasks.Archethic.Migrate.run/1" --argv "post_upgrade" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/usr/bin/env bash | ||
|
||
release_remote_ctl eval --mfa "Mix.Tasks.Archethic.Migrate.run/1" --argv "$@" | ||
release_remote_ctl eval --mfa "Mix.Tasks.Archethic.Migrate.run/1" --argv "pre_start" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env bash | ||
|
||
release_remote_ctl eval --mfa "Mix.Tasks.Archethic.Migrate.run/1" --argv "pre_upgrade" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
defmodule Mix.Tasks.Archethic.MigrateTest do | ||
use ArchethicCase | ||
|
||
alias Archethic.Crypto | ||
alias Archethic.DB.EmbeddedImpl | ||
alias Archethic.DB.EmbeddedImpl.ChainWriter | ||
alias Archethic.P2P | ||
alias Archethic.P2P.Node | ||
|
||
alias Mix.Tasks.Archethic.Migrate | ||
|
||
import Mox | ||
|
||
describe "run/1" do | ||
setup do | ||
EmbeddedImpl.Supervisor.start_link() | ||
migration_path = EmbeddedImpl.db_path() |> ChainWriter.migration_file_path() | ||
|
||
P2P.add_and_connect_node(%Node{ | ||
ip: {127, 0, 0, 1}, | ||
port: 3001, | ||
first_public_key: Crypto.first_node_public_key(), | ||
last_public_key: Crypto.last_node_public_key(), | ||
available?: true, | ||
geo_patch: "AAA", | ||
network_patch: "AAA", | ||
authorized?: true, | ||
authorization_date: DateTime.utc_now() | ||
}) | ||
|
||
on_exit(fn -> Process.sleep(50) end) | ||
|
||
%{migration_path: migration_path} | ||
end | ||
|
||
test "pre_start should create migration file with current version", %{ | ||
migration_path: migration_path | ||
} do | ||
refute File.exists?(migration_path) | ||
|
||
Migrate.run([:pre_start, "0.0.1"]) | ||
|
||
assert File.exists?(migration_path) | ||
assert "0.0.1" = File.read!(migration_path) | ||
end | ||
|
||
test "pre_start should update version number even without migration", %{ | ||
migration_path: migration_path | ||
} do | ||
Migrate.run([:pre_start, "0.0.2"]) | ||
assert "0.0.2" = File.read!(migration_path) | ||
|
||
Migrate.run([:pre_start, "0.0.3"]) | ||
assert "0.0.3" = File.read!(migration_path) | ||
end | ||
|
||
test "pre_start should run all missed upgrade", %{ | ||
migration_path: migration_path | ||
} do | ||
File.write!(migration_path, "0.0.0") | ||
|
||
MockDB | ||
|> expect(:transaction_exists?, fn "pre_start 0.0.1", _ -> true end) | ||
|> expect(:transaction_exists?, fn "pre_start 0.0.2", _ -> true end) | ||
|
||
Migrate.run([:pre_start, "0.0.3"]) | ||
assert "0.0.3" = File.read!(migration_path) | ||
end | ||
|
||
test "pre_start should not run migration already done", %{ | ||
migration_path: migration_path | ||
} do | ||
File.write!(migration_path, "0.0.1") | ||
|
||
me = self() | ||
|
||
MockDB | ||
|> stub(:transaction_exists?, fn version, _ -> send(me, version) end) | ||
|
||
Migrate.run([:pre_start, "0.0.2"]) | ||
|
||
refute_receive "pre_start 0.0.1" | ||
assert_receive "pre_start 0.0.2" | ||
end | ||
|
||
test "post_upgrade should run migration of the current_version if not already done", %{ | ||
migration_path: migration_path | ||
} do | ||
File.write!(migration_path, "0.0.2") | ||
|
||
MockDB | ||
|> expect(:transaction_exists?, fn "post_upgrade 0.0.2", _ -> true end) | ||
|
||
Migrate.run([:post_upgrade, "0.0.2"]) | ||
|
||
assert "0.0.2" = File.read!(migration_path) | ||
end | ||
|
||
test "post_upgrade should not try to run function if module does not implement it", %{ | ||
migration_path: migration_path | ||
} do | ||
File.write!(migration_path, "0.0.1") | ||
Migrate.run([:post_upgrade, "0.0.1"]) | ||
# No assert, the test should just not crash | ||
end | ||
|
||
test "pre_upgrade and post_upgrade should be started", %{ | ||
migration_path: migration_path | ||
} do | ||
File.write!(migration_path, "0.0.1") | ||
|
||
MockDB | ||
|> expect(:transaction_exists?, fn "pre_upgrade 0.0.2", _ -> true end) | ||
|> expect(:transaction_exists?, fn "post_upgrade 0.0.2", _ -> true end) | ||
|
||
Migrate.run([:pre_upgrade, "0.0.1"]) | ||
Migrate.run([:post_upgrade, "0.0.2"]) | ||
|
||
assert "0.0.2" = File.read!(migration_path) | ||
end | ||
end | ||
end |