diff --git a/CHANGELOG.md b/CHANGELOG.md index 29722fb9..709d4457 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,17 @@ All notable changes to paopao-ce are documented in this file. ## 0.3.0+dev ([`dev`](https://github.com/rocboss/paopao-ce/tree/dev)) +## 0.2.3 + +### Added + +- add PostgreSQL DDL file [#229](https://github.com/rocboss/paopao-ce/pull/229) + +### Changed + +- optimize MySQL DDL file [#229](https://github.com/rocboss/paopao-ce/pull/229) +- optimize Sqlite3 DDL file [#229](https://github.com/rocboss/paopao-ce/pull/229) + ## 0.2.2 ### Fixed diff --git a/internal/conf/config.yaml b/internal/conf/config.yaml index 8713e813..f8d34aaf 100644 --- a/internal/conf/config.yaml +++ b/internal/conf/config.yaml @@ -171,10 +171,11 @@ Postgres: # PostgreSQL数据库 User: paopao Password: paopao DBName: paopao + Schema: public Host: localhost Port: 5432 SSLMode: disable - TimeZone: Asia/Shanghai + ApplicationName: Sqlite3: # Sqlite3数据库 Path: custom/data/sqlite3/paopao-ce.db Redis: diff --git a/internal/conf/settting.go b/internal/conf/settting.go index c5ed4d82..a415be60 100644 --- a/internal/conf/settting.go +++ b/internal/conf/settting.go @@ -301,8 +301,18 @@ func (s *MySQLSettingS) Dsn() string { func (s PostgresSettingS) Dsn() string { var params []string for k, v := range s { - if len(v) > 0 { - params = append(params, strings.ToLower(k)+"="+v) + if len(v) == 0 { + continue + } + lk := strings.ToLower(k) + tv := strings.Trim(v, " ") + switch lk { + case "schema": + params = append(params, "search_path="+tv) + case "applicationname": + params = append(params, "application_name="+tv) + default: + params = append(params, lk+"="+tv) } } return strings.Join(params, " ") diff --git a/internal/dao/jinzhu/dbr/contact.go b/internal/dao/jinzhu/dbr/contact.go index e8c12bd0..a1777e28 100644 --- a/internal/dao/jinzhu/dbr/contact.go +++ b/internal/dao/jinzhu/dbr/contact.go @@ -7,6 +7,7 @@ package dbr import ( "github.com/sirupsen/logrus" "gorm.io/gorm" + "gorm.io/gorm/clause" ) const ( @@ -73,7 +74,7 @@ func (c *Contact) List(db *gorm.DB, conditions ConditionsT, offset, limit int) ( } } - db.Joins("User").Order("`User`.`nickname` ASC") + db.Joins("User").Order(clause.OrderByColumn{Column: clause.Column{Name: "nickname"}, Desc: false}) if err = db.Find(&contacts).Error; err != nil { return nil, err } diff --git a/internal/dao/jinzhu/dbr/post_collection.go b/internal/dao/jinzhu/dbr/post_collection.go index 04ca2647..33d32f31 100644 --- a/internal/dao/jinzhu/dbr/post_collection.go +++ b/internal/dao/jinzhu/dbr/post_collection.go @@ -8,6 +8,7 @@ import ( "time" "gorm.io/gorm" + "gorm.io/gorm/clause" ) type PostCollection struct { @@ -31,7 +32,7 @@ func (p *PostCollection) Get(db *gorm.DB) (*PostCollection, error) { db = db.Where(tn+"user_id = ?", p.UserID) } - db = db.Joins("Post").Where("Post.visibility <> ?", PostVisitPrivate).Order("Post.id DESC") + db = db.Joins("Post").Where("visibility <> ?", PostVisitPrivate).Order(clause.OrderByColumn{Column: clause.Column{Table: "Post", Name: "id"}, Desc: true}) err := db.First(&star).Error if err != nil { return &star, err @@ -73,7 +74,7 @@ func (p *PostCollection) List(db *gorm.DB, conditions *ConditionsT, offset, limi } } - db = db.Joins("Post").Where("Post.visibility <> ?", PostVisitPrivate).Order("Post.id DESC") + db = db.Joins("Post").Where(`visibility <> ?`, PostVisitPrivate).Order(clause.OrderByColumn{Column: clause.Column{Table: "Post", Name: "id"}, Desc: true}) if err = db.Where(tn+"is_del = ?", 0).Find(&collections).Error; err != nil { return nil, err } @@ -97,7 +98,7 @@ func (p *PostCollection) Count(db *gorm.DB, conditions *ConditionsT) (int64, err } } - db = db.Joins("Post").Where("Post.visibility <> ?", PostVisitPrivate) + db = db.Joins("Post").Where(`visibility <> ?`, PostVisitPrivate) if err := db.Model(p).Count(&count).Error; err != nil { return 0, err } diff --git a/internal/dao/jinzhu/dbr/post_star.go b/internal/dao/jinzhu/dbr/post_star.go index 45c48f45..ca434523 100644 --- a/internal/dao/jinzhu/dbr/post_star.go +++ b/internal/dao/jinzhu/dbr/post_star.go @@ -8,6 +8,7 @@ import ( "time" "gorm.io/gorm" + "gorm.io/gorm/clause" ) type PostStar struct { @@ -31,7 +32,7 @@ func (p *PostStar) Get(db *gorm.DB) (*PostStar, error) { db = db.Where(tn+"user_id = ?", p.UserID) } - db = db.Joins("Post").Where("Post.visibility <> ?", PostVisitPrivate).Order("Post.id DESC") + db = db.Joins("Post").Where("visibility <> ?", PostVisitPrivate).Order(clause.OrderByColumn{Column: clause.Column{Table: "Post", Name: "id"}, Desc: true}) if err := db.First(&star).Error; err != nil { return nil, err } @@ -71,7 +72,7 @@ func (p *PostStar) List(db *gorm.DB, conditions *ConditionsT, offset, limit int) } } - db = db.Joins("Post").Where("Post.visibility <> ?", PostVisitPrivate).Order("Post.id DESC") + db = db.Joins("Post").Where("visibility <> ?", PostVisitPrivate).Order(clause.OrderByColumn{Column: clause.Column{Table: "Post", Name: "id"}, Desc: true}) if err = db.Find(&stars).Error; err != nil { return nil, err } @@ -94,7 +95,7 @@ func (p *PostStar) Count(db *gorm.DB, conditions *ConditionsT) (int64, error) { } } - db = db.Joins("Post").Where("Post.visibility <> ?", PostVisitPrivate) + db = db.Joins("Post").Where("visibility <> ?", PostVisitPrivate) if err := db.Model(p).Count(&count).Error; err != nil { return 0, err } diff --git a/scripts/migration/postgres/0001_initialize_schema.down.sql b/scripts/migration/postgres/0001_initialize_schema.down.sql index 1e8371fd..52326aff 100644 --- a/scripts/migration/postgres/0001_initialize_schema.down.sql +++ b/scripts/migration/postgres/0001_initialize_schema.down.sql @@ -13,3 +13,4 @@ DROP TABLE IF EXISTS p_tag; DROP TABLE IF EXISTS p_user; DROP TABLE IF EXISTS p_wallet_recharge; DROP TABLE IF EXISTS p_wallet_statement; +DROP SEQUENCE IF EXISTS post_id_seq; diff --git a/scripts/migration/postgres/0001_initialize_schema.up.sql b/scripts/migration/postgres/0001_initialize_schema.up.sql index 08e6f5b3..cfaccf28 100644 --- a/scripts/migration/postgres/0001_initialize_schema.up.sql +++ b/scripts/migration/postgres/0001_initialize_schema.up.sql @@ -94,8 +94,9 @@ CREATE INDEX idx_message_receiver_user_id ON p_message USING btree (receiver_use CREATE INDEX idx_message_is_read ON p_message USING btree (is_read); CREATE INDEX idx_message_type ON p_message USING btree ("type"); +CREATE SEQUENCE IF NOT EXISTS post_id_seq AS BIGINT MINVALUE 1080017989 NO MAXVALUE; CREATE TABLE p_post ( - id BIGSERIAL PRIMARY KEY, + id BIGINT NOT NULL DEFAULT nextval('post_id_seq'::regclass), user_id BIGINT NOT NULL DEFAULT 0, comment_count BIGINT NOT NULL DEFAULT 0, collection_count BIGINT NOT NULL DEFAULT 0, @@ -111,7 +112,8 @@ CREATE TABLE p_post ( created_on BIGINT NOT NULL DEFAULT 0, modified_on BIGINT NOT NULL DEFAULT 0, deleted_on BIGINT NOT NULL DEFAULT 0, - is_del SMALLINT NOT NULL DEFAULT 0 + is_del SMALLINT NOT NULL DEFAULT 0, + PRIMARY KEY (id) ); CREATE INDEX idx_post_user_id ON p_post USING btree (user_id); @@ -191,7 +193,7 @@ CREATE TABLE p_user ( status SMALLINT NOT NULL DEFAULT 1, -- 状态,1正常,2停用 avatar VARCHAR(255) NOT NULL DEFAULT '', balance BIGINT NOT NULL, -- 用户余额(分) - is_admin SMALLINT NOT NULL DEFAULT 0, -- 是否管理员 + is_admin BOOLEAN NOT NULL DEFAULT false, -- 是否管理员 created_on BIGINT NOT NULL DEFAULT 0, modified_on BIGINT NOT NULL DEFAULT 0, deleted_on BIGINT NOT NULL DEFAULT 0, diff --git a/scripts/migration/postgres/0002_post_visibility.up.sql b/scripts/migration/postgres/0002_post_visibility.up.sql index 5be19c66..673432ab 100644 --- a/scripts/migration/postgres/0002_post_visibility.up.sql +++ b/scripts/migration/postgres/0002_post_visibility.up.sql @@ -1,2 +1 @@ ALTER TABLE p_post ADD COLUMN visibility SMALLINT NOT NULL DEFAULT 0; -- 可见性 0公开 1私密 2好友可见 -CREATE INDEX idx_visibility ON p_post USING btree (visibility) diff --git a/scripts/migration/postgres/0003_feature_contact.up.sql b/scripts/migration/postgres/0003_feature_contact.up.sql index ba285d31..9177db77 100644 --- a/scripts/migration/postgres/0003_feature_contact.up.sql +++ b/scripts/migration/postgres/0003_feature_contact.up.sql @@ -7,7 +7,7 @@ CREATE TABLE p_contact ( status SMALLINT NOT NULL DEFAULT 0, -- 好友状态: 1请求好友, 2已好友, 3拒绝好友, 4已删好友 is_top SMALLINT NOT NULL DEFAULT 0, -- 是否置顶, 0否, 1是 is_black SMALLINT NOT NULL DEFAULT 0, -- 是否为黑名单, 0否, 1是 - is_delete SMALLINT NOT NULL DEFAULT 0, -- 否删除好友, 0否, 1是 + is_del SMALLINT NOT NULL DEFAULT 0, -- 否删除好友, 0否, 1是 notice_enable SMALLINT NOT NULL DEFAULT 0, -- 是否有消息提醒, 0否, 1是 created_on BIGINT NOT NULL DEFAULT 0, modified_on BIGINT NOT NULL DEFAULT 0, @@ -20,7 +20,7 @@ CREATE TABLE p_contact_group ( id BIGSERIAL PRIMARY KEY, user_id int NOT NULL DEFAULT 0, name VARCHAR(32) NOT NULL DEFAULT '', -- 分组名称 - is_delete SMALLINT NOT NULL DEFAULT 1, -- 是否删除, 0否, 1是 + is_del SMALLINT NOT NULL DEFAULT 1, -- 是否删除, 0否, 1是 created_on BIGINT NOT NULL DEFAULT 0, modified_on BIGINT NOT NULL DEFAULT 0, deleted_on BIGINT NOT NULL DEFAULT 0 diff --git a/scripts/paopao-postgres.sql b/scripts/paopao-postgres.sql index c7b00959..ca536740 100644 --- a/scripts/paopao-postgres.sql +++ b/scripts/paopao-postgres.sql @@ -106,9 +106,10 @@ CREATE INDEX idx_message_receiver_user_id ON p_message USING btree (receiver_use CREATE INDEX idx_message_is_read ON p_message USING btree (is_read); CREATE INDEX idx_message_type ON p_message USING btree ("type"); +CREATE SEQUENCE IF NOT EXISTS post_id_seq AS BIGINT MINVALUE 1080017989 NO MAXVALUE; DROP TABLE IF EXISTS p_post; CREATE TABLE p_post ( - id BIGSERIAL PRIMARY KEY, + id BIGINT NOT NULL DEFAULT nextval('post_id_seq'::regclass), user_id BIGINT NOT NULL DEFAULT 0, comment_count BIGINT NOT NULL DEFAULT 0, collection_count BIGINT NOT NULL DEFAULT 0, @@ -125,7 +126,8 @@ CREATE TABLE p_post ( created_on BIGINT NOT NULL DEFAULT 0, modified_on BIGINT NOT NULL DEFAULT 0, deleted_on BIGINT NOT NULL DEFAULT 0, - is_del SMALLINT NOT NULL DEFAULT 0 + is_del SMALLINT NOT NULL DEFAULT 0, + PRIMARY KEY (id) ); CREATE INDEX idx_post_user_id ON p_post USING btree (user_id); CREATE INDEX idx_post_visibility ON p_post USING btree (visibility); @@ -212,7 +214,7 @@ CREATE TABLE p_user ( status SMALLINT NOT NULL DEFAULT 1, -- 状态,1正常,2停用 avatar VARCHAR(255) NOT NULL DEFAULT '', balance BIGINT NOT NULL, -- 用户余额(分) - is_admin SMALLINT NOT NULL DEFAULT 0, -- 是否管理员 + is_admin BOOLEAN NOT NULL DEFAULT false, -- 是否管理员 created_on BIGINT NOT NULL DEFAULT 0, modified_on BIGINT NOT NULL DEFAULT 0, deleted_on BIGINT NOT NULL DEFAULT 0, @@ -231,7 +233,7 @@ CREATE TABLE p_contact ( status SMALLINT NOT NULL DEFAULT 0, -- 好友状态: 1请求好友, 2已好友, 3拒绝好友, 4已删好友 is_top SMALLINT NOT NULL DEFAULT 0, -- 是否置顶, 0否, 1是 is_black SMALLINT NOT NULL DEFAULT 0, -- 是否为黑名单, 0否, 1是 - is_delete SMALLINT NOT NULL DEFAULT 0, -- 否删除好友, 0否, 1是 + is_del SMALLINT NOT NULL DEFAULT 0, -- 否删除好友, 0否, 1是 notice_enable SMALLINT NOT NULL DEFAULT 0, -- 是否有消息提醒, 0否, 1是 created_on BIGINT NOT NULL DEFAULT 0, modified_on BIGINT NOT NULL DEFAULT 0, @@ -245,7 +247,7 @@ CREATE TABLE p_contact_group ( id BIGSERIAL PRIMARY KEY, user_id int NOT NULL DEFAULT 0, name VARCHAR(32) NOT NULL DEFAULT '', -- 分组名称 - is_delete SMALLINT NOT NULL DEFAULT 1, -- 是否删除, 0否, 1是 + is_del SMALLINT NOT NULL DEFAULT 1, -- 是否删除, 0否, 1是 created_on BIGINT NOT NULL DEFAULT 0, modified_on BIGINT NOT NULL DEFAULT 0, deleted_on BIGINT NOT NULL DEFAULT 0