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

Revert "Upgrade to Ecto 3" #2001

Merged
merged 1 commit into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .iex.exs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ alias SanbaseWeb.Graphql.Resolvers.{
UserResolver,
ApikeyResolver,
EthAccountResolver,
ElasticsearchResolver,
EtherbiResolver,
FileResolver,
GithubResolver,
Expand Down
6 changes: 3 additions & 3 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ config :sanbase, ecto_repos: [Sanbase.Repo]

config :phoenix, :json_library, Jason

config :postgrex, :json_library, Jason
config :ecto, json_library: Jason

config :sanbase, Sanbase, environment: "#{Mix.env()}"

Expand Down Expand Up @@ -51,8 +51,7 @@ config :sanbase, Sanbase.Repo,
pool_size: {:system, "SANBASE_POOL_SIZE", "20"},
max_overflow: 5,
# because of pgbouncer
prepare: :unnamed,
migration_timestamps: [type: :naive_datetime_usec]
prepare: :unnamed

config :sanbase, Sanbase.Auth.Hmac, secret_key: {:system, "APIKEY_HMAC_SECRET_KEY", nil}

Expand Down Expand Up @@ -180,6 +179,7 @@ import_config "ex_admin_config.exs"
import_config "influxdb_config.exs"
import_config "scrapers_config.exs"
import_config "notifications_config.exs"
import_config "elasticsearch_config.exs"
import_config "prometheus_config.exs"
import_config "stripe_config.exs"
import_config "scheduler_config.exs"
Expand Down
3 changes: 1 addition & 2 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ config :sanbase, Sanbase.ClickhouseRepo,
password: "",
pool_timeout: 60_000,
timeout: 60_000,
pool_size: {:system, "CLICKHOUSE_POOL_SIZE", "3"},
show_sensitive_data_on_connection_error: true
pool_size: {:system, "CLICKHOUSE_POOL_SIZE", "3"}

config :ex_admin,
basic_auth: [
Expand Down
9 changes: 9 additions & 0 deletions config/elasticsearch_config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Config

config :sanbase, Sanbase.Elasticsearch.Cluster,
url: "http://managed-elasticsearch-scraping-data.default.svc.cluster.local",
api: Elasticsearch.API.HTTP,
json_library: Jason

config :sanbase, Sanbase.Elasticsearch,
indices: {:system, "ELASTICSEARCH_STATS_INDICES", "reddit"}
9 changes: 4 additions & 5 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ config :sanbase, Sanbase.Repo,
database: "sanbase_test",
pool_size: 5

config :sanbase, Sanbase.ClickhouseRepo,
pool: Ecto.Adapters.SQL.Sandbox,
database: "sanbase_test",
pool_size: 5

config :sanbase, Sanbase.Auth.Hmac, secret_key: "Non_empty_key_used_in_tests_only"

config :sanbase, Sanbase.ExternalServices.Coinmarketcap, sync_enabled: false
Expand Down Expand Up @@ -65,6 +60,10 @@ config :arc,
storage: Arc.Storage.Local,
storage_dir: "/tmp/sanbase/filestore-test/"

config :sanbase, Sanbase.Elasticsearch.Cluster, api: Sanbase.ElasticsearchMock

config :sanbase, Sanbase.Elasticsearch, indices: "index1,index2,index3,index4"

config :sanbase, SanbaseWeb.Plug.VerifyStripeWebhook, webhook_secret: "stripe_webhook_secret"

config :sanbase, Sanbase.Signal, email_channel_enabled: {:system, "EMAIL_CHANNEL_ENABLED", "true"}
Expand Down
4 changes: 3 additions & 1 deletion lib/sanbase/application.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Sanbase.Application do
use Application

import Sanbase.ApplicationUtils

require Logger
require Sanbase.Utils.Config, as: Config

Expand Down Expand Up @@ -165,7 +167,7 @@ defmodule Sanbase.Application do
Sanbase.Repo,

# Start the Clickhouse Repo
Sanbase.ClickhouseRepo,
start_in({Sanbase.ClickhouseRepo, []}, [:dev, :prod]),

# Time series Prices DB connection
Sanbase.Prices.Store.child_spec(),
Expand Down
47 changes: 13 additions & 34 deletions lib/sanbase/auth/user.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Sanbase.Auth.User do
use Ecto.Schema
use Timex.Ecto.Timestamps

import Ecto.Changeset
import Ecto.Query
Expand Down Expand Up @@ -99,17 +100,15 @@ defmodule Sanbase.Auth.User do
timestamps()
end

def generate_salt() do
def generate_salt do
:crypto.strong_rand_bytes(@salt_length) |> Base.url_encode64() |> binary_part(0, @salt_length)
end

def generate_email_token() do
def generate_email_token do
:crypto.strong_rand_bytes(@email_token_length) |> Base.url_encode64()
end

def changeset(%User{} = user, attrs \\ %{}) do
attrs = Sanbase.DateTimeUtils.truncate_datetimes(attrs)

user
|> cast(attrs, [
:email,
Expand Down Expand Up @@ -189,20 +188,15 @@ defmodule Sanbase.Auth.User do
def san_balance_cache_stale?(%User{san_balance_updated_at: nil}), do: true

def san_balance_cache_stale?(%User{san_balance_updated_at: san_balance_updated_at}) do
naive_now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
Timex.diff(naive_now, san_balance_updated_at, :seconds) > @san_balance_cache_seconds
Timex.diff(Timex.now(), san_balance_updated_at, :seconds) > @san_balance_cache_seconds
end

def update_san_balance_changeset(user) do
user = Repo.preload(user, :eth_accounts)
san_balance = san_balance_for_eth_accounts(user)
naive_now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)

user
|> change(
san_balance_updated_at: naive_now,
san_balance: san_balance
)
|> change(san_balance: san_balance, san_balance_updated_at: Timex.now())
end

@spec san_balance(%User{}) :: {:ok, float()} | {:ok, nil} | {:error, String.t()}
Expand Down Expand Up @@ -279,7 +273,7 @@ defmodule Sanbase.Auth.User do
user
|> change(
email_token: generate_email_token(),
email_token_generated_at: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second),
email_token_generated_at: Timex.now(),
email_token_validated_at: nil,
consent_id: consent
)
Expand All @@ -291,57 +285,44 @@ defmodule Sanbase.Auth.User do
|> changeset(%{
email_candidate: email_candidate,
email_candidate_token: generate_email_token(),
email_candidate_token_generated_at:
NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second),
email_candidate_token_generated_at: Timex.now(),
email_candidate_token_validated_at: nil
})
|> Repo.update()
end

def mark_email_token_as_validated(user) do
validated_at =
(user.email_token_validated_at || Timex.now())
|> Timex.to_naive_datetime()
|> NaiveDateTime.truncate(:second)

user
|> change(
email_token_validated_at: validated_at,
email_token_validated_at: user.email_token_validated_at || Timex.now(),
is_registered: true
)
|> Repo.update()
end

def update_email_from_email_candidate(user) do
validated_at =
(user.email_candidate_token_validated_at || Timex.now())
|> Timex.to_naive_datetime()
|> NaiveDateTime.truncate(:second)

user
|> changeset(%{
email: user.email_candidate,
email_candidate: nil,
email_candidate_token_validated_at: validated_at
email_candidate_token_validated_at: user.email_candidate_token_validated_at || Timex.now()
})
|> Repo.update()
end

def email_token_valid?(user, token) do
naive_now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)

