Ver Fonte

:construction: migrate user-operationframe

Bhikkhu-Kosalla há 4 anos atrás
pai
commit
8fd7749851

+ 11 - 0
app/Models/UserOperationFrame.php

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

+ 40 - 0
database/migrations/2022_01_28_102330_create_user_operation_frames_table.php

@@ -0,0 +1,40 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateUserOperationFramesTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('user_operation_frames', function (Blueprint $table) {
+			#使用雪花id
+            $table->bigInteger('id')->primary();
+		    $table->bigInteger('user_id')->index();
+		    $table->bigInteger('duration');
+            $table->integer('hit')->default(1);
+		    $table->bigInteger('timezone')->default(0);
+		    $table->bigInteger('op_start');
+		    $table->bigInteger('op_end');
+		    
+			$table->timestamp('created_at')->useCurrent();
+			$table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('user_operation_frames');
+    }
+}

+ 27 - 13
v1/scripts/migrations/20211214190200_user_operation_frames_copy.php

@@ -5,27 +5,40 @@
 插入时用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');
 
 #user info
 $user_db=_FILE_DB_USERINFO_;#user数据库
 $user_table=_TABLE_USER_INFO_;#user表名
 # 更新索引表
-$src_db = _FILE_SRC_USER_ACTIVE_;#源数据库
-$src_table = _TABLE_SRC_USER_OPERATION_FRAME_;#源表名
+$src_db = _SQLITE_DB_USER_ACTIVE_;#源数据库
+$src_table = _SQLITE_TABLE_USER_OPERATION_FRAME_;#源表名
 
-$dest_db = _FILE_DB_USER_ACTIVE_;#目标数据库
-$dest_table = _TABLE_USER_OPERATION_FRAME_;#目标表名
+$dest_db = _PG_DB_USER_ACTIVE_;#目标数据库
+$dest_table = _PG_TABLE_USER_OPERATION_FRAME_;#目标表名
 
 fwrite(STDOUT,"migarate user opration frame".PHP_EOL);
 #打开user数据库
 $PDO_USER = new PDO($user_db,_DB_USERNAME_,_DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
-$PDO_USER->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
+$PDO_USER->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 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);
 
 #打开目标数据库
@@ -42,6 +55,7 @@ $stmtDest->execute();
 
 $queryInsert = "INSERT INTO ".$dest_table." 
 								(
+                                    id,
 									user_id,
 									duration,
 									hit,
@@ -50,7 +64,7 @@ $queryInsert = "INSERT INTO ".$dest_table."
 									op_end,
 									created_at,
 									updated_at) 
-									VALUES ( ? , ? , ? , ? , ? , ? , to_timestamp(?), to_timestamp(?))";
+									VALUES ( ?, ? , ? , ? , ? , ? , ? , to_timestamp(?), to_timestamp(?))";
 $stmtDEST = $PDO_DEST->prepare($queryInsert);
 
 $commitData = [];
@@ -68,6 +82,9 @@ $stmtSrc = $PDO_SRC->prepare($query);
 $stmtSrc->execute();
 while($srcData = $stmtSrc->fetch(PDO::FETCH_ASSOC)){
 	$allSrcCount++;
+    if($srcData["user_id"]=='290fd808-2f46-4b8c-b300-0367badd67ed'){
+		$srcData["user_id"] = 'f81c7140-64b4-4025-b58c-45a3b386324a';
+	}
 	$stmtUser->execute(array($srcData["user_id"]));
 	$userId = $stmtUser->fetch(PDO::FETCH_ASSOC);
 	if(!$userId){
@@ -76,6 +93,7 @@ while($srcData = $stmtSrc->fetch(PDO::FETCH_ASSOC)){
 	}
 	#插入目标表
 	$commitData = array(
+            $snowflake->id(),
 			$userId["id"],
 			$srcData["duration"],
 			$srcData["hit"],
@@ -86,11 +104,7 @@ while($srcData = $stmtSrc->fetch(PDO::FETCH_ASSOC)){
 			$srcData["end"]/1000
 		);	
 	$stmtDEST->execute($commitData);
-	if (!$stmtDEST || ($stmtDEST && $stmtDEST->errorCode() != 0)) {
-		$error = $PDO_DEST->errorInfo();
-		echo "error - $error[2] ";
-		exit;
-	}
+
 	$count++;	
 	$allInsertCount++;