mirror of
https://github.com/VibedByKaKi/t3-code-android-nightly.git
synced 2026-10-13 22:07:44 +02:00
751 lines
24 KiB
TypeScript
751 lines
24 KiB
TypeScript
import * as Planetscale from "@/Planetscale";
|
|
import * as Provider from "@/Provider";
|
|
import * as RemovalPolicy from "@/RemovalPolicy.ts";
|
|
import * as Test from "@/Test/Alchemy";
|
|
import * as ps from "@distilled.cloud/planetscale";
|
|
import { describe, expect } from "alchemy-test";
|
|
import { Redacted } from "effect";
|
|
import * as Cause from "effect/Cause";
|
|
import * as Data from "effect/Data";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Exit from "effect/Exit";
|
|
import { MinimumLogLevel } from "effect/References";
|
|
import * as Schedule from "effect/Schedule";
|
|
|
|
const { test } = Test.make({ providers: Planetscale.providers() });
|
|
|
|
const logLevel = Effect.provideService(
|
|
MinimumLogLevel,
|
|
process.env.DEBUG ? "Debug" : "Info",
|
|
);
|
|
|
|
describe
|
|
.skipIf(!process.env.PLANETSCALE_TEST)
|
|
.concurrent(
|
|
"PostgresRole",
|
|
{ tags: ["provider:planetscale", "provider:planetscale:postgres", "live"] },
|
|
() => {
|
|
// Read-only: PARENT FAN-OUT enumeration (org -> databases -> branches ->
|
|
// default role) against the live org, without provisioning anything.
|
|
test.provider("list enumerates default roles (read-only)", () =>
|
|
Effect.gen(function* () {
|
|
const provider = yield* Provider.findProvider(
|
|
Planetscale.PostgresDefaultRole,
|
|
);
|
|
const all = yield* provider.list();
|
|
|
|
expect(Array.isArray(all)).toBe(true);
|
|
for (const r of all) {
|
|
expect(r).toMatchObject({
|
|
id: expect.any(String),
|
|
name: expect.any(String),
|
|
organization: expect.any(String),
|
|
database: expect.any(String),
|
|
branch: expect.any(String),
|
|
privateHost: expect.any(String),
|
|
privateConnectionServiceName: expect.any(String),
|
|
});
|
|
}
|
|
}).pipe(logLevel),
|
|
);
|
|
|
|
// Deploy-and-find coverage, opt-in only (slow provisioning).
|
|
test.provider.skipIf(!process.env.PLANETSCALE_DEPLOY_TEST)(
|
|
"list finds a freshly deployed default role",
|
|
(stack) =>
|
|
Effect.gen(function* () {
|
|
yield* stack.destroy();
|
|
|
|
const { database, role } = yield* stack.deploy(
|
|
Effect.gen(function* () {
|
|
const database = yield* Planetscale.PostgresDatabase("ListDb", {
|
|
name: "alchemy-pg-default-list",
|
|
clusterSize: "PS_10",
|
|
arch: "arm",
|
|
});
|
|
const role = yield* Planetscale.PostgresDefaultRole(
|
|
"ListRole",
|
|
{
|
|
database,
|
|
forceReset: true,
|
|
},
|
|
);
|
|
return { database, role };
|
|
}),
|
|
);
|
|
|
|
const provider = yield* Provider.findProvider(
|
|
Planetscale.PostgresDefaultRole,
|
|
);
|
|
const all = yield* provider.list();
|
|
|
|
expect(
|
|
all.some(
|
|
(r) =>
|
|
r.organization === database.organization &&
|
|
r.database === database.name &&
|
|
r.branch === role.branch,
|
|
),
|
|
).toBe(true);
|
|
|
|
yield* stack.destroy();
|
|
yield* waitForDatabaseToBeDeleted(
|
|
database.name,
|
|
database.organization,
|
|
);
|
|
}).pipe(logLevel),
|
|
5_000_000,
|
|
);
|
|
|
|
// Read-only: PARENT FAN-OUT enumeration (org -> databases -> branches ->
|
|
// roles, excluding the default role) against the live org, without
|
|
// provisioning anything.
|
|
test.provider("list enumerates roles (read-only)", () =>
|
|
Effect.gen(function* () {
|
|
const provider = yield* Provider.findProvider(
|
|
Planetscale.PostgresRole,
|
|
);
|
|
const all = yield* provider.list();
|
|
|
|
expect(Array.isArray(all)).toBe(true);
|
|
for (const r of all) {
|
|
expect(r).toMatchObject({
|
|
id: expect.any(String),
|
|
name: expect.any(String),
|
|
organization: expect.any(String),
|
|
database: expect.any(String),
|
|
branch: expect.any(String),
|
|
privateHost: expect.any(String),
|
|
privateConnectionServiceName: expect.any(String),
|
|
});
|
|
}
|
|
}).pipe(logLevel),
|
|
);
|
|
|
|
// Deploy-and-find coverage, opt-in only (slow provisioning).
|
|
test.provider.skipIf(!process.env.PLANETSCALE_DEPLOY_TEST)(
|
|
"list finds a freshly deployed role",
|
|
(stack) =>
|
|
Effect.gen(function* () {
|
|
yield* stack.destroy();
|
|
|
|
const { database, role } = yield* stack.deploy(
|
|
Effect.gen(function* () {
|
|
const database = yield* Planetscale.PostgresDatabase("ListDb", {
|
|
name: "alchemy-pg-role-list",
|
|
clusterSize: "PS_10",
|
|
arch: "arm",
|
|
});
|
|
const role = yield* Planetscale.PostgresRole("ListRole", {
|
|
database,
|
|
inheritedRoles: ["pg_read_all_data"],
|
|
});
|
|
return { database, role };
|
|
}),
|
|
);
|
|
|
|
const provider = yield* Provider.findProvider(
|
|
Planetscale.PostgresRole,
|
|
);
|
|
const all = yield* provider.list();
|
|
|
|
expect(
|
|
all.some(
|
|
(r) =>
|
|
r.organization === database.organization &&
|
|
r.database === database.name &&
|
|
r.id === role.id,
|
|
),
|
|
).toBe(true);
|
|
|
|
yield* stack.destroy();
|
|
yield* waitForDatabaseToBeDeleted(
|
|
database.name,
|
|
database.organization,
|
|
);
|
|
}).pipe(logLevel),
|
|
5_000_000,
|
|
);
|
|
|
|
test.provider(
|
|
"default role - create, duplicate fails, forceReset returns new id",
|
|
(stack) =>
|
|
Effect.gen(function* () {
|
|
yield* stack.destroy();
|
|
|
|
// First: create default role, expect success
|
|
const { database, role1 } = yield* stack.deploy(
|
|
Effect.gen(function* () {
|
|
const database = yield* Planetscale.PostgresDatabase(
|
|
"Database",
|
|
{
|
|
clusterSize: "PS_10",
|
|
arch: "arm",
|
|
},
|
|
);
|
|
const role1 = yield* Planetscale.PostgresDefaultRole("Role1", {
|
|
database,
|
|
});
|
|
|
|
return { role1, database };
|
|
}),
|
|
);
|
|
|
|
expect(role1).toMatchObject({
|
|
id: expect.any(String),
|
|
name: expect.any(String),
|
|
host: expect.any(String),
|
|
username: expect.any(String),
|
|
password: expect.any(Object),
|
|
databaseName: "postgres",
|
|
branch: "main",
|
|
organization: database.organization,
|
|
privateHost: expect.any(String),
|
|
privateConnectionServiceName: expect.any(String),
|
|
});
|
|
|
|
const defaultRoleFromApi = yield* ps.getDefaultRole({
|
|
organization: database.organization,
|
|
database: database.name,
|
|
branch: "main",
|
|
});
|
|
expect(role1.privateConnectionServiceName).toEqual(
|
|
defaultRoleFromApi.private_connection_service_name,
|
|
);
|
|
expect(role1.privateHost).toEqual(
|
|
defaultRoleFromApi.private_access_host_url,
|
|
);
|
|
|
|
// Second: create again without forceReset — should fail (default already exists)
|
|
const exit = yield* stack
|
|
.deploy(
|
|
Effect.gen(function* () {
|
|
const database = yield* Planetscale.PostgresDatabase(
|
|
"Database",
|
|
{
|
|
clusterSize: "PS_10",
|
|
arch: "arm",
|
|
},
|
|
);
|
|
const role2 = yield* Planetscale.PostgresDefaultRole(
|
|
"Role2",
|
|
{
|
|
database,
|
|
},
|
|
);
|
|
|
|
return { role2 };
|
|
}),
|
|
)
|
|
.pipe(Effect.exit);
|
|
|
|
expect(Exit.isFailure(exit)).toBe(true);
|
|
|
|
if (Exit.isFailure(exit)) {
|
|
expect(Cause.pretty(exit.cause)).toMatch(
|
|
/Default role already exists.*Use forceReset/,
|
|
);
|
|
}
|
|
|
|
// Third: create with forceReset — should succeed and return a different role id
|
|
const { role3 } = yield* stack.deploy(
|
|
Effect.gen(function* () {
|
|
const database = yield* Planetscale.PostgresDatabase(
|
|
"Database",
|
|
{
|
|
clusterSize: "PS_10",
|
|
arch: "arm",
|
|
},
|
|
);
|
|
const role3 = yield* Planetscale.PostgresDefaultRole("Role3", {
|
|
database,
|
|
forceReset: true,
|
|
});
|
|
|
|
return { role3, database };
|
|
}),
|
|
);
|
|
|
|
expect(role3).toMatchObject({
|
|
id: expect.any(String),
|
|
name: expect.any(String),
|
|
host: expect.any(String),
|
|
username: expect.any(String),
|
|
password: expect.any(Object),
|
|
databaseName: "postgres",
|
|
branch: "main",
|
|
organization: database.organization,
|
|
});
|
|
|
|
// the default role ID is the same, but the password is different
|
|
expect(Redacted.value(role3.password)).not.toEqual(
|
|
Redacted.value(role1.password),
|
|
);
|
|
|
|
yield* stack.destroy();
|
|
yield* waitForDatabaseToBeDeleted(
|
|
database.name,
|
|
database.organization,
|
|
);
|
|
}).pipe(logLevel),
|
|
5_000_000,
|
|
);
|
|
|
|
test.provider(
|
|
"create and delete role",
|
|
(stack) =>
|
|
Effect.gen(function* () {
|
|
yield* stack.destroy();
|
|
|
|
// Create a role
|
|
const { database, role } = yield* stack.deploy(
|
|
Effect.gen(function* () {
|
|
const database = yield* Planetscale.PostgresDatabase(
|
|
"Database",
|
|
{
|
|
clusterSize: "PS_10",
|
|
arch: "arm",
|
|
},
|
|
);
|
|
const role = yield* Planetscale.PostgresRole("Role", {
|
|
database,
|
|
// Empty array means no permissions, which is fine for testing.
|
|
inheritedRoles: [],
|
|
});
|
|
|
|
return { database, role };
|
|
}),
|
|
);
|
|
|
|
expect(role).toMatchObject({
|
|
id: expect.any(String),
|
|
name: expect.any(String),
|
|
host: expect.any(String),
|
|
username: expect.any(String),
|
|
password: expect.any(Object),
|
|
privateHost: expect.any(String),
|
|
privateConnectionServiceName: expect.any(String),
|
|
});
|
|
|
|
const roleFromApi = yield* ps.getRole({
|
|
id: role.id,
|
|
database: database.name,
|
|
organization: database.organization,
|
|
branch: "main",
|
|
});
|
|
expect(role.privateConnectionServiceName).toEqual(
|
|
roleFromApi.private_connection_service_name,
|
|
);
|
|
expect(role.privateHost).toEqual(
|
|
roleFromApi.private_access_host_url,
|
|
);
|
|
|
|
// Update role with different ttl (should trigger replacement)
|
|
const { updatedRole } = yield* stack.deploy(
|
|
Effect.gen(function* () {
|
|
const database = yield* Planetscale.PostgresDatabase(
|
|
"Database",
|
|
{
|
|
clusterSize: "PS_10",
|
|
arch: "arm",
|
|
},
|
|
);
|
|
const updatedRole = yield* Planetscale.PostgresRole("Role", {
|
|
database,
|
|
ttl: 3600,
|
|
inheritedRoles: [],
|
|
});
|
|
|
|
return { database, updatedRole };
|
|
}),
|
|
);
|
|
|
|
expect(role.id).not.toEqual(updatedRole.id);
|
|
expect(updatedRole.ttl).toEqual(3600);
|
|
|
|
const found = yield* ps
|
|
.getRole({
|
|
id: role.id,
|
|
database: database.name,
|
|
organization: database.organization,
|
|
branch: "main",
|
|
})
|
|
.pipe(
|
|
Effect.map(() => true),
|
|
Effect.catchTag("NotFound", () => Effect.succeed(false)),
|
|
);
|
|
|
|
expect(found).toBe(false);
|
|
|
|
const updatedRoleFromApi = yield* ps.getRole({
|
|
id: updatedRole.id,
|
|
database: database.name,
|
|
organization: database.organization,
|
|
branch: "main",
|
|
});
|
|
|
|
expect(updatedRoleFromApi.ttl).toEqual(3600);
|
|
|
|
yield* stack.destroy();
|
|
|
|
yield* waitForDatabaseToBeDeleted(
|
|
database.name,
|
|
database.organization,
|
|
);
|
|
}).pipe(logLevel),
|
|
5_000_000,
|
|
);
|
|
|
|
test.provider(
|
|
"role with replication is created with the attribute and replaced when it changes",
|
|
(stack) =>
|
|
Effect.gen(function* () {
|
|
yield* stack.destroy();
|
|
|
|
const { database, role1 } = yield* stack.deploy(
|
|
Effect.gen(function* () {
|
|
const database = yield* Planetscale.PostgresDatabase(
|
|
"Database",
|
|
{
|
|
clusterSize: "PS_10",
|
|
arch: "arm",
|
|
},
|
|
);
|
|
const role1 = yield* Planetscale.PostgresRole(
|
|
"RoleReplication",
|
|
{
|
|
database,
|
|
inheritedRoles: ["postgres"],
|
|
withReplication: true,
|
|
},
|
|
);
|
|
|
|
return { database, role1 };
|
|
}),
|
|
);
|
|
|
|
expect(role1).toMatchObject({
|
|
id: expect.any(String),
|
|
inheritedRoles: ["postgres"],
|
|
withReplication: true,
|
|
});
|
|
|
|
const { role2 } = yield* stack.deploy(
|
|
Effect.gen(function* () {
|
|
const database = yield* Planetscale.PostgresDatabase(
|
|
"Database",
|
|
{
|
|
clusterSize: "PS_10",
|
|
arch: "arm",
|
|
},
|
|
);
|
|
const role2 = yield* Planetscale.PostgresRole(
|
|
"RoleReplication",
|
|
{
|
|
database,
|
|
inheritedRoles: ["postgres"],
|
|
},
|
|
);
|
|
|
|
return { role2 };
|
|
}),
|
|
);
|
|
|
|
expect(role2).toMatchObject({ withReplication: false });
|
|
expect(role2.id).not.toEqual(role1.id);
|
|
|
|
yield* stack.destroy();
|
|
|
|
yield* waitForDatabaseToBeDeleted(
|
|
database.name,
|
|
database.organization,
|
|
);
|
|
}).pipe(logLevel),
|
|
5_000_000,
|
|
);
|
|
|
|
test.provider(
|
|
"role gets replaced when properties change",
|
|
(stack) =>
|
|
Effect.gen(function* () {
|
|
yield* stack.destroy();
|
|
|
|
const { database, role1 } = yield* stack.deploy(
|
|
Effect.gen(function* () {
|
|
const database = yield* Planetscale.PostgresDatabase(
|
|
"Database",
|
|
{
|
|
clusterSize: "PS_10",
|
|
arch: "arm",
|
|
},
|
|
);
|
|
const role1 = yield* Planetscale.PostgresRole("RoleReplace", {
|
|
database: database,
|
|
inheritedRoles: ["pg_read_all_data"],
|
|
ttl: 3600,
|
|
});
|
|
|
|
return { database, role1 };
|
|
}),
|
|
);
|
|
|
|
expect(role1).toMatchObject({
|
|
id: expect.any(String),
|
|
name: expect.any(String),
|
|
inheritedRoles: ["pg_read_all_data"],
|
|
});
|
|
|
|
const originalId = role1.id;
|
|
const originalName = role1.name;
|
|
|
|
const { role2 } = yield* stack.deploy(
|
|
Effect.gen(function* () {
|
|
const database = yield* Planetscale.PostgresDatabase(
|
|
"Database",
|
|
{
|
|
clusterSize: "PS_10",
|
|
arch: "arm",
|
|
},
|
|
);
|
|
const role2 = yield* Planetscale.PostgresRole("RoleReplace", {
|
|
database: database,
|
|
inheritedRoles: ["postgres"],
|
|
ttl: 7200,
|
|
});
|
|
|
|
return { role2 };
|
|
}),
|
|
);
|
|
|
|
expect(role2).toMatchObject({
|
|
id: expect.any(String),
|
|
name: expect.any(String),
|
|
inheritedRoles: ["postgres"],
|
|
});
|
|
|
|
expect(role2.id).not.toEqual(originalId);
|
|
expect(role2.name).not.toEqual(originalName);
|
|
|
|
yield* stack.destroy();
|
|
|
|
yield* waitForDatabaseToBeDeleted(
|
|
database.name,
|
|
database.organization,
|
|
);
|
|
}).pipe(logLevel),
|
|
5_000_000,
|
|
);
|
|
|
|
test.provider(
|
|
"role with RemovalPolicy.retain(true) should not be deleted via API",
|
|
(stack) =>
|
|
Effect.gen(function* () {
|
|
yield* stack.destroy();
|
|
|
|
const { organization } = yield* yield* Planetscale.Credentials;
|
|
|
|
const { database, role } = yield* stack.deploy(
|
|
Effect.gen(function* () {
|
|
const database = yield* Planetscale.PostgresDatabase(
|
|
"Database",
|
|
{
|
|
clusterSize: "PS_10",
|
|
arch: "arm",
|
|
},
|
|
).pipe(RemovalPolicy.retain(true));
|
|
const role = yield* Planetscale.PostgresRole(
|
|
"RoleRetainRemoval",
|
|
{
|
|
database,
|
|
inheritedRoles: ["postgres"],
|
|
},
|
|
).pipe(RemovalPolicy.retain(true));
|
|
|
|
return { database, role };
|
|
}),
|
|
);
|
|
|
|
expect(role).toMatchObject({
|
|
id: expect.any(String),
|
|
name: expect.any(String),
|
|
database: database.name,
|
|
inheritedRoles: ["postgres"],
|
|
});
|
|
|
|
yield* stack.destroy();
|
|
|
|
const liveRole = yield* ps
|
|
.getRole({
|
|
organization,
|
|
database: database.name,
|
|
branch: "main",
|
|
id: role.id,
|
|
})
|
|
.pipe(
|
|
Effect.catchTag("NotFound", () => Effect.succeed(undefined)),
|
|
);
|
|
|
|
expect(liveRole).toBeDefined();
|
|
expect(liveRole?.id).toEqual(role.id);
|
|
|
|
// deleting the db takes care of deleting the role
|
|
yield* ps
|
|
.deleteDatabase({
|
|
organization,
|
|
database: database.name,
|
|
})
|
|
.pipe(Effect.catchTag("NotFound", () => Effect.void));
|
|
|
|
yield* waitForDatabaseToBeDeleted(
|
|
database.name,
|
|
database.organization,
|
|
);
|
|
}).pipe(logLevel),
|
|
5_000_000,
|
|
);
|
|
|
|
test.provider(
|
|
"role update: successor is updatable without replacement",
|
|
(stack) =>
|
|
Effect.gen(function* () {
|
|
yield* stack.destroy();
|
|
|
|
const { database, role1 } = yield* stack.deploy(
|
|
Effect.gen(function* () {
|
|
const database = yield* Planetscale.PostgresDatabase(
|
|
"Database",
|
|
{
|
|
clusterSize: "PS_10",
|
|
arch: "arm",
|
|
},
|
|
);
|
|
const role1 = yield* Planetscale.PostgresRole("RoleSuccessor", {
|
|
database,
|
|
inheritedRoles: ["postgres"],
|
|
successor: "postgres",
|
|
});
|
|
|
|
return { database, role1 };
|
|
}),
|
|
);
|
|
|
|
expect(role1).toMatchObject({
|
|
id: expect.any(String),
|
|
name: expect.any(String),
|
|
successor: "postgres",
|
|
});
|
|
|
|
const originalId = role1.id;
|
|
|
|
const { role2 } = yield* stack.deploy(
|
|
Effect.gen(function* () {
|
|
const database = yield* Planetscale.PostgresDatabase(
|
|
"Database",
|
|
{
|
|
clusterSize: "PS_10",
|
|
arch: "arm",
|
|
},
|
|
);
|
|
const role2 = yield* Planetscale.PostgresRole("RoleSuccessor", {
|
|
database,
|
|
inheritedRoles: ["postgres"],
|
|
successor: "pg_read_all_data",
|
|
});
|
|
|
|
return { role2 };
|
|
}),
|
|
);
|
|
|
|
expect(role2).toMatchObject({
|
|
id: originalId,
|
|
successor: "pg_read_all_data",
|
|
});
|
|
|
|
expect(role2.id).toEqual(originalId);
|
|
|
|
yield* stack.destroy();
|
|
|
|
yield* waitForDatabaseToBeDeleted(
|
|
database.name,
|
|
database.organization,
|
|
);
|
|
}).pipe(logLevel),
|
|
5_000_000,
|
|
);
|
|
|
|
test.provider(
|
|
"role with custom branch",
|
|
(stack) =>
|
|
Effect.gen(function* () {
|
|
yield* stack.destroy();
|
|
|
|
const { database, branch, role } = yield* stack.deploy(
|
|
Effect.gen(function* () {
|
|
const database = yield* Planetscale.PostgresDatabase(
|
|
"Database",
|
|
{
|
|
clusterSize: "PS_10",
|
|
arch: "arm",
|
|
},
|
|
);
|
|
|
|
const branch = yield* Planetscale.PostgresBranch(
|
|
"CustomBranch",
|
|
{
|
|
database,
|
|
},
|
|
);
|
|
|
|
const role = yield* Planetscale.PostgresRole(
|
|
"RoleCustomBranch",
|
|
{
|
|
database,
|
|
branch,
|
|
inheritedRoles: ["postgres"],
|
|
},
|
|
);
|
|
|
|
return { database, branch, role };
|
|
}),
|
|
);
|
|
|
|
expect(role).toMatchObject({
|
|
id: expect.any(String),
|
|
name: expect.any(String),
|
|
database: database.name,
|
|
branch: branch.name,
|
|
inheritedRoles: ["postgres"],
|
|
});
|
|
|
|
yield* stack.destroy();
|
|
|
|
yield* waitForDatabaseToBeDeleted(
|
|
database.name,
|
|
database.organization,
|
|
);
|
|
}).pipe(logLevel),
|
|
5_000_000,
|
|
);
|
|
},
|
|
);
|
|
const waitForDatabaseToBeDeleted = Effect.fn(function* (
|
|
database: string,
|
|
organization: string,
|
|
) {
|
|
yield* ps
|
|
.getDatabase({
|
|
organization,
|
|
database,
|
|
})
|
|
.pipe(
|
|
Effect.flatMap(() => Effect.fail(new DatabaseStillExists())),
|
|
Effect.retry({
|
|
while: (e): e is DatabaseStillExists =>
|
|
e instanceof DatabaseStillExists,
|
|
schedule: Schedule.exponential(100),
|
|
}),
|
|
Effect.catchTag("NotFound", () => Effect.void),
|
|
);
|
|
});
|
|
|
|
class DatabaseStillExists extends Data.TaggedError("DatabaseStillExists") {}
|