From 0eb0a727f17fdf8b874fb45ed4368e7d9eb794e5 Mon Sep 17 00:00:00 2001
From: wanli <wanliofficial@aliyun.com>
Date: Wed, 21 Jul 2021 14:26:27 +0800
Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=88=20perf():=20=E4=BC=98=E5=8C=96?=
 =?UTF-8?q?=E5=90=AF=E5=8A=A8=E8=84=9A=E6=9C=AC=E4=BB=A5=E5=8F=8A=E8=BF=81?=
 =?UTF-8?q?=E7=A7=BB=E5=B7=A5=E5=85=B7?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 backend/run.sh              | 10 +++-
 backend/start.sh            | 10 +++-
 tools/database_migration.go | 92 +++++++++++++++++++++++++++++++++++++
 tools/database_migration.py | 24 +++++-----
 tools/go.mod                |  5 ++
 tools/go.sum                |  2 +
 6 files changed, 130 insertions(+), 13 deletions(-)
 create mode 100644 tools/database_migration.go
 create mode 100644 tools/go.mod
 create mode 100644 tools/go.sum

diff --git a/backend/run.sh b/backend/run.sh
index 87d7850..879f7cf 100644
--- a/backend/run.sh
+++ b/backend/run.sh
@@ -1,4 +1,12 @@
 #!/usr/bin/env bash
+###
+ # @Author: your name
+ # @Date: 2021-07-15 09:33:39
+ # @LastEditTime: 2021-07-21 14:25:03
+ # @LastEditors: Please set LastEditors
+ # @Description: In User Settings Edit
+ # @FilePath: \evm-store\backend\run.sh
+### 
 
 # 获取当前路径
 Cur_Dir=$(pwd)
@@ -26,4 +34,4 @@ supervisorctl start evm_store
 Cur_Dir=$(pwd)
 
 source venv/bin/activate
-nohup ${Cur_Dir}/venv/bin/python3 ${Cur_Dir}/start.py > runoob.log 2>&1 &
+nohup ${Cur_Dir}/venv/bin/python3 ${Cur_Dir}/start.py > running.log 2>&1 &
diff --git a/backend/start.sh b/backend/start.sh
index 5cc2618..376cedc 100644
--- a/backend/start.sh
+++ b/backend/start.sh
@@ -1,6 +1,14 @@
 #!/usr/bin/env bash
+###
+ # @Author: your name
+ # @Date: 2021-07-15 09:33:39
+ # @LastEditTime: 2021-07-21 14:25:13
+ # @LastEditors: your name
+ # @Description: In User Settings Edit
+ # @FilePath: \evm-store\backend\start.sh
+### 
 
 Cur_Dir=$(pwd)
 
 source venv/bin/activate
