Skip to content
Closed
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
31 changes: 20 additions & 11 deletions lib/migration_generator/operation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ defmodule AshPostgres.MigrationGenerator.Operation do
end

def on_update(_), do: nil

def integer_to_bigint(:integer), do: ":bigint"
def integer_to_bigint(type), do: inspect(type)
end

defmodule CreateTable do
Expand Down Expand Up @@ -68,7 +71,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
"add #{inspect(attribute.name)}",
"references(:#{table}",
[
"type: #{inspect(attribute.type)}",
"type: #{integer_to_bigint(attribute.type)}",
"column: #{inspect(destination_field)}",
"with: [#{source_attribute}: :#{destination_attribute}]",
"name: #{inspect(reference.name)}",
Expand Down Expand Up @@ -98,7 +101,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
"add #{inspect(attribute.name)}",
"references(:#{table}",
[
"type: #{inspect(attribute.type)}",
"type: #{integer_to_bigint(attribute.type)}",
"column: #{inspect(destination_field)}",
"prefix: \"public\"",
"name: #{inspect(reference.name)}",
Expand Down Expand Up @@ -140,7 +143,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
"add #{inspect(attribute.name)}",
"references(:#{table}",
[
"type: #{inspect(attribute.type)}",
"type: #{integer_to_bigint(attribute.type)}",
"column: #{inspect(destination_field)}",
"name: #{inspect(reference.name)}",
on_delete(reference),
Expand All @@ -162,7 +165,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
"add #{inspect(attribute.name)}",
"references(:#{table}",
[
"type: #{inspect(attribute.type)}",
"type: #{integer_to_bigint(attribute.type)}",
"column: #{inspect(destination_field)}",
"name: #{inspect(reference.name)}",
on_delete(reference),
Expand All @@ -178,7 +181,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
def up(%{attribute: %{type: :integer, default: "nil", generated?: true} = attribute}) do
[
"add #{inspect(attribute.name)}",
":serial",
":bigserial",
maybe_add_null(attribute.allow_nil?),
maybe_add_primary_key(attribute.primary_key?)
]
Expand Down Expand Up @@ -269,7 +272,9 @@ defmodule AshPostgres.MigrationGenerator.Operation do
} = reference
}) do
join([
"references(:#{table}, type: #{inspect(type)}, column: #{inspect(destination_field)}",
"references(:#{table}, type: #{integer_to_bigint(type)}, column: #{
inspect(destination_field)
}",
"name: #{inspect(reference.name)}",
on_delete(reference),
on_update(reference),
Expand All @@ -287,9 +292,9 @@ defmodule AshPostgres.MigrationGenerator.Operation do
} = reference
}) do
join([
"references(:#{table}, type: #{inspect(type)}, column: #{inspect(destination_field)}, with: [#{
source_attribute
}: :#{destination_attribute}]",
"references(:#{table}, type: #{integer_to_bigint(type)}, column: #{
inspect(destination_field)
}, with: [#{source_attribute}: :#{destination_attribute}]",
"name: #{inspect(reference.name)}",
on_delete(reference),
on_update(reference),
Expand All @@ -309,7 +314,9 @@ defmodule AshPostgres.MigrationGenerator.Operation do
}
) do
join([
"references(:#{table}, type: #{inspect(type)}, column: #{inspect(destination_field)}, prefix: \"public\"",
"references(:#{table}, type: #{integer_to_bigint(type)}, column: #{
inspect(destination_field)
}, prefix: \"public\"",
"name: #{inspect(reference.name)}",
on_delete(reference),
on_update(reference),
Expand All @@ -329,7 +336,9 @@ defmodule AshPostgres.MigrationGenerator.Operation do
}
) do
join([
"references(:#{table}, type: #{inspect(type)}, column: #{inspect(destination_field)}",
"references(:#{table}, type: #{integer_to_bigint(type)}, column: #{
inspect(destination_field)
}",
"name: #{inspect(reference.name)}",
on_delete(reference),
on_update(reference),
Expand Down
4 changes: 2 additions & 2 deletions test/migration_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,11 @@ defmodule AshPostgres.MigrationGeneratorTest do
:ok
end

test "when an integer is generated and default nil, it is a serial" do
test "when an integer is generated and default nil, it is a bigserial" do
assert [file] = Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")

assert File.read!(file) =~
~S[add :id, :serial, null: false, primary_key: true]
~S[add :id, :bigserial, null: false, primary_key: true]

assert File.read!(file) =~
~S[add :views, :integer]
Expand Down