From 58fd137ca4c48e4e10ebbf30cf882f0855afe2fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Gle=C5=84?= <33288445+karmek-k@users.noreply.github.com> Date: Mon, 21 Jun 2021 12:29:39 +0200 Subject: [PATCH 01/10] Don't use SQLite anymore in .env --- .env | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.env b/.env index 4abc61a..7fa5704 100644 --- a/.env +++ b/.env @@ -26,7 +26,7 @@ APP_SECRET=80021d294317eab0437313a51a05d31b # Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url # IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml # -DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db" +# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db" # DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=5.7" -# DATABASE_URL="postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=13&charset=utf8" +DATABASE_URL="postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=13&charset=utf8" ###< doctrine/doctrine-bundle ### From 486e7a7193c63e59841d22ba5c330fe626feaf08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Gle=C5=84?= <33288445+karmek-k@users.noreply.github.com> Date: Mon, 21 Jun 2021 12:29:57 +0200 Subject: [PATCH 02/10] Delete old migrations --- migrations/Version20210610161354.php | 33 --------------- migrations/Version20210611132919.php | 31 -------------- migrations/Version20210611141027.php | 36 ---------------- migrations/Version20210615102516.php | 61 ---------------------------- migrations/Version20210621102752.php | 54 ++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 161 deletions(-) delete mode 100644 migrations/Version20210610161354.php delete mode 100644 migrations/Version20210611132919.php delete mode 100644 migrations/Version20210611141027.php delete mode 100644 migrations/Version20210615102516.php create mode 100644 migrations/Version20210621102752.php diff --git a/migrations/Version20210610161354.php b/migrations/Version20210610161354.php deleted file mode 100644 index 132115c..0000000 --- a/migrations/Version20210610161354.php +++ /dev/null @@ -1,33 +0,0 @@ -addSql('CREATE TABLE "user" (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username VARCHAR(180) NOT NULL, roles CLOB NOT NULL --(DC2Type:json) - , password VARCHAR(255) NOT NULL)'); - $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649F85E0677 ON "user" (username)'); - } - - public function down(Schema $schema): void - { - // this down() migration is auto-generated, please modify it to your needs - $this->addSql('DROP TABLE "user"'); - } -} diff --git a/migrations/Version20210611132919.php b/migrations/Version20210611132919.php deleted file mode 100644 index 0304c0a..0000000 --- a/migrations/Version20210611132919.php +++ /dev/null @@ -1,31 +0,0 @@ -addSql('CREATE TABLE feed_source (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR(255) NOT NULL, url CLOB NOT NULL)'); - } - - public function down(Schema $schema): void - { - // this down() migration is auto-generated, please modify it to your needs - $this->addSql('DROP TABLE feed_source'); - } -} diff --git a/migrations/Version20210611141027.php b/migrations/Version20210611141027.php deleted file mode 100644 index fd5cff7..0000000 --- a/migrations/Version20210611141027.php +++ /dev/null @@ -1,36 +0,0 @@ -addSql('CREATE TABLE article (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, source_id INTEGER NOT NULL, title VARCHAR(255) NOT NULL, url CLOB NOT NULL, excerpt CLOB NOT NULL)'); - $this->addSql('CREATE INDEX IDX_23A0E66953C1C61 ON article (source_id)'); - $this->addSql('CREATE TABLE user_feed_source (user_id INTEGER NOT NULL, feed_source_id INTEGER NOT NULL, PRIMARY KEY(user_id, feed_source_id))'); - $this->addSql('CREATE INDEX IDX_34B3C612A76ED395 ON user_feed_source (user_id)'); - $this->addSql('CREATE INDEX IDX_34B3C612DDAEFFBD ON user_feed_source (feed_source_id)'); - } - - public function down(Schema $schema): void - { - // this down() migration is auto-generated, please modify it to your needs - $this->addSql('DROP TABLE article'); - $this->addSql('DROP TABLE user_feed_source'); - } -} diff --git a/migrations/Version20210615102516.php b/migrations/Version20210615102516.php deleted file mode 100644 index a9245ff..0000000 --- a/migrations/Version20210615102516.php +++ /dev/null @@ -1,61 +0,0 @@ -addSql('DROP INDEX IDX_23A0E66953C1C61'); - $this->addSql('CREATE TEMPORARY TABLE __temp__article AS SELECT id, source_id, title, url, excerpt FROM article'); - $this->addSql('DROP TABLE article'); - $this->addSql('CREATE TABLE article (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, source_id INTEGER NOT NULL, title VARCHAR(255) NOT NULL COLLATE BINARY, url CLOB NOT NULL COLLATE BINARY, excerpt CLOB NOT NULL COLLATE BINARY, last_modified DATETIME DEFAULT NULL, CONSTRAINT FK_23A0E66953C1C61 FOREIGN KEY (source_id) REFERENCES feed_source (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); - $this->addSql('INSERT INTO article (id, source_id, title, url, excerpt) SELECT id, source_id, title, url, excerpt FROM __temp__article'); - $this->addSql('DROP TABLE __temp__article'); - $this->addSql('CREATE INDEX IDX_23A0E66953C1C61 ON article (source_id)'); - $this->addSql('DROP INDEX IDX_34B3C612DDAEFFBD'); - $this->addSql('DROP INDEX IDX_34B3C612A76ED395'); - $this->addSql('CREATE TEMPORARY TABLE __temp__user_feed_source AS SELECT user_id, feed_source_id FROM user_feed_source'); - $this->addSql('DROP TABLE user_feed_source'); - $this->addSql('CREATE TABLE user_feed_source (user_id INTEGER NOT NULL, feed_source_id INTEGER NOT NULL, PRIMARY KEY(user_id, feed_source_id), CONSTRAINT FK_34B3C612A76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_34B3C612DDAEFFBD FOREIGN KEY (feed_source_id) REFERENCES feed_source (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE)'); - $this->addSql('INSERT INTO user_feed_source (user_id, feed_source_id) SELECT user_id, feed_source_id FROM __temp__user_feed_source'); - $this->addSql('DROP TABLE __temp__user_feed_source'); - $this->addSql('CREATE INDEX IDX_34B3C612DDAEFFBD ON user_feed_source (feed_source_id)'); - $this->addSql('CREATE INDEX IDX_34B3C612A76ED395 ON user_feed_source (user_id)'); - } - - public function down(Schema $schema): void - { - // this down() migration is auto-generated, please modify it to your needs - $this->addSql('DROP INDEX IDX_23A0E66953C1C61'); - $this->addSql('CREATE TEMPORARY TABLE __temp__article AS SELECT id, source_id, title, url, excerpt FROM article'); - $this->addSql('DROP TABLE article'); - $this->addSql('CREATE TABLE article (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, source_id INTEGER NOT NULL, title VARCHAR(255) NOT NULL, url CLOB NOT NULL, excerpt CLOB NOT NULL)'); - $this->addSql('INSERT INTO article (id, source_id, title, url, excerpt) SELECT id, source_id, title, url, excerpt FROM __temp__article'); - $this->addSql('DROP TABLE __temp__article'); - $this->addSql('CREATE INDEX IDX_23A0E66953C1C61 ON article (source_id)'); - $this->addSql('DROP INDEX IDX_34B3C612A76ED395'); - $this->addSql('DROP INDEX IDX_34B3C612DDAEFFBD'); - $this->addSql('CREATE TEMPORARY TABLE __temp__user_feed_source AS SELECT user_id, feed_source_id FROM user_feed_source'); - $this->addSql('DROP TABLE user_feed_source'); - $this->addSql('CREATE TABLE user_feed_source (user_id INTEGER NOT NULL, feed_source_id INTEGER NOT NULL, PRIMARY KEY(user_id, feed_source_id))'); - $this->addSql('INSERT INTO user_feed_source (user_id, feed_source_id) SELECT user_id, feed_source_id FROM __temp__user_feed_source'); - $this->addSql('DROP TABLE __temp__user_feed_source'); - $this->addSql('CREATE INDEX IDX_34B3C612A76ED395 ON user_feed_source (user_id)'); - $this->addSql('CREATE INDEX IDX_34B3C612DDAEFFBD ON user_feed_source (feed_source_id)'); - } -} diff --git a/migrations/Version20210621102752.php b/migrations/Version20210621102752.php new file mode 100644 index 0000000..a5b5f95 --- /dev/null +++ b/migrations/Version20210621102752.php @@ -0,0 +1,54 @@ +addSql('CREATE SEQUENCE article_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE SEQUENCE feed_source_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE SEQUENCE "user_id_seq" INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE TABLE article (id INT NOT NULL, source_id INT NOT NULL, title VARCHAR(255) NOT NULL, url TEXT NOT NULL, excerpt TEXT NOT NULL, last_modified TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE INDEX IDX_23A0E66953C1C61 ON article (source_id)'); + $this->addSql('CREATE TABLE feed_source (id INT NOT NULL, name VARCHAR(255) NOT NULL, url TEXT NOT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE TABLE "user" (id INT NOT NULL, username VARCHAR(180) NOT NULL, roles JSON NOT NULL, password VARCHAR(255) NOT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649F85E0677 ON "user" (username)'); + $this->addSql('CREATE TABLE user_feed_source (user_id INT NOT NULL, feed_source_id INT NOT NULL, PRIMARY KEY(user_id, feed_source_id))'); + $this->addSql('CREATE INDEX IDX_34B3C612A76ED395 ON user_feed_source (user_id)'); + $this->addSql('CREATE INDEX IDX_34B3C612DDAEFFBD ON user_feed_source (feed_source_id)'); + $this->addSql('ALTER TABLE article ADD CONSTRAINT FK_23A0E66953C1C61 FOREIGN KEY (source_id) REFERENCES feed_source (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE user_feed_source ADD CONSTRAINT FK_34B3C612A76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE user_feed_source ADD CONSTRAINT FK_34B3C612DDAEFFBD FOREIGN KEY (feed_source_id) REFERENCES feed_source (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('ALTER TABLE article DROP CONSTRAINT FK_23A0E66953C1C61'); + $this->addSql('ALTER TABLE user_feed_source DROP CONSTRAINT FK_34B3C612DDAEFFBD'); + $this->addSql('ALTER TABLE user_feed_source DROP CONSTRAINT FK_34B3C612A76ED395'); + $this->addSql('DROP SEQUENCE article_id_seq CASCADE'); + $this->addSql('DROP SEQUENCE feed_source_id_seq CASCADE'); + $this->addSql('DROP SEQUENCE "user_id_seq" CASCADE'); + $this->addSql('DROP TABLE article'); + $this->addSql('DROP TABLE feed_source'); + $this->addSql('DROP TABLE "user"'); + $this->addSql('DROP TABLE user_feed_source'); + } +} From 0b0b350286f06cbb5ffe416be3e7eff1fbbf939d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Gle=C5=84?= <33288445+karmek-k@users.noreply.github.com> Date: Mon, 21 Jun 2021 12:30:57 +0200 Subject: [PATCH 03/10] Create database init script --- init_db.sh | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 init_db.sh diff --git a/init_db.sh b/init_db.sh new file mode 100644 index 0000000..ac00739 --- /dev/null +++ b/init_db.sh @@ -0,0 +1,2 @@ +php bin/console doctrine:database:create +php bin/console doctrine:migrations:migrate -n From 8f220b2c5c7ebfa86b8636631ba7a89ffb72c7a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Gle=C5=84?= <33288445+karmek-k@users.noreply.github.com> Date: Mon, 21 Jun 2021 15:54:54 +0200 Subject: [PATCH 04/10] Add PostgreSQL PDO to docker --- Dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dockerfile b/Dockerfile index f9b31e1..7f9bf4d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,5 +17,7 @@ FROM php:8.0-fpm WORKDIR /app COPY --from=composer /app . +RUN apt-get update && apt-get install -y libpq-dev +RUN docker-php-ext-install pgsql pdo pdo_pgsql ENV APP_ENV=prod EXPOSE 9000 From e575a8c0033a135c658fe183b063f68a105c9de7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Gle=C5=84?= <33288445+karmek-k@users.noreply.github.com> Date: Mon, 21 Jun 2021 16:38:59 +0200 Subject: [PATCH 05/10] Remove database creation from init_db.sh --- init_db.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init_db.sh b/init_db.sh index ac00739..e701dfe 100644 --- a/init_db.sh +++ b/init_db.sh @@ -1,2 +1,2 @@ -php bin/console doctrine:database:create +# php bin/console doctrine:database:create php bin/console doctrine:migrations:migrate -n From 637fa1edf735ac1da59f5c8fde39335c1428ce29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Gle=C5=84?= <33288445+karmek-k@users.noreply.github.com> Date: Mon, 21 Jun 2021 16:39:19 +0200 Subject: [PATCH 06/10] Add docker-compose --- docker-compose.yaml | 42 ++++++++++++++++++++++++++++++++++++++++++ server/tinyreader.conf | 8 ++++---- 2 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 docker-compose.yaml diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..c7353d3 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,42 @@ +# Don't use this file in production. + +version: '3.8' + +services: + database: + image: postgres:13 + volumes: + - db_data:/var/lib/postgres/data + environment: + POSTGRES_USER: tinyreader + POSTGRES_PASSWORD: '12345' + POSTGRES_DB: tinyreader + ports: + - '5432' + + app: + build: . + depends_on: + - database + environment: + DATABASE_URL: 'postgresql://tinyreader:12345@database:5432/tinyreader?serverVersion=13&charset=utf8' + ports: + - 9000:9000 + volumes: + - tr_build:/app + + server: + image: nginx:1.21 + depends_on: + - app + volumes: + - tr_build:/var/www/tinyreader + - ./server:/etc/nginx/conf.d + ports: + - 8000:80 + expose: + - '8000' + +volumes: + db_data: + tr_build: diff --git a/server/tinyreader.conf b/server/tinyreader.conf index 6023131..69aed2c 100644 --- a/server/tinyreader.conf +++ b/server/tinyreader.conf @@ -13,12 +13,12 @@ server { # optionally disable falling back to PHP script for the asset directories; # nginx will return a 404 error when files are not found instead of passing the # request to Symfony (improves performance but Symfony's 404 page is not displayed) - # location /bundles { - # try_files $uri =404; - # } + location /build { + try_files $uri =404; + } location ~ ^/index\.php(/|$) { - fastcgi_pass 127.0.0.1:9000; + fastcgi_pass app:9000; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; From c5dbda068efd8f78f8fcde66b559de4a6171f8d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Gle=C5=84?= <33288445+karmek-k@users.noreply.github.com> Date: Mon, 21 Jun 2021 16:50:51 +0200 Subject: [PATCH 07/10] Make db scripts use docker-compose --- create_user.sh | 1 + init_db.sh | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100755 create_user.sh mode change 100644 => 100755 init_db.sh diff --git a/create_user.sh b/create_user.sh new file mode 100755 index 0000000..8b08cc7 --- /dev/null +++ b/create_user.sh @@ -0,0 +1 @@ +docker-compose exec app bash -c "php bin/console tr:user:create $1" \ No newline at end of file diff --git a/init_db.sh b/init_db.sh old mode 100644 new mode 100755 index e701dfe..d46aa5b --- a/init_db.sh +++ b/init_db.sh @@ -1,2 +1 @@ -# php bin/console doctrine:database:create -php bin/console doctrine:migrations:migrate -n +docker-compose exec app bash -c "php bin/console doctrine:migrations:migrate -n" \ No newline at end of file From 5093201788630476e9483a88d67b0c6195c8b986 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Gle=C5=84?= <33288445+karmek-k@users.noreply.github.com> Date: Mon, 21 Jun 2021 16:53:50 +0200 Subject: [PATCH 08/10] Add docs for docker-compose --- README.md | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8253336..c2089cc 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,37 @@ ## Installation -### Docker (PHP-FPM) +### Docker Compose + +Build and launch docker-compose: + +``` +docker-compose up +``` + +You are not able right now to use the database, as there are no tables. +Migrate the database using the `init_db.sh` script **while `docker-compose` is running**: + +``` +chmod +x init_db.sh +./init_db.sh +``` + +Create a user: + +```bash +chmod +x create_user.sh + +# normal user +./create_user.sh + +# admin user +./create_user.sh -a +``` + +Now you should be able to see the login page at `http://localhost:8000`. + + From 4c436d4969544b99ab35d16d1ed8261cd09e113a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Gle=C5=84?= <33288445+karmek-k@users.noreply.github.com> Date: Tue, 22 Jun 2021 12:00:20 +0200 Subject: [PATCH 09/10] Fix nginx not serving static files --- server/tinyreader.conf | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/server/tinyreader.conf b/server/tinyreader.conf index 69aed2c..27c0e5a 100644 --- a/server/tinyreader.conf +++ b/server/tinyreader.conf @@ -14,7 +14,12 @@ server { # nginx will return a 404 error when files are not found instead of passing the # request to Symfony (improves performance but Symfony's 404 page is not displayed) location /build { - try_files $uri =404; + # try_files $uri =404; + root /var/www/tinyreader/public; + } + + location /bundles { + root /var/www/tinyreader/public; } location ~ ^/index\.php(/|$) { From 3fd82d5a1b7613636b896e49a2ffc41d933a2167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Gle=C5=84?= <33288445+karmek-k@users.noreply.github.com> Date: Tue, 22 Jun 2021 12:18:30 +0200 Subject: [PATCH 10/10] Prepare docker-compose for production use --- .gitignore | 2 ++ README.md | 8 +++++++- db.example.env | 5 +++++ docker-compose.yaml | 13 +++++-------- 4 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 db.example.env diff --git a/.gitignore b/.gitignore index 61ba0fe..0b4f31a 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,5 @@ npm-debug.log yarn-error.log ###< symfony/webpack-encore-bundle ### + +db.env diff --git a/README.md b/README.md index c2089cc..d92fbf8 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,19 @@ ### Docker Compose +Create the `db.env` from a template and edit it: + +``` +cp db.example.env db.env +``` + Build and launch docker-compose: ``` docker-compose up ``` -You are not able right now to use the database, as there are no tables. +You are not able to use the database right now, as it has not been migrated. Migrate the database using the `init_db.sh` script **while `docker-compose` is running**: ``` diff --git a/db.example.env b/db.example.env new file mode 100644 index 0000000..17d44bf --- /dev/null +++ b/db.example.env @@ -0,0 +1,5 @@ +POSTGRES_USER=tinyreader +POSTGRES_PASSWORD="12345" +POSTGRES_DB=tinyreader +DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:5432/${POSTGRES_DB}?serverVersion=13&charset=utf8" +APP_SECRET="keyboard cat" diff --git a/docker-compose.yaml b/docker-compose.yaml index c7353d3..573aa18 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,5 +1,3 @@ -# Don't use this file in production. - version: '3.8' services: @@ -7,19 +5,18 @@ services: image: postgres:13 volumes: - db_data:/var/lib/postgres/data - environment: - POSTGRES_USER: tinyreader - POSTGRES_PASSWORD: '12345' - POSTGRES_DB: tinyreader + env_file: + - db.env ports: - '5432' + restart: always app: build: . depends_on: - database - environment: - DATABASE_URL: 'postgresql://tinyreader:12345@database:5432/tinyreader?serverVersion=13&charset=utf8' + env_file: + - db.env ports: - 9000:9000 volumes: