feat: some more decomposition in hive_config

This commit is contained in:
Aliaksei Tratseuski 2024-06-30 18:50:21 +04:00
parent 4bda23eaf9
commit 2fbcc3c232
3 changed files with 65 additions and 47 deletions

View file

@ -116,7 +116,7 @@ class HiveConfig {
/// add new migrations here, like: /// add new migrations here, like:
/// if (version < 3) {...}, etc. /// if (version < 3) {...}, etc.
/// update saved version after successfull migraions /// update saved version after successfull migrations
await localSettingsBox.put(BNames.databaseVersion, version); await localSettingsBox.put(BNames.databaseVersion, version);
} }
} catch (error, stackTrace) { } catch (error, stackTrace) {
@ -135,60 +135,82 @@ class HiveConfig {
if (resourcesBox.isEmpty) { if (resourcesBox.isEmpty) {
final Box serverInstallationBox = Hive.box(BNames.serverInstallationBox); final Box serverInstallationBox = Hive.box(BNames.serverInstallationBox);
final String? serverProviderKey =
serverInstallationBox.get(BNames.hetznerKey);
final String? serverLocation =
serverInstallationBox.get(BNames.serverLocation);
final String? dnsProviderKey =
serverInstallationBox.get(BNames.cloudFlareKey);
final BackupsCredential? backblazeCredential =
serverInstallationBox.get(BNames.backblazeCredential);
final ServerDomain? serverDomain =
serverInstallationBox.get(BNames.serverDomain);
final ServerHostingDetails? serverDetails = final ServerHostingDetails? serverDetails =
serverInstallationBox.get(BNames.serverDetails); serverInstallationBox.get(BNames.serverDetails);
final BackblazeBucket? backblazeBucket =
serverInstallationBox.get(BNames.backblazeBucket);
final String? serverType =
serverInstallationBox.get(BNames.serverTypeIdentifier);
final ServerProviderType? serverProvider =
serverInstallationBox.get(BNames.serverProvider);
final DnsProviderType? dnsProvider =
serverInstallationBox.get(BNames.dnsProvider);
if (serverProviderKey != null && // move server provider config
(serverProvider != null ||
(serverDetails != null && final ServerProviderType? serverProvider =
serverDetails.provider != ServerProviderType.unknown))) { serverInstallationBox.get(BNames.serverProvider) ??
serverDetails?.provider;
final String? serverProviderKey =
serverInstallationBox.get(BNames.hetznerKey);
if (serverProviderKey != null && serverProvider.isSpecified) {
final ServerProviderCredential serverProviderCredential = final ServerProviderCredential serverProviderCredential =
ServerProviderCredential( ServerProviderCredential(
tokenId: null, tokenId: null,
token: serverProviderKey, token: serverProviderKey,
provider: serverProvider ?? serverDetails!.provider, provider: serverProvider!,
associatedServerIds: serverDetails != null ? [serverDetails.id] : [], associatedServerIds: [if (serverDetails != null) serverDetails.id],
); );
await resourcesBox await resourcesBox.put(
.put(BNames.serverProviderTokens, [serverProviderCredential]); BNames.serverProviderTokens,
[serverProviderCredential],
);
} }
if (dnsProviderKey != null && final String? serverLocation =
(dnsProvider != null || serverInstallationBox.get(BNames.serverLocation);
(serverDomain != null && final String? serverType =
serverDomain.provider != DnsProviderType.unknown))) { serverInstallationBox.get(BNames.serverTypeIdentifier);
final ServerDomain? serverDomain =
serverInstallationBox.get(BNames.serverDomain);
if (serverDetails != null && serverDomain != null) {
await resourcesBox.put(
BNames.servers,
[
Server(
domain: serverDomain,
hostingDetails: serverDetails.copyWith(
serverLocation: serverLocation,
serverType: serverType,
),
),
],
);
}
// move dns config
final String? dnsProviderKey =
serverInstallationBox.get(BNames.cloudFlareKey);
final DnsProviderType? dnsProvider =
serverInstallationBox.get(BNames.dnsProvider) ??
serverDomain?.provider;
if (dnsProviderKey != null && dnsProvider.isSpecified) {
final DnsProviderCredential dnsProviderCredential = final DnsProviderCredential dnsProviderCredential =
DnsProviderCredential( DnsProviderCredential(
tokenId: null, tokenId: null,
token: dnsProviderKey, token: dnsProviderKey,
provider: dnsProvider ?? serverDomain!.provider, provider: dnsProvider!,
associatedDomainNames: associatedDomainNames: [
serverDomain != null ? [serverDomain.domainName] : [], if (serverDomain != null) serverDomain.domainName,
],
); );
await resourcesBox await resourcesBox
.put(BNames.dnsProviderTokens, [dnsProviderCredential]); .put(BNames.dnsProviderTokens, [dnsProviderCredential]);
} }
// move backblaze (backups) config
final BackupsCredential? backblazeCredential =
serverInstallationBox.get(BNames.backblazeCredential);
final BackblazeBucket? backblazeBucket =
serverInstallationBox.get(BNames.backblazeBucket);
if (backblazeCredential != null) { if (backblazeCredential != null) {
await resourcesBox await resourcesBox
.put(BNames.backupsProviderTokens, [backblazeCredential]); .put(BNames.backupsProviderTokens, [backblazeCredential]);
@ -197,20 +219,8 @@ class HiveConfig {
if (backblazeBucket != null) { if (backblazeBucket != null) {
await resourcesBox.put(BNames.backblazeBucket, backblazeBucket); await resourcesBox.put(BNames.backblazeBucket, backblazeBucket);
} }
if (serverDetails != null && serverDomain != null) {
await resourcesBox.put(BNames.servers, [
Server(
domain: serverDomain,
hostingDetails: serverDetails.copyWith(
serverLocation: serverLocation,
serverType: serverType,
),
),
]);
}
} }
log('successfully migration of db from 1 to 2 version'); log('successfully migrated db from 1 to 2 version');
} }
} }

View file

@ -118,3 +118,7 @@ enum ServerProviderType {
unknown => 'Unknown', unknown => 'Unknown',
}; };
} }
extension ServerProviderTypeIsSpecified on ServerProviderType? {
bool get isSpecified => this != null && this != ServerProviderType.unknown;
}

View file

@ -57,3 +57,7 @@ enum DnsProviderType {
unknown => 'Unknown', unknown => 'Unknown',
}; };
} }
extension DnsProviderTypeIsSpecified on DnsProviderType? {
bool get isSpecified => this != null && this != DnsProviderType.unknown;
}