Bladeren bron

Update migrations

Dima 1 jaar geleden
bovenliggende
commit
3469cd7f66

+ 1 - 1
cmd/app/main.go

@@ -5,7 +5,7 @@ import (
 
 	"github.com/dmitriygnatenko/internal/fiber"
 	sp "github.com/dmitriygnatenko/internal/service_provider"
-	_ "github.com/go-sql-driver/mysql"
+	_ "github.com/lib/pq"
 )
 
 func main() {

+ 1 - 1
go.mod

@@ -7,10 +7,10 @@ require (
 	github.com/go-playground/locales v0.14.0
 	github.com/go-playground/universal-translator v0.18.0
 	github.com/go-playground/validator/v10 v10.11.1
-	github.com/go-sql-driver/mysql v1.6.0
 	github.com/gofiber/fiber/v2 v2.38.1
 	github.com/gofiber/template v1.7.1
 	github.com/gojuno/minimock/v3 v3.0.10
+	github.com/lib/pq v1.10.7
 	github.com/spf13/viper v1.10.0
 	github.com/stretchr/testify v1.7.0
 )

+ 2 - 2
go.sum

@@ -136,8 +136,6 @@ github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/j
 github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
 github.com/go-playground/validator/v10 v10.11.1 h1:prmOlTVv+YjZjmRmNSF3VmspqJIxJWXmqUsHwfTRRkQ=
 github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
-github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
-github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
 github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
 github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
 github.com/gofiber/fiber/v2 v2.37.1/go.mod h1:j3UslgQeJQP3mNhBxHnLLE8TPqA1Fd/lrl4gD25rRUY=
@@ -285,6 +283,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
 github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
 github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
 github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
+github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
+github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
 github.com/lyft/protoc-gen-star v0.5.3/go.mod h1:V0xaHgaf5oCCqmcxYcWiDfTiKsZsRc87/1qhoTACD8w=
 github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
 github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=

+ 11 - 11
migrations/20220914205500_add_article_table.sql

@@ -1,17 +1,17 @@
 -- +goose Up
 CREATE TABLE article
 (
-    `id`            INT(11) PRIMARY KEY AUTO_INCREMENT,
-    `title`         VARCHAR(255) NOT NULL,
-    `image`         VARCHAR(255) DEFAULT NULL,
-    `preview_text`  LONGTEXT     DEFAULT NULL,
-    `text`          LONGTEXT     NOT NULL,
-    `meta_keywords` VARCHAR(255) DEFAULT NULL,
-    `meta_desc`     VARCHAR(255) DEFAULT NULL,
-    `is_active`     TINYINT(1)   NOT NULL,
-    `url`           VARCHAR(255) UNIQUE NOT NULL,
-    `publish_time`  DATETIME     NOT NULL
-) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
+    id            SERIAL PRIMARY KEY,
+    url           TEXT      NOT NULL UNIQUE,
+    title         TEXT      NOT NULL,
+    image         TEXT      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
+);
 
 -- +goose Down
 DROP TABLE article;

+ 4 - 4
migrations/20220914211000_add_tag_table.sql

@@ -1,10 +1,10 @@
 -- +goose Up
 CREATE TABLE tag
 (
-    `id`  INT(11) PRIMARY KEY AUTO_INCREMENT,
-    `tag` VARCHAR(255)        NOT NULL,
-    `url` VARCHAR(255) UNIQUE NOT NULL
-) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
+    id  SERIAL PRIMARY KEY,
+    tag TEXT NOT NULL,
+    url TEXT NOT NULL UNIQUE
+);
 
 -- +goose Down
 DROP TABLE tag;

+ 6 - 6
migrations/20220914212000_add_article_tag_table.sql

@@ -2,13 +2,13 @@
 CREATE TABLE article_tag
 (
     article_id INT NOT NULL,
-    tag_id     INT NOT NULL
-) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
+    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)
+);
 
-ALTER TABLE article_tag
-    ADD UNIQUE(article_id, tag_id),
-    ADD CONSTRAINT fk_article_tag_tag FOREIGN KEY (tag_id) REFERENCES tag (id) ON DELETE NO ACTION ON UPDATE NO ACTION,
-    ADD CONSTRAINT fk_article_tag_article FOREIGN KEY (article_id) REFERENCES article (id) ON DELETE NO ACTION ON UPDATE NO ACTION;
+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;

+ 12 - 0
migrations/20221115102300_add_user_table.sql

@@ -0,0 +1,12 @@
+-- +goose Up
+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()
+);
+
+-- +goose Down
+DROP TABLE "user";

+ 6 - 0
migrations/20221115102500_add_user.sql

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