Coder Social home page Coder Social logo

ahub's Introduction

ahub

Access hub command-line utility.

Setup

Copy .env.example to .env in root and edit.

export DATABASE_URL="sqlite://db/dev.db"
export ACCESS_API_URL="http://localhost:3000"

Launch config for debugging

"env": {
    "DATABASE_URL": "sqlite://db/dev.db",
}

WSL

Use "$(hostname).local" or nameserver ip to connect to Windows localhost. Must open port on Windows.

echo "$(hostname).local"
export ACCESS_API_URL="http://$(hostname).local:3000"
cat /etc/resolv.conf

Cargo in root

cargo run -- --help
cargo run dump sqlite-version
cargo run dump events
cargo run dump users -t2
cargo run mock grant -u1 -p1
cargo run mock deny -p1 -c666
cargo run token
cargo run token --set <token>
cargo run access -c <code> -p <position>
cargo run heartbeat
cargo run heartbeat -a <api_url with no trailing slash>

cargo build --release -v # Outputs to target/release. Must create .env

Sqlx CLI cheatsheet

cargo sqlx --help
cargo sqlx database setup
cargo sqlx migrate add schema
cargo sqlx migrate run
cargo sqlx database reset -y

Did you know you can embed your migrations in your application binary? On startup, after creating your database connection or pool, add:

sqlx::migrate!().run(<&your_pool OR &mut your_connection>).await?;

Note that the compiler won't pick up new migrations if no Rust source files have changed. You can create a Cargo build script to work around this with sqlx migrate build-script.

See: https://docs.rs/sqlx/0.5/sqlx/macro.migrate.html

Sqlx CLI run using cargo

All commands require that a database url is provided. This can be done either with the --database-url command line option or by setting DATABASE_URL, either in the environment or in a .env file in the current working directory.

For more details, run cargo sqlx <command> --help.

# Postgres
DATABASE_URL=postgres://postgres@localhost/my_database

Create/drop the database at DATABASE_URL

cargo sqlx database create
cargo sqlx database drop

Create and run migrations

$ cargo sqlx migrate add <name>

Creates a new file in migrations/<timestamp>-<name>.sql. Add your database schema changes to this new file.


$ cargo sqlx migrate run

Compares the migration history of the running database against the migrations/ folder and runs any scripts that are still pending.

Reverting Migrations

If you would like to create reversible migrations with corresponding "up" and "down" scripts, you use the -r flag when creating new migrations:

$ cargo sqlx migrate add -r <name>
Creating migrations/20211001154420_<name>.up.sql
Creating migrations/20211001154420_<name>.down.sql

After that, you can run these as above:

$ cargo sqlx migrate run
Applied migrations/20211001154420 <name> (32.517835ms)

And reverts work as well:

$ cargo sqlx migrate revert
Applied 20211001154420/revert <name>

Note: attempting to mix "simple" migrations with reversible migrations with result in an error.

$ cargo sqlx migrate add <name1>
Creating migrations/20211001154420_<name>.sql

$ cargo sqlx migrate add -r <name2>
error: cannot mix reversible migrations with simple migrations. All migrations should be reversible or simple migrations

Enable building in "offline mode" with query!()

Note: must be run as cargo sqlx.

cargo sqlx prepare

Saves query metadata to sqlx-data.json in the current directory; check this file into version control and an active database connection will no longer be needed to build your project.

Has no effect unless the offline feature of sqlx is enabled in your project. Omitting that feature is the most likely cause if you get a sqlx-data.json file that looks like this:

{
  "database": "PostgreSQL"
}

cargo sqlx prepare --check

Exits with a nonzero exit status if the data in sqlx-data.json is out of date with the current database schema and queries in the project. Intended for use in Continuous Integration.

Force building in offline mode

To make sure an accidentally-present DATABASE_URL environment variable or .env file does not result in cargo build (trying to) access the database, you can set the SQLX_OFFLINE environment variable to true.

If you want to make this the default, just add it to your .env file. cargo sqlx prepare will still do the right thing and connect to the database.

Include queries behind feature flags (such as queryies inside of tests)

In order for sqlx to be able to find queries behind certain feature flags you need to turn them on by passing arguments to rustc.

This is how you would turn all targets and features on.

cargo sqlx prepare -- --all-targets --all-features

Sqlite

Queries

  • select * from AccessUser u join AccessPointToAccessUser p2u on u.id = p2u.access_user_id join AccessPoint p on p2u.access_point_id = p.id;
  • select * from AccessUser u join AccessPointToAccessUser p2u on u.id = p2u.access_user_id join AccessPoint p on p2u.access_point_id = p.id order by position asc, code asc;
  • select access_point_id, position, code, access_user_id, activate_code_at, expire_code_at from AccessUser u join AccessPointToAccessUser p2u on u.id = p2u.access_user_id join AccessPoint p on p2u.access_point_id = p.id order by position asc, code asc;
  • select access_point_id, position, code, access_user_id, activate_code_at, expire_code_at from AccessUser u join AccessPointToAccessUser p2u on u.id = p2u.access_user_id join AccessPoint p on p2u.access_point_id = p.id where (activate_code_at is null or activate_code_at <= current_timestamp) and (expire_code_at is null or current_timestamp < expire_code_at) order by position asc, code asc;

ahub's People

Contributors

mw10013 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.