Explorar el Código

Update migrations

Dmitriy Gnatenko hace 3 días
padre
commit
ea8f75837a

+ 8 - 19
Makefile

@@ -1,20 +1,6 @@
 include .env
 
-GOOSE_DB_STRING = "user=${DB_USER} password=${DB_PASSWORD} dbname=${DB_NAME} port=${DB_PORT} host=${DB_HOST} sslmode=disable"
-
-usage:
-	@echo "make run"
-	@echo "make test"
-	@echo "make test-cover"
-	@echo "make lint"
-	@echo "make migration-status"
-	@echo "make migration-up"
-	@echo "make migration-down"
-	@echo "make docker-build"
-	@echo "make docker-up"
-	@echo "make docker-down"
-	@echo "make install-deps"
-	@echo "make app-build"
+GOOSE_DB_STRING = ${DB_USER}:${DB_PASSWORD}@tcp\(${DB_HOST}:${DB_PORT}\)/${DB_NAME}
 
 run:
 	cd cmd/app && go run main.go
@@ -33,13 +19,16 @@ lint:
 	golangci-lint run --timeout=3m
 
 migration-status:
-	goose -dir migrations postgres ${GOOSE_DB_STRING} status
+	goose -dir migrations ${DB_DRIVER} ${GOOSE_DB_STRING} status
+
+migration-up-one:
+	goose -dir migrations ${DB_DRIVER} ${GOOSE_DB_STRING} up-by-one
 
 migration-up:
-	goose -dir migrations postgres ${GOOSE_DB_STRING} up
+	goose -dir migrations ${DB_DRIVER} ${GOOSE_DB_STRING} up
 
-migration-down:
-	goose -dir migrations postgres ${GOOSE_DB_STRING} down
+migration-down-one:
+	goose -dir migrations ${DB_DRIVER} ${GOOSE_DB_STRING} down
 
 docker-build:
 	docker compose up --build --detach

+ 15 - 0
migrations/20220914205000_add_language_table.sql

@@ -0,0 +1,15 @@
+-- +goose Up
+CREATE TABLE language
+(
+    id         TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+    url        CHAR(2)          NOT NULL UNIQUE,
+    title      VARCHAR(255)     NOT NULL,
+    is_default BOOL             NOT NULL DEFAULT false
+);
+
+INSERT INTO language (url, title, is_default)
+VALUES ('ru', 'Русский', true),
+       ('en', 'English', false);
+
+-- +goose Down
+DROP TABLE language;

+ 12 - 7
migrations/20220914205500_add_article_table.sql

@@ -1,16 +1,21 @@
 -- +goose Up
 CREATE TABLE article
 (
-    id            SERIAL PRIMARY KEY,
-    url           TEXT      NOT NULL UNIQUE,
-    title         TEXT      NOT NULL,
-    image         TEXT      NOT NULL,
-    text          TEXT      NOT NULL,
+    id            BIGINT UNSIGNED  NOT NULL AUTO_INCREMENT PRIMARY KEY,
+    url           VARCHAR(255)     NOT NULL UNIQUE,
+    title         TEXT             NOT NULL,
+    image         VARCHAR(255)     NOT NULL,
+    text          TEXT             NOT NULL,
     preview_text  TEXT,
     meta_keywords TEXT,
     meta_desc     TEXT,
-    publish_time  TIMESTAMP NOT NULL DEFAULT NOW(),
-    is_active     BOOLEAN   NOT NULL
+    publish_time  TIMESTAMP        NOT NULL,
+    language      TINYINT UNSIGNED NOT NULL,
+    is_active     BOOL             NOT NULL,
+    CONSTRAINT `fk_language`
+        FOREIGN KEY (language) REFERENCES language (id)
+            ON DELETE RESTRICT
+            ON UPDATE RESTRICT
 );
 
 -- +goose Down

+ 3 - 3
migrations/20220914211000_add_tag_table.sql

@@ -1,9 +1,9 @@
 -- +goose Up
 CREATE TABLE tag
 (
-    id  SERIAL PRIMARY KEY,
-    tag TEXT NOT NULL,
-    url TEXT NOT NULL UNIQUE
+    id  INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+    tag VARCHAR(255) NOT NULL,
+    url VARCHAR(255) NOT NULL UNIQUE
 );
 
 -- +goose Down

+ 11 - 7
migrations/20220914212000_add_article_tag_table.sql

@@ -1,14 +1,18 @@
 -- +goose Up
 CREATE TABLE article_tag
 (
-    article_id INT NOT NULL,
-    tag_id     INT NOT NULL,
-    CONSTRAINT fk_article_tag_article FOREIGN KEY (article_id) REFERENCES article (id),
-    CONSTRAINT fk_article_tag_tag FOREIGN KEY (tag_id) REFERENCES tag (id)
+    article_id BIGINT UNSIGNED NOT NULL,
+    tag_id     INT UNSIGNED NOT NULL,
+    UNIQUE (article_id, tag_id),
+    CONSTRAINT `fk_article`
+        FOREIGN KEY (article_id) REFERENCES article (id)
+            ON DELETE RESTRICT
+            ON UPDATE RESTRICT,
+    CONSTRAINT `fk_tag`
+        FOREIGN KEY (tag_id) REFERENCES tag (id)
+            ON DELETE RESTRICT
+            ON UPDATE RESTRICT
 );
 
-CREATE UNIQUE INDEX idx_unique_article_tag ON article_tag (article_id, tag_id);
-CREATE INDEX idx_article_tag_tag ON article_tag (tag_id);
-
 -- +goose Down
 DROP TABLE article_tag;

+ 7 - 7
migrations/20220915102300_add_user_table.sql

@@ -1,12 +1,12 @@
 -- +goose Up
-CREATE TABLE "user"
+CREATE TABLE user
 (
-    id         SERIAL PRIMARY KEY,
-    username   TEXT      NOT NULL UNIQUE,
-    password   TEXT      NOT NULL,
-    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
-    updated_at TIMESTAMP NOT NULL DEFAULT NOW()
+    id         INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+    username   VARCHAR(255) NOT NULL UNIQUE,
+    password   VARCHAR(255) NOT NULL,
+    created_at TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
+    updated_at TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
 );
 
 -- +goose Down
-DROP TABLE "user";
+DROP TABLE user;

+ 2 - 2
migrations/20220916102500_add_user.sql

@@ -1,6 +1,6 @@
 -- +goose Up
 -- admin/admin
-INSERT INTO "user" (username, password) VALUES ('admin', '$2a$10$kSMPeUITJB9tZ2bs.Wm5gukp0Vv9qP3P.K.To.S2OrqAQv0mhzgtS');
+INSERT INTO user ('username', 'password') VALUES ('admin', '$2a$10$kSMPeUITJB9tZ2bs.Wm5gukp0Vv9qP3P.K.To.S2OrqAQv0mhzgtS');
 
 -- +goose Down
-DELETE FROM "user" WHERE username='admin';
+DELETE FROM user WHERE username='admin';