cond do
user.email_token != token ->
false

Timex.diff(naive_now, user.email_token_generated_at, :minutes) >
Timex.diff(Timex.now(), user.email_token_generated_at, :minutes) >
@login_email_valid_minutes ->
false

user.email_token_validated_at == nil ->
true

Timex.diff(naive_now, user.email_token_validated_at, :minutes) >
Timex.diff(Timex.now(), user.email_token_validated_at, :minutes) >
@login_email_valid_after_validation_minutes ->
false

Expand All @@ -351,20 +332,18 @@ defmodule Sanbase.Auth.User do
end

def email_candidate_token_valid?(user, email_candidate_token) do
naive_now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)

cond do
user.email_candidate_token != email_candidate_token ->
false

Timex.diff(naive_now, user.email_candidate_token_generated_at, :minutes) >
Timex.diff(Timex.now(), user.email_candidate_token_generated_at, :minutes) >
@login_email_valid_minutes ->
false

user.email_candidate_token_validated_at == nil ->
true

Timex.diff(naive_now, user.email_candidate_token_validated_at, :minutes) >
Timex.diff(Timex.now(), user.email_candidate_token_validated_at, :minutes) >
@login_email_valid_after_validation_minutes ->
false

Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase/clickhouse/erc20_transfers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule Sanbase.Clickhouse.Erc20Transfers do
@table "erc20_transfers"

@primary_key false
@timestamps_opts [updated_at: false]
@timestamps_opts updated_at: false
schema @table do
field(:datetime, :utc_datetime, source: :dt)
field(:contract, :string, primary_key: true)
Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase/clickhouse/eth_transfers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule Sanbase.Clickhouse.EthTransfers do
@eth_decimals 1_000_000_000_000_000_000

@primary_key false
@timestamps_opts [updated_at: false]
@timestamps_opts updated_at: false
schema @table do
field(:datetime, :utc_datetime, source: :dt)
field(:from_address, :string, primary_key: true, source: :from)
Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase/clickhouse/github/github.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule Sanbase.Clickhouse.Github do
@table "github"

@primary_key false
@timestamps_opts [updated_at: false]
@timestamps_opts updated_at: false
schema @table do
field(:datetime, :utc_datetime, source: :dt, primary_key: true)
field(:repo, :string, primary_key: true)
Expand Down
Loading