From fd34f7ba42e2ce75151a3ce686fdde1b3cf3fc0e Mon Sep 17 00:00:00 2001 From: tamsin johnson Date: Wed, 24 Jan 2024 22:56:58 -0800 Subject: [PATCH] lets-go:4.3 setup a mariadb container and test data --- snippetbox/.env | 4 ++++ snippetbox/db/initdb.d/init.sql | 14 ++++++++++++++ snippetbox/db/initdb.d/seed.sql | 20 ++++++++++++++++++++ snippetbox/docker-compose.yml | 14 ++++++++++++++ snippetbox/go.mod | 2 ++ snippetbox/go.sum | 2 ++ 6 files changed, 56 insertions(+) create mode 100644 snippetbox/.env create mode 100644 snippetbox/db/initdb.d/init.sql create mode 100644 snippetbox/db/initdb.d/seed.sql create mode 100644 snippetbox/docker-compose.yml create mode 100644 snippetbox/go.sum diff --git a/snippetbox/.env b/snippetbox/.env new file mode 100644 index 0000000..be4be9a --- /dev/null +++ b/snippetbox/.env @@ -0,0 +1,4 @@ +MARIADB_DATABASE=snippetbox +MARIADB_PASSWORD=dbpass +MARIADB_RANDOM_ROOT_PASSWORD=1 +MARIADB_USER=snippetbox \ No newline at end of file diff --git a/snippetbox/db/initdb.d/init.sql b/snippetbox/db/initdb.d/init.sql new file mode 100644 index 0000000..69550c4 --- /dev/null +++ b/snippetbox/db/initdb.d/init.sql @@ -0,0 +1,14 @@ +USE snippetbox; + +CREATE TABLE snippets ( + id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, + title VARCHAR(100) NOT NULL, + content TEXT NOT NULL, + created DATETIME NOT NULL, + expires DATETIME NOT NULL +); + +CREATE USER 'web'@'localhost'; +GRANT SELECT, INSERT, UPDATE, DELETE ON snippetbox.* TO 'web'@'localhost' IDENTIFIED BY 'dbpass'; + +CREATE INDEX idx_snippets_created ON snippets(created); diff --git a/snippetbox/db/initdb.d/seed.sql b/snippetbox/db/initdb.d/seed.sql new file mode 100644 index 0000000..057c785 --- /dev/null +++ b/snippetbox/db/initdb.d/seed.sql @@ -0,0 +1,20 @@ +INSERT INTO snippets (title, content, created, expires) VALUES ( + 'An old silent pond', + 'An old silent pond...\nA frog jumps into the pont,\nsplash! Silence again.\n\n- Masuo Bashō', + UTC_TIMESTAMP(), + DATE_ADD(UTC_TIMESTAMP(), INTERVAL 365 DAY) +); + +INSERT INTO snippets (title, content, created, expires) VALUES ( + 'Over the wintry forest', + 'Over the wintry\nforest, winds howl in rage\nwith no leaves to blow.\n\n- Natsume Soseki', + UTC_TIMESTAMP(), + DATE_ADD(UTC_TIMESTAMP(), INTERVAL 365 DAY) +); + +INSERT INTO snippets (title, content, created, expires) VALUES ( + 'First autumn morning', + 'First autumn morning\nthe mirror I stare into\nshows my father''s face.\n\n- Murakami Kijo', + UTC_TIMESTAMP(), + DATE_ADD(UTC_TIMESTAMP(), INTERVAL 7 DAY) +); diff --git a/snippetbox/docker-compose.yml b/snippetbox/docker-compose.yml new file mode 100644 index 0000000..5ff274f --- /dev/null +++ b/snippetbox/docker-compose.yml @@ -0,0 +1,14 @@ +version: "3.7" + +services: + db: + image: mariadb + env_file: .env + volumes: + - mysql_data:/var/lib/mysql + - ./db/initdb.d:/docker-entrypoint-initdb.d + ports: + - "3306:3306" + +volumes: + mysql_data: diff --git a/snippetbox/go.mod b/snippetbox/go.mod index ed1cbc8..81ca862 100644 --- a/snippetbox/go.mod +++ b/snippetbox/go.mod @@ -1,3 +1,5 @@ module snippetbox.chaosfem.tw go 1.21.4 + +require github.com/go-sql-driver/mysql v1.7.1 // indirect diff --git a/snippetbox/go.sum b/snippetbox/go.sum new file mode 100644 index 0000000..fd7ae07 --- /dev/null +++ b/snippetbox/go.sum @@ -0,0 +1,2 @@ +github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= +github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=