Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
nlopes committed Aug 28, 2023
0 parents commit 69f76e1
Show file tree
Hide file tree
Showing 55 changed files with 2,023 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: cargo
directory: "/"
schedule:
interval: daily
79 changes: 79 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Test

on:
push:
branches: [ main ]
pull_request:
branches: '*'

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest
continue-on-error: ${{ matrix.nightly }}

strategy:
fail-fast: false
matrix:
toolchain: [ 'stable' ]
nightly: [false]
include:
- toolchain: 'nightly'
nightly: true

steps:
- uses: actions/checkout@v2

- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.toolchain }}
override: true
components: clippy, rustfmt

- name: Cache cargo registry
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
key: rust_${{ matrix.toolchain }}-cargo-${{ hashFiles('**/Cargo.toml') }}

- name: Run cargo fmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

- name: Run test - sqlite | sqlx | runtime-async-std | macros
uses: actions-rs/cargo@v1
with:
command: test
args: --features sqlite,sqlx,runtime-async-std,macros --all-targets --verbose

- name: Run test - sqlite | diesel | runtime-async-std | macros
uses: actions-rs/cargo@v1
with:
command: test
args: --features sqlite,diesel,runtime-async-std,macros --all-targets --verbose

- name: Run test - sqlite | diesel
uses: actions-rs/cargo@v1
with:
command: test
args: --features sqlite,diesel --all-targets --verbose

- name: Run doc tests
uses: actions-rs/cargo@v1
with:
command: test
args: --features sqlite,sqlx,runtime-async-std,macros --doc --verbose

- name: Run clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: --features sqlite,sqlx,runtime-async-std,macros
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
49 changes: 49 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[package]
name = "database-schema"
version = "0.1.0"
edition = "2021"
description = "A library to dump a database schema into a file in SQL format"
documentation = "https://docs.rs/database-schema"
authors = ["Norberto Lopes <nlopesml@gmail.com>"]
repository = "https://github.com/nlopes/database-schema.git"
homepage = "https://github.com/nlopes/database-schema.git"
license = "MIT OR Apache-2.0"
keywords = ["database", "schema", "structuresql"]
categories = ["database", "web-programming"]
exclude = [".gitignore", ".github/", "README.tpl"]

[workspace]
members = [".", "examples/*"]
default-members = ["."]
resolver = "2"

[features]
default = []
sqlx = ["dep:sqlx", "sqlx?/tls-native-tls"]
diesel = ["dep:diesel", "dep:diesel_migrations"]
macros = []
runtime-async-std = ["sqlx?/runtime-async-std", "dep:async-std"]
runtime-tokio = ["sqlx?/runtime-tokio", "dep:tokio"]
sqlite = ["sqlx?/sqlite", "diesel?/sqlite", "diesel_migrations?/sqlite"]
mysql = ["sqlx?/mysql", "diesel?/mysql", "diesel_migrations?/mysql", "url", "percent-encoding"]
postgres = ["sqlx?/postgres", "url", "percent-encoding"]

[dependencies]
async-std = { version = "1", optional = true } # this has to include default due to task::block_on usage
chrono = { version = "0.4", features = ["clock"], default-features = false }
diesel = { version = "2.1", optional = true, default-features = false }
diesel_migrations = { version = "2.1", optional = true, default-features = false}
http = "0.2.9"
percent-encoding = { version = "2.3", optional = true }
sqlx = { version = "0.7", features = ["migrate", "macros"], optional = true, default-features = false }
thiserror = "1"
tokio = { version = "1", features = ["rt-multi-thread"], optional = true, default-features = false }
tracing = { version = "0.1", default-features = false }
url = { version = "2.1", optional = true }

[dev-dependencies]
tokio = { version = "1", features = ["rt", "macros"], default-features = false }

[package.metadata.docs.rs]
features = ["sqlite", "sqlx", "macros", "runtime-async-std"]
rustdoc-args = ["--cfg", "docsrs"]
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) 2023 Norberto Lopes

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.
132 changes: 132 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# database-schema

