hamiltop / rethinkdb_ecto

Shim library for using Ecto with RethinkDB. Not a full adapter.
24 stars 4 forks source link

Migrations #13

Closed katafrakt closed 5 years ago

katafrakt commented 8 years ago

Of course, in schemaless databases we generally don't need migrations. However, with databases like RethinkDB there are two exceptions: tables and indices. In a project I'm currently developing this is what I came up with:

defmodule Mix.Tasks.Rdb.Load do
  use Mix.Task
  import RethinkDB.Query
  alias RethinkDatabase, as: RDB

  def run(_args) do
    create_tables
  end

  def create_tables do
    RDB.start_link(Application.get_env(:myapp, :rdb))
    tables = Application.get_env(:rethinkdb_ecto, :tables)
    Enum.each(tables, fn(table_name) ->
      query = table_list
        |> contains(table_name)
        |> branch("", do_r(fn -> table_create(table_name) end))
      case RDB.run(query) do
        %RethinkDB.Record{data: %{"tables_created" => 1}} ->
          IO.puts "Created table `#{table_name}`."
        %RethinkDB.Record{data: ""} ->
          IO.puts "Table `#{table_name}` already exists."
      end
    end)
  end
end

This handles tables only, but I suspect indices would be similar (althought a little bit more complex.

My questions are:

hamiltop commented 8 years ago

I'd be curious to see if https://github.com/almightycouch/rethinkdb_ecto supports migrations.

I built this package as a proof of concept, and don't (yet) have plans to support this as a first class citizen. If the other project supports it then perhaps that's a better direction to go. Care to give it a shot and report back?

katafrakt commented 8 years ago

The other repo does not have migrations (yet), but it's built in a way that it could actually support standard ecto syntax for migrations (mix ecto.migrate).

hamiltop commented 8 years ago

I've got a branch here, next which supports standard ecto migrations. It's a proof of concept and I don't have plans for maintenance, so use at your own risk.

On Thu, Apr 14, 2016 at 8:19 AM Paweł Świątkowski notifications@github.com wrote:

The other repo does not have migrations (yet), but it's built in a way that it could actually support standard ecto syntax for migrations (mix ecto.migrate).

— You are receiving this because you commented.

Reply to this email directly or view it on GitHub https://github.com/hamiltop/rethinkdb_ecto/issues/13#issuecomment-209998749

katafrakt commented 8 years ago

It looks pretty good :+1: If you don't plan to maintain it, would you mind if I ported this into the other repo? I guess it could use that code.

hamiltop commented 8 years ago

Go for it!

On Fri, Apr 15, 2016 at 1:50 PM Paweł Świątkowski notifications@github.com wrote:

It looks pretty good [image: :+1:] If you don't plan to maintain it, would you mind if I ported this into the other repo? I guess it could use that code.

— You are receiving this because you commented.

Reply to this email directly or view it on GitHub https://github.com/hamiltop/rethinkdb_ecto/issues/13#issuecomment-210641574