Skip to content

Commit

Permalink
sql formatting lowercase
Browse files Browse the repository at this point in the history
  • Loading branch information
qdrs committed Aug 3, 2023
1 parent 747792f commit 5ea9a3b
Show file tree
Hide file tree
Showing 8 changed files with 513 additions and 510 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.
DROP FUNCTION IF EXISTS diesel_manage_updated_at (_tbl regclass);
drop function if exists diesel_manage_updated_at (_tbl regclass);

DROP FUNCTION IF EXISTS diesel_set_updated_at ();
drop function if exists diesel_set_updated_at ();
34 changes: 17 additions & 17 deletions src/rust/db/migrations/00000000000000_diesel_initial_setup/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@
--
-- SELECT diesel_manage_updated_at('users');
-- ```
CREATE OR REPLACE FUNCTION diesel_manage_updated_at (_tbl regclass)
RETURNS void
AS $$
BEGIN
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
create or replace function diesel_manage_updated_at (_tbl regclass)
returns void
as $$
begin
execute format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
END;
end;
$$
LANGUAGE plpgsql;
language plpgsql;

CREATE OR REPLACE FUNCTION diesel_set_updated_at ()
RETURNS TRIGGER
AS $$
BEGIN
IF (NEW IS DISTINCT FROM OLD AND NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at) THEN
NEW.updated_at := CURRENT_TIMESTAMP;
END IF;
RETURN NEW;
END;
create or replace function diesel_set_updated_at ()
returns trigger
as $$
begin
if (NEW is distinct from OLD and new.updated_at is not distinct from old.updated_at) then
new.updated_at := current_timestamp;
end if;
return NEW;
end;
$$
LANGUAGE plpgsql;
language plpgsql;
16 changes: 8 additions & 8 deletions src/rust/db/migrations/2023-03-16-025536_create_markets/down.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
DROP TABLE recognized_market_events;
drop table recognized_market_events;

DROP TYPE market_event_type;
drop type market_event_type;

DROP TABLE recognized_markets;
drop table recognized_markets;

DROP TRIGGER register_market_trigger ON market_registration_events;
drop trigger register_market_trigger on market_registration_events;

DROP FUNCTION register_market;
drop function register_market;

DROP TABLE market_registration_events;
drop table market_registration_events;

DROP TABLE markets;
drop table markets;

DROP TABLE coins;
drop table coins;
144 changes: 72 additions & 72 deletions src/rust/db/migrations/2023-03-16-025536_create_markets/up.sql
Original file line number Diff line number Diff line change
@@ -1,115 +1,115 @@
-- Corresponds to aptos_std::type_info::TypeInfo and
-- aptos_framework::coin::CoinInfo. GenericAsset will also be included.
CREATE TABLE coins (
account_address varchar(70) NOT NULL,
module_name text NOT NULL,
struct_name text NOT NULL,
symbol varchar(10) NOT NULL,
name text NOT NULL,
decimals smallint NOT NULL,
PRIMARY KEY (account_address, module_name, struct_name)
create table coins (
account_address varchar(70) not null,
module_name text not null,
struct_name text not null,
symbol varchar(10) not null,
name text not null,
decimals smallint not null,
primary key (account_address, module_name, struct_name)
);

-- Corresponds to econia::registry::MarketInfo.
-- Only recognized markets should be stored in the api database.
CREATE TABLE markets (
market_id numeric(20) NOT NULL PRIMARY KEY,
name text NOT NULL,
create table markets (
market_id numeric(20) not null primary key,
name text not null,
base_account_address varchar(70),
base_module_name text,
base_struct_name text,
base_name_generic text,
quote_account_address varchar(70) NOT NULL,
quote_module_name text NOT NULL,
quote_struct_name text NOT NULL,
lot_size numeric(20) NOT NULL,
tick_size numeric(20) NOT NULL,
min_size numeric(20) NOT NULL,
underwriter_id numeric(20) NOT NULL,
created_at timestamptz NOT NULL,
FOREIGN KEY (base_account_address, base_module_name, base_struct_name) REFERENCES coins (account_address, module_name, struct_name),
FOREIGN KEY (quote_account_address, quote_module_name, quote_struct_name) REFERENCES coins (account_address, module_name, struct_name)
quote_account_address varchar(70) not null,
quote_module_name text not null,
quote_struct_name text not null,
lot_size numeric(20) not null,
tick_size numeric(20) not null,
min_size numeric(20) not null,
underwriter_id numeric(20) not null,
created_at timestamptz not null,
foreign key (base_account_address, base_module_name, base_struct_name) references coins (account_address, module_name, struct_name),
foreign key (quote_account_address, quote_module_name, quote_struct_name) references coins (account_address, module_name, struct_name)
);

-- Corresponds to econia::registry::MarketRegistrationEvent
CREATE TABLE market_registration_events (
market_id numeric(20) NOT NULL PRIMARY KEY,
time timestamptz NOT NULL,
create table market_registration_events (
market_id numeric(20) not null primary key,
time timestamptz not null,
base_account_address varchar(70),
base_module_name text,
base_struct_name text,
base_name_generic text,
quote_account_address varchar(70) NOT NULL,
quote_module_name text NOT NULL,
quote_struct_name text NOT NULL,
lot_size numeric(20) NOT NULL,
tick_size numeric(20) NOT NULL,
min_size numeric(20) NOT NULL,
underwriter_id numeric(20) NOT NULL,
FOREIGN KEY (market_id) REFERENCES markets (market_id)
quote_account_address varchar(70) not null,
quote_module_name text not null,
quote_struct_name text not null,
lot_size numeric(20) not null,
tick_size numeric(20) not null,
min_size numeric(20) not null,
underwriter_id numeric(20) not null,
foreign key (market_id) references markets (market_id)
);

CREATE FUNCTION register_market ()
RETURNS TRIGGER
AS $register_market$
DECLARE
create function register_market ()
returns trigger
as $register_market$
declare
base_symbol varchar(8);
quote_symbol varchar(8);
BEGIN
IF NEW.base_name_generic IS NULL THEN
SELECT
begin
if new.base_name_generic is null then
select
symbol
FROM
from
coins
WHERE
account_address = NEW.base_account_address
AND module_name = NEW.base_module_name
AND struct_name = NEW.base_struct_name INTO base_symbol;
SELECT
where
account_address = new.base_account_address
and module_name = new.base_module_name
and struct_name = new.base_struct_name into base_symbol;
select
symbol
FROM
from
coins
WHERE
account_address = NEW.quote_account_address
AND module_name = NEW.quote_module_name
AND struct_name = NEW.quote_struct_name INTO quote_symbol;
INSERT INTO markets
VALUES (NEW.market_id, base_symbol || '-' || quote_symbol, NEW.base_account_address, NEW.base_module_name, NEW.base_struct_name, NEW.base_name_generic, NEW.quote_account_address, NEW.quote_module_name, NEW.quote_struct_name, NEW.lot_size, NEW.tick_size, NEW.min_size, NEW.underwriter_id, NEW.time);
ELSE
INSERT INTO markets
VALUES (NEW.market_id, NEW.base_name_generic, NEW.base_account_address, NEW.base_module_name, NEW.base_struct_name, NEW.base_name_generic, NEW.quote_account_address, NEW.quote_module_name, NEW.quote_struct_name, NEW.lot_size, NEW.tick_size, NEW.min_size, NEW.underwriter_id, NEW.time);
END IF;
RETURN NEW;
END;
where
account_address = new.quote_account_address
and module_name = new.quote_module_name
and struct_name = new.quote_struct_name into quote_symbol;
insert into markets
values (new.market_id, base_symbol || '-' || quote_symbol, new.base_account_address, new.base_module_name, new.base_struct_name, new.base_name_generic, new.quote_account_address, new.quote_module_name, new.quote_struct_name, new.lot_size, new.tick_size, new.min_size, new.underwriter_id, new.time);
else
insert into markets
values (new.market_id, new.base_name_generic, new.base_account_address, new.base_module_name, new.base_struct_name, new.base_name_generic, new.quote_account_address, new.quote_module_name, new.quote_struct_name, new.lot_size, new.tick_size, new.min_size, new.underwriter_id, new.time);
end if;
return NEW;
end;
$register_market$
LANGUAGE plpgsql;
language plpgsql;

CREATE TRIGGER register_market_trigger
BEFORE INSERT ON market_registration_events
FOR EACH ROW
EXECUTE PROCEDURE register_market ();
create trigger register_market_trigger
before insert on market_registration_events
for each row
execute procedure register_market ();

-- Corresponds to econia::registry::RecognizedMarketInfo
-- This id does not need to exist, but diesel only supports tables
-- with primary keys
CREATE TABLE recognized_markets (
id serial NOT NULL PRIMARY KEY,
market_id numeric(20) NOT NULL,
FOREIGN KEY (market_id) REFERENCES markets (market_id)
create table recognized_markets (
id serial not null primary key,
market_id numeric(20) not null,
foreign key (market_id) references markets (market_id)
);

-- Type of events that can be emitted for a recognized market.
CREATE TYPE market_event_type AS enum (
create type market_event_type as enum (
'add',
'remove',
'update'
);

-- Corresponds to econia::registry::RecognizedMarketEvent.
CREATE TABLE recognized_market_events (
market_id numeric(20) NOT NULL PRIMARY KEY,
time timestamptz NOT NULL,
event_type market_event_type NOT NULL,
create table recognized_market_events (
market_id numeric(20) not null primary key,
time timestamptz not null,
event_type market_event_type not null,
lot_size numeric(20),
tick_size numeric(20),
min_size numeric(20)
Expand Down
23 changes: 11 additions & 12 deletions src/rust/db/migrations/2023-03-16-080047_order_events/down.sql
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
-- TODO: need to finish this correctly
drop table fills;

DROP TABLE fills;
drop trigger handle_taker_event_trigger on taker_events;

DROP TRIGGER handle_taker_event_trigger ON taker_events;
drop function handle_taker_event;

DROP FUNCTION handle_taker_event;
drop table taker_events;

DROP TABLE taker_events;
drop trigger handle_maker_event_trigger on maker_events;

DROP TRIGGER handle_maker_event_trigger ON maker_events;
drop function handle_maker_event;

DROP FUNCTION handle_maker_event;
drop table maker_events;

DROP TABLE maker_events;
drop type maker_event_type;

DROP TYPE maker_event_type;
drop table orders;

DROP TABLE orders;
drop type order_state;

DROP TYPE order_state;

DROP TYPE side;
drop type side;
Loading

0 comments on commit 5ea9a3b

Please sign in to comment.