-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
307634e
commit 7ccdcc4
Showing
7 changed files
with
254 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const consumers = require('node:stream/consumers'); | ||
const { GetObjectCommand, PutObjectCommand } = require("@aws-sdk/client-s3"); | ||
const fetch = require('node-fetch'); | ||
const { fromUtf8 } = require("@aws-sdk/util-utf8-node"); | ||
|
||
async function hasPluginsFile(pgDatastore) { | ||
const client = await pgDatastore.pool.connect(); | ||
|
||
const res = await client.query("SELECT * FROM plugins"); | ||
client.release() | ||
|
||
return res.rowCount > 0 | ||
} | ||
|
||
async function getUsers(pgDatastore) { | ||
const client = await pgDatastore.pool.connect(); | ||
|
||
const res = await client.query("SELECT * FROM users"); | ||
client.release() | ||
|
||
return res.rows | ||
} | ||
|
||
async function createPlugin(pgDatastore, plugin) { | ||
pgDatastore.pool.connect() | ||
.then(client => { | ||
return client.query("INSERT INTO plugins(id, content) VALUES($1, $2::jsonb)", [plugin.pluginId, JSON.stringify({ plugin })]) | ||
.then(() => client.release()) | ||
}) | ||
} | ||
|
||
async function PG_1_22(pgDatastore) { | ||
const migrated = await hasPluginsFile(pgDatastore) | ||
|
||
console.log('apply PG_1_22', migrated) | ||
|
||
if (!migrated) { | ||
const usersPlugins = await getUsers(pgDatastore) | ||
|
||
console.log('migrate users : ', users) | ||
|
||
return usersPlugins.map(user => { | ||
return Promise.all(user.plugins.map(plugin => createPlugin(pgDatastore, plugin))) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = { | ||
PG_1_22 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
const consumers = require('node:stream/consumers'); | ||
const { GetObjectCommand, PutObjectCommand } = require("@aws-sdk/client-s3"); | ||
const fetch = require('node-fetch'); | ||
const { fromUtf8 } = require("@aws-sdk/util-utf8-node"); | ||
|
||
async function hasPluginsFile(s3Datastore) { | ||
const { instance, Bucket } = s3Datastore.state; | ||
|
||
try { | ||
const rawData = await instance.send(new GetObjectCommand({ | ||
Bucket, | ||
Key: 'plugins.json' | ||
})) | ||
await new fetch.Response(rawData.Body).json(); | ||
|
||
return true | ||
} catch (err) { | ||
return false | ||
} | ||
} | ||
|
||
async function getUsers(s3Datastore) { | ||
const { instance, Bucket } = s3Datastore.state; | ||
try { | ||
const rawData = await instance.send(new GetObjectCommand({ | ||
Bucket, | ||
Key: 'users.json' | ||
})) | ||
return new fetch.Response(rawData.Body).json(); | ||
} catch (err) { | ||
return [] | ||
} | ||
} | ||
|
||
function getUser(s3Datastore, email) { | ||
const { instance, Bucket } = s3Datastore.state; | ||
|
||
return new Promise(resolve => { | ||
instance.send(new GetObjectCommand({ | ||
Bucket, | ||
Key: `${email}.json` | ||
})) | ||
.then(data => { | ||
try { | ||
if (data && data.Body) { | ||
consumers.json(data.Body) | ||
.then(resolve) | ||
} | ||
else | ||
resolve({}) | ||
} catch (_err) { | ||
resolve({}) | ||
} | ||
}) | ||
.catch(_err => { | ||
resolve({}) | ||
}) | ||
}) | ||
} | ||
|
||
function getUserPlugins(s3Datastore, email) { | ||
return getUser(s3Datastore, email) | ||
.then(data => data.plugins || []) | ||
} | ||
|
||
async function createPlugin(s3Datastore, email, pluginId, content) { | ||
const { instance, Bucket } = s3Datastore.state; | ||
|
||
console.log(content) | ||
|
||
const params = { | ||
Bucket, | ||
Key: `${pluginId}.json`, | ||
Body: fromUtf8(JSON.stringify(content)), | ||
ContentType: 'application/json', | ||
} | ||
|
||
return instance.send(new PutObjectCommand(params)) | ||
} | ||
|
||
async function S3_1_22(s3Datastore) { | ||
const migrated = await hasPluginsFile(s3Datastore) | ||
|
||
console.log('apply S3_1_22', migrated) | ||
|
||
if (!migrated) { | ||
const users = await getUsers(s3Datastore) | ||
|
||
console.log('migrate users : ', users) | ||
|
||
const usersPlugins = await Promise.all(users.map(user => getUserPlugins(s3Datastore, user))); | ||
|
||
await Promise.all(usersPlugins.map((userPlugins, idx) => { | ||
return Promise.all(userPlugins.map(plugin => createPlugin(s3Datastore, users[idx], plugin.pluginId, { | ||
...plugin, | ||
admins: [users[idx]], | ||
users: [] | ||
}))) | ||
})) | ||
|
||
// TODO - problem came from the email formatter [email protected] and wasmototools | ||
|
||
const pluginsByUser = usersPlugins.reduce((acc, userPlugins, idx) => { | ||
return [...acc, ...userPlugins.map(plugin => ({ | ||
plugin: { | ||
...plugin, | ||
admins: [users[idx]], | ||
users: [] | ||
}, email: users[idx] | ||
}))] | ||
}, []) | ||
|
||
pluginsByUser | ||
.reduce((promise, item) => promise.then(() => new Promise(resolve => { | ||
const { plugin, email } = item; | ||
|
||
console.log('add ', email, plugin) | ||
s3Datastore.addPluginToList(email, plugin) | ||
.then(resolve) | ||
})), Promise.resolve()) | ||
} | ||
} | ||
|
||
module.exports = { | ||
S3_1_22 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.