-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Organize migrations for multiple backends (#134)
* re-estructured migrations * change workflow * change workflow again * fix readme link and fix readme explanation to setup `PostgreSQL` * modify `diesel_cli` install * I believe this does the same thing as postgreSQL initial setup * implementing postgres migrations programatically * add some logs in example * sqlite migrations
- Loading branch information
Showing
24 changed files
with
233 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ Cargo.lock | |
src/schema.rs | ||
docs/content/docs/CHANGELOG.md | ||
docs/content/docs/README.md | ||
fang.db |
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 |
---|---|---|
@@ -1,15 +1,41 @@ | ||
db: | ||
db_postgres: | ||
docker run --rm -d --name postgres -p 5432:5432 \ | ||
-e POSTGRES_DB=fang \ | ||
-e POSTGRES_USER=postgres \ | ||
-e POSTGRES_PASSWORD=postgres \ | ||
postgres:latest | ||
|
||
# login is root fang | ||
db_mysql: | ||
docker run --rm -d --name mysql -p 3306:3306 \ | ||
-e MYSQL_DATABASE=fang \ | ||
-e MYSQL_ROOT_PASSWORD=fang \ | ||
-e TZ=UTC \ | ||
mysql:latest | ||
|
||
db_sqlite: | ||
sqlite3 fang.db "VACUUM;" | ||
|
||
clippy: | ||
cargo clippy --all-features | ||
diesel: | ||
cd fang && DATABASE_URL=postgres://postgres:postgres@localhost/fang diesel migration run | ||
stop: | ||
cargo clippy --verbose --all-targets --all-features -- -D warnings | ||
|
||
diesel_sqlite: | ||
cd fang/sqlite_migrations && DATABASE_URL=sqlite://../../fang.db diesel migration run | ||
|
||
diesel_postgres: | ||
cd fang/postgres_migrations && DATABASE_URL=postgres://postgres:postgres@localhost/fang diesel migration run | ||
|
||
diesel_mysql: | ||
cd fang/mysql_migrations && DATABASE_URL=mysql://root:[email protected]/fang diesel migration run | ||
|
||
stop_mysql: | ||
docker kill mysql | ||
|
||
stop_postgres: | ||
docker kill postgres | ||
|
||
stop_sqlite: | ||
rm fang.db | ||
tests: | ||
DATABASE_URL=postgres://postgres:postgres@localhost/fang cargo test --all-features -- --color always --nocapture | ||
|
||
|
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 was deleted.
Oops, something went wrong.
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
2 changes: 2 additions & 0 deletions
2
fang/mysql_migrations/migrations/2023-08-17-102017_create_fang_tasks/down.sql
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,2 @@ | ||
-- This file should undo anything in `up.sql` | ||
DROP TABLE fang_tasks; |
22 changes: 22 additions & 0 deletions
22
fang/mysql_migrations/migrations/2023-08-17-102017_create_fang_tasks/up.sql
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,22 @@ | ||
-- Your SQL goes here | ||
|
||
|
||
-- docker exec -ti mysql mysql -u root -pfang -P 3360 fang -e "$(catn fang/mysql_migrations/migrations/2023-08-17-102017_create_fang_tasks/up.sql)" | ||
|
||
CREATE TABLE fang_tasks ( | ||
id VARCHAR(36) DEFAULT (uuid()) PRIMARY KEY, | ||
metadata JSON NOT NULL, | ||
error_message TEXT, | ||
state ENUM('new', 'in_progress', 'failed', 'finished', 'retried') NOT NULL DEFAULT 'new', | ||
task_type VARCHAR(255) NOT NULL DEFAULT 'common', -- TEXT type can not have default value, stupid MySQL policy | ||
uniq_hash CHAR(64), | ||
retries INTEGER NOT NULL DEFAULT 0, | ||
scheduled_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
); | ||
|
||
CREATE INDEX fang_tasks_state_index ON fang_tasks(state); | ||
CREATE INDEX fang_tasks_type_index ON fang_tasks(task_type); | ||
CREATE INDEX fang_tasks_scheduled_at_index ON fang_tasks(scheduled_at); | ||
CREATE INDEX fang_tasks_uniq_hash ON fang_tasks(uniq_hash); |
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,2 @@ | ||
[print_schema] | ||
file = "../src/blocking/postgres_schema.rs" |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,2 @@ | ||
[print_schema] | ||
file = "../src/blocking/sqlite_schema.rs" |
2 changes: 2 additions & 0 deletions
2
fang/sqlite_migrations/migrations/2023-08-17-102017_create_fang_tasks/down.sql
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,2 @@ | ||
-- This file should undo anything in `up.sql` | ||
DROP TABLE fang_tasks; |
28 changes: 28 additions & 0 deletions
28
fang/sqlite_migrations/migrations/2023-08-17-102017_create_fang_tasks/up.sql
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,28 @@ | ||
-- Your SQL goes here | ||
|
||
|
||
-- docker exec -ti mysql mysql -u root -pfang -P 3360 fang -e "$(catn fang/mysql_migrations/migrations/2023-08-17-102017_create_fang_tasks/up.sql)" | ||
|
||
CREATE TABLE fang_tasks ( | ||
id TEXT CHECK (LENGTH(id) = 36) NOT NULL PRIMARY KEY, -- UUID generated inside the language | ||
-- why uuid is a text ? https://stackoverflow.com/questions/17277735/using-uuids-in-sqlite | ||
metadata TEXT NOT NULL, | ||
-- why metadata is text ? https://stackoverflow.com/questions/16603621/how-to-store-json-object-in-sqlite-database#16603687 | ||
error_message TEXT, | ||
state TEXT CHECK ( state IN ('new', 'in_progress', 'failed', 'finished', 'retried') ) NOT NULL DEFAULT 'new', | ||
-- why state is a text ? https://stackoverflow.com/questions/5299267/how-to-create-enum-type-in-sqlite#17203007 | ||
task_type TEXT NOT NULL DEFAULT 'common', | ||
uniq_hash CHAR(64), | ||
retries INTEGER NOT NULL DEFAULT 0, | ||
-- The datetime() function returns the date and time as text in this formats: YYYY-MM-DD HH:MM:SS. | ||
-- https://www.sqlite.org/lang_datefunc.html | ||
scheduled_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
-- why timestamps are texts ? https://www.sqlite.org/datatype3.html | ||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
); | ||
|
||
CREATE INDEX fang_tasks_state_index ON fang_tasks(state); | ||
CREATE INDEX fang_tasks_type_index ON fang_tasks(task_type); | ||
CREATE INDEX fang_tasks_scheduled_at_index ON fang_tasks(scheduled_at); | ||
CREATE INDEX fang_tasks_uniq_hash ON fang_tasks(uniq_hash); |
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 |
---|---|---|
@@ -1,12 +1,14 @@ | ||
mod error; | ||
pub mod mysql_schema; | ||
pub mod postgres_schema; | ||
pub mod queue; | ||
pub mod runnable; | ||
pub mod schema; | ||
pub mod sqlite_schema; | ||
pub mod worker; | ||
pub mod worker_pool; | ||
|
||
pub use postgres_schema::*; | ||
pub use queue::*; | ||
pub use runnable::Runnable; | ||
pub use schema::*; | ||
pub use worker::*; | ||
pub use worker_pool::*; |
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,29 @@ | ||
// @generated automatically by Diesel CLI. | ||
|
||
pub mod sql_types { | ||
#[derive(diesel::sql_types::SqlType)] | ||
#[diesel(mysql_type(name = "Enum"))] | ||
pub struct FangTasksStateEnum; | ||
} | ||
|
||
diesel::table! { | ||
use diesel::sql_types::*; | ||
use super::sql_types::FangTasksStateEnum; | ||
|
||
fang_tasks (id) { | ||
#[max_length = 36] | ||
id -> Varchar, | ||
metadata -> Json, | ||
error_message -> Nullable<Text>, | ||
#[max_length = 11] | ||
state -> FangTasksStateEnum, | ||
#[max_length = 255] | ||
task_type -> Varchar, | ||
#[max_length = 64] | ||
uniq_hash -> Nullable<Char>, | ||
retries -> Integer, | ||
scheduled_at -> Timestamp, | ||
created_at -> Timestamp, | ||
updated_at -> Timestamp, | ||
} | ||
} |
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,16 @@ | ||
// @generated automatically by Diesel CLI. | ||
|
||
diesel::table! { | ||
fang_tasks (id) { | ||
id -> Text, | ||
metadata -> Text, | ||
error_message -> Nullable<Text>, | ||
state -> Text, | ||
task_type -> Text, | ||
uniq_hash -> Nullable<Text>, | ||
retries -> Integer, | ||
scheduled_at -> Timestamp, | ||
created_at -> Timestamp, | ||
updated_at -> Timestamp, | ||
} | ||
} |
Oops, something went wrong.