-module(parrot@internal@db). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/parrot/internal/db.gleam"). -export([fetch_schema_mysql/1, fetch_schema_postgresql/1, fetch_schema_sqlite/1]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC(false). -file("src/parrot/internal/db.gleam", 12). ?DOC(false). -spec fetch_schema_mysql(binary()) -> {ok, binary()} | {error, parrot@internal@errors:parrot_error()}. fetch_schema_mysql(Db) -> Conn@1 = case gleam_stdlib:uri_parse(Db) of {ok, Conn} -> Conn; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"parrot/internal/db"/utf8>>, function => <<"fetch_schema_mysql"/utf8>>, line => 13, value => _assert_fail, start => 298, 'end' => 333, pattern_start => 309, pattern_end => 317}) end, Creds = case erlang:element(3, Conn@1) of none -> none; {some, Userinfo} -> case gleam@string:split(Userinfo, <<":"/utf8>>) of [User] -> {some, {User, <<""/utf8>>}}; [User@1, Pass] -> {some, {User@1, Pass}}; _ -> none end end, gleam@result:'try'( gleam@option:to_result(Creds, {my_sql_d_b_not_found, <<""/utf8>>}), fun(_use0) -> {User@2, Pass@1} = _use0, Port@1 = case erlang:element(5, Conn@1) of none -> <<"3306"/utf8>>; {some, Port} -> erlang:integer_to_binary(Port) end, Host@1 = case erlang:element(4, Conn@1) of none -> <<"localhost"/utf8>>; {some, Host} -> Host end, Db@1 = gleam@string:replace( erlang:element(6, Conn@1), <<"/"/utf8>>, <<""/utf8>> ), gleam@result:'try'( begin _pipe = parrot@internal@shellout:command( <<"mysqldump"/utf8>>, [<<"--no-data"/utf8>>, <<"-u"/utf8>>, User@2, <<"-p"/utf8, Pass@1/binary>>, <<"-h"/utf8>>, Host@1, <<"-P"/utf8>>, Port@1, Db@1], <<"."/utf8>>, [] ), gleam@result:replace_error(_pipe, mysqldump_error) end, fun(Out) -> _pipe@1 = Out, _pipe@2 = gleam@string:split(_pipe@1, <<"\n"/utf8>>), _pipe@3 = gleam@list:filter( _pipe@2, fun(Line) -> gleam_stdlib:contains_string( Line, <<"mysqldump:"/utf8>> ) =:= false end ), _pipe@4 = gleam@string:join(_pipe@3, <<"\n"/utf8>>), {ok, _pipe@4} end ) end ). -file("src/parrot/internal/db.gleam", 58). ?DOC(false). -spec fetch_schema_postgresql(binary()) -> {ok, binary()} | {error, parrot@internal@errors:parrot_error()}. fetch_schema_postgresql(Db) -> _pipe = parrot@internal@shellout:command( <<"pg_dump"/utf8>>, [<<"--no-privileges"/utf8>>, <<"--no-acl"/utf8>>, <<"--no-owner"/utf8>>, <<"--schema-only"/utf8>>, <<"--no-comments"/utf8>>, <<"--encoding=utf8"/utf8>>, Db], <<"."/utf8>>, [] ), gleam@result:replace_error(_pipe, pgdump_error). -file("src/parrot/internal/db.gleam", 76). ?DOC(false). -spec fetch_schema_sqlite(binary()) -> {ok, list(binary())} | {error, parrot@internal@errors:parrot_error()}. fetch_schema_sqlite(Db) -> sqlight:with_connection( Db, fun(Conn) -> Schema_decoder = begin gleam@dynamic@decode:field( 0, {decoder, fun gleam@dynamic@decode:decode_string/1}, fun(Sql) -> gleam@dynamic@decode:success(Sql) end ) end, Sql@1 = <<" SELECT sql FROM sqlite_master WHERE type IN ('table', 'view', 'index', 'trigger') AND name NOT LIKE 'sqlite_%' AND sql IS NOT NULL ORDER BY CASE type WHEN 'table' THEN 1 WHEN 'view' THEN 2 WHEN 'index' THEN 3 WHEN 'trigger' THEN 4 ELSE 5 END, name; "/utf8>>, gleam@result:'try'( begin _pipe = sqlight:'query'(Sql@1, Conn, [], Schema_decoder), gleam@result:replace_error( _pipe, {sqlite_d_b_not_found, <<""/utf8>>} ) end, fun(Result) -> {ok, Result} end ) end ).