Jelajahi Sumber

:construction: migrate UserOperationLog

Bhikkhu-Kosalla 4 tahun lalu
induk
melakukan
4244593fd1

+ 11 - 0
app/Models/UserOperationLog.php

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

+ 55 - 0
database/migrations/2022_01_28_105209_create_user_operation_logs_table.php

@@ -0,0 +1,55 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateUserOperationLogsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('user_operation_logs', function (Blueprint $table) {
+			#使用雪花id
+            $table->bigInteger('id')->primary();
+		    $table->bigInteger('user_id')->index();
+		    $table->bigInteger('op_type_id')->index();
+            $table->enum('op_type',['channel_update',
+                                    'channel_create',
+                                    'article_update',
+                                    'article_create',
+                                    'dict_lookup',
+                                    'term_create',
+                                    'term_update',
+                                    'term_lookup',
+                                    'wbw_update',
+                                    'wbw_create',
+                                    'sent_update',
+                                    'sent_create',
+                                    'collection_update',
+                                    'collection_create',
+                                    'nissaya_open'
+                                    ]);
+            $table->text('content')->nullable();
+		    $table->bigInteger('timezone')->default(0);
+		    $table->bigInteger('create_time');
+			$table->timestamp('created_at')->useCurrent();
+
+            $table->index(['user_id','op_type_id']);
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('user_operation_logs');
+    }
+}

+ 28 - 15
v1/scripts/migrations/20211214181900_user_operation_log_copy.php

@@ -5,7 +5,23 @@
 插入时用uuid判断是否曾经插入
 曾经插入就不插入了
 */
-require_once __DIR__."/../../app/config.php";
+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');
+
+
+
 $active_type[10] = "channel_update";
 $active_type[11] = "channel_create";
 $active_type[20] = "article_update";
@@ -25,11 +41,11 @@ $active_type[90] = "nissaya_open";
 $user_db=_FILE_DB_USERINFO_;#user数据库
 $user_table=_TABLE_USER_INFO_;#user表名
 # 更新索引表
-$src_db=_FILE_SRC_USER_ACTIVE_LOG_;#源数据库
-$src_table=_TABLE_SRC_USER_OPERATION_LOG_;#源表名
+$src_db=_SQLITE_DB_USER_ACTIVE_LOG_;#源数据库
+$src_table=_SQLITE_TABLE_USER_OPERATION_LOG_;#源表名
 
-$dest_db=_FILE_DB_USER_ACTIVE_LOG_;#目标数据库
-$dest_table=_TABLE_USER_OPERATION_LOG_;#目标表名
+$dest_db=_PG_DB_USER_ACTIVE_LOG_;#目标数据库
+$dest_table=_PG_TABLE_USER_OPERATION_LOG_;#目标表名
 
 fwrite(STDOUT,"migarate user opration log".PHP_EOL);
 #打开user数据库
@@ -39,12 +55,12 @@ fwrite(STDOUT,"open user table".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_WARNING);
+$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_WARNING);
+$PDO_DEST->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 fwrite(STDOUT,"open dest table".PHP_EOL);
 
 #删除源数据表中全部数据
@@ -56,6 +72,7 @@ $stmtDest->execute();
 
 $queryInsert = "INSERT INTO ".$dest_table." 
 								(
+                                    id,
 									user_id,
 									op_type_id,
 									op_type,
@@ -63,7 +80,7 @@ $queryInsert = "INSERT INTO ".$dest_table."
 									timezone,
 									create_time,
 									created_at) 
-									VALUES ( ? , ? , ? , ? ,? , ? , to_timestamp(?))";
+									VALUES ( ? , ? , ? , ? , ? ,? , ? , to_timestamp(?))";
 $stmtDEST = $PDO_DEST->prepare($queryInsert);
 
 $commitData = [];
@@ -83,6 +100,7 @@ while($srcData = $stmtSrc->fetch(PDO::FETCH_ASSOC)){
 	$allSrcCount++;
 	#插入目标表
 	$commitData = array(
+            $snowflake->id(),
 			$srcData["user_id"],
 			$srcData["active"],
 			$active_type[$srcData["active"]],
@@ -92,11 +110,7 @@ while($srcData = $stmtSrc->fetch(PDO::FETCH_ASSOC)){
 			$srcData["time"]/1000
 		);	
 	$stmtDEST->execute($commitData);
-	if (!$stmtDEST || ($stmtDEST && $stmtDEST->errorCode() != 0)) {
-		$error = $PDO_DEST->errorInfo();
-		echo "error - $error[2] ";
-		exit;
-	}
+
 	$count++;	
 	$allInsertCount++;
 
@@ -109,8 +123,7 @@ while($srcData = $stmtSrc->fetch(PDO::FETCH_ASSOC)){
 }
 
 fwrite(STDOUT,"insert done $allInsertCount in $allSrcCount ".PHP_EOL) ;
-fwrite(STDOUT,"all done".PHP_EOL);
-
+fwrite(STDOUT, "all done in ".(time()-$start)."s".PHP_EOL);