-
Notifications
You must be signed in to change notification settings - Fork 485
Add options to the bootstrap command to specify a schema file #1942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
4e1766b
2fe7cdc
3be2225
0a8c6d0
9969903
6e9546b
db19939
6fee80f
183cfbd
1b53b8e
6a929e2
06e2a51
b80f6ae
e99d70e
49cce5b
e4a2f2a
3559234
28617e0
b7e2e5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,12 @@ | |
| */ | ||
| package org.apache.polaris.persistence.relational.jdbc; | ||
|
|
||
| import jakarta.annotation.Nonnull; | ||
| import java.io.FileInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.Locale; | ||
| import org.apache.polaris.core.persistence.bootstrap.SchemaOptions; | ||
|
|
||
| public enum DatabaseType { | ||
| POSTGRES("postgres"), | ||
|
|
@@ -43,7 +48,24 @@ public static DatabaseType fromDisplayName(String displayName) { | |
| }; | ||
| } | ||
|
|
||
| public String getInitScriptResource() { | ||
| return String.format("%s/schema-v1.sql", this.getDisplayName()); | ||
| public InputStream getInitScriptResource(@Nonnull SchemaOptions schemaOptions) { | ||
| if (schemaOptions.schemaFile() != null) { | ||
| try { | ||
| return new FileInputStream(schemaOptions.schemaFile()); | ||
| } catch (IOException e) { | ||
| throw new IllegalArgumentException("Unable to load file " + schemaOptions.schemaFile(), e); | ||
| } | ||
| } else { | ||
| final String schemaSuffix; | ||
| switch (schemaOptions.schemaVersion()) { | ||
| case SchemaOptions.LATEST -> schemaSuffix = "schema-v1.sql"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: make
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In SchemaOptions it is:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean default in bootstrap code to whatever version happens to be latest when no explicit version is requested, but avoid having the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alternatively: use actual current schema version in the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wait... that does not carry over to NoSQL 🤔
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking it's nice to have so that users can be explicit about taking the LATEST schema -- the alternative is just that you get LATEST by not setting the version, right? Besides the ability to be explicit about wanting the LATEST, I think the constant is potentially useful in the contrived scenario where a persistence wants to default to a non-LATEST schema
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough... but in this case I'd prefer to go back to the non-null
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, in that case we actually lose this:
Since there is no way for the value to be unset, it's either LATEST or some number. But I don't feel too strongly about this, as I think that scenario is pretty unlikely. I'm happy to either leave the default or remove LATEST; I'll try getting rid of LATEST for now
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right 🤦 |
||
| case "1" -> schemaSuffix = "schema-v1.sql"; | ||
| default -> | ||
| throw new IllegalArgumentException( | ||
| "Unknown schema version " + schemaOptions.schemaVersion()); | ||
| } | ||
| ClassLoader classLoader = DatasourceOperations.class.getClassLoader(); | ||
| return classLoader.getResourceAsStream(this.getDisplayName() + "/" + schemaSuffix); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
|
|
||
| import static org.apache.polaris.core.persistence.PrincipalSecretsGenerator.RANDOM_SECRETS; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.sql.SQLException; | ||
| import java.time.ZoneId; | ||
| import java.util.Optional; | ||
|
|
@@ -49,8 +50,11 @@ protected PolarisTestMetaStoreManager createPolarisTestMetaStoreManager() { | |
| try { | ||
| datasourceOperations = | ||
| new DatasourceOperations(createH2DataSource(), new H2JdbcConfiguration()); | ||
| datasourceOperations.executeScript( | ||
| String.format("%s/schema-v1.sql", DatabaseType.H2.getDisplayName())); | ||
| ClassLoader classLoader = DatasourceOperations.class.getClassLoader(); | ||
| InputStream scriptStream = | ||
| classLoader.getResourceAsStream( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this stream normally needs to be closed, AFAIK.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto the above -- I think we just weren't closing it before, but added an explicit
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not a try-with-resources block?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per the javadoc, I think it's better to have this method take on the responsibility for closing the stream after it's used rather than scatter try-with-resources amongst the callers. The loan pattern would be better, but we're already getting pretty far away from the intent of this change. |
||
| String.format("%s/schema-v1.sql", DatabaseType.H2.getDisplayName())); | ||
| datasourceOperations.executeScript(scriptStream); | ||
| } catch (SQLException e) { | ||
| throw new RuntimeException( | ||
| String.format( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.polaris.core.persistence.bootstrap; | ||
|
|
||
| import org.apache.polaris.immutables.PolarisImmutable; | ||
|
|
||
| @PolarisImmutable | ||
| public interface BootstrapOptions { | ||
| Iterable<String> realms(); | ||
|
|
||
| RootCredentialsSet rootCredentialsSet(); | ||
|
|
||
| SchemaOptions schemaOptions(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.polaris.core.persistence.bootstrap; | ||
|
|
||
| import jakarta.annotation.Nullable; | ||
| import org.apache.polaris.immutables.PolarisImmutable; | ||
| import org.immutables.value.Value; | ||
|
|
||
| @PolarisImmutable | ||
| public interface SchemaOptions { | ||
| public static final String LATEST = "LATEST"; | ||
|
|
||
| @Value.Default | ||
| default String schemaVersion() { | ||
| return LATEST; | ||
| } | ||
|
|
||
| @Nullable | ||
| String schemaFile(); | ||
|
eric-maynard marked this conversation as resolved.
Outdated
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.