-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ricardo Arturo Cabral Mejía <me@ricardocabral.io>
- Loading branch information
Showing
80 changed files
with
2,126 additions
and
677 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
exports.up = function (knex) { | ||
return knex.schema.createTable('users', (table) => { | ||
table.binary('pubkey').primary() | ||
table.boolean('is_admitted').default(0) | ||
table.bigint('balance').default(0) | ||
table.datetime('tos_accepted_at', { useTz: false, precision: 3 }) | ||
table.timestamps(true, true, false) | ||
}) | ||
} | ||
|
||
exports.down = function (knex) { | ||
return knex.schema.dropTable('users') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Adapted from: https://github.com/stackernews/stacker.news | ||
// Original Author: Keyan Kousha https://github.com/huumn | ||
/** | ||
MIT License | ||
Copyright (c) 2023 Keyan Kousha / Stacker News | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
exports.up = async function (knex) { | ||
return knex.schema | ||
.raw(`create function now_utc() returns timestamp as $$ | ||
select now() at time zone 'utc'; | ||
$$ language sql;`) | ||
.raw(`create function ASSERT_SERIALIZED() returns void as $$ | ||
BEGIN | ||
IF (select current_setting('transaction_isolation') <> 'serializable') THEN | ||
RAISE EXCEPTION 'SN_NOT_SERIALIZABLE'; | ||
END IF; | ||
END; | ||
$$ language plpgsql; | ||
`) | ||
.raw( | ||
`CREATE OR REPLACE FUNCTION confirm_invoice(invoice_id UUID, amount_received BIGINT, confirmation_date TIMESTAMP WITHOUT TIME ZONE) | ||
RETURNS INTEGER | ||
LANGUAGE plpgsql | ||
AS $$ | ||
DECLARE | ||
payee BYTEA; | ||
confirmed_date TIMESTAMP WITHOUT TIME ZONE; | ||
BEGIN | ||
PERFORM ASSERT_SERIALIZED(); | ||
SELECT "pubkey", "confirmed_at" INTO payee, confirmed_date FROM "invoices" WHERE id = invoice_id; | ||
IF confirmed_date IS NULL THEN | ||
UPDATE invoices | ||
SET | ||
"confirmed_at" = confirmation_date, | ||
"amount_paid" = amount_received, | ||
"updated_at" = now_utc() | ||
WHERE id = invoice_id; | ||
UPDATE users SET balance = balance + amount_received WHERE "pubkey" = payee; | ||
END IF; | ||
RETURN 0; | ||
END; | ||
$$;`) | ||
} | ||
|
||
exports.down = function (knex) { | ||
return knex.schema | ||
.raw('DROP FUNCTION IF EXISTS confirm_invoice(UUID, BYTEA, TIMESTAMP);') | ||
.raw('DROP FUNCTION IF EXISTS ASSERT_SERIALIZED();') | ||
.raw('DROP FUNCTION IF EXISTS now_utc();') | ||
} |
14 changes: 14 additions & 0 deletions
14
migrations/20230119_170900_add_kind_tags_created_at_index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
exports.up = async function (knex) { | ||
return knex.schema | ||
.raw('CREATE EXTENSION btree_gin;') | ||
.raw( | ||
`CREATE INDEX kind_tags_created_at_idx | ||
ON events USING GIN ( event_kind, event_tags, event_created_at );`, | ||
) | ||
} | ||
|
||
exports.down = function (knex) { | ||
return knex.schema | ||
.raw('DROP INDEX IF EXISTS kind_tags_created_at_idx;') | ||
.raw('DROP EXTENSION btree_gin;') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
exports.up = async function (knex) { | ||
return knex.schema | ||
.raw( | ||
`CREATE OR REPLACE FUNCTION charge_user(charged_user BYTEA, amount BIGINT) | ||
RETURNS INTEGER | ||
LANGUAGE plpgsql | ||
AS $$ | ||
DECLARE | ||
current_balance BIGINT; | ||
BEGIN | ||
PERFORM ASSERT_SERIALIZED(); | ||
SELECT "balance" INTO current_balance FROM "users" WHERE "pubkey" = charged_user; | ||
IF current_balance - amount >= 0 THEN | ||
UPDATE "users" SET balance = balance - amount WHERE "pubkey" = charged_user; | ||
RETURN 1; | ||
ELSE | ||
RETURN 0; | ||
END IF; | ||
END; | ||
$$;`) | ||
} | ||
|
||
exports.down = function (knex) { | ||
return knex.schema | ||
.raw('DROP FUNCTION IF EXISTS charge_user(BYTEA, BIGINT);') | ||
} |
9 changes: 9 additions & 0 deletions
9
migrations/20230126_230000_add_remote_address_to_events_table.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
exports.up = function (knex) { | ||
return knex.raw('ALTER TABLE events ADD remote_address inet NULL;') | ||
} | ||
|
||
exports.down = function (knex) { | ||
return knex.schema.alterTable('events', function (table) { | ||
table.dropColumn('remote_address') | ||
}) | ||
} |
Oops, something went wrong.