using Generated; using Microsoft.Data.Sqlite; using TeaQL.Provider.Sqlite; using TeaQL.Sql; var phase = args.Single(); if (phase is not ("bootstrap" or "create" or "read")) throw new ArgumentException("expected bootstrap, create or read"); var database = Environment.GetEnvironmentVariable("SCHOOL_DATABASE") ?? throw new ArgumentException("SCHOOL_DATABASE is required"); var module = GeneratedRuntimeModule.Module; var context = module.IntoContext(); await using var connection = new SqliteConnection(new SqliteConnectionStringBuilder { DataSource = database }.ToString()); await connection.OpenAsync(); context.WithDataService(new SqlDataServiceExecutor(new SqliteDialect(), new SqliteTransport(connection), new MetadataSchemaProvider(module.Metadata.GetEntity))); await context.EnsureSchemaAsync(); if (phase == "create") { var school = Q.Schools().Comment("Initialize Riverside Primary School") .Purpose("Verify School persistence").NewEntity(context); school.UpdatePlatformId(1); school.UpdateSchoolTypeToPrimary(); school.UpdateName("Riverside Primary School"); school.UpdateAddress("12 River Road, Springfield"); school.UpdateEstablishedDate(new DateTime(1995, 9, 1, 0, 0, 0, DateTimeKind.Utc)); school.UpdateStudentCapacity(800); school.UpdateActive(true); await school.AuditAs("Create the documented School fixture").SaveAsync(context); } var rows = await Q.SchoolsWithMinimalFields().SelectName() .WithNameIs("Riverside Primary School").OrderByIdAscending().Limit(2) .Comment("Read the persisted School fixture").Purpose("Verify School persistence") .ExecuteForListAsync(context); if (rows.Count != (phase == "bootstrap" ? 0 : 1)) throw new Exception("Unexpected School row count"); Console.WriteLine($"SCHOOL_OK phase={phase}");