-nohup ${Cur_Dir}/venv/bin/python3 ${Cur_Dir}/start.py > runoob.log 2>&1 &
+nohup ${Cur_Dir}/venv/bin/python3 ${Cur_Dir}/start.py > running.log 2>&1 &
diff --git a/tools/database_migration.go b/tools/database_migration.go
new file mode 100644
index 0000000..619114d
--- /dev/null
+++ b/tools/database_migration.go
@@ -0,0 +1,92 @@
+/*
+ * @Author: your name
+ * @Date: 2021-07-21 13:02:17
+ * @LastEditTime: 2021-07-21 13:09:34
+ * @LastEditors: Please set LastEditors
+ * @Description: In User Settings Edit
+ * @FilePath: \evm-store\tools\database_migration.go
+ */
+package main
+
+import (
+	"database/sql"
+	"fmt"
+
+	_ "github.com/mattn/go-sqlite3"
+)
+
+func main() {
+	fmt.Println("打开数据")
+	db, err := sql.Open("sqlite3", "./evue_store.db")
+	checkErr(err)
+
+	fmt.Println("生成数据表")
+	sql_table := `
+CREATE TABLE IF NOT EXISTS "userinfo" (
+   "uid" INTEGER PRIMARY KEY AUTOINCREMENT,
+   "username" VARCHAR(64) NULL,
+   "departname" VARCHAR(64) NULL,
+   "created" TIMESTAMP default (datetime('now', 'localtime'))  
+);
+CREATE TABLE IF NOT EXISTS "userdeatail" (
+   "uid" INT(10) NULL,
+   "intro" TEXT NULL,
+   "profile" TEXT NULL,
+   PRIMARY KEY (uid)
+);
+   `
+	db.Exec(sql_table)
+
+	//插入数据
+	fmt.Print("插入数据, ID=")
+	stmt, err := db.Prepare("INSERT INTO userinfo(username, departname)  values(?, ?)")
+	checkErr(err)
+	res, err := stmt.Exec("astaxie", "研发部门")
+	checkErr(err)
+	id, err := res.LastInsertId()
+	checkErr(err)
+	fmt.Println(id)
+
+	//更新数据
+	fmt.Print("更新数据 ")
+	stmt, err = db.Prepare("update userinfo set username=? where uid=?")
+	checkErr(err)
+	res, err = stmt.Exec("astaxieupdate", id)
+	checkErr(err)
+	affect, err := res.RowsAffected()
+	checkErr(err)
+	fmt.Println(affect)
+
+	//查询数据
+	fmt.Println("查询数据")
+	rows, err := db.Query("SELECT * FROM userinfo")
+	checkErr(err)
+	for rows.Next() {
+		var uid int
+		var username string
+		var department string
+		var created string
+		err = rows.Scan(&uid, &username, &department, &created)
+		checkErr(err)
+		fmt.Println(uid, username, department, created)
+	}
+
+	/*
+	   //删除数据
+	   fmt.Println("删除数据")
+	   stmt, err = db.Prepare("delete from userinfo where uid=?")
+	   checkErr(err)
+	   res, err = stmt.Exec(id)
+	   checkErr(err)
+	   affect, err = res.RowsAffected()
+	   checkErr(err)
+	   fmt.Println(affect)
+	*/
+	db.Close()
+}
+
+func checkErr(err error) {
+	if err != nil {
+		panic(err)
+	}
+}
diff --git a/tools/database_migration.py b/tools/database_migration.py
index 49dd1c2..de99cf1 100644
--- a/tools/database_migration.py
+++ b/tools/database_migration.py
@@ -3,7 +3,7 @@
 '''
 Author: your name
 Date: 2021-07-20 19:04:27
-LastEditTime: 2021-07-21 10:05:11
+LastEditTime: 2021-07-21 13:34:32
 LastEditors: Please set LastEditors
 Description: In User Settings Edit
 FilePath: \evm-store\tools\database_migration.py
@@ -33,6 +33,8 @@ with open("database_migration.sql", "w+") as fd:
     ]
 
     sqls = [
+        "update evm_store_apps set create_by = {a} where create_by = {b};",
+        "update evm_store_apps set update_by = {a} where update_by = {b};",
         "update evm_store_annex set create_by = {a} where create_by = {b};",
         "update evm_store_annex set update_by = {a} where update_by = {b};",
         "update evm_store_app_logs set create_by = {a} where create_by = {b};",
@@ -51,7 +53,7 @@ with open("database_migration.sql", "w+") as fd:
 
     # 先插入user表
     source_cur.execute('SELECT account, username, password, email, phone, create_at, create_by, update_at, update_by FROM evm_store_user')
-    sql = "insert into evm_user (id, uuid, account, username, password, email, phone, create_at, create_by, update_at, update_by, is_delete) values ({a}, '{b}', '{c}', '{d}', '{e}', '{f}', '{g}', '{h}', {i}, '{j}', {k}, 0);"
+    sql = "insert into evm_user (id, uuid, account, username, password, email, phone, create_at, create_by, update_at, update_by, is_delete, role) values ({a}, '{b}', '{c}', '{d}', '{e}', '{f}', '{g}', '{h}', {i}, '{j}', {k}, 0, 0);"
     res = source_cur.fetchall()
     i = 0
     for line in res:
@@ -65,9 +67,9 @@ with open("database_migration.sql", "w+") as fd:
     # login logs
     source_cur.execute('SELECT id, username, ip, address, create_at, create_by, remarks FROM evm_store_login_logs')
     res = source_cur.fetchall()
-    sql = "insert into evm_login (id, uuid, user, login_at, user_agent, ip, geo_location, operator, create_at, create_by, update_at, update_by, is_delete) values ({a}, '{b}', {c}, '{d}', '{e}', '{f}', '{g}', '{h}', '{i}', {j}, '{k}', {l}, 0);"
+    sql = "insert into evm_login (uuid, user, login_at, user_agent, ip, geo_location, operator, create_at, create_by, update_at, update_by, is_delete) values ('{b}', {c}, '{d}', '{e}', '{f}', '{g}', '{h}', '{i}', {j}, '{k}', {l}, 0);"
     for line in res:
-        sql_str = sql.format(a=line[0], b=uuid.uuid1().hex, c=line[5], d=line[6], e="", f=line[2], g=line[3], h="", i=line[4], j=line[5], k=line[4], l=line[5])
+        sql_str = sql.format(b=uuid.uuid1().hex, c=line[5], d=line[6], e="", f=line[2], g=line[3], h="", i=line[4], j=line[5], k=line[4], l=line[5])
         # target_cur.execute(sql_str)
         fd.write(sql_str + "\n")
     target_conn.commit()
@@ -78,12 +80,12 @@ with open("database_migration.sql", "w+") as fd:
     res = source_cur.fetchall()
     for line in res:
         sql_str = string = "update evm_store_apps set app_url = '{u}' where id = {a}".format(u=line[1], a=line[0])
-        # target_cur.execute(sql_str)
-        fd.write(sql_str + "\n")
+        source_cur.execute(sql_str)
+        # fd.write(sql_str + "\n")
     target_conn.commit()
 
     # annex
-    source_cur.execute('SELECT id, uuid, app, title, path, size, create_by, create_at, update_by, update_at, is_delete FROM evm_store_annex')
+    source_cur.execute('SELECT id, uuid, app, title, path, size, create_at, create_by, update_at, update_by, is_delete FROM evm_store_annex')
     res = source_cur.fetchall()
     sql = "insert into evm_annex (id, uuid, app, title, path, size, create_at, create_by, update_at, update_by, is_delete) values ({a}, '{b}', {c}, '{d}', '{e}', {f}, '{g}', {h}, '{i}', {j}, 0);"
     for line in res:
@@ -118,7 +120,7 @@ with open("database_migration.sql", "w+") as fd:
     res = source_cur.fetchall()
     sql = "insert into evm_package (id, uuid, app_name, app_path, app_version, app_info, create_at, create_by, update_at, update_by, is_delete, source) values ({a}, '{b}', '{c}', '{d}', '{e}', '{f}', '{g}', {h}, '{i}', {j}, {k}, {l});"
     for line in res:
-        s = 1 if line[3].find("evueapps") > -1 else 0
+        s = 1 if line[5] and isinstance(line[5], str) and line[5].find("evueapps") > -1 else 0
         sql_str = sql.format(a=line[0], b=uuid.uuid1().hex, c=line[2], d=line[3], e=line[4], f=line[5], g=line[6], h=line[7], i=line[6], j=line[7], k=0, l=s)
         # target_cur.execute(sql_str)
         fd.write(sql_str + "\n")
@@ -132,18 +134,18 @@ with open("database_migration.sql", "w+") as fd:
     for line in res:
         sql_str = "select id, uuid, app_path from evm_store_app_logs where app_path = '{}'".format(line[3])
         source_cur.execute(sql_str)
-        print("%%%%%%%%%%%%%%", sql_str)
         tmp = source_cur.fetchone()
+        print("=======>", line[0])
         if tmp:
             continue
 
         print("===========>", line)
-        sql_str = string = "select app_name, app_version from evm_store_apps where id == {id}".format(id=line[2])
+        sql_str = "select app_name, app_version from evm_store_apps where id == {id}".format(id=line[2])
         source_cur.execute(sql_str)
         app = source_cur.fetchone()
         print("app:", app)
         if app:
-            s = 1 if line[5] and line[5].find("evueapps") > -1 else 0
+            s = 1 if line[5] and isinstance(line[5], str) and line[5].find("evueapps") > -1 else 0
             sql_str = sql.format(a=line[0], b=uuid.uuid1().hex, c=app[0], d=line[3], e=app[1], f=line[4], g=s, h=line[6], i=line[7], j=line[8], k=line[9], l=line[10])
             # target_cur.execute(sql_str)
             fd.write(sql_str + "\n")
diff --git a/tools/go.mod b/tools/go.mod
new file mode 100644
index 0000000..94aa5e1
--- /dev/null
+++ b/tools/go.mod
@@ -0,0 +1,5 @@
+module tool
+
+go 1.16
+
+require github.com/mattn/go-sqlite3 v1.14.8
diff --git a/tools/go.sum b/tools/go.sum
new file mode 100644
index 0000000..0293daa
--- /dev/null
+++ b/tools/go.sum
@@ -0,0 +1,2 @@
+github.com/mattn/go-sqlite3 v1.14.8 h1:gDp86IdQsN/xWjIEmr9MF6o9mpksUgh0fu+9ByFxzIU=
+github.com/mattn/go-sqlite3 v1.14.8/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
-- 
2.24.1