Packages
riptide
0.5.0-beta7
0.5.2
0.5.1
0.5.0-beta9
0.5.0-beta8
0.5.0-beta7
0.5.0-beta6
0.5.0-beta5
0.5.0-beta4
0.5.0-beta3
0.5.0-beta2
0.5.0-beta11
0.5.0-beta10
0.5.0-beta
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.13
0.3.12
0.3.11
0.3.10
0.3.9
0.3.8
0.3.7
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.3.0-bd63a38
0.2.79
0.2.78
0.2.74
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.15
0.1.14
0.1.13
0.1.12
0.1.11
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
A data first framework for building realtime applications
Current section
Files
Jump to
Current section
Files
lib/riptide/store/migration.ex
defmodule Riptide.Migration do
@moduledoc false
require Logger
@callback run() :: Riptide.Mutation.t() | Enum.t(Riptide.Mutation.t())
@root "riptide:migrations"
def all() do
for {module, _} <- :code.all_loaded(),
__MODULE__ in (module.module_info(:attributes)[:behaviour] || []) do
module
end
|> Enum.sort()
end
def run() do
now = :os.system_time(:millisecond)
[@root]
|> Riptide.query_path!()
|> case do
nil ->
Logger.info("Migrations never run. Skipping all.")
Riptide.merge!(
[@root],
all()
|> Stream.map(fn mod -> {Atom.to_string(mod), now} end)
|> Enum.into(%{})
)
migrations ->
all()
|> Stream.filter(fn mod -> Map.has_key?(migrations, Atom.to_string(mod)) == false end)
|> Enum.each(fn mod ->
Logger.info("Migration #{mod} running...")
record = Riptide.Mutation.put_merge([@root, Atom.to_string(mod)], now)
case mod.run() do
mut = %{merge: _, delete: _} -> Stream.concat([mut], [record])
result -> Stream.concat(result, [record])
end
|> Riptide.Mutation.chunk(1000)
|> Enum.each(fn mut ->
:ok = Riptide.Store.mutation(mut)
end)
Logger.info("Migration #{mod} completed successfully!")
end)
end
:ok
end
end
defmodule Riptide.Migration.Initial do
@moduledoc false
@behaviour Riptide.Migration
def run(), do: Riptide.Mutation.new()
end