Current section
Files
Jump to
Current section
Files
src/galchemy@examples@schema_tooling.erl
-module(galchemy@examples@schema_tooling).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src\\galchemy\\examples\\schema_tooling.gleam").
-export([current_schema/0, target_schema/0, main/0]).
-file("src\\galchemy\\examples\\schema_tooling.gleam", 78).
-spec column(
binary(),
galchemy@schema@model:column_type(),
boolean(),
gleam@option:option(binary()),
integer()
) -> galchemy@schema@model:column_schema().
column(Name, Data_type, Nullable, Default, Ordinal_position) ->
{column_schema, Name, Data_type, Nullable, Default, Ordinal_position}.
-file("src\\galchemy\\examples\\schema_tooling.gleam", 9).
-spec current_schema() -> galchemy@schema@model:schema_snapshot().
current_schema() ->
{schema_snapshot,
[{table_schema,
<<"public"/utf8>>,
<<"users"/utf8>>,
[column(<<"id"/utf8>>, integer_type, false, none, 1),
column(<<"name"/utf8>>, text_type, false, none, 2)],
{some, {primary_key, <<"users_pkey"/utf8>>, [<<"id"/utf8>>]}},
[],
[],
[]}]}.
-file("src\\galchemy\\examples\\schema_tooling.gleam", 28).
-spec target_schema() -> galchemy@schema@model:schema_snapshot().
target_schema() ->
{schema_snapshot,
[{table_schema,
<<"public"/utf8>>,
<<"users"/utf8>>,
[column(<<"id"/utf8>>, integer_type, false, none, 1),
column(<<"name"/utf8>>, text_type, false, none, 2),
column(<<"email"/utf8>>, text_type, false, none, 3)],
{some, {primary_key, <<"users_pkey"/utf8>>, [<<"id"/utf8>>]}},
[{unique_constraint,
<<"users_email_key"/utf8>>,
[<<"email"/utf8>>]}],
[],
[]}]}.
-file("src\\galchemy\\examples\\schema_tooling.gleam", 50).
-spec main() -> nil.
main() ->
Operations = galchemy@schema@diff:diff(current_schema(), target_schema()),
Modules = galchemy@schema@generator@gleam:generate(
target_schema(),
galchemy@schema@generator@gleam:default_options(
<<"app/generated"/utf8>>
)
),
gleam_stdlib:println(<<"Schema tooling example"/utf8>>),
gleam_stdlib:println(<<"Diff operations:"/utf8>>),
gleam_stdlib:println(gleam@string:inspect(Operations)),
gleam_stdlib:println(<<"Generated modules:"/utf8>>),
gleam_stdlib:println(gleam@string:inspect(Modules)),
case galchemy@schema@migration@postgres:plan(
<<"20260329_add_user_email"/utf8>>,
current_schema(),
target_schema()
) of
{ok, Plan} ->
gleam_stdlib:println(<<"Migration plan:"/utf8>>),
gleam_stdlib:println(gleam@string:inspect(Plan));
{error, Error} ->
gleam_stdlib:println(<<"Migration plan error:"/utf8>>),
gleam_stdlib:println(gleam@string:inspect(Error))
end.