2
0
Эх сурвалжийг харах

:construction: migrate CustomBook

visuddhinanda@gmail.com 4 жил өмнө
parent
commit
ee86276dde

+ 11 - 0
app/Models/CustomBookId.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class CustomBookId extends Model
+{
+    use HasFactory;
+}

+ 1 - 1
database/migrations/2022_02_11_090826_create_custom_books_table.php

@@ -15,7 +15,7 @@ class CreateCustomBooksTable extends Migration
     {
         Schema::create('custom_books', function (Blueprint $table) {
             $table->bigInteger('id')->primary();
-
+            $table->integer('book_id')->uniqid();
             $table->string('title',512)->index();
             $table->string('owner',36)->index();
             $table->bigInteger('editor_id')->index();

+ 34 - 0
database/migrations/2022_02_14_094254_create_custom_book_ids_table.php

@@ -0,0 +1,34 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateCustomBookIdsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('custom_book_ids', function (Blueprint $table) {
+            $table->id();
+            $table->string('key',32)->default('max_book_number');
+            $table->integer('value');
+            $table->timestamp('created_at')->useCurrent()->index();
+			$table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate()->index();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('custom_book_ids');
+    }
+}

+ 10 - 2
public/app/config.table.php

@@ -403,8 +403,13 @@ define("_FILE_DB_USER_RBAC_",  __DIR__ . "/../tmp/user/rbac.db3");
 define("_TABLE_FTS_", "fts_texts");
 
 //很少使用
-# 网站设置
-define("_FILE_DB_HOSTSETTING_", "sqlite:" . __DIR__ . "/../tmp/user/hostsetting.db3");
+
+
+define("_SQLITE_DB_HOSTSETTING_", "sqlite:" . __DIR__ . "/../tmp/user/hostsetting.db3");
+define("_SQLITE_TABLE_HOSTSETTING_", "setting");
+
+define("_PG_DB_CUSTOM_BOOK_ID_", _PDO_DB_DSN_);
+define("_PG_TABLE_CUSTOM_BOOK_ID_", "custom_book_ids");
 
 #巴缅字典
 //define("_DICT_DB_PM_", _DB_ENGIN_.":host="._DB_HOST_.";port="._DB_PORT_.";dbname="._DB_NAME_.";user="._DB_USERNAME_.";password="._DB_PASSWORD_.";");
@@ -687,4 +692,7 @@ define("_TABLE_MESSAGE_", _SQLITE_TABLE_MESSAGE_);
 define("_FILE_DB_USER_DICT_", _SQLITE_DB_USER_DICT_);
 define("_TABLE_USER_DICT_", _SQLITE_TABLE_USER_DICT_);
 
+# 网站设置
+define("_FILE_DB_HOSTSETTING_", _PG_DB_CUSTOM_BOOK_ID_);
+define("_TABLE_HOSTSETTING_", _PG_TABLE_CUSTOM_BOOK_ID_);
 ?>

+ 22 - 23
public/app/db/custom_book.php

@@ -37,17 +37,16 @@ class CustomBook extends Table
 
 			$query="INSERT INTO {$this->table} 
             (
-                'id',
-                'book_id',
-                'title',
-                'owner',
-                'lang',
-                'status',
-                'modify_time',
-                'create_time'
-                ) VALUES (?,?, ?, ?, ?, ?, ?, ?)";
-
-			$stmt = $this->execute($query,array($this->SnowFlake->id(),$currBook,$title,$_COOKIE["user_uid"],$lang,10,mTime(),mTime()));
+                id,
+                book_id,
+                title,
+                owner,
+                editor_id,
+                lang,
+                status
+                ) VALUES (?,?, ?, ?, ?, ? , ?)";
+
+			$stmt = $this->execute($query,array($this->SnowFlake->id(),$currBook,$title,$_COOKIE["user_uid"],$_COOKIE["user_id"],$lang,10));
 			if($stmt){
 				$CSent = new CustomBookSentence($this->redis);
 				$respond = $CSent->insert($currBook,$sent,$lang);
@@ -89,18 +88,18 @@ class CustomBookSentence extends Table
 		$this->dbh->beginTransaction();
 		$query="INSERT INTO {$this->table} 
         (
-            'id',
-            'book',
-            'paragraph',
-            'begin',
-            'end',
-            'length',
-            'text',
-            'lang',
-            'owner',
-            'status',
-            'create_time',
-            'modify_time'
+            id,
+            book,
+            paragraph,
+            word_start,
+            word_end,
+            length,
+            content,
+            lang,
+            owner,
+            status,
+            create_time,
+            modify_time
         ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
 		
 		$sth = $this->dbh->prepare($query);

+ 1 - 1
public/app/db/table.php

@@ -23,7 +23,7 @@ class Table
     protected $SnowFlake;
     function __construct($db,$table,$user="",$password="",$redis=false) {
         $this->dbh = new PDO($db, _DB_USERNAME_, _DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
-        $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
+        $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 		$database = new Medoo([
 			// Initialized and connected PDO object.
 			'pdo' => $this->dbh,

+ 4 - 4
public/app/hostsetting/function.php

@@ -6,15 +6,15 @@ class Hostsetting
     public $dbh;
     public function __construct()
     {
-        $dns = "" . _FILE_DB_HOSTSETTING_;
-        $this->dbh = new PDO($dns, "", "", array(PDO::ATTR_PERSISTENT => true));
+        $dns = _FILE_DB_HOSTSETTING_;
+        $this->dbh = new PDO($dns, _DB_USERNAME_,_DB_PASSWORD_, array(PDO::ATTR_PERSISTENT => true));
         $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
     }
 
     public function get($key)
     {
         if ($this->dbh) {
-            $query = "SELECT value FROM setting WHERE key= ? ";
+            $query = "SELECT value FROM "._TABLE_HOSTSETTING_." WHERE key= ? ";
             $stmt = $this->dbh->prepare($query);
             $stmt->execute(array($key));
             $row = $stmt->fetch(PDO::FETCH_NUM);
@@ -31,7 +31,7 @@ class Hostsetting
     public function set($key, $value)
     {
         if ($this->dbh) {
-            $query = "UPDATE setting SET value = ?  WHERE key= ? ";
+            $query = "UPDATE "._TABLE_HOSTSETTING_." SET value = ? , updated_at = now() WHERE key= ? ";
             $stmt = $this->dbh->prepare($query);
             $stmt->execute(array($value, $key));
             if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {

+ 17 - 0
public/db/sqlite/custom_book/book_sn.sql

@@ -0,0 +1,17 @@
+--
+-- 由SQLiteStudio v3.1.1 产生的文件 周一 2月 14 16:11:21 2022
+--
+-- 文本编码:UTF-8
+--
+PRAGMA foreign_keys = off;
+BEGIN TRANSACTION;
+
+-- 表:setting
+CREATE TABLE setting (

+    [key]     TEXT PRIMARY KEY,

+    value     TEXT,

+    [default] TEXT

+);
+
+COMMIT TRANSACTION;
+PRAGMA foreign_keys = on;

+ 1 - 0
v1/scripts/install5.sh

@@ -4,5 +4,6 @@ date
 
 php ./migrations/20220211155400_custom_book_copy.php
 php ./migrations/20220213092400_custom_book_sentence_copy.php
+php ./migrations/20220214163000_custom_book_id_copy.php
 
 date

+ 4 - 2
v1/scripts/migrations/20220211155400_custom_book_copy.php

@@ -50,6 +50,7 @@ fwrite(STDOUT,"open dest table".PHP_EOL);
 $queryInsert = "INSERT INTO ".$dest_table." 
 								(
                                     id,
+                                    book_id,
 									title,
 									owner,
 									editor_id,
@@ -57,7 +58,7 @@ $queryInsert = "INSERT INTO ".$dest_table."
 									status,
 									updated_at,
 									created_at) 
-									VALUES (? , ? , ? , ?, ? , ? , ? , ? )";
+									VALUES (? , ? , ? , ? , ?, ? , ? , ? , ? )";
 $stmtDEST = $PDO_DEST->prepare($queryInsert);
 
 $commitData = [];
@@ -141,7 +142,8 @@ while($srcData = $stmtSrc->fetch(PDO::FETCH_ASSOC)){
 	$created_at = date("Y-m-d H:i:s.",$srcData["create_time"]/1000).($srcData["create_time"]%1000)." UTC";
 	$updated_at = date("Y-m-d H:i:s.",$srcData["modify_time"]/1000).($srcData["modify_time"]%1000)." UTC";
 	$commitData = array(
-			$srcData["id"],
+            $snowflake->id(),
+			$srcData["book_id"],
 			$srcData["title"],
 			$srcData["owner"],
 			$userId["id"],

+ 95 - 0
v1/scripts/migrations/20220214163000_custom_book_id_copy.php

@@ -0,0 +1,95 @@
+<?php
+/*
+迁移  article 库
+从旧数据表中提取数据插入到新的表
+插入时用uuid判断是否曾经插入
+曾经插入就不插入了
+*/
+require_once __DIR__."/../../../public/app/config.php";
+require_once __DIR__."/../../../public/app/public/snowflakeid.php";
+
+set_exception_handler(function($e){
+	fwrite(STDERR,"error-msg:".$e->getMessage().PHP_EOL);
+	fwrite(STDERR,"error-file:".$e->getFile().PHP_EOL);
+	fwrite(STDERR,"error-line:".$e->getLine().PHP_EOL);
+	exit;
+});
+$start = time();
+# 雪花id
+$snowflake = new SnowFlakeId();
+
+$fpError = fopen(__DIR__.'/log/'.basename($_SERVER['PHP_SELF'],'.php').".err.data.csv",'w');
+
+
+
+# 
+$src_db = _SQLITE_DB_HOSTSETTING_;#源数据库
+$src_table = _SQLITE_TABLE_HOSTSETTING_;#源表名
+
+$dest_db = _PG_DB_CUSTOM_BOOK_ID_;#目标数据库
+$dest_table = _PG_TABLE_CUSTOM_BOOK_ID_;#目标表名
+
+fwrite(STDOUT,"migarate custom book id".PHP_EOL);
+
+
+#打开源数据库
+$PDO_SRC = new PDO($src_db,_DB_USERNAME_,_DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
+$PDO_SRC->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+fwrite(STDOUT,"open src table".PHP_EOL);
+
+#打开目标数据库
+$PDO_DEST = new PDO($dest_db,_DB_USERNAME_,_DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
+$PDO_DEST->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+fwrite(STDOUT,"open dest table".PHP_EOL);
+
+$queryInsert = "INSERT INTO ".$dest_table." 
+								(
+									key,
+									value) 
+									VALUES (? , ?  )";
+$stmtDEST = $PDO_DEST->prepare($queryInsert);
+
+$commitData = [];
+$allInsertCount = 0;
+$allSrcCount = 0;
+$count = 0;
+
+
+#从源数据表中读取
+$query = "SELECT key,value  FROM ".$src_table. " where key = 'max_book_number' ";
+$stmtSrc = $PDO_SRC->prepare($query);
+$stmtSrc->execute();
+if($srcData = $stmtSrc->fetch(PDO::FETCH_ASSOC)){
+	$allSrcCount++;
+	//查询是否已经插入
+	$queryExsit = "SELECT id  FROM ".$dest_table." WHERE key = ? ";
+	$getExist = $PDO_DEST->prepare($queryExsit);
+	$getExist->execute(array($srcData["key"]));
+	$exist = $getExist->fetch(PDO::FETCH_ASSOC);
+	if($exist){
+        echo "key max_book_number exist value=".$getExist['value'];
+		exit;
+	}
+	#插入目标表
+
+	$commitData = array(
+			$srcData["key"],
+			$srcData["value"]
+		);
+	$stmtDEST->execute($commitData);
+
+	$count++;	
+	$allInsertCount++;
+
+}else{
+    echo "no row read".PHP_EOL;
+}
+
+fwrite(STDOUT,"insert done $allInsertCount in $allSrcCount ".PHP_EOL) ;
+fwrite(STDOUT, "all done in ".(time()-$start)."s".PHP_EOL);
+
+fclose($fpError);
+
+
+
+