如何在纯 SQL 中创建新用户

创建用户并使其在 Discourse 中显示为活跃状态,最少需要向哪些表添加行?

我刚刚在 user_emails 表中插入了一行:

insert into user_emails (user_id, email, "primary", created_at, updated_at) values (2,'user@email.com',true, now(), now());

不出所料,该用户在 GUI 中并未出现。那么最少需要执行哪些步骤?我是否需要向更多表添加数据,或者需要重启某些服务?

现在可能有人会说这不是正确的做法,我理解这一点。但对我而言,情况可能如下(我的一些假设可能不正确,但这是我的理解):

  • 所有从其他数据库论坛导入数据的脚本都期望能够从 Discourse 实例连接到另一个论坛。然而,让正在运行的 Discourse 实例访问另一个论坛(jforum)可能非常困难甚至不可能;同样,尝试安装一个包含旧论坛(位于非常旧的服务器上)的 Discourse 开发环境也是不可行的。
  • 我不熟悉 Ruby on Rails,因此无法修改现有脚本,但我懂 SQL。
  • 我可以使用 API(我只有 5000 个用户),但我希望有一种插件或方法能够保留 MD5 密码哈希,而不要求所有用户重置密码,因此我认为无法通过 API 实现这一点。
  • 存在一个 jforum 导入脚本,我可以利用它来查看从 jforum 提取数据所需的 SQL,然后将其放入 CSV 文件并加载到数据库中。
  • 批量导入工具可能提供了向 Discourse 添加 SQL 数据的方法,但我难以找到相关的 SQL 语句。
  • 看起来,关键是将用户导入系统,之后添加分类、主题和帖子应该不会太困难。

Right!

If you did, you’d not be asking. :wink:

The likelihood that you’ll end up with a corrupt database is pretty high. And then no one will be able to help you.

Dump your database and restore on your local Discourse server (or anywhere that it can connect to–I’ve done larger imports with a remote database over my home internet connection) and then run the existing script.

It’ll be much easier to use Ruby that you don’t understand than to understand it well enough to figure out how to write the SQL. I learned Ruby a long time after I wrote my first importer.

5 个赞

That is a bit condesensing !
If the bulk import script is running sql (as I think it is)then documenting the SQL required to create a user is really no more dangerous then trying to use a bulk import script. And since I currently have a vanilla discourse install it wouldn’t terribly matter it if got messed up.I expect I have many more years in development than yourself.

What I don’t have great experience is all the admin tasks, and my first attempt to install a local Discourse server just opened a can of worms, and if I cant connect to my JForum mysql server from my discourse server then I’m going to have to install mysql as well and transfer the database. The existing jforum script isn’t checked into the main source tree, and Im on a very old version of jforum so not expecting it to work.

2 个赞

I meant no offense. I guess you missed the :wink:?

I bet you’re right!

Sometimes free advice from someone that’s worked with Discourse full time for over three years, has done scores of imports, and written several importers from scratch is worth what you pay for it.

5 个赞

How about setting up an SSH tunnel?

Inconsistent, maybe. Corrupt, no.

But you can create the users using the API and then insert the md5 hashes in a custom field using SQL. You will have best of both worlds.

5 个赞

Ooh! That’s a pretty great idea.

This is the free advice you’re looking for!

3 个赞

Yes, I had the thought but as I dont quite understand how the md5 hashing works wasn’t clear if that was possibility, okay I will give that a go.

你只需插入一个自定义字段:

  • user_id:显而易见
  • name:“import_pass”
  • value:MD5 摘要

然后安装 migratepassword 插件。

2 个赞

谢谢,我有几个问题,希望能得到您的帮助。

安装

