Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid snapshots state #103

Closed
smt116 opened this issue Jul 5, 2022 · 5 comments
Closed

Invalid snapshots state #103

smt116 opened this issue Jul 5, 2022 · 5 comments
Labels
bug Something isn't working needs review

Comments

@smt116
Copy link

smt116 commented Jul 5, 2022

Describe the bug

After upgrading Ash from 1.52.0-rc.11 to 15cd3fb7bcc6968ff1ae40a7589b068ea686e328:

diff --git a/elixir/mix.lock b/elixir/mix.lock
index 83259136..588e4a2c 100644
--- a/elixir/mix.lock
+++ b/elixir/mix.lock
@@ -5,7 +5,7 @@
   "appsignal": {:hex, :appsignal, "2.2.14", "f2747cb051eb9e93c02896ce19f95ebbdf1b25c3facafeb8e81d8b0f44b49289", [:make, :mix], [{:decorator, "~> 1.2.3 or ~> 1.3", [hex: :decorator, repo: "hexpm", optional: false]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:poison, ">= 1.3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f25a293e063a4213d9314eb15f2a31a95767d094836c51b73febd816e762b058"},
   "appsignal_phoenix": {:hex, :appsignal_phoenix, "2.0.14", "c2e7ee802a5ee037d399ec9cbe3c3cf12f8cf0dfcc2bbfa595214d14a3ff99ea", [:mix], [{:appsignal_plug, ">= 2.0.
9 and < 3.0.0", [hex: :appsignal_plug, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.11 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_live_view, "~> 0.9", [hex: :phoenix_live_view, repo: "hexpm", optional: true]}], "hexpm", "7e987756dc15f0b86b5c10671c9706db98663c0ec4a1448e58eaf01acd5bbeec"},
   "appsignal_plug": {:hex, :appsignal_plug, "2.0.11", "96ab6656a6a2450e7a32c2686bb4f8955d4b25124c0f867293bfa0bdfd6c1c34", [:mix], [{:appsignal, ">= 2.2.13 and < 3.0.0", [hex: :appsignal, repo: "hexpm", optional: false]}, {:plug, ">= 1.1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "b16cffc492f276e883f7e5430f28a55a49c50dcd1e7f09a770eb4ae26a3da717"},
-  "ash": {:hex, :ash, "1.52.0-rc.11", "3bfd3a883b4deaf7e05736d43a347cfcf937f679064c05ab9f6f4b62e5c5738c", [:mix], [{:comparable, "~> 1.0", [hex: :comparable, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: true]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8.0", [hex: :ets, repo: "hexpm", optional: false]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.3.5", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: false]}, {:sourceror, "~> 0.9", [hex: :sourceror, repo: "hexpm", optional: false]}, {:timex, ">= 3.0.0", [hex: :timex, repo: "hexpm", optional: false]}], "hexpm", "1f674fe260d2c415ee9a3fd1202fa82562253a95539d19a2e102179aabf49a7d"},
+  "ash": {:git, "https://github.com/ash-project/ash.git", "15cd3fb7bcc6968ff1ae40a7589b068ea686e328", []},
   "ash_admin": {:hex, :ash_admin, "0.4.5-rc.0", "ca955436208301ab18b934859f1c845090af491134bcdc968cd68a1c54217624", [:mix], [{:ash, "~> 1.52.0-rc.5", [hex: :ash, repo: "hexpm", optional: false]}, {:ash_phoenix, "~> 0.7.2-rc.1", [hex: :ash_phoenix, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 3.2", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.17", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:surface, "~> 0.7", [hex: :surface, repo: "hexpm", optional: false]}], "hexpm", "fa1c6f5b67cf8de3d4d3824ec0746e78ac6b6aa6fd323fd7e3aee105c471260e"},
   "ash_graphql": {:git, "https://github.com/ash-project/ash_graphql.git", "9bb12460d4408f67d2460b64f4196b698e8c60f7", []},
   "ash_json_api_wrapper": {:git, "https://github.com/ash-project/ash_json_api_wrapper.git", "f42756c3426d9746acf71a53e05094f23d05240e", []},

The migration generator breaks the snapshots of already migrated resources (see the output in "expected behavior").

To Reproduce
Not sure.

Expected behavior
It should not (1) try to rename the timestamps, and definitely (2) should not do that if I type n while generating the migration (note the source: null in the snapshot):

elixir λ cat priv/repo/migrations/20220705093537_migrate_resources23.exs
defmodule [filtered].Repo.Migrations.MigrateResources23 do
  @moduledoc """
  Updates resources based on their most recent snapshots.

  This file was autogenerated with `mix ash_postgres.generate_migrations`
  """

  use Ecto.Migration

  def up do
    alter table(:users) do
      # Attribute removal has been commented out to avoid data loss. See the migration generator documentation for more
      # If you uncomment this, be sure to also uncomment the corresponding attribute *addition* in the `down` migration
      # remove :totp_verified

      add :totp_enabled, :boolean, null: false, default: false
      add :totp_secret, :text
    end

    alter table(:user_referral_rewards) do
      # Attribute removal has been commented out to avoid data loss. See the migration generator documentation for more
      # If you uncomment this, be sure to also uncomment the corresponding attribute *addition* in the `down` migration
      # remove :updated_at

      # Attribute removal has been commented out to avoid data loss. See the migration generator documentation for more
      # If you uncomment this, be sure to also uncomment the corresponding attribute *addition* in the `down` migration
      # remove :inserted_at

      add nil, :utc_datetime_usec, null: false, default: fragment("now()")
    end
  end

  def down do
    alter table(:user_referral_rewards) do
      remove nil
      # This is the `down` migration of the statement:
      #
      #     remove :inserted_at
      #

      # add :inserted_at, :utc_datetime_usec, null: false, default: fragment("now()")
      # This is the `down` migration of the statement:
      #
      #     remove :updated_at
      #

      # add :updated_at, :utc_datetime_usec, null: false, default: fragment("now()")
    end

    alter table(:users) do
      remove :totp_secret
      remove :totp_enabled
      # This is the `down` migration of the statement:
      #
      #     remove :totp_verified
      #

      # add :totp_verified, :boolean, null: false, default: false
    end
  end
end⏎

elixir λ g
## sandbox...origin/sandbox
?? priv/repo/migrations/20220705093537_migrate_resources23.exs
?? priv/resource_snapshots/repo/user_referral_rewards/20220705093537.json
?? priv/resource_snapshots/repo/users/20220705093537.json

elixir λ cat priv/resource_snapshots/repo/user_referral_rewards/20220705093537.json
{
  "attributes": [
    {
      "allow_nil?": false,
      "default": "fragment(\"now()\")",
      "generated?": false,
      "primary_key?": false,
      "references": null,
      "size": null,
      "source": null,
      "type": "utc_datetime_usec"
    },
    {
      "allow_nil?": false,
      "default": "fragment(\"uuid_generate_v4()\")",
      "generated?": false,
      "primary_key?": true,
      "references": null,
      "size": null,
      "source": "id",
      "type": "uuid"
    },
    {
      "allow_nil?": false,
      "default": "false",
      "generated?": false,
      "primary_key?": false,
      "references": null,
      "size": null,
      "source": "fulfilled",
      "type": "boolean"
    },
    {
      "allow_nil?": false,
      "default": "nil",
      "generated?": false,
      "primary_key?": false,
      "references": {
        "destination_field": "id",
        "destination_field_default": null,
        "destination_field_generated": null,
        "multitenancy": {
          "attribute": null,
          "global": null,
          "strategy": null
        },
        "name": "user_referral_rewards_referred_id_fkey",
        "on_delete": null,
        "on_update": null,
        "schema": "public",
        "table": "users"
      },
      "size": null,
      "source": "referred_id",
      "type": "text"
    },
    {
      "allow_nil?": false,
      "default": "nil",
      "generated?": false,
      "primary_key?": false,
      "references": {
        "destination_field": "id",
        "destination_field_default": null,
        "destination_field_generated": null,
        "multitenancy": {
          "attribute": null,
          "global": null,
          "strategy": null
        },
        "name": "user_referral_rewards_referrer_id_fkey",
        "on_delete": null,
        "on_update": null,
        "schema": "public",
        "table": "users"
      },
      "size": null,
      "source": "referrer_id",
      "type": "text"
    }
  ],
  "base_filter": null,
  "check_constraints": [],
  "custom_indexes": [],
  "has_create_action": true,
  "hash": "CF755B6DFB9427D6B62485F8B1D70304D8B554FE6D5E70776FC5B122F61F3B56",
  "identities": [
    {
      "base_filter": null,
      "index_name": "user_referral_rewards_referred_index",
      "keys": [
        "referred_id",
        "referrer_id"
      ],
      "name": "referred"
    }
  ],
  "multitenancy": {
    "attribute": null,
    "global": null,
    "strategy": null
  },
  "repo": "[filtered]",
  "schema": null,
  "table": "user_referral_rewards"
}

** Runtime

  • Elixir version 1.13.4
  • Erlang version 24.3.4
  "ash": {:git, "https://github.com/ash-project/ash.git", "15cd3fb7bcc6968ff1ae40a7589b068ea686e328", []},
  "ash_admin": {:hex, :ash_admin, "0.4.5-rc.0", "ca955436208301ab18b934859f1c845090af491134bcdc968cd68a1c54217624", [:mix], [{:ash, "~> 1.52.0-rc.5", [hex: :ash, repo: "hexpm", optional: false]}, {:ash_phoenix, "~> 0.7.2-rc.1", [hex: :ash_phoenix, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 3.2", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.17", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:surface, "~> 0.7", [hex: :surface, repo: "hexpm", optional: false]}], "hexpm", "fa1c6f5b67cf8de3d4d3824ec0746e78ac6b6aa6fd323fd7e3aee105c471260e"},
  "ash_graphql": {:git, "https://github.com/ash-project/ash_graphql.git", "9bb12460d4408f67d2460b64f4196b698e8c60f7", []},
  "ash_json_api_wrapper": {:git, "https://github.com/ash-project/ash_json_api_wrapper.git", "f42756c3426d9746acf71a53e05094f23d05240e", []},
  "ash_phoenix": {:hex, :ash_phoenix, "0.7.2-rc.1", "90572ecaeb5a957e8eb444d258c5903693fab867258fc265ed3834522937de61", [:mix], [{:ash, "~> 1.52.0-rc.2", [hex: :ash, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.5.6 or ~> 1.6.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.15", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}], "hexpm", "dd8b44ccd9bfe001336d3f9cd83571fe606afb9322d5e43c60ee797f1050a49b"},
  "ash_postgres": {:git, "https://github.com/ash-project/ash_postgres.git", "5d435d92b664aae9ae868a69907d8c1793f82a4a", []},

Additional context
ash-project/ash#344
#102

@smt116 smt116 added bug Something isn't working needs review labels Jul 5, 2022
@smt116
Copy link
Author

smt116 commented Jul 5, 2022

Ah.. it looks like the snapshots are broken because someone else introduced a change on the same resource (without a git conflict). This mechanism is a pain because it is pretty hard to fix the state later :/

@smt116 smt116 closed this as completed Jul 5, 2022
@smt116
Copy link
Author

smt116 commented Jul 5, 2022

Why do we even need the snapshots? Can't we rely on migrations only?

@smt116
Copy link
Author

smt116 commented Jul 5, 2022

I had to apply this:

diff --git a/elixir/lib/[filtered]/resources/user/referral_reward.ex b/elixir/lib/[filtered]/resources/user/referral_reward.ex
index 0b5adff8..dacb0294 100644
--- a/elixir/lib/[filtered]/resources/user/referral_reward.ex
+++ b/elixir/lib/[filtered]/resources/user/referral_reward.ex
@@ -33,7 +33,8 @@ defmodule [filtered].Resources.User.ReferralReward do

     attribute :fulfilled, :boolean, allow_nil?: false, default: false

-    timestamps()
+    create_timestamp :inserted_at
+    update_timestamp :updated_at
   end

   aggregates do

and regenerate snapshots manually to fix the state.

@zachdaniel
Copy link
Contributor

Those should ultimately be the same thing, likely just regenerating fixed it.

@zachdaniel
Copy link
Contributor

#75 this issue should resolve the issues around conflicts when using the migration generator in parallel with others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs review
Projects
None yet
Development

No branches or pull requests

2 participants