Skip to content

Commit

Permalink
Merge pull request #229 from alimy/pr-pg-ddl
Browse files Browse the repository at this point in the history
 add PostgreSQL DDL file
  • Loading branch information
alimy authored Mar 11, 2023
2 parents d14db3c + 866de12 commit d560a90
Show file tree
Hide file tree
Showing 38 changed files with 1,379 additions and 260 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 12 additions & 2 deletions internal/conf/settting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, " ")
Expand Down
3 changes: 2 additions & 1 deletion internal/dao/jinzhu/dbr/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package dbr
import (
"github.com/sirupsen/logrus"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

const (
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions internal/dao/jinzhu/dbr/post_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"gorm.io/gorm"
"gorm.io/gorm/clause"
)

type PostCollection struct {
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions internal/dao/jinzhu/dbr/post_star.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"gorm.io/gorm"
"gorm.io/gorm/clause"
)

type PostStar struct {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
ALTER TABLE `p_post` DROP COLUMN `visibility`;

DROP INDEX `idx_visibility` ON `p_post`;
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
ALTER TABLE `p_post` ADD COLUMN `visibility` tinyint unsigned NOT NULL DEFAULT '0' COMMENT '可见性 0公开 1私密 2好友可见';

CREATE INDEX `idx_visibility` ON `p_post` ( `visibility` ) USING BTREE;
66 changes: 66 additions & 0 deletions scripts/migration/mysql/0004_optimize_idx.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
ALTER TABLE `p_attachment`
RENAME KEY `idx_attachment_user_id` TO `idx_user`;

ALTER TABLE `p_captcha`
RENAME KEY `idx_captcha_phone` TO `idx_phone`,
RENAME KEY `idx_captcha_expired_on` TO `idx_expired_on`,
RENAME KEY `idx_captcha_use_times` TO `idx_use_times`;

ALTER TABLE `p_comment`
RENAME KEY `idx_comment_post_id` TO `idx_post`,
RENAME KEY `idx_comment_user_id` TO `idx_user`;

ALTER TABLE `p_comment_content`
RENAME KEY `idx_comment_content_comment_id` TO `idx_reply`,
RENAME KEY `idx_comment_content_user_id` TO `idx_user`,
RENAME KEY `idx_comment_content_type` TO `idx_type`,
RENAME KEY `idx_comment_content_sort` TO `idx_sort`;

ALTER TABLE `p_comment_reply`
RENAME KEY `idx_comment_reply_comment_id` TO `idx_comment`;

ALTER TABLE `p_message`
RENAME KEY `idx_message_receiver_user_id` TO `idx_receiver`,
RENAME KEY `idx_message_is_read` TO `idx_is_read`,
RENAME KEY `idx_message_type` TO `idx_type`;

ALTER TABLE `p_post`
RENAME KEY `idx_post_user_id` TO `idx_user`,
RENAME KEY `idx_post_visibility` TO `idx_visibility`;

ALTER TABLE `p_post_attachment_bill`
RENAME KEY `idx_post_attachment_bill_post_id` TO `idx_post`,
RENAME KEY `idx_post_attachment_bill_user_id` TO `idx_user`;

ALTER TABLE `p_post_collection`
RENAME KEY `idx_post_collection_post_id` TO `idx_post`,
RENAME KEY `idx_post_collection_user_id` TO `idx_user`;

ALTER TABLE `p_post_content`
RENAME KEY `idx_post_content_post_id` TO `idx_post`,
RENAME KEY `idx_post_content_user_id` TO `idx_user`;

ALTER TABLE `p_post_star`
RENAME KEY `idx_post_star_post_id` TO `idx_post`,
RENAME KEY `idx_post_star_user_id` TO `idx_user`;

ALTER TABLE `p_tag`
RENAME KEY `idx_tag_user_id` TO `idx_user`,
RENAME KEY `idx_tag_quote_num` TO `idx_num`;

ALTER TABLE `p_user`
RENAME KEY `idx_user_username` TO `idx_username`,
RENAME KEY `idx_user_phone` TO `idx_phone`;

ALTER TABLE `p_wallet_recharge`
RENAME KEY `idx_wallet_recharge_user_id` TO `idx_user`,
RENAME KEY `idx_wallet_recharge_trade_no` TO `idx_trade_no`,
RENAME KEY `idx_wallet_recharge_trade_status` TO `idx_trade_status`;

ALTER TABLE `p_wallet_statement`
RENAME KEY `idx_wallet_statement_user_id` TO `idx_user`;

ALTER TABLE `p_contact`
RENAME KEY `idx_contact_user_friend` TO `idx_user_friend_id`,
RENAME KEY `idx_contact_user_friend_status` TO `idx_user_friend_status`;

65 changes: 65 additions & 0 deletions scripts/migration/mysql/0004_optimize_idx.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
ALTER TABLE `p_attachment`
RENAME KEY `idx_user` TO `idx_attachment_user_id`;

ALTER TABLE `p_captcha`
RENAME KEY `idx_phone` TO `idx_captcha_phone`,
RENAME KEY `idx_expired_on` TO `idx_captcha_expired_on`,
RENAME KEY `idx_use_times` TO `idx_captcha_use_times`;

ALTER TABLE `p_comment`
RENAME KEY `idx_post` TO `idx_comment_post_id`,
RENAME KEY `idx_user` TO `idx_comment_user_id`;

ALTER TABLE `p_comment_content`
RENAME KEY `idx_reply` TO `idx_comment_content_comment_id`,
RENAME KEY `idx_user` TO `idx_comment_content_user_id`,
RENAME KEY `idx_type` TO `idx_comment_content_type`,
RENAME KEY `idx_sort` TO `idx_comment_content_sort`;

ALTER TABLE `p_comment_reply`
RENAME KEY `idx_comment` TO `idx_comment_reply_comment_id`;

ALTER TABLE `p_message`
RENAME KEY `idx_receiver` TO `idx_message_receiver_user_id`,
RENAME KEY `idx_is_read` TO `idx_message_is_read`,
RENAME KEY `idx_type` TO `idx_message_type`;

ALTER TABLE `p_post`
RENAME KEY `idx_user` TO `idx_post_user_id`,
RENAME KEY `idx_visibility` TO `idx_post_visibility`;

ALTER TABLE `p_post_attachment_bill`
RENAME KEY `idx_post` TO `idx_post_attachment_bill_post_id`,
RENAME KEY `idx_user` TO `idx_post_attachment_bill_user_id`;

ALTER TABLE `p_post_collection`
RENAME KEY `idx_post` TO `idx_post_collection_post_id`,
RENAME KEY `idx_user` TO `idx_post_collection_user_id`;

ALTER TABLE `p_post_content`
RENAME KEY `idx_post` TO `idx_post_content_post_id`,
RENAME KEY `idx_user` TO `idx_post_content_user_id`;

ALTER TABLE `p_post_star`
RENAME KEY `idx_post` TO `idx_post_star_post_id`,
RENAME KEY `idx_user` TO `idx_post_star_user_id`;

ALTER TABLE `p_tag`
RENAME KEY `idx_user` TO `idx_tag_user_id`,
RENAME KEY `idx_num` TO `idx_tag_quote_num`;

ALTER TABLE `p_user`
RENAME KEY `idx_username` TO `idx_user_username`,
RENAME KEY `idx_phone` TO `idx_user_phone`;

ALTER TABLE `p_wallet_recharge`
RENAME KEY `idx_user` TO `idx_wallet_recharge_user_id`,
RENAME KEY `idx_trade_no` TO `idx_wallet_recharge_trade_no`,
RENAME KEY `idx_trade_status` TO `idx_wallet_recharge_trade_status`;

ALTER TABLE `p_wallet_statement`
RENAME KEY `idx_user` To `idx_wallet_statement_user_id`;

ALTER TABLE `p_contact`
RENAME KEY `idx_user_friend_id` TO `idx_contact_user_friend`,
RENAME KEY `idx_user_friend_status` TO `idx_contact_user_friend_status`;
16 changes: 16 additions & 0 deletions scripts/migration/postgres/0001_initialize_schema.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
DROP TABLE IF EXISTS p_attachment;
DROP TABLE IF EXISTS p_captcha;
DROP TABLE IF EXISTS p_comment;
DROP TABLE IF EXISTS p_comment_content;
DROP TABLE IF EXISTS p_comment_reply;
DROP TABLE IF EXISTS p_message;
DROP TABLE IF EXISTS p_post;
DROP TABLE IF EXISTS p_post_attachment_bill;
DROP TABLE IF EXISTS p_post_collection;
DROP TABLE IF EXISTS p_post_content;
DROP TABLE IF EXISTS p_post_star;
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;
Loading

0 comments on commit d560a90

Please sign in to comment.