[![CI Status](https://github.com/nlopes/database-schema/workflows/Test/badge.svg)](https://github.com/nlopes/database-schema/actions)
[![docs.rs](https://docs.rs/database-schema/badge.svg)](https://docs.rs/database-schema)
[![crates.io](https://img.shields.io/crates/v/database-schema.svg)](https://crates.io/crates/database-schema)
[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/nlopes/database-schema/blob/master/LICENSE)

This crate provides a simple way to dump a database structure to a file, in SQL
format.

It takes inspiration by the ruby on rails [schema dump].

## Usage

```rust
use std::path::PathBuf;

database_structure::generate_structure_sql(PathBuf::from("./structure.sql"));
```


## Feature flags

`database-schema` uses a set of [feature flags] to reduce the size of the libray and
therefore your binary. The way one should use this package is to pick the right
combination of feature flags for their use case. Below is a list of the available
feature flags and the combinations that are recommended for each use case.

- `sqlite`: Enables SQLite support.
- `postgres`: Enables PostgreSQL support.
- `mysql`: Enables MySQL support.
- `sqlx`: Enables [sqlx] support.
- `diesel`: Enables [diesel] support.

### Feature flag matrix
| Database | Query builder | Runtime |
|----------|---------------|---------|
| `sqlite` | `sqlx` | `runtime-async-std` |
| `sqlite` | `sqlx` | `runtime-tokio` |
| `sqlite` | `diesel` | |
| `mysql` | `sqlx` | `runtime-async-std` |
| `mysql` | `sqlx` | `runtime-tokio` |
| `mysql` | `diesel` | |
| `postgres` | `sqlx` | `runtime-async-std` |
| `postgres` | `sqlx` | `runtime-tokio` |
| `postgres` | `diesel` | |

### Combining feature flags

The following are the recommended feature flag combinations for each use case.

First pick one of the following database feature flags:

* `sqlite`
* `mysql`
* `postgres`

Then pick one of the following database query building feature flags:

* `sqlx`
* `diesel`

If you're using `sqlx`, you also have to pick one of the following runtime feature flags:

* `runtime-async-std`
* `runtime-tokio`

### Example

```toml
[dependencies]
database-schema = { version = "0.1", features = ["sqlite", "sqlx", "runtime-async-std"] }
```

alternatively, if you're using `diesel`:
```toml
[dependencies]
database-schema = { version = "0.1", features = ["sqlite", "diesel"] }
```

### Macros

This crate also provides a set of macros that can be used to generate the SQL
structure of a database at compile time. This is useful for generating the SQL from
`build.rs`.


```toml
[dependencies]
database-schema = { version = "0.1", features = ["sqlite", "diesel", "macros"] }
```

```rust
use database_schema::macros::generate_without_runtime;

let sql = generate_without_runtime!("./migrations", "structure.sql");
```

The above is strictly equivalent to calling:

```rust
use database_schema::macros::generate_without_runtime_using_defaults;

let sql = generate_without_runtime!();
```

## Customization

```rust
use database_schema::DatabaseSchemaBuilder;

let migrations_path = "db/migrations";
let destination_path = "db/structure.sql";

// This assumes you're using SQLite in memory.
//
// If you need to set up a `connection_url` you can use
// `DatabaseSchemaBuilder::connection_url` before calling
// `build()`.

DatabaseSchemaBuilder::new()
.migrations_dir(migrations_path)?
.destination_path(destination_path)
.build()
.dump()
.await
```

[feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section
[sqlx]: https://docs.rs/sqlx/latest/sqlx/
[diesel]: https://docs.rs/diesel/latest/diesel/
[schema dump]: https://guides.rubyonrails.org/active_record_migrations.html#schema-dumping-and-you
8 changes: 8 additions & 0 deletions README.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# {{crate}}

[![CI Status](https://github.com/nlopes/database-schema/workflows/Test/badge.svg)](https://github.com/nlopes/database-schema/actions)
[![docs.rs](https://docs.rs/database-schema/badge.svg)](https://docs.rs/database-schema)
[![crates.io](https://img.shields.io/crates/v/database-schema.svg)](https://crates.io/crates/database-schema)
[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/nlopes/database-schema/blob/master/LICENSE)

{{readme}}
9 changes: 9 additions & 0 deletions examples/mysql-diesel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "mysql-diesel"
version = "0.1.0"
edition = "2021"
authors = ["Norberto Lopes <nlopesml@gmail.com>"]

[dependencies]
tokio = { version = "1", features = ["rt-multi-thread", "macros"], default-features = false }
database-schema = { path = "../../", features = ["diesel", "mysql"] }
30 changes: 30 additions & 0 deletions examples/mysql-diesel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# mysql-diesel

This simple example will use `diesel` to run the migrations found in
[`./migrations`](./migrations) and then generate a [`./structure.sql`](./structure.sql)
file with the database schema;

# Running

```shell
# this is an example in my MacOS laptop, using mysql from homebrew, adjust as you see fit!
$ docker compose up
$ export PATH=/opt/homebrew/opt/mysql-client@8.0/bin:$PATH
$ export RUSTFLAGS="-L/opt/homebrew/opt/mysql-client@8.0/lib"
$ cargo run
```

## Dependencies

- `libmysqlclient`
- `mysqldump`
- `docker compose` - if you want to run the example against a docker instance running
`mysql` server.

If they are not in your default paths you will have to set them like so:

```shell
export PATH=/path/to/your/mysql-client/install-folder/bin:$PATH
export RUSTFLAGS="-L/path/to/your/lib-mysql-folder/lib"
```

19 changes: 19 additions & 0 deletions examples/mysql-diesel/certs/client-cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDBzCCAe8CAQEwDQYJKoZIhvcNAQELBQAwRjELMAkGA1UEBhMCR0IxDzANBgNV
BAgMBkxvbmRvbjEPMA0GA1UEBwwGTG9uZG9uMRUwEwYDVQQDDAxteXNxbC1kaWVz
ZWwwHhcNMjMwODI4MTk0MTM1WhcNMzMwNzA2MTk0MTM1WjBNMQswCQYDVQQGEwJH
QjEPMA0GA1UECAwGTG9uZG9uMQ8wDQYDVQQHDAZMb25kb24xHDAaBgNVBAMME215
c3FsLWRpZXNlbC1jbGllbnQwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB
AQCtEfRGJilyVwRC7rAs7VzLkPoXg6gOHeZz9eblUx8lc16uwu/SsS2POZ8QUOdX
fupMj79vK9WSHy59DWBCPfHUSQonJzkP7EehZlhJ5IZG3vH6jtk9RQvCpZYudGev
bFyMPNT5ZqsOnF9u0DnQPNcjts1jgzpQj2tFYXJYtf9YP8GfeudgTcU4Y6+a/uYs
WlidQMPOurOTpdtQW6aS5rOsE2zovcWPboWYahgWn/bl17lX3MprJ7suDIr8zuf+
Jyiw1jywNi5ZdBoeBeHh9fXZtGynX8X2WicL6kfPOTFRw5/39TLGhRrLXjK8DlVq
F3/R6VAs4gxkOr8jzCLUohsrAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAIlZ3Skj
gtqPk/NwzbDBRHivmeOTSBXx9/KP+zPwR5psuJGqRNqo8V4X4l2rd7Cf0g249PLf
FUziwXAUsp6U8k8JfOvW2l70wRhFOZlVR54Pz4g4JH/lSTasbo3JfE57wVQz3XQK
qJi1ZkaQB5mbOG1SJRTf5X32L9ADCkaGV2rwPZbKEqId2RO9ExfL2MG8OnDfSBK2
ciVTVIH/5J6+I4YgvFYy6qyGyHZcgc5hYynokD2Kfnqnl8pmVITh7xreM7VfWIhW
Tz1vmk58cbDpnDoqahuSf5e8rKcUavrV1x7tYUmnMdO/baOY7jc3CXsHgbBhWZZY
W70jJgfB6jr9Xs4=
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions examples/mysql-diesel/certs/client-key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEArRH0RiYpclcEQu6wLO1cy5D6F4OoDh3mc/Xm5VMfJXNersLv
0rEtjzmfEFDnV37qTI+/byvVkh8ufQ1gQj3x1EkKJyc5D+xHoWZYSeSGRt7x+o7Z
PUULwqWWLnRnr2xcjDzU+WarDpxfbtA50DzXI7bNY4M6UI9rRWFyWLX/WD/Bn3rn
YE3FOGOvmv7mLFpYnUDDzrqzk6XbUFumkuazrBNs6L3Fj26FmGoYFp/25de5V9zK
aye7LgyK/M7n/icosNY8sDYuWXQaHgXh4fX12bRsp1/F9lonC+pHzzkxUcOf9/Uy
xoUay14yvA5Vahd/0elQLOIMZDq/I8wi1KIbKwIDAQABAoIBAH9tMbqYjHmoQfX6
AfMCTQmA0/KOOCU0tKH6kqeUXOFZIYRw+NzbIR1MIqaDuuF8C4yVZjC3SIdOuA7Q
02fSbgSMRpJvWZ80q8TVMvos7QSvT+DYXnCzLqaA/qNzh4fss/N5MqHyis22KrnP
TFHbCdg81tqHG1+HSUcLKYLRdZEGIQxUIN7Pr7DMKR2ykNN/xulqaN1aJ7D5kScj
rPJVd225UQNXWWh8RYVA9iN25oYOBBOrsJFxnULP3KRkDSdruok9F9GRourL1ELp
Gemxo1CIV+iM+RmsHpYxsh1qgkqVxowaz4KJMQ9PT6oOyljLDuA1g/dJc+rXSjBf
bHpacuECgYEA5pjtVTlSj+o0JV9ABC3XOBv4rYVDB0UcAJvutrqPzj5Oj+m+83sm
N+jAD2zThjwbIQtCEnmeI55iLcZWYlHhGdxJyM74fafVZe7lsKxRWoaecNPaJfT6
JJHOji9NA3WA4KO4jErEmEB/HOf4bUK91caiUFLKiltGUYSF41j26dsCgYEAwCKz
zj8bbi0pGuKRwgH9RTODhuUMHflA2LC3zknfRaFKcXlV1ddz6IPrOr0HxSVew3G0
wC6Z7yPVTE2gAy9T2MjeQX8dofFOk5g2bDN3Ac1a8MfyP78i5qt9OxiqBy2uj/lV
M0fgrw/95iRPmxyBAVvAnmjEVEu7fabHO6CUHPECgYBDw68C+2xqxF18mgga8kmr
wHSMsXuoGEQJXcmBw0NdTWwS2JL3xDnP9kLyhX2HlgQ26rMI8NprBzE82GssS3mF
+vln3IKjkn2gjdrL12e03ZiT+X3C58HWm06C9B2CpbYwzYv/Fj29rD5uhTC5EwLs
Xon2Zs4EaJw6emJKFCvDPwKBgQCGkwfbqun8lpcW5KDxAVGzOayjPCTrjZy06bok
PCutapZounK7j+f4cQW+o44gsNcaD7dpcHqTPEb25dvwvyJ8Ud0ShQVtW0YNLOzZ
hoaRdZN/2Jw9uBOq+2yAivr0gjOlVh8uBudB1vKgUsiLPUDCgdB9Y6Y34L+W98zO
X9++EQKBgHtQWPYXwoe0I7UAumPfktbBMQDUtleCEdwGISn3z1zPHSM/9uJL9hvk
mGFaJhGt9Ts1dASxdjkaZ3FEwO1yn+/ToxzODkY6u7LIpkFE0lR7rPfmWN579Q1R
rm4+saCQDNWH8vJS7ToRAz0oheO/k2x9Om3Z1L1fTsYY4U5AIHVJ
-----END RSA PRIVATE KEY-----
Loading

0 comments on commit 69f76e1

Please sign in to comment.