Skip to content

Commit

Permalink
Update xsql support for PB non-scalar fields
Browse files Browse the repository at this point in the history
  • Loading branch information
onanying committed Jun 15, 2024
1 parent f10917a commit 4688268
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 53 deletions.
21 changes: 5 additions & 16 deletions src/xsql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/timestamppb"
"log"
"strings"
"os"
"testing"
"time"
)
Expand Down Expand Up @@ -91,22 +91,11 @@ func newDB() *xsql.DB {

func TestCreateTable(t *testing.T) {
a := assert.New(t)
q := `DROP TABLE IF EXISTS #xsql#;
CREATE TABLE #xsql# (
#id# int unsigned NOT NULL AUTO_INCREMENT,
#foo# varchar(255) DEFAULT NULL,
#bar# datetime DEFAULT NULL,
#bool# int NOT NULL DEFAULT '0',
#enum# int NOT NULL DEFAULT '0',
#json# json DEFAULT NULL,
PRIMARY KEY (#id#)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO #xsql# (#id#, #foo#, #bar#, #bool#, #enum#, #json#) VALUES (1, 'v', '2022-04-12 23:50:00', 1, 1, '{"foo":"bar"}');
INSERT INTO #xsql# (#id#, #foo#, #bar#, #bool#, #enum#, #json#) VALUES (2, 'v1', '2022-04-13 23:50:00', 1, 1, '[1,2]');
INSERT INTO #xsql# (#id#, #foo#, #bar#, #bool#, #enum#, #json#) VALUES (3, 'v2', '2022-04-14 23:50:00', 1, 1, null);
`
DB := newDB()
_, err := DB.Exec(strings.ReplaceAll(q, "#", "`"))

b, err := os.ReadFile("./xsql.sql")
a.Nil(err)
_, err = DB.Exec(string(b))
a.Empty(err)
}

Expand Down
48 changes: 11 additions & 37 deletions src/xsql/xsql.sql
Original file line number Diff line number Diff line change
@@ -1,39 +1,13 @@
/*
Navicat Premium Data Transfer
Source Server : localhost_3306
Source Server Type : MySQL
Source Server Version : 80027
Source Host : localhost:3306
Source Schema : test
Target Server Type : MySQL
Target Server Version : 80027
File Encoding : 65001
Date: 15/04/2022 22:20:33
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for xsql
-- ----------------------------
DROP TABLE IF EXISTS `xsql`;
CREATE TABLE `xsql` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`foo` varchar(255) DEFAULT NULL,
`bar` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- ----------------------------
-- Records of xsql
-- ----------------------------
BEGIN;
INSERT INTO `xsql` (`id`, `foo`, `bar`) VALUES (1, 'v', '2022-04-14 23:49:48');
INSERT INTO `xsql` (`id`, `foo`, `bar`) VALUES (2, 'v1', '2022-04-14 23:50:00');
COMMIT;

SET FOREIGN_KEY_CHECKS = 1;
`id` int unsigned NOT NULL AUTO_INCREMENT,
`foo` varchar(255) DEFAULT NULL,
`bar` datetime DEFAULT NULL,
`bool` int NOT NULL DEFAULT '0',
`enum` int NOT NULL DEFAULT '0',
`json` json DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO `xsql` (`id`, `foo`, `bar`, `bool`, `enum`, `json`) VALUES (1, 'v', '2022-04-12 23:50:00', 1, 1, '{"foo":"bar"}');
INSERT INTO `xsql` (`id`, `foo`, `bar`, `bool`, `enum`, `json`) VALUES (2, 'v1', '2022-04-13 23:50:00', 1, 1, '[1,2]');
INSERT INTO `xsql` (`id`, `foo`, `bar`, `bool`, `enum`, `json`) VALUES (3, 'v2', '2022-04-14 23:50:00', 1, 1, null);

0 comments on commit 4688268

Please sign in to comment.