more migrations
This commit is contained in:
32
rust/migrations/20230820094036_add_user_to_inventory.sql
Normal file
32
rust/migrations/20230820094036_add_user_to_inventory.sql
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
CREATE TABLE "inventory_items_tmp" (
|
||||||
|
id VARCHAR(36) NOT NULL,
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
description TEXT,
|
||||||
|
weight INTEGER NOT NULL,
|
||||||
|
category_id VARCHAR(36) NOT NULL,
|
||||||
|
product_id VARCHAR(36),
|
||||||
|
user_id VARCHAR(36) NOT NULL,
|
||||||
|
PRIMARY KEY (id),
|
||||||
|
FOREIGN KEY (category_id) REFERENCES inventory_items_categories(id),
|
||||||
|
FOREIGN KEY (product_id) REFERENCES inventory_products(id),
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE "inventory_items_categories_tmp" (
|
||||||
|
id VARCHAR(36) NOT NULL,
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
description TEXT,
|
||||||
|
user_id VARCHAR(36) NOT NULL,
|
||||||
|
PRIMARY KEY (id),
|
||||||
|
UNIQUE (name),
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO inventory_items_tmp SELECT *, (SELECT id FROM users LIMIT 1) as user_id FROM inventory_items;
|
||||||
|
INSERT INTO inventory_items_categories_tmp SELECT *, (SELECT id FROM users LIMIT 1) as user_id FROM inventory_items_categories;
|
||||||
|
|
||||||
|
DROP TABLE inventory_items;
|
||||||
|
DROP TABLE inventory_items_categories;
|
||||||
|
|
||||||
|
ALTER TABLE inventory_items_tmp RENAME TO inventory_items;
|
||||||
|
ALTER TABLE inventory_items_categories_tmp RENAME TO inventory_items_categories;
|
||||||
@@ -82,15 +82,15 @@ async fn main() -> MainResult {
|
|||||||
.with_max_level(tracing::Level::DEBUG)
|
.with_max_level(tracing::Level::DEBUG)
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
|
if let Err(e) = sqlite::migrate(&args.database_url).await {
|
||||||
|
return <_ as Into<Error>>::into(e).into();
|
||||||
|
}
|
||||||
|
|
||||||
let database_pool = match sqlite::init_database_pool(&args.database_url).await {
|
let database_pool = match sqlite::init_database_pool(&args.database_url).await {
|
||||||
Ok(pool) => pool,
|
Ok(pool) => pool,
|
||||||
Err(e) => return <_ as Into<Error>>::into(e).into(),
|
Err(e) => return <_ as Into<Error>>::into(e).into(),
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(e) = sqlite::migrate(&database_pool).await {
|
|
||||||
return <_ as Into<Error>>::into(e).into();
|
|
||||||
}
|
|
||||||
|
|
||||||
let state = AppState {
|
let state = AppState {
|
||||||
database_pool,
|
database_pool,
|
||||||
client_state: ClientState::new(),
|
client_state: ClientState::new(),
|
||||||
@@ -159,12 +159,7 @@ async fn main() -> MainResult {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
Command::Migrate => {
|
Command::Migrate => {
|
||||||
let database_pool = match sqlite::init_database_pool(&args.database_url).await {
|
if let Err(e) = sqlite::migrate(&args.database_url).await {
|
||||||
Ok(pool) => pool,
|
|
||||||
Err(e) => return <_ as Into<Error>>::into(e).into(),
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Err(e) = sqlite::migrate(&database_pool).await {
|
|
||||||
return <_ as Into<Error>>::into(e).into();
|
return <_ as Into<Error>>::into(e).into();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,12 @@ pub async fn init_database_pool(url: &str) -> Result<Pool<Sqlite>, StartError> {
|
|||||||
.await?)
|
.await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn migrate(pool: &Pool<Sqlite>) -> Result<(), StartError> {
|
pub async fn migrate(url: &str) -> Result<(), StartError> {
|
||||||
sqlx::migrate!().run(pool).await?;
|
let pool = SqlitePoolOptions::new()
|
||||||
|
.max_connections(5)
|
||||||
|
.connect_with(SqliteConnectOptions::from_str(url)?.pragma("foreign_keys", "0"))
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
sqlx::migrate!().run(&pool).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user