From 2fcdf3c5e6de4fc82d7a78d0a6391afb3f70a61d Mon Sep 17 00:00:00 2001 From: arttu Date: Fri, 5 Apr 2024 13:45:45 +0300 Subject: [PATCH] Add db container and restore script & instructions --- .gitignore | 1 + README.md | 18 ++++++++++++++++-- data/restore_and_create_user.sql | 14 ++++++++++++++ docker-compose.dev.yml | 15 +++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 data/restore_and_create_user.sql diff --git a/.gitignore b/.gitignore index 3c3629e6..edbf7f22 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +data/sqlfiles/* \ No newline at end of file diff --git a/README.md b/README.md index 67b3b1f6..96468d5b 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,22 @@ Rebuild of a fossil database application, old version at https://nowdatabase.luo New version not deployed yet. -### Developer instructions +## Developer instructions Run `npm install` in all three folders: Project root, frontend, and backend. -If you have docker compose installed, run `npm run dev` in root to launch docker in development mode. Then, open localhost:5173 in browser. +If you have docker compose installed, run `npm run dev` in root to launch docker in development mode. Then, open `localhost:5173` in browser. + +### Restoring db from dump + +Make an empty directory `sqlfiles` inside `data` folder which is in the repository root. Then, put the database dump files `now_test.sql`, `now_log_test.sql` and `now_view.sql` inside it. + +The initialization file (`restore_and_create.sql`) will then restore the database from these files when the container is first run, and also creates a user `'now_test'@'localhost'` with password `mariadb_password` for the database. The root users password will be `admin`. + +**Test that it worked:** + +`docker exec -it nowdb-db mariadb -u now_test -p`. Enter the password `mariadb_password`. + +Select database with `USE now_test;`. + +Execute query: `select loc_name from now_loc where loc_name like "amb%";` which should print 9 rows if everything went right. \ No newline at end of file diff --git a/data/restore_and_create_user.sql b/data/restore_and_create_user.sql new file mode 100644 index 00000000..c4882444 --- /dev/null +++ b/data/restore_and_create_user.sql @@ -0,0 +1,14 @@ + +DROP DATABASE IF EXISTS now_test; CREATE DATABASE now_test CHARACTER SET latin1; +DROP DATABASE IF EXISTS now_log_test; CREATE DATABASE now_log_test CHARACTER SET latin1; +SET names 'latin1'; +USE now_test +SOURCE /docker-entrypoint-initdb.d/sqlfiles/now_test.sql +USE now_log_test +SOURCE /docker-entrypoint-initdb.d/sqlfiles/now_log_test.sql +USE now_test +SOURCE /docker-entrypoint-initdb.d/sqlfiles/now_view.sql + +CREATE USER 'now_test'@'localhost' IDENTIFIED BY 'mariadb_password'; +GRANT SELECT, INSERT, UPDATE ON `log_test`.* TO 'now_test'@'localhost'; +GRANT SELECT, INSERT, UPDATE, DELETE ON `now_test`.* TO 'now_test'@'localhost'; diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 74b138c0..75989750 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -17,3 +17,18 @@ services: - 4000:4000 command: > sh -c "cd /usr/src/app && npm ci && npm run dev" + now-db: + container_name: nowdb-db + image: mariadb + environment: + MARIADB_ROOT_PASSWORD: admin + MARIADB_HOST: localhost + MARIADB_DATABASE: now_test + MARIADB_LOG_DATABASE: now_log_test.log + MARIADB_USER: now_test + MARIADB_PASSWORD: mariadb_password + volumes: + - ./data/:/docker-entrypoint-initdb.d +volumes: + nowdb-db: +