在 Discourse 目录下运行 bundle exec rake plugin:install repo=http://github.com/[Communiteq](https://www.communiteq.com)(原 DiscourseHosting)/discourse-migratepassword
重启 Discourse
  1. 我已经安装了插件,但该如何重启 Discourse?

  2. 如何插入自定义字段?能否提供 SQL 语句?或者是否可以通过 API 使用 "user_fields[1]": "string" 部分来完成?目前我可以通过 API 编程方式创建用户,但暂时忽略了该字段。

  3. 如果已安装该插件,在通过 API 创建用户时传入的密码值是否无关紧要?

  1. no, it will prevail over the md5 digest in the custom field.

The answers to the other two questions can easily be found on this forum. If you want to save time we can do the import and/or hosting for you as a paid service.

1 个赞

I did search for both answers, was unable to find. In the first case it is confusing that some installs are done via editing the yaml file and then relaunching the docker , and some seem such as this are installed whilst in the docker container, its not cxlear what restart means in this case.

I cant really find any pure sql in this forum

Surprised there seems such a push to sale paid services for using this opensrc software , never like this when i ask questions on stackoverflow.

1 个赞

I love to think along, innovate and contribute. But I also need to make a living and that means that I need to draw a line somewhere, and I try to draw it where I get the idea that I’m doing work instead of innovating, contributing and thinking along.

Looking up the SQL query for a user custom field is work for me, and as you’re an experienced dev I estimate that you can figure it out as well :smiley:

4 个赞

Well sure I can try and work it out but no harm in asking since its not

can easily be found on this forum

As for the other question, well it remains unclear to me.

I dont expect you to do my work for me, on the other hand side i do genrally find it frustrating how documentation assumes alot from users.

OK question 2, an example of adding a custom import_pass field for user_id 5, (user_id can be seen in the user_emails table or id from users table)

insert into user_custom_fields (user_id, name, value, created_at, updated_at) values (5,‘import_pass’,‘2aee1c40222c7754d4534e61452612cc’, now(), now());

2 个赞

Okay I found it
./launcher restart app
but doesnt seem to be restarting, I can enter app and connect to db, but cant connect to website. May just rebuild and start again

1 个赞

So I ran ./launcher rebuild app and now it is back and working.

I thought this would recreate the database but it has not so I guess that is because the actual database is persisting outside of docker and rebuild just rebuilds the docker app. Also I see the dataexplorer plugin I added (by modifying the yaml file is still there). But is the passwordmigration plugin there or does it get wiped out by rebuild, there is certainly nothing in the plugs in section of the adminpage.

Right I tried installing passwordmigration and then ran restart again, and once again my site is down, it does seem like passwordmigration is breaking something ?

So rebuilt again, got it working (but now passwordmigration plugin missing). If I run ./launcher restart app without installing migrationpassword first it restarts okay, so does seem to be a particular problem with migrationpassword on my setup

1 个赞

Last time I tried, the passwordmigration plugin was incompatible with running an import, so you’ll want to install it after you do the import.

It could be that it’s breaking your rebuild, but that’d be fairly surprising, as Richard uses it in his hosting. I recommend that you install it in app.yml just as you did the data explorer after you’ve done your import.

rm -rf the postgres directories in /var/discourse/shared/standalone if you want to get a new database. Maybe have a look at Advanced Troubleshooting with Docker

2 个赞

disabling it during the import should be sufficient

3 个赞

Weird, dropped database and ran ./discourse-setup so had a brand new install, everything fine.
Installed migrationpassword with rake and restarted, again website fails with 502 !
DataExplorer plugin still existed, so removed that, rebuilt, ok, then reinstalled migrationpassword and fails with 502 again.

So confusing because clearly works for others but doesn’t work on my vanilla install.

Will try to install via editing the app.yml file next.

1 个赞

(圣诞购物耽误了时间)但我终于尝试通过编辑 app.yml 并重新构建来安装 migratepassword……成功了!
不过,bundle exec rake plugin:install repo=http://github.com/[Communiteq](https://www.communiteq.com) (formerly DiscourseHosting)/discourse-migratepassword 这个选项在我的标准安装环境中无法使用。或许需要在 GitHub - communiteq/discourse-migratepassword: Support migrated password hashes · GitHub 的 README.md 中对此进行检查或移除。

1